2 * ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is the elliptic curve math library.
17 * The Initial Developer of the Original Code is
18 * Sun Microsystems, Inc.
19 * Portions created by the Initial Developer are Copyright (C) 2003
20 * the Initial Developer. All Rights Reserved.
23 * Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
40 * Use is subject to license terms.
42 * Sun elects to use this software under the MPL license.
45 #pragma ident "%Z%%M% %I% %E% SMI"
47 /* Uses Montgomery reduction for field arithmetic. See mpi/mpmontg.c for
48 * code implementation. */
60 /* Construct a generic GFMethod for arithmetic over prime fields with
63 GFMethod_consGFp_mont(const mp_int
*irr
)
67 GFMethod
*meth
= NULL
;
70 meth
= GFMethod_consGFp(irr
);
75 mmm
= (mp_mont_modulus
*) kmem_alloc(sizeof(mp_mont_modulus
),
78 mmm
= (mp_mont_modulus
*) malloc(sizeof(mp_mont_modulus
));
85 meth
->field_mul
= &ec_GFp_mul_mont
;
86 meth
->field_sqr
= &ec_GFp_sqr_mont
;
87 meth
->field_div
= &ec_GFp_div_mont
;
88 meth
->field_enc
= &ec_GFp_enc_mont
;
89 meth
->field_dec
= &ec_GFp_dec_mont
;
92 meth
->extra_free
= &ec_GFp_extra_free_mont
;
95 i
= mpl_significant_bits(&meth
->irr
);
96 i
+= MP_DIGIT_BIT
- 1;
97 mmm
->b
= i
- i
% MP_DIGIT_BIT
;
98 mmm
->n0prime
= 0 - s_mp_invmod_radix(MP_DIGIT(&meth
->irr
, 0));
101 if (res
!= MP_OKAY
) {
108 /* Wrapper functions for generic prime field arithmetic. */
110 /* Field multiplication using Montgomery reduction. */
112 ec_GFp_mul_mont(const mp_int
*a
, const mp_int
*b
, mp_int
*r
,
113 const GFMethod
*meth
)
115 mp_err res
= MP_OKAY
;
117 #ifdef MP_MONT_USE_MP_MUL
118 /* if MP_MONT_USE_MP_MUL is defined, then the function s_mp_mul_mont
119 * is not implemented and we have to use mp_mul and s_mp_redc directly
121 MP_CHECKOK(mp_mul(a
, b
, r
));
122 MP_CHECKOK(s_mp_redc(r
, (mp_mont_modulus
*) meth
->extra1
));
127 /* s_mp_mul_mont doesn't allow source and destination to be the same */
128 if ((a
== r
) || (b
== r
)) {
129 MP_CHECKOK(mp_init(&s
, FLAG(a
)));
130 MP_CHECKOK(s_mp_mul_mont
131 (a
, b
, &s
, (mp_mont_modulus
*) meth
->extra1
));
132 MP_CHECKOK(mp_copy(&s
, r
));
135 return s_mp_mul_mont(a
, b
, r
, (mp_mont_modulus
*) meth
->extra1
);
142 /* Field squaring using Montgomery reduction. */
144 ec_GFp_sqr_mont(const mp_int
*a
, mp_int
*r
, const GFMethod
*meth
)
146 return ec_GFp_mul_mont(a
, a
, r
, meth
);
149 /* Field division using Montgomery reduction. */
151 ec_GFp_div_mont(const mp_int
*a
, const mp_int
*b
, mp_int
*r
,
152 const GFMethod
*meth
)
154 mp_err res
= MP_OKAY
;
156 /* if A=aZ represents a encoded in montgomery coordinates with Z and #
157 * and \ respectively represent multiplication and division in
158 * montgomery coordinates, then A\B = (a/b)Z = (A/B)Z and Binv =
159 * (1/b)Z = (1/B)(Z^2) where B # Binv = Z */
160 MP_CHECKOK(ec_GFp_div(a
, b
, r
, meth
));
161 MP_CHECKOK(ec_GFp_enc_mont(r
, r
, meth
));
163 MP_CHECKOK(ec_GFp_enc_mont(r
, r
, meth
));
169 /* Encode a field element in Montgomery form. See s_mp_to_mont in
172 ec_GFp_enc_mont(const mp_int
*a
, mp_int
*r
, const GFMethod
*meth
)
174 mp_mont_modulus
*mmm
;
175 mp_err res
= MP_OKAY
;
177 mmm
= (mp_mont_modulus
*) meth
->extra1
;
178 MP_CHECKOK(mpl_lsh(a
, r
, mmm
->b
));
179 MP_CHECKOK(mp_mod(r
, &mmm
->N
, r
));
184 /* Decode a field element from Montgomery form. */
186 ec_GFp_dec_mont(const mp_int
*a
, mp_int
*r
, const GFMethod
*meth
)
188 mp_err res
= MP_OKAY
;
191 MP_CHECKOK(mp_copy(a
, r
));
193 MP_CHECKOK(s_mp_redc(r
, (mp_mont_modulus
*) meth
->extra1
));
198 /* Free the memory allocated to the extra fields of Montgomery GFMethod
201 ec_GFp_extra_free_mont(GFMethod
*meth
)
203 if (meth
->extra1
!= NULL
) {
205 kmem_free(meth
->extra1
, sizeof(mp_mont_modulus
));