Back to index

lightning-sunbird  0.9+nobinonly
ec2_mont.c
Go to the documentation of this file.
00001 /* 
00002  * ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the elliptic curve math library for binary polynomial field curves.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Sun Microsystems, Inc.
00019  * Portions created by the Initial Developer are Copyright (C) 2003
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Sheueling Chang-Shantz <sheueling.chang@sun.com>,
00024  *   Stephen Fung <fungstep@hotmail.com>, and
00025  *   Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories.
00026  *
00027  * Alternatively, the contents of this file may be used under the terms of
00028  * either the GNU General Public License Version 2 or later (the "GPL"), or
00029  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00030  * in which case the provisions of the GPL or the LGPL are applicable instead
00031  * of those above. If you wish to allow use of your version of this file only
00032  * under the terms of either the GPL or the LGPL, and not to allow others to
00033  * use your version of this file under the terms of the MPL, indicate your
00034  * decision by deleting the provisions above and replace them with the notice
00035  * and other provisions required by the GPL or the LGPL. If you do not delete
00036  * the provisions above, a recipient may use your version of this file under
00037  * the terms of any one of the MPL, the GPL or the LGPL.
00038  *
00039  * ***** END LICENSE BLOCK ***** */
00040 
00041 #include "ec2.h"
00042 #include "mplogic.h"
00043 #include "mp_gf2m.h"
00044 #include <stdlib.h>
00045 
00046 /* Compute the x-coordinate x/z for the point 2*(x/z) in Montgomery
00047  * projective coordinates. Uses algorithm Mdouble in appendix of Lopez, J. 
00048  * and Dahab, R.  "Fast multiplication on elliptic curves over GF(2^m)
00049  * without precomputation". modified to not require precomputation of
00050  * c=b^{2^{m-1}}. */
00051 static mp_err
00052 gf2m_Mdouble(mp_int *x, mp_int *z, const ECGroup *group)
00053 {
00054        mp_err res = MP_OKAY;
00055        mp_int t1;
00056 
00057        MP_DIGITS(&t1) = 0;
00058        MP_CHECKOK(mp_init(&t1));
00059 
00060        MP_CHECKOK(group->meth->field_sqr(x, x, group->meth));
00061        MP_CHECKOK(group->meth->field_sqr(z, &t1, group->meth));
00062        MP_CHECKOK(group->meth->field_mul(x, &t1, z, group->meth));
00063        MP_CHECKOK(group->meth->field_sqr(x, x, group->meth));
00064        MP_CHECKOK(group->meth->field_sqr(&t1, &t1, group->meth));
00065        MP_CHECKOK(group->meth->
00066                         field_mul(&group->curveb, &t1, &t1, group->meth));
00067        MP_CHECKOK(group->meth->field_add(x, &t1, x, group->meth));
00068 
00069   CLEANUP:
00070        mp_clear(&t1);
00071        return res;
00072 }
00073 
00074 /* Compute the x-coordinate x1/z1 for the point (x1/z1)+(x2/x2) in
00075  * Montgomery projective coordinates. Uses algorithm Madd in appendix of
00076  * Lopex, J. and Dahab, R.  "Fast multiplication on elliptic curves over
00077  * GF(2^m) without precomputation". */
00078 static mp_err
00079 gf2m_Madd(const mp_int *x, mp_int *x1, mp_int *z1, mp_int *x2, mp_int *z2,
00080                 const ECGroup *group)
00081 {
00082        mp_err res = MP_OKAY;
00083        mp_int t1, t2;
00084 
00085        MP_DIGITS(&t1) = 0;
00086        MP_DIGITS(&t2) = 0;
00087        MP_CHECKOK(mp_init(&t1));
00088        MP_CHECKOK(mp_init(&t2));
00089 
00090        MP_CHECKOK(mp_copy(x, &t1));
00091        MP_CHECKOK(group->meth->field_mul(x1, z2, x1, group->meth));
00092        MP_CHECKOK(group->meth->field_mul(z1, x2, z1, group->meth));
00093        MP_CHECKOK(group->meth->field_mul(x1, z1, &t2, group->meth));
00094        MP_CHECKOK(group->meth->field_add(z1, x1, z1, group->meth));
00095        MP_CHECKOK(group->meth->field_sqr(z1, z1, group->meth));
00096        MP_CHECKOK(group->meth->field_mul(z1, &t1, x1, group->meth));
00097        MP_CHECKOK(group->meth->field_add(x1, &t2, x1, group->meth));
00098 
00099   CLEANUP:
00100        mp_clear(&t1);
00101        mp_clear(&t2);
00102        return res;
00103 }
00104 
00105 /* Compute the x, y affine coordinates from the point (x1, z1) (x2, z2)
00106  * using Montgomery point multiplication algorithm Mxy() in appendix of
00107  * Lopex, J. and Dahab, R.  "Fast multiplication on elliptic curves over
00108  * GF(2^m) without precomputation". Returns: 0 on error 1 if return value
00109  * should be the point at infinity 2 otherwise */
00110 static int
00111 gf2m_Mxy(const mp_int *x, const mp_int *y, mp_int *x1, mp_int *z1,
00112                mp_int *x2, mp_int *z2, const ECGroup *group)
00113 {
00114        mp_err res = MP_OKAY;
00115        int ret = 0;
00116        mp_int t3, t4, t5;
00117 
00118        MP_DIGITS(&t3) = 0;
00119        MP_DIGITS(&t4) = 0;
00120        MP_DIGITS(&t5) = 0;
00121        MP_CHECKOK(mp_init(&t3));
00122        MP_CHECKOK(mp_init(&t4));
00123        MP_CHECKOK(mp_init(&t5));
00124 
00125        if (mp_cmp_z(z1) == 0) {
00126               mp_zero(x2);
00127               mp_zero(z2);
00128               ret = 1;
00129               goto CLEANUP;
00130        }
00131 
00132        if (mp_cmp_z(z2) == 0) {
00133               MP_CHECKOK(mp_copy(x, x2));
00134               MP_CHECKOK(group->meth->field_add(x, y, z2, group->meth));
00135               ret = 2;
00136               goto CLEANUP;
00137        }
00138 
00139        MP_CHECKOK(mp_set_int(&t5, 1));
00140        if (group->meth->field_enc) {
00141               MP_CHECKOK(group->meth->field_enc(&t5, &t5, group->meth));
00142        }
00143 
00144        MP_CHECKOK(group->meth->field_mul(z1, z2, &t3, group->meth));
00145 
00146        MP_CHECKOK(group->meth->field_mul(z1, x, z1, group->meth));
00147        MP_CHECKOK(group->meth->field_add(z1, x1, z1, group->meth));
00148        MP_CHECKOK(group->meth->field_mul(z2, x, z2, group->meth));
00149        MP_CHECKOK(group->meth->field_mul(z2, x1, x1, group->meth));
00150        MP_CHECKOK(group->meth->field_add(z2, x2, z2, group->meth));
00151 
00152        MP_CHECKOK(group->meth->field_mul(z2, z1, z2, group->meth));
00153        MP_CHECKOK(group->meth->field_sqr(x, &t4, group->meth));
00154        MP_CHECKOK(group->meth->field_add(&t4, y, &t4, group->meth));
00155        MP_CHECKOK(group->meth->field_mul(&t4, &t3, &t4, group->meth));
00156        MP_CHECKOK(group->meth->field_add(&t4, z2, &t4, group->meth));
00157 
00158        MP_CHECKOK(group->meth->field_mul(&t3, x, &t3, group->meth));
00159        MP_CHECKOK(group->meth->field_div(&t5, &t3, &t3, group->meth));
00160        MP_CHECKOK(group->meth->field_mul(&t3, &t4, &t4, group->meth));
00161        MP_CHECKOK(group->meth->field_mul(x1, &t3, x2, group->meth));
00162        MP_CHECKOK(group->meth->field_add(x2, x, z2, group->meth));
00163 
00164        MP_CHECKOK(group->meth->field_mul(z2, &t4, z2, group->meth));
00165        MP_CHECKOK(group->meth->field_add(z2, y, z2, group->meth));
00166 
00167        ret = 2;
00168 
00169   CLEANUP:
00170        mp_clear(&t3);
00171        mp_clear(&t4);
00172        mp_clear(&t5);
00173        if (res == MP_OKAY) {
00174               return ret;
00175        } else {
00176               return 0;
00177        }
00178 }
00179 
00180 /* Computes R = nP based on algorithm 2P of Lopex, J. and Dahab, R.  "Fast 
00181  * multiplication on elliptic curves over GF(2^m) without
00182  * precomputation". Elliptic curve points P and R can be identical. Uses
00183  * Montgomery projective coordinates. */
00184 mp_err
00185 ec_GF2m_pt_mul_mont(const mp_int *n, const mp_int *px, const mp_int *py,
00186                                    mp_int *rx, mp_int *ry, const ECGroup *group)
00187 {
00188        mp_err res = MP_OKAY;
00189        mp_int x1, x2, z1, z2;
00190        int i, j;
00191        mp_digit top_bit, mask;
00192 
00193        MP_DIGITS(&x1) = 0;
00194        MP_DIGITS(&x2) = 0;
00195        MP_DIGITS(&z1) = 0;
00196        MP_DIGITS(&z2) = 0;
00197        MP_CHECKOK(mp_init(&x1));
00198        MP_CHECKOK(mp_init(&x2));
00199        MP_CHECKOK(mp_init(&z1));
00200        MP_CHECKOK(mp_init(&z2));
00201 
00202        /* if result should be point at infinity */
00203        if ((mp_cmp_z(n) == 0) || (ec_GF2m_pt_is_inf_aff(px, py) == MP_YES)) {
00204               MP_CHECKOK(ec_GF2m_pt_set_inf_aff(rx, ry));
00205               goto CLEANUP;
00206        }
00207 
00208        MP_CHECKOK(mp_copy(rx, &x2));      /* x2 = rx */
00209        MP_CHECKOK(mp_copy(ry, &z2));      /* z2 = ry */
00210 
00211        MP_CHECKOK(mp_copy(px, &x1));      /* x1 = px */
00212        MP_CHECKOK(mp_set_int(&z1, 1));    /* z1 = 1 */
00213        MP_CHECKOK(group->meth->field_sqr(&x1, &z2, group->meth));     /* z2 =
00214                                                                                                                  * x1^2 =
00215                                                                                                                  * x2^2 */
00216        MP_CHECKOK(group->meth->field_sqr(&z2, &x2, group->meth));
00217        MP_CHECKOK(group->meth->field_add(&x2, &group->curveb, &x2, group->meth));   /* x2 
00218                                                                                                                                              * = 
00219                                                                                                                                              * px^4 
00220                                                                                                                                              * + 
00221                                                                                                                                              * b 
00222                                                                                                                                              */
00223 
00224        /* find top-most bit and go one past it */
00225        i = MP_USED(n) - 1;
00226        j = MP_DIGIT_BIT - 1;
00227        top_bit = 1;
00228        top_bit <<= MP_DIGIT_BIT - 1;
00229        mask = top_bit;
00230        while (!(MP_DIGITS(n)[i] & mask)) {
00231               mask >>= 1;
00232               j--;
00233        }
00234        mask >>= 1;
00235        j--;
00236 
00237        /* if top most bit was at word break, go to next word */
00238        if (!mask) {
00239               i--;
00240               j = MP_DIGIT_BIT - 1;
00241               mask = top_bit;
00242        }
00243 
00244        for (; i >= 0; i--) {
00245               for (; j >= 0; j--) {
00246                      if (MP_DIGITS(n)[i] & mask) {
00247                             MP_CHECKOK(gf2m_Madd(px, &x1, &z1, &x2, &z2, group));
00248                             MP_CHECKOK(gf2m_Mdouble(&x2, &z2, group));
00249                      } else {
00250                             MP_CHECKOK(gf2m_Madd(px, &x2, &z2, &x1, &z1, group));
00251                             MP_CHECKOK(gf2m_Mdouble(&x1, &z1, group));
00252                      }
00253                      mask >>= 1;
00254               }
00255               j = MP_DIGIT_BIT - 1;
00256               mask = top_bit;
00257        }
00258 
00259        /* convert out of "projective" coordinates */
00260        i = gf2m_Mxy(px, py, &x1, &z1, &x2, &z2, group);
00261        if (i == 0) {
00262               res = MP_BADARG;
00263               goto CLEANUP;
00264        } else if (i == 1) {
00265               MP_CHECKOK(ec_GF2m_pt_set_inf_aff(rx, ry));
00266        } else {
00267               MP_CHECKOK(mp_copy(&x2, rx));
00268               MP_CHECKOK(mp_copy(&z2, ry));
00269        }
00270 
00271   CLEANUP:
00272        mp_clear(&x1);
00273        mp_clear(&x2);
00274        mp_clear(&z1);
00275        mp_clear(&z2);
00276        return res;
00277 }