Back to index

lightning-sunbird  0.9+nobinonly
ec2_aff.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  *   Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "ec2.h"
00040 #include "mplogic.h"
00041 #include "mp_gf2m.h"
00042 #include <stdlib.h>
00043 
00044 /* Checks if point P(px, py) is at infinity.  Uses affine coordinates. */
00045 mp_err
00046 ec_GF2m_pt_is_inf_aff(const mp_int *px, const mp_int *py)
00047 {
00048 
00049        if ((mp_cmp_z(px) == 0) && (mp_cmp_z(py) == 0)) {
00050               return MP_YES;
00051        } else {
00052               return MP_NO;
00053        }
00054 
00055 }
00056 
00057 /* Sets P(px, py) to be the point at infinity.  Uses affine coordinates. */
00058 mp_err
00059 ec_GF2m_pt_set_inf_aff(mp_int *px, mp_int *py)
00060 {
00061        mp_zero(px);
00062        mp_zero(py);
00063        return MP_OKAY;
00064 }
00065 
00066 /* Computes R = P + Q based on IEEE P1363 A.10.2. Elliptic curve points P, 
00067  * Q, and R can all be identical. Uses affine coordinates. */
00068 mp_err
00069 ec_GF2m_pt_add_aff(const mp_int *px, const mp_int *py, const mp_int *qx,
00070                                const mp_int *qy, mp_int *rx, mp_int *ry,
00071                                const ECGroup *group)
00072 {
00073        mp_err res = MP_OKAY;
00074        mp_int lambda, tempx, tempy;
00075 
00076        MP_DIGITS(&lambda) = 0;
00077        MP_DIGITS(&tempx) = 0;
00078        MP_DIGITS(&tempy) = 0;
00079        MP_CHECKOK(mp_init(&lambda));
00080        MP_CHECKOK(mp_init(&tempx));
00081        MP_CHECKOK(mp_init(&tempy));
00082        /* if P = inf, then R = Q */
00083        if (ec_GF2m_pt_is_inf_aff(px, py) == 0) {
00084               MP_CHECKOK(mp_copy(qx, rx));
00085               MP_CHECKOK(mp_copy(qy, ry));
00086               res = MP_OKAY;
00087               goto CLEANUP;
00088        }
00089        /* if Q = inf, then R = P */
00090        if (ec_GF2m_pt_is_inf_aff(qx, qy) == 0) {
00091               MP_CHECKOK(mp_copy(px, rx));
00092               MP_CHECKOK(mp_copy(py, ry));
00093               res = MP_OKAY;
00094               goto CLEANUP;
00095        }
00096        /* if px != qx, then lambda = (py+qy) / (px+qx), tempx = a + lambda^2
00097         * + lambda + px + qx */
00098        if (mp_cmp(px, qx) != 0) {
00099               MP_CHECKOK(group->meth->field_add(py, qy, &tempy, group->meth));
00100               MP_CHECKOK(group->meth->field_add(px, qx, &tempx, group->meth));
00101               MP_CHECKOK(group->meth->
00102                                field_div(&tempy, &tempx, &lambda, group->meth));
00103               MP_CHECKOK(group->meth->field_sqr(&lambda, &tempx, group->meth));
00104               MP_CHECKOK(group->meth->
00105                                field_add(&tempx, &lambda, &tempx, group->meth));
00106               MP_CHECKOK(group->meth->
00107                                field_add(&tempx, &group->curvea, &tempx, group->meth));
00108               MP_CHECKOK(group->meth->
00109                                field_add(&tempx, px, &tempx, group->meth));
00110               MP_CHECKOK(group->meth->
00111                                field_add(&tempx, qx, &tempx, group->meth));
00112        } else {
00113               /* if py != qy or qx = 0, then R = inf */
00114               if (((mp_cmp(py, qy) != 0)) || (mp_cmp_z(qx) == 0)) {
00115                      mp_zero(rx);
00116                      mp_zero(ry);
00117                      res = MP_OKAY;
00118                      goto CLEANUP;
00119               }
00120               /* lambda = qx + qy / qx */
00121               MP_CHECKOK(group->meth->field_div(qy, qx, &lambda, group->meth));
00122               MP_CHECKOK(group->meth->
00123                                field_add(&lambda, qx, &lambda, group->meth));
00124               /* tempx = a + lambda^2 + lambda */
00125               MP_CHECKOK(group->meth->field_sqr(&lambda, &tempx, group->meth));
00126               MP_CHECKOK(group->meth->
00127                                field_add(&tempx, &lambda, &tempx, group->meth));
00128               MP_CHECKOK(group->meth->
00129                                field_add(&tempx, &group->curvea, &tempx, group->meth));
00130        }
00131        /* ry = (qx + tempx) * lambda + tempx + qy */
00132        MP_CHECKOK(group->meth->field_add(qx, &tempx, &tempy, group->meth));
00133        MP_CHECKOK(group->meth->
00134                         field_mul(&tempy, &lambda, &tempy, group->meth));
00135        MP_CHECKOK(group->meth->
00136                         field_add(&tempy, &tempx, &tempy, group->meth));
00137        MP_CHECKOK(group->meth->field_add(&tempy, qy, ry, group->meth));
00138        /* rx = tempx */
00139        MP_CHECKOK(mp_copy(&tempx, rx));
00140 
00141   CLEANUP:
00142        mp_clear(&lambda);
00143        mp_clear(&tempx);
00144        mp_clear(&tempy);
00145        return res;
00146 }
00147 
00148 /* Computes R = P - Q. Elliptic curve points P, Q, and R can all be
00149  * identical. Uses affine coordinates. */
00150 mp_err
00151 ec_GF2m_pt_sub_aff(const mp_int *px, const mp_int *py, const mp_int *qx,
00152                                const mp_int *qy, mp_int *rx, mp_int *ry,
00153                                const ECGroup *group)
00154 {
00155        mp_err res = MP_OKAY;
00156        mp_int nqy;
00157 
00158        MP_DIGITS(&nqy) = 0;
00159        MP_CHECKOK(mp_init(&nqy));
00160        /* nqy = qx+qy */
00161        MP_CHECKOK(group->meth->field_add(qx, qy, &nqy, group->meth));
00162        MP_CHECKOK(group->point_add(px, py, qx, &nqy, rx, ry, group));
00163   CLEANUP:
00164        mp_clear(&nqy);
00165        return res;
00166 }
00167 
00168 /* Computes R = 2P. Elliptic curve points P and R can be identical. Uses
00169  * affine coordinates. */
00170 mp_err
00171 ec_GF2m_pt_dbl_aff(const mp_int *px, const mp_int *py, mp_int *rx,
00172                                mp_int *ry, const ECGroup *group)
00173 {
00174        return group->point_add(px, py, px, py, rx, ry, group);
00175 }
00176 
00177 /* by default, this routine is unused and thus doesn't need to be compiled */
00178 #ifdef ECL_ENABLE_GF2M_PT_MUL_AFF
00179 /* Computes R = nP based on IEEE P1363 A.10.3. Elliptic curve points P and 
00180  * R can be identical. Uses affine coordinates. */
00181 mp_err
00182 ec_GF2m_pt_mul_aff(const mp_int *n, const mp_int *px, const mp_int *py,
00183                                mp_int *rx, mp_int *ry, const ECGroup *group)
00184 {
00185        mp_err res = MP_OKAY;
00186        mp_int k, k3, qx, qy, sx, sy;
00187        int b1, b3, i, l;
00188 
00189        MP_DIGITS(&k) = 0;
00190        MP_DIGITS(&k3) = 0;
00191        MP_DIGITS(&qx) = 0;
00192        MP_DIGITS(&qy) = 0;
00193        MP_DIGITS(&sx) = 0;
00194        MP_DIGITS(&sy) = 0;
00195        MP_CHECKOK(mp_init(&k));
00196        MP_CHECKOK(mp_init(&k3));
00197        MP_CHECKOK(mp_init(&qx));
00198        MP_CHECKOK(mp_init(&qy));
00199        MP_CHECKOK(mp_init(&sx));
00200        MP_CHECKOK(mp_init(&sy));
00201 
00202        /* if n = 0 then r = inf */
00203        if (mp_cmp_z(n) == 0) {
00204               mp_zero(rx);
00205               mp_zero(ry);
00206               res = MP_OKAY;
00207               goto CLEANUP;
00208        }
00209        /* Q = P, k = n */
00210        MP_CHECKOK(mp_copy(px, &qx));
00211        MP_CHECKOK(mp_copy(py, &qy));
00212        MP_CHECKOK(mp_copy(n, &k));
00213        /* if n < 0 then Q = -Q, k = -k */
00214        if (mp_cmp_z(n) < 0) {
00215               MP_CHECKOK(group->meth->field_add(&qx, &qy, &qy, group->meth));
00216               MP_CHECKOK(mp_neg(&k, &k));
00217        }
00218 #ifdef ECL_DEBUG                          /* basic double and add method */
00219        l = mpl_significant_bits(&k) - 1;
00220        MP_CHECKOK(mp_copy(&qx, &sx));
00221        MP_CHECKOK(mp_copy(&qy, &sy));
00222        for (i = l - 1; i >= 0; i--) {
00223               /* S = 2S */
00224               MP_CHECKOK(group->point_dbl(&sx, &sy, &sx, &sy, group));
00225               /* if k_i = 1, then S = S + Q */
00226               if (mpl_get_bit(&k, i) != 0) {
00227                      MP_CHECKOK(group->
00228                                       point_add(&sx, &sy, &qx, &qy, &sx, &sy, group));
00229               }
00230        }
00231 #else                                            /* double and add/subtract method from
00232                                                          * standard */
00233        /* k3 = 3 * k */
00234        MP_CHECKOK(mp_set_int(&k3, 3));
00235        MP_CHECKOK(mp_mul(&k, &k3, &k3));
00236        /* S = Q */
00237        MP_CHECKOK(mp_copy(&qx, &sx));
00238        MP_CHECKOK(mp_copy(&qy, &sy));
00239        /* l = index of high order bit in binary representation of 3*k */
00240        l = mpl_significant_bits(&k3) - 1;
00241        /* for i = l-1 downto 1 */
00242        for (i = l - 1; i >= 1; i--) {
00243               /* S = 2S */
00244               MP_CHECKOK(group->point_dbl(&sx, &sy, &sx, &sy, group));
00245               b3 = MP_GET_BIT(&k3, i);
00246               b1 = MP_GET_BIT(&k, i);
00247               /* if k3_i = 1 and k_i = 0, then S = S + Q */
00248               if ((b3 == 1) && (b1 == 0)) {
00249                      MP_CHECKOK(group->
00250                                       point_add(&sx, &sy, &qx, &qy, &sx, &sy, group));
00251                      /* if k3_i = 0 and k_i = 1, then S = S - Q */
00252               } else if ((b3 == 0) && (b1 == 1)) {
00253                      MP_CHECKOK(group->
00254                                       point_sub(&sx, &sy, &qx, &qy, &sx, &sy, group));
00255               }
00256        }
00257 #endif
00258        /* output S */
00259        MP_CHECKOK(mp_copy(&sx, rx));
00260        MP_CHECKOK(mp_copy(&sy, ry));
00261 
00262   CLEANUP:
00263        mp_clear(&k);
00264        mp_clear(&k3);
00265        mp_clear(&qx);
00266        mp_clear(&qy);
00267        mp_clear(&sx);
00268        mp_clear(&sy);
00269        return res;
00270 }
00271 #endif
00272 
00273 /* Validates a point on a GF2m curve. */
00274 mp_err 
00275 ec_GF2m_validate_point(const mp_int *px, const mp_int *py, const ECGroup *group)
00276 {
00277        mp_err res = MP_NO;
00278        mp_int accl, accr, tmp, pxt, pyt;
00279 
00280        MP_DIGITS(&accl) = 0;
00281        MP_DIGITS(&accr) = 0;
00282        MP_DIGITS(&tmp) = 0;
00283        MP_DIGITS(&pxt) = 0;
00284        MP_DIGITS(&pyt) = 0;
00285        MP_CHECKOK(mp_init(&accl));
00286        MP_CHECKOK(mp_init(&accr));
00287        MP_CHECKOK(mp_init(&tmp));
00288        MP_CHECKOK(mp_init(&pxt));
00289        MP_CHECKOK(mp_init(&pyt));
00290 
00291     /* 1: Verify that publicValue is not the point at infinity */
00292        if (ec_GF2m_pt_is_inf_aff(px, py) == MP_YES) {
00293               res = MP_NO;
00294               goto CLEANUP;
00295        }
00296     /* 2: Verify that the coordinates of publicValue are elements 
00297      *    of the field.
00298      */
00299        if ((MP_SIGN(px) == MP_NEG) || (mp_cmp(px, &group->meth->irr) >= 0) || 
00300               (MP_SIGN(py) == MP_NEG) || (mp_cmp(py, &group->meth->irr) >= 0)) {
00301               res = MP_NO;
00302               goto CLEANUP;
00303        }
00304     /* 3: Verify that publicValue is on the curve. */
00305        if (group->meth->field_enc) {
00306               group->meth->field_enc(px, &pxt, group->meth);
00307               group->meth->field_enc(py, &pyt, group->meth);
00308        } else {
00309               mp_copy(px, &pxt);
00310               mp_copy(py, &pyt);
00311        }
00312        /* left-hand side: y^2 + x*y  */
00313        MP_CHECKOK( group->meth->field_sqr(&pyt, &accl, group->meth) );
00314        MP_CHECKOK( group->meth->field_mul(&pxt, &pyt, &tmp, group->meth) );
00315        MP_CHECKOK( group->meth->field_add(&accl, &tmp, &accl, group->meth) );
00316        /* right-hand side: x^3 + a*x^2 + b */
00317        MP_CHECKOK( group->meth->field_sqr(&pxt, &tmp, group->meth) );
00318        MP_CHECKOK( group->meth->field_mul(&pxt, &tmp, &accr, group->meth) );
00319        MP_CHECKOK( group->meth->field_mul(&group->curvea, &tmp, &tmp, group->meth) );
00320        MP_CHECKOK( group->meth->field_add(&tmp, &accr, &accr, group->meth) );
00321        MP_CHECKOK( group->meth->field_add(&accr, &group->curveb, &accr, group->meth) );
00322        /* check LHS - RHS == 0 */
00323        MP_CHECKOK( group->meth->field_add(&accl, &accr, &accr, group->meth) );
00324        if (mp_cmp_z(&accr) != 0) {
00325               res = MP_NO;
00326               goto CLEANUP;
00327        }
00328     /* 4: Verify that the order of the curve times the publicValue
00329      *    is the point at infinity.
00330      */
00331        MP_CHECKOK( ECPoint_mul(group, &group->order, px, py, &pxt, &pyt) );
00332        if (ec_GF2m_pt_is_inf_aff(&pxt, &pyt) != MP_YES) {
00333               res = MP_NO;
00334               goto CLEANUP;
00335        }
00336 
00337        res = MP_YES;
00338 
00339 CLEANUP:
00340        mp_clear(&accl);
00341        mp_clear(&accr);
00342        mp_clear(&tmp);
00343        mp_clear(&pxt);
00344        mp_clear(&pyt);
00345        return res;
00346 }