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 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdint.h>
  25. #include <string.h>
  26. #include <assert.h>
  27. #include <ipxe/profile.h>
  28. #include <ipxe/bigint.h>
  29. /** @file
  30. *
  31. * Big integer support
  32. */
  33. /** Modular multiplication overall profiler */
  34. static struct profiler bigint_mod_multiply_profiler __profiler =
  35. { .name = "bigint_mod_multiply" };
  36. /** Modular multiplication multiply step profiler */
  37. static struct profiler bigint_mod_multiply_multiply_profiler __profiler =
  38. { .name = "bigint_mod_multiply.multiply" };
  39. /** Modular multiplication rescale step profiler */
  40. static struct profiler bigint_mod_multiply_rescale_profiler __profiler =
  41. { .name = "bigint_mod_multiply.rescale" };
  42. /** Modular multiplication subtract step profiler */
  43. static struct profiler bigint_mod_multiply_subtract_profiler __profiler =
  44. { .name = "bigint_mod_multiply.subtract" };
  45. /**
  46. * Perform modular multiplication of big integers
  47. *
  48. * @v multiplicand0 Element 0 of big integer to be multiplied
  49. * @v multiplier0 Element 0 of big integer to be multiplied
  50. * @v modulus0 Element 0 of big integer modulus
  51. * @v result0 Element 0 of big integer to hold result
  52. * @v size Number of elements in base, modulus, and result
  53. * @v tmp Temporary working space
  54. */
  55. void bigint_mod_multiply_raw ( const bigint_element_t *multiplicand0,
  56. const bigint_element_t *multiplier0,
  57. const bigint_element_t *modulus0,
  58. bigint_element_t *result0,
  59. unsigned int size, void *tmp ) {
  60. const bigint_t ( size ) __attribute__ (( may_alias )) *multiplicand =
  61. ( ( const void * ) multiplicand0 );
  62. const bigint_t ( size ) __attribute__ (( may_alias )) *multiplier =
  63. ( ( const void * ) multiplier0 );
  64. const bigint_t ( size ) __attribute__ (( may_alias )) *modulus =
  65. ( ( const void * ) modulus0 );
  66. bigint_t ( size ) __attribute__ (( may_alias )) *result =
  67. ( ( void * ) result0 );
  68. struct {
  69. bigint_t ( size * 2 ) result;
  70. bigint_t ( size * 2 ) modulus;
  71. } *temp = tmp;
  72. int rotation;
  73. int i;
  74. /* Start profiling */
  75. profile_start ( &bigint_mod_multiply_profiler );
  76. /* Sanity check */
  77. assert ( sizeof ( *temp ) == bigint_mod_multiply_tmp_len ( modulus ) );
  78. /* Perform multiplication */
  79. profile_start ( &bigint_mod_multiply_multiply_profiler );
  80. bigint_multiply ( multiplicand, multiplier, &temp->result );
  81. profile_stop ( &bigint_mod_multiply_multiply_profiler );
  82. /* Rescale modulus to match result */
  83. profile_start ( &bigint_mod_multiply_rescale_profiler );
  84. bigint_grow ( modulus, &temp->modulus );
  85. rotation = ( bigint_max_set_bit ( &temp->result ) -
  86. bigint_max_set_bit ( &temp->modulus ) );
  87. for ( i = 0 ; i < rotation ; i++ )
  88. bigint_rol ( &temp->modulus );
  89. profile_stop ( &bigint_mod_multiply_rescale_profiler );
  90. /* Subtract multiples of modulus */
  91. profile_start ( &bigint_mod_multiply_subtract_profiler );
  92. for ( i = 0 ; i <= rotation ; i++ ) {
  93. if ( bigint_is_geq ( &temp->result, &temp->modulus ) )
  94. bigint_subtract ( &temp->modulus, &temp->result );
  95. bigint_ror ( &temp->modulus );
  96. }
  97. profile_stop ( &bigint_mod_multiply_subtract_profiler );
  98. /* Resize result */
  99. bigint_shrink ( &temp->result, result );
  100. /* Sanity check */
  101. assert ( bigint_is_geq ( modulus, result ) );
  102. /* Stop profiling */
  103. profile_stop ( &bigint_mod_multiply_profiler );
  104. }
  105. /**
  106. * Perform modular exponentiation of big integers
  107. *
  108. * @v base0 Element 0 of big integer base
  109. * @v modulus0 Element 0 of big integer modulus
  110. * @v exponent0 Element 0 of big integer exponent
  111. * @v result0 Element 0 of big integer to hold result
  112. * @v size Number of elements in base, modulus, and result
  113. * @v exponent_size Number of elements in exponent
  114. * @v tmp Temporary working space
  115. */
  116. void bigint_mod_exp_raw ( const bigint_element_t *base0,
  117. const bigint_element_t *modulus0,
  118. const bigint_element_t *exponent0,
  119. bigint_element_t *result0,
  120. unsigned int size, unsigned int exponent_size,
  121. void *tmp ) {
  122. const bigint_t ( size ) __attribute__ (( may_alias )) *base =
  123. ( ( const void * ) base0 );
  124. const bigint_t ( size ) __attribute__ (( may_alias )) *modulus =
  125. ( ( const void * ) modulus0 );
  126. const bigint_t ( exponent_size ) __attribute__ (( may_alias ))
  127. *exponent = ( ( const void * ) exponent0 );
  128. bigint_t ( size ) __attribute__ (( may_alias )) *result =
  129. ( ( void * ) result0 );
  130. size_t mod_multiply_len = bigint_mod_multiply_tmp_len ( modulus );
  131. struct {
  132. bigint_t ( size ) base;
  133. bigint_t ( exponent_size ) exponent;
  134. uint8_t mod_multiply[mod_multiply_len];
  135. } *temp = tmp;
  136. static const uint8_t start[1] = { 0x01 };
  137. memcpy ( &temp->base, base, sizeof ( temp->base ) );
  138. memcpy ( &temp->exponent, exponent, sizeof ( temp->exponent ) );
  139. bigint_init ( result, start, sizeof ( start ) );
  140. while ( ! bigint_is_zero ( &temp->exponent ) ) {
  141. if ( bigint_bit_is_set ( &temp->exponent, 0 ) ) {
  142. bigint_mod_multiply ( result, &temp->base, modulus,
  143. result, temp->mod_multiply );
  144. }
  145. bigint_ror ( &temp->exponent );
  146. bigint_mod_multiply ( &temp->base, &temp->base, modulus,
  147. &temp->base, temp->mod_multiply );
  148. }
  149. }