Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

bigint.c 4.5KB

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