BN_mod_mul_montgomery.pod revision 59191
1233294Sstas=pod
272445Sassar
355682Smarkm=head1 NAME
455682Smarkm
5233294SstasBN_mod_mul_montgomery, BN_MONT_CTX_new, BN_MONT_CTX_init,
6233294SstasBN_MONT_CTX_free, BN_MONT_CTX_set, BN_MONT_CTX_copy,
7233294SstasBN_from_montgomery, BN_to_montgomery - Montgomery multiplication
855682Smarkm
9233294Sstas=head1 SYNOPSIS
10233294Sstas
11233294Sstas #include <openssl/bn.h>
1255682Smarkm
13120945Snectar BN_MONT_CTX *BN_MONT_CTX_new(void);
1455682Smarkm void BN_MONT_CTX_init(BN_MONT_CTX *ctx);
1555682Smarkm void BN_MONT_CTX_free(BN_MONT_CTX *mont);
1655682Smarkm
1755682Smarkm int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *m, BN_CTX *ctx);
1855682Smarkm BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to, BN_MONT_CTX *from);
1955682Smarkm
2055682Smarkm int BN_mod_mul_montgomery(BIGNUM *r, BIGNUM *a, BIGNUM *b,
2155682Smarkm         BN_MONT_CTX *mont, BN_CTX *ctx);
22178825Sdfr
2355682Smarkm int BN_from_montgomery(BIGNUM *r, BIGNUM *a, BN_MONT_CTX *mont,
2455682Smarkm         BN_CTX *ctx);
2555682Smarkm
2655682Smarkm int BN_to_montgomery(BIGNUM *r, BIGNUM *a, BN_MONT_CTX *mont,
2755682Smarkm         BN_CTX *ctx);
2855682Smarkm
2955682Smarkm=head1 DESCRIPTION
3055682Smarkm
3155682SmarkmThese functions implement Montgomery multiplication. They are used
3255682Smarkmautomatically when L<BN_mod_exp(3)|BN_mod_exp(3)> is called with suitable input,
3355682Smarkmbut they may be useful when several operations are to be performed
3455682Smarkmusing the same modulus.
3555682Smarkm
3655682SmarkmBN_MONT_CTX_new() allocates and initializes a B<BN_MONT_CTX> structure.
3778527SassarBN_MONT_CTX_init() initializes an existing uninitialized B<BN_MONT_CTX>.
3878527Sassar
3978527SassarBN_MONT_CTX_set() sets up the B<mont> structure from the modulus B<m>
4078527Sassarby precomputing its inverse and a value R.
4178527Sassar
4278527SassarBN_MONT_CTX_copy() copies the B<N_MONT_CTX> B<from> to B<to>.
4378527Sassar
44BN_MONT_CTX_free() frees the components of the B<BN_MONT_CTX>, and, if
45it was created by BN_MONT_CTX_new(), also the structure itself.
46
47BN_mod_mul_montgomery() computes Mont(B<a>,B<b>):=B<a>*B<b>*R^-1 and places
48the result in B<r>.
49
50BN_from_montgomery() performs the Montgomery reduction B<r> = B<a>*R^-1.
51
52BN_to_montgomery() computes Mont(B<a>,R^2).
53
54For all functions, B<ctx> is a previously allocated B<BN_CTX> used for
55temporary variables.
56
57The B<BN_MONT_CTX> structure is defined as follows:
58
59 typedef struct bn_mont_ctx_st
60        {
61        int ri;         /* number of bits in R */
62        BIGNUM RR;      /* R^2 (used to convert to Montgomery form) */
63        BIGNUM N;       /* The modulus */
64        BIGNUM Ni;      /* R*(1/R mod N) - N*Ni = 1
65                         * (Ni is only stored for bignum algorithm) */
66        BN_ULONG n0;    /* least significant word of Ni */
67        int flags;
68        } BN_MONT_CTX;
69
70BN_to_montgomery() is a macro.
71
72=head1 RETURN VALUES
73
74BN_MONT_CTX_new() returns the newly allocated B<BN_MONT_CTX>, and NULL
75on error.
76
77BN_MONT_CTX_init() and BN_MONT_CTX_free() have no return values.
78
79For the other functions, 1 is returned for success, 0 on error.
80The error codes can be obtained by L<ERR_get_error(3)|ERR_get_error(3)>.
81
82=head1 SEE ALSO
83
84L<bn(3)|bn(3)>, L<err(3)|err(3)>, L<BN_add(3)|BN_add(3)>,
85L<BN_CTX_new(3)|BN_CTX_new(3)>
86
87=head1 HISTORY
88
89BN_MONT_CTX_new(), BN_MONT_CTX_free(), BN_MONT_CTX_set(),
90BN_mod_mul_montgomery(), BN_from_montgomery() and BN_to_montgomery()
91are available in all versions of SSLeay and OpenSSL.
92
93BN_MONT_CTX_init() and BN_MONT_CTX_copy() were added in SSLeay 0.9.1b.
94
95=cut
96