You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bigint.c 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <stdint.h>
  20. #include <string.h>
  21. #include <assert.h>
  22. #include <ipxe/bigint.h>
  23. /** @file
  24. *
  25. * Big integer support
  26. */
  27. /**
  28. * Perform modular multiplication of big integers
  29. *
  30. * @v multiplicand0 Element 0 of big integer to be multiplied
  31. * @v multiplier0 Element 0 of big integer to be multiplied
  32. * @v modulus0 Element 0 of big integer modulus
  33. * @v result0 Element 0 of big integer to hold result
  34. */
  35. void bigint_mod_multiply_raw ( const bigint_element_t *multiplicand0,
  36. const bigint_element_t *multiplier0,
  37. const bigint_element_t *modulus0,
  38. bigint_element_t *result0,
  39. unsigned int size ) {
  40. const bigint_t ( size ) __attribute__ (( may_alias )) *multiplicand =
  41. ( ( const void * ) multiplicand0 );
  42. const bigint_t ( size ) __attribute__ (( may_alias )) *multiplier =
  43. ( ( const void * ) multiplier0 );
  44. const bigint_t ( size ) __attribute__ (( may_alias )) *modulus =
  45. ( ( const void * ) modulus0 );
  46. bigint_t ( size ) __attribute__ (( may_alias )) *result =
  47. ( ( void * ) result0 );
  48. bigint_t ( size * 2 ) temp_result;
  49. bigint_t ( size * 2 ) temp_modulus;
  50. int rotation;
  51. int i;
  52. /* Perform multiplication */
  53. bigint_multiply ( multiplicand, multiplier, &temp_result );
  54. /* Rescale modulus to match result */
  55. bigint_grow ( modulus, &temp_modulus );
  56. rotation = ( bigint_max_set_bit ( &temp_result ) -
  57. bigint_max_set_bit ( &temp_modulus ) );
  58. for ( i = 0 ; i < rotation ; i++ )
  59. bigint_rol ( &temp_modulus );
  60. /* Subtract multiples of modulus */
  61. for ( i = 0 ; i <= rotation ; i++ ) {
  62. if ( bigint_is_geq ( &temp_result, &temp_modulus ) )
  63. bigint_subtract ( &temp_modulus, &temp_result );
  64. bigint_ror ( &temp_modulus );
  65. }
  66. /* Resize result */
  67. bigint_shrink ( &temp_result, result );
  68. /* Sanity check */
  69. assert ( bigint_is_geq ( modulus, result ) );
  70. }
  71. /**
  72. * Perform modular exponentiation of big integers
  73. *
  74. * @v base0 Element 0 of big integer base
  75. * @v modulus0 Element 0 of big integer modulus
  76. * @v exponent0 Element 0 of big integer exponent
  77. * @v result0 Element 0 of big integer to hold result
  78. * @v size Number of elements in base, modulus, and result
  79. * @v exponent_size Number of elements in exponent
  80. */
  81. void bigint_mod_exp_raw ( const bigint_element_t *base0,
  82. const bigint_element_t *modulus0,
  83. const bigint_element_t *exponent0,
  84. bigint_element_t *result0,
  85. unsigned int size,
  86. unsigned int exponent_size ) {
  87. const bigint_t ( size ) __attribute__ (( may_alias )) *base =
  88. ( ( const void * ) base0 );
  89. const bigint_t ( size ) __attribute__ (( may_alias )) *modulus =
  90. ( ( const void * ) modulus0 );
  91. const bigint_t ( exponent_size ) __attribute__ (( may_alias ))
  92. *exponent = ( ( const void * ) exponent0 );
  93. bigint_t ( size ) __attribute__ (( may_alias )) *result =
  94. ( ( void * ) result0 );
  95. bigint_t ( size ) temp_base;
  96. bigint_t ( exponent_size ) temp_exponent;
  97. static const uint8_t start[1] = { 0x01 };
  98. memcpy ( &temp_base, base, sizeof ( temp_base ) );
  99. memcpy ( &temp_exponent, exponent, sizeof ( temp_exponent ) );
  100. bigint_init ( result, start, sizeof ( start ) );
  101. while ( ! bigint_is_zero ( &temp_exponent ) ) {
  102. if ( bigint_bit_is_set ( &temp_exponent, 0 ) ) {
  103. bigint_mod_multiply ( result, &temp_base,
  104. modulus, result );
  105. }
  106. bigint_ror ( &temp_exponent );
  107. bigint_mod_multiply ( &temp_base, &temp_base, modulus,
  108. &temp_base );
  109. }
  110. }