BN_mod_mul_montgomery.pod 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. =pod
  2. =head1 NAME
  3. BN_mod_mul_montgomery, BN_MONT_CTX_new, BN_MONT_CTX_init,
  4. BN_MONT_CTX_free, BN_MONT_CTX_set, BN_MONT_CTX_copy,
  5. BN_from_montgomery, BN_to_montgomery - Montgomery multiplication
  6. =head1 SYNOPSIS
  7. #include <openssl/bn.h>
  8. BN_MONT_CTX *BN_MONT_CTX_new(void);
  9. void BN_MONT_CTX_init(BN_MONT_CTX *ctx);
  10. void BN_MONT_CTX_free(BN_MONT_CTX *mont);
  11. int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *m, BN_CTX *ctx);
  12. BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to, BN_MONT_CTX *from);
  13. int BN_mod_mul_montgomery(BIGNUM *r, BIGNUM *a, BIGNUM *b,
  14. BN_MONT_CTX *mont, BN_CTX *ctx);
  15. int BN_from_montgomery(BIGNUM *r, BIGNUM *a, BN_MONT_CTX *mont,
  16. BN_CTX *ctx);
  17. int BN_to_montgomery(BIGNUM *r, BIGNUM *a, BN_MONT_CTX *mont,
  18. BN_CTX *ctx);
  19. =head1 DESCRIPTION
  20. These functions implement Montgomery multiplication. They are used
  21. automatically when L<BN_mod_exp(3)|BN_mod_exp(3)> is called with suitable input,
  22. but they may be useful when several operations are to be performed
  23. using the same modulus.
  24. BN_MONT_CTX_new() allocates and initializes a B<BN_MONT_CTX> structure.
  25. BN_MONT_CTX_init() initializes an existing uninitialized B<BN_MONT_CTX>.
  26. BN_MONT_CTX_set() sets up the I<mont> structure from the modulus I<m>
  27. by precomputing its inverse and a value R.
  28. BN_MONT_CTX_copy() copies the B<BN_MONT_CTX> I<from> to I<to>.
  29. BN_MONT_CTX_free() frees the components of the B<BN_MONT_CTX>, and, if
  30. it was created by BN_MONT_CTX_new(), also the structure itself.
  31. BN_mod_mul_montgomery() computes Mont(I<a>,I<b>):=I<a>*I<b>*R^-1 and places
  32. the result in I<r>.
  33. BN_from_montgomery() performs the Montgomery reduction I<r> = I<a>*R^-1.
  34. BN_to_montgomery() computes Mont(I<a>,R^2), i.e. I<a>*R.
  35. Note that I<a> must be non-negative and smaller than the modulus.
  36. For all functions, I<ctx> is a previously allocated B<BN_CTX> used for
  37. temporary variables.
  38. The B<BN_MONT_CTX> structure is defined as follows:
  39. typedef struct bn_mont_ctx_st
  40. {
  41. int ri; /* number of bits in R */
  42. BIGNUM RR; /* R^2 (used to convert to Montgomery form) */
  43. BIGNUM N; /* The modulus */
  44. BIGNUM Ni; /* R*(1/R mod N) - N*Ni = 1
  45. * (Ni is only stored for bignum algorithm) */
  46. BN_ULONG n0; /* least significant word of Ni */
  47. int flags;
  48. } BN_MONT_CTX;
  49. BN_to_montgomery() is a macro.
  50. =head1 RETURN VALUES
  51. BN_MONT_CTX_new() returns the newly allocated B<BN_MONT_CTX>, and NULL
  52. on error.
  53. BN_MONT_CTX_init() and BN_MONT_CTX_free() have no return values.
  54. For the other functions, 1 is returned for success, 0 on error.
  55. The error codes can be obtained by L<ERR_get_error(3)|ERR_get_error(3)>.
  56. =head1 WARNING
  57. The inputs must be reduced modulo B<m>, otherwise the result will be
  58. outside the expected range.
  59. =head1 SEE ALSO
  60. L<bn(3)|bn(3)>, L<ERR_get_error(3)|ERR_get_error(3)>, L<BN_add(3)|BN_add(3)>,
  61. L<BN_CTX_new(3)|BN_CTX_new(3)>
  62. =head1 HISTORY
  63. BN_MONT_CTX_new(), BN_MONT_CTX_free(), BN_MONT_CTX_set(),
  64. BN_mod_mul_montgomery(), BN_from_montgomery() and BN_to_montgomery()
  65. are available in all versions of SSLeay and OpenSSL.
  66. BN_MONT_CTX_init() and BN_MONT_CTX_copy() were added in SSLeay 0.9.1b.
  67. =cut