Back to index

lightning-sunbird  0.9+nobinonly
ecp_jm.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 prime 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  *   Stephen Fung <fungstep@hotmail.com>, 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 "ecp.h"
00040 #include "ecl-priv.h"
00041 #include "mplogic.h"
00042 #include <stdlib.h>
00043 
00044 /* Computes R = 2P.  Elliptic curve points P and R can be identical.  Uses 
00045  * Modified Jacobian coordinates.
00046  *
00047  * Assumes input is already field-encoded using field_enc, and returns 
00048  * output that is still field-encoded.
00049  *
00050  */
00051 mp_err
00052 ec_GFp_pt_dbl_jm(const mp_int *px, const mp_int *py, const mp_int *pz,
00053                              const mp_int *paz4, mp_int *rx, mp_int *ry, mp_int *rz,
00054                              mp_int *raz4, const ECGroup *group)
00055 {
00056        mp_err res = MP_OKAY;
00057        mp_int t0, t1, M, S;
00058 
00059        MP_DIGITS(&t0) = 0;
00060        MP_DIGITS(&t1) = 0;
00061        MP_DIGITS(&M) = 0;
00062        MP_DIGITS(&S) = 0;
00063        MP_CHECKOK(mp_init(&t0));
00064        MP_CHECKOK(mp_init(&t1));
00065        MP_CHECKOK(mp_init(&M));
00066        MP_CHECKOK(mp_init(&S));
00067 
00068        /* Check for point at infinity */
00069        if (ec_GFp_pt_is_inf_jac(px, py, pz) == MP_YES) {
00070               /* Set r = pt at infinity by setting rz = 0 */
00071 
00072               MP_CHECKOK(ec_GFp_pt_set_inf_jac(rx, ry, rz));
00073               goto CLEANUP;
00074        }
00075 
00076        /* M = 3 (px^2) + a*(pz^4) */
00077        MP_CHECKOK(group->meth->field_sqr(px, &t0, group->meth));
00078        MP_CHECKOK(group->meth->field_add(&t0, &t0, &M, group->meth));
00079        MP_CHECKOK(group->meth->field_add(&t0, &M, &t0, group->meth));
00080        MP_CHECKOK(group->meth->field_add(&t0, paz4, &M, group->meth));
00081 
00082        /* rz = 2 * py * pz */
00083        MP_CHECKOK(group->meth->field_mul(py, pz, rz, group->meth));
00084        MP_CHECKOK(group->meth->field_add(rz, rz, rz, group->meth));
00085 
00086        /* t0 = 2y^2 , t1 = 8y^4 */
00087        MP_CHECKOK(group->meth->field_sqr(py, &t0, group->meth));
00088        MP_CHECKOK(group->meth->field_add(&t0, &t0, &t0, group->meth));
00089        MP_CHECKOK(group->meth->field_sqr(&t0, &t1, group->meth));
00090        MP_CHECKOK(group->meth->field_add(&t1, &t1, &t1, group->meth));
00091 
00092        /* S = 4 * px * py^2 = 2 * px * t0 */
00093        MP_CHECKOK(group->meth->field_mul(px, &t0, &S, group->meth));
00094        MP_CHECKOK(group->meth->field_add(&S, &S, &S, group->meth));
00095 
00096        /* rx = M^2 - 2S */
00097        MP_CHECKOK(group->meth->field_sqr(&M, rx, group->meth));
00098        MP_CHECKOK(group->meth->field_sub(rx, &S, rx, group->meth));
00099        MP_CHECKOK(group->meth->field_sub(rx, &S, rx, group->meth));
00100 
00101        /* ry = M * (S - rx) - t1 */
00102        MP_CHECKOK(group->meth->field_sub(&S, rx, ry, group->meth));
00103        MP_CHECKOK(group->meth->field_mul(ry, &M, ry, group->meth));
00104        MP_CHECKOK(group->meth->field_sub(ry, &t1, ry, group->meth));
00105 
00106        /* ra*z^4 = 2*t1*(apz4) */
00107        MP_CHECKOK(group->meth->field_mul(paz4, &t1, raz4, group->meth));
00108        MP_CHECKOK(group->meth->field_add(raz4, raz4, raz4, group->meth));
00109 
00110   CLEANUP:
00111        mp_clear(&t0);
00112        mp_clear(&t1);
00113        mp_clear(&M);
00114        mp_clear(&S);
00115        return res;
00116 }
00117 
00118 /* Computes R = P + Q where R is (rx, ry, rz), P is (px, py, pz) and Q is
00119  * (qx, qy, 1).  Elliptic curve points P, Q, and R can all be identical.
00120  * Uses mixed Modified_Jacobian-affine coordinates. Assumes input is
00121  * already field-encoded using field_enc, and returns output that is still
00122  * field-encoded. */
00123 mp_err
00124 ec_GFp_pt_add_jm_aff(const mp_int *px, const mp_int *py, const mp_int *pz,
00125                                     const mp_int *paz4, const mp_int *qx,
00126                                     const mp_int *qy, mp_int *rx, mp_int *ry, mp_int *rz,
00127                                     mp_int *raz4, const ECGroup *group)
00128 {
00129        mp_err res = MP_OKAY;
00130        mp_int A, B, C, D, C2, C3;
00131 
00132        MP_DIGITS(&A) = 0;
00133        MP_DIGITS(&B) = 0;
00134        MP_DIGITS(&C) = 0;
00135        MP_DIGITS(&D) = 0;
00136        MP_DIGITS(&C2) = 0;
00137        MP_DIGITS(&C3) = 0;
00138        MP_CHECKOK(mp_init(&A));
00139        MP_CHECKOK(mp_init(&B));
00140        MP_CHECKOK(mp_init(&C));
00141        MP_CHECKOK(mp_init(&D));
00142        MP_CHECKOK(mp_init(&C2));
00143        MP_CHECKOK(mp_init(&C3));
00144 
00145        /* If either P or Q is the point at infinity, then return the other
00146         * point */
00147        if (ec_GFp_pt_is_inf_jac(px, py, pz) == MP_YES) {
00148               MP_CHECKOK(ec_GFp_pt_aff2jac(qx, qy, rx, ry, rz, group));
00149               MP_CHECKOK(group->meth->field_sqr(rz, raz4, group->meth));
00150               MP_CHECKOK(group->meth->field_sqr(raz4, raz4, group->meth));
00151               MP_CHECKOK(group->meth->
00152                                field_mul(raz4, &group->curvea, raz4, group->meth));
00153               goto CLEANUP;
00154        }
00155        if (ec_GFp_pt_is_inf_aff(qx, qy) == MP_YES) {
00156               MP_CHECKOK(mp_copy(px, rx));
00157               MP_CHECKOK(mp_copy(py, ry));
00158               MP_CHECKOK(mp_copy(pz, rz));
00159               MP_CHECKOK(mp_copy(paz4, raz4));
00160               goto CLEANUP;
00161        }
00162 
00163        /* A = qx * pz^2, B = qy * pz^3 */
00164        MP_CHECKOK(group->meth->field_sqr(pz, &A, group->meth));
00165        MP_CHECKOK(group->meth->field_mul(&A, pz, &B, group->meth));
00166        MP_CHECKOK(group->meth->field_mul(&A, qx, &A, group->meth));
00167        MP_CHECKOK(group->meth->field_mul(&B, qy, &B, group->meth));
00168 
00169        /* C = A - px, D = B - py */
00170        MP_CHECKOK(group->meth->field_sub(&A, px, &C, group->meth));
00171        MP_CHECKOK(group->meth->field_sub(&B, py, &D, group->meth));
00172 
00173        /* C2 = C^2, C3 = C^3 */
00174        MP_CHECKOK(group->meth->field_sqr(&C, &C2, group->meth));
00175        MP_CHECKOK(group->meth->field_mul(&C, &C2, &C3, group->meth));
00176 
00177        /* rz = pz * C */
00178        MP_CHECKOK(group->meth->field_mul(pz, &C, rz, group->meth));
00179 
00180        /* C = px * C^2 */
00181        MP_CHECKOK(group->meth->field_mul(px, &C2, &C, group->meth));
00182        /* A = D^2 */
00183        MP_CHECKOK(group->meth->field_sqr(&D, &A, group->meth));
00184 
00185        /* rx = D^2 - (C^3 + 2 * (px * C^2)) */
00186        MP_CHECKOK(group->meth->field_add(&C, &C, rx, group->meth));
00187        MP_CHECKOK(group->meth->field_add(&C3, rx, rx, group->meth));
00188        MP_CHECKOK(group->meth->field_sub(&A, rx, rx, group->meth));
00189 
00190        /* C3 = py * C^3 */
00191        MP_CHECKOK(group->meth->field_mul(py, &C3, &C3, group->meth));
00192 
00193        /* ry = D * (px * C^2 - rx) - py * C^3 */
00194        MP_CHECKOK(group->meth->field_sub(&C, rx, ry, group->meth));
00195        MP_CHECKOK(group->meth->field_mul(&D, ry, ry, group->meth));
00196        MP_CHECKOK(group->meth->field_sub(ry, &C3, ry, group->meth));
00197 
00198        /* raz4 = a * rz^4 */
00199        MP_CHECKOK(group->meth->field_sqr(rz, raz4, group->meth));
00200        MP_CHECKOK(group->meth->field_sqr(raz4, raz4, group->meth));
00201        MP_CHECKOK(group->meth->
00202                         field_mul(raz4, &group->curvea, raz4, group->meth));
00203 
00204   CLEANUP:
00205        mp_clear(&A);
00206        mp_clear(&B);
00207        mp_clear(&C);
00208        mp_clear(&D);
00209        mp_clear(&C2);
00210        mp_clear(&C3);
00211        return res;
00212 }
00213 
00214 /* Computes R = nP where R is (rx, ry) and P is the base point. Elliptic
00215  * curve points P and R can be identical. Uses mixed Modified-Jacobian
00216  * co-ordinates for doubling and Chudnovsky Jacobian coordinates for
00217  * additions. Assumes input is already field-encoded using field_enc, and
00218  * returns output that is still field-encoded. Uses 5-bit window NAF
00219  * method (algorithm 11) for scalar-point multiplication from Brown,
00220  * Hankerson, Lopez, Menezes. Software Implementation of the NIST Elliptic 
00221  * Curves Over Prime Fields. */
00222 mp_err
00223 ec_GFp_pt_mul_jm_wNAF(const mp_int *n, const mp_int *px, const mp_int *py,
00224                                      mp_int *rx, mp_int *ry, const ECGroup *group)
00225 {
00226        mp_err res = MP_OKAY;
00227        mp_int precomp[16][2], rz, tpx, tpy;
00228        mp_int raz4;
00229        signed char *naf = NULL;
00230        int i, orderBitSize;
00231 
00232        MP_DIGITS(&rz) = 0;
00233        MP_DIGITS(&raz4) = 0;
00234        MP_DIGITS(&tpx) = 0;
00235        MP_DIGITS(&tpy) = 0;
00236        for (i = 0; i < 16; i++) {
00237               MP_DIGITS(&precomp[i][0]) = 0;
00238               MP_DIGITS(&precomp[i][1]) = 0;
00239        }
00240 
00241        ARGCHK(group != NULL, MP_BADARG);
00242        ARGCHK((n != NULL) && (px != NULL) && (py != NULL), MP_BADARG);
00243 
00244        /* initialize precomputation table */
00245        MP_CHECKOK(mp_init(&tpx));
00246        MP_CHECKOK(mp_init(&tpy));;
00247        MP_CHECKOK(mp_init(&rz));
00248        MP_CHECKOK(mp_init(&raz4));
00249 
00250        for (i = 0; i < 16; i++) {
00251               MP_CHECKOK(mp_init(&precomp[i][0]));
00252               MP_CHECKOK(mp_init(&precomp[i][1]));
00253        }
00254 
00255        /* Set out[8] = P */
00256        MP_CHECKOK(mp_copy(px, &precomp[8][0]));
00257        MP_CHECKOK(mp_copy(py, &precomp[8][1]));
00258 
00259        /* Set (tpx, tpy) = 2P */
00260        MP_CHECKOK(group->
00261                         point_dbl(&precomp[8][0], &precomp[8][1], &tpx, &tpy,
00262                                            group));
00263 
00264        /* Set 3P, 5P, ..., 15P */
00265        for (i = 8; i < 15; i++) {
00266               MP_CHECKOK(group->
00267                                point_add(&precomp[i][0], &precomp[i][1], &tpx, &tpy,
00268                                                   &precomp[i + 1][0], &precomp[i + 1][1],
00269                                                   group));
00270        }
00271 
00272        /* Set -15P, -13P, ..., -P */
00273        for (i = 0; i < 8; i++) {
00274               MP_CHECKOK(mp_copy(&precomp[15 - i][0], &precomp[i][0]));
00275               MP_CHECKOK(group->meth->
00276                                field_neg(&precomp[15 - i][1], &precomp[i][1],
00277                                                   group->meth));
00278        }
00279 
00280        /* R = inf */
00281        MP_CHECKOK(ec_GFp_pt_set_inf_jac(rx, ry, &rz));
00282 
00283        orderBitSize = mpl_significant_bits(&group->order);
00284 
00285        /* Allocate memory for NAF */
00286        naf = (signed char *) malloc(sizeof(signed char) * (orderBitSize + 1));
00287        if (naf == NULL) {
00288               res = MP_MEM;
00289               goto CLEANUP;
00290        }
00291 
00292        /* Compute 5NAF */
00293        ec_compute_wNAF(naf, orderBitSize, n, 5);
00294 
00295        /* wNAF method */
00296        for (i = orderBitSize; i >= 0; i--) {
00297               /* R = 2R */
00298               ec_GFp_pt_dbl_jm(rx, ry, &rz, &raz4, rx, ry, &rz, &raz4, group);
00299               if (naf[i] != 0) {
00300                      ec_GFp_pt_add_jm_aff(rx, ry, &rz, &raz4,
00301                                                          &precomp[(naf[i] + 15) / 2][0],
00302                                                          &precomp[(naf[i] + 15) / 2][1], rx, ry,
00303                                                          &rz, &raz4, group);
00304               }
00305        }
00306 
00307        /* convert result S to affine coordinates */
00308        MP_CHECKOK(ec_GFp_pt_jac2aff(rx, ry, &rz, rx, ry, group));
00309 
00310   CLEANUP:
00311        for (i = 0; i < 16; i++) {
00312               mp_clear(&precomp[i][0]);
00313               mp_clear(&precomp[i][1]);
00314        }
00315        mp_clear(&tpx);
00316        mp_clear(&tpy);
00317        mp_clear(&rz);
00318        mp_clear(&raz4);
00319        free(naf);
00320        return res;
00321 }