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

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