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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/bigint.h>
  28. /** @file
  29. *
  30. * Big integer support
  31. */
  32. /**
  33. * Perform modular multiplication of big integers
  34. *
  35. * @v multiplicand0 Element 0 of big integer to be multiplied
  36. * @v multiplier0 Element 0 of big integer to be multiplied
  37. * @v modulus0 Element 0 of big integer modulus
  38. * @v result0 Element 0 of big integer to hold result
  39. * @v size Number of elements in base, modulus, and result
  40. * @v tmp Temporary working space
  41. */
  42. void bigint_mod_multiply_raw ( const bigint_element_t *multiplicand0,
  43. const bigint_element_t *multiplier0,
  44. const bigint_element_t *modulus0,
  45. bigint_element_t *result0,
  46. unsigned int size, void *tmp ) {
  47. const bigint_t ( size ) __attribute__ (( may_alias )) *multiplicand =
  48. ( ( const void * ) multiplicand0 );
  49. const bigint_t ( size ) __attribute__ (( may_alias )) *multiplier =
  50. ( ( const void * ) multiplier0 );
  51. const bigint_t ( size ) __attribute__ (( may_alias )) *modulus =
  52. ( ( const void * ) modulus0 );
  53. bigint_t ( size ) __attribute__ (( may_alias )) *result =
  54. ( ( void * ) result0 );
  55. struct {
  56. bigint_t ( size * 2 ) result;
  57. bigint_t ( size * 2 ) modulus;
  58. } *temp = tmp;
  59. int rotation;
  60. int i;
  61. /* Sanity check */
  62. assert ( sizeof ( *temp ) == bigint_mod_multiply_tmp_len ( modulus ) );
  63. /* Perform multiplication */
  64. bigint_multiply ( multiplicand, multiplier, &temp->result );
  65. /* Rescale modulus to match result */
  66. bigint_grow ( modulus, &temp->modulus );
  67. rotation = ( bigint_max_set_bit ( &temp->result ) -
  68. bigint_max_set_bit ( &temp->modulus ) );
  69. for ( i = 0 ; i < rotation ; i++ )
  70. bigint_rol ( &temp->modulus );
  71. /* Subtract multiples of modulus */
  72. for ( i = 0 ; i <= rotation ; i++ ) {
  73. if ( bigint_is_geq ( &temp->result, &temp->modulus ) )
  74. bigint_subtract ( &temp->modulus, &temp->result );
  75. bigint_ror ( &temp->modulus );
  76. }
  77. /* Resize result */
  78. bigint_shrink ( &temp->result, result );
  79. /* Sanity check */
  80. assert ( bigint_is_geq ( modulus, result ) );
  81. }
  82. /**
  83. * Perform modular exponentiation of big integers
  84. *
  85. * @v base0 Element 0 of big integer base
  86. * @v modulus0 Element 0 of big integer modulus
  87. * @v exponent0 Element 0 of big integer exponent
  88. * @v result0 Element 0 of big integer to hold result
  89. * @v size Number of elements in base, modulus, and result
  90. * @v exponent_size Number of elements in exponent
  91. * @v tmp Temporary working space
  92. */
  93. void bigint_mod_exp_raw ( const bigint_element_t *base0,
  94. const bigint_element_t *modulus0,
  95. const bigint_element_t *exponent0,
  96. bigint_element_t *result0,
  97. unsigned int size, unsigned int exponent_size,
  98. void *tmp ) {
  99. const bigint_t ( size ) __attribute__ (( may_alias )) *base =
  100. ( ( const void * ) base0 );
  101. const bigint_t ( size ) __attribute__ (( may_alias )) *modulus =
  102. ( ( const void * ) modulus0 );
  103. const bigint_t ( exponent_size ) __attribute__ (( may_alias ))
  104. *exponent = ( ( const void * ) exponent0 );
  105. bigint_t ( size ) __attribute__ (( may_alias )) *result =
  106. ( ( void * ) result0 );
  107. size_t mod_multiply_len = bigint_mod_multiply_tmp_len ( modulus );
  108. struct {
  109. bigint_t ( size ) base;
  110. bigint_t ( exponent_size ) exponent;
  111. uint8_t mod_multiply[mod_multiply_len];
  112. } *temp = tmp;
  113. static const uint8_t start[1] = { 0x01 };
  114. memcpy ( &temp->base, base, sizeof ( temp->base ) );
  115. memcpy ( &temp->exponent, exponent, sizeof ( temp->exponent ) );
  116. bigint_init ( result, start, sizeof ( start ) );
  117. while ( ! bigint_is_zero ( &temp->exponent ) ) {
  118. if ( bigint_bit_is_set ( &temp->exponent, 0 ) ) {
  119. bigint_mod_multiply ( result, &temp->base, modulus,
  120. result, temp->mod_multiply );
  121. }
  122. bigint_ror ( &temp->exponent );
  123. bigint_mod_multiply ( &temp->base, &temp->base, modulus,
  124. &temp->base, temp->mod_multiply );
  125. }
  126. }