Back to index

lightning-sunbird  0.9+nobinonly
ecp_224.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  *   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 "ecp.h"
00040 #include "mpi.h"
00041 #include "mplogic.h"
00042 #include "mpi-priv.h"
00043 #include <stdlib.h>
00044 
00045 #define ECP224_DIGITS ECL_CURVE_DIGITS(224)
00046 
00047 /* Fast modular reduction for p224 = 2^224 - 2^96 + 1.  a can be r. Uses
00048  * algorithm 7 from Brown, Hankerson, Lopez, Menezes. Software
00049  * Implementation of the NIST Elliptic Curves over Prime Fields. */
00050 mp_err
00051 ec_GFp_nistp224_mod(const mp_int *a, mp_int *r, const GFMethod *meth)
00052 {
00053        mp_err res = MP_OKAY;
00054        mp_size a_used = MP_USED(a);
00055 
00056        int    r3b;
00057        mp_digit carry;
00058 #ifdef ECL_THIRTY_TWO_BIT
00059         mp_digit a6a = 0, a6b = 0,
00060                 a5a = 0, a5b = 0, a4a = 0, a4b = 0, a3a = 0, a3b = 0;
00061         mp_digit r0a, r0b, r1a, r1b, r2a, r2b, r3a;
00062 #else
00063        mp_digit a6 = 0, a5 = 0, a4 = 0, a3b = 0, a5a = 0;
00064         mp_digit a6b = 0, a6a_a5b = 0, a5b = 0, a5a_a4b = 0, a4a_a3b = 0;
00065         mp_digit r0, r1, r2, r3;
00066 #endif
00067 
00068        /* reduction not needed if a is not larger than field size */
00069        if (a_used < ECP224_DIGITS) {
00070               if (a == r) return MP_OKAY;
00071               return mp_copy(a, r);
00072        }
00073        /* for polynomials larger than twice the field size, use regular
00074         * reduction */
00075        if (a_used > ECL_CURVE_DIGITS(224*2)) {
00076               MP_CHECKOK(mp_mod(a, &meth->irr, r));
00077        } else {
00078 #ifdef ECL_THIRTY_TWO_BIT
00079               /* copy out upper words of a */
00080               switch (a_used) {
00081               case 14:
00082                      a6b = MP_DIGIT(a, 13);
00083               case 13:
00084                      a6a = MP_DIGIT(a, 12);
00085               case 12:
00086                      a5b = MP_DIGIT(a, 11);
00087               case 11:
00088                      a5a = MP_DIGIT(a, 10);
00089               case 10:
00090                      a4b = MP_DIGIT(a, 9);
00091               case 9:
00092                      a4a = MP_DIGIT(a, 8);
00093               case 8:
00094                      a3b = MP_DIGIT(a, 7);
00095               }
00096               r3a = MP_DIGIT(a, 6);
00097               r2b= MP_DIGIT(a, 5);
00098               r2a= MP_DIGIT(a, 4);
00099               r1b = MP_DIGIT(a, 3);
00100               r1a = MP_DIGIT(a, 2);
00101               r0b = MP_DIGIT(a, 1);
00102               r0a = MP_DIGIT(a, 0);
00103 
00104 
00105               /* implement r = (a3a,a2,a1,a0)
00106                      +(a5a, a4,a3b,  0)
00107                      +(  0, a6,a5b,  0)
00108                      -(  0   0,    0|a6b, a6a|a5b )
00109                      -(  a6b, a6a|a5b, a5a|a4b, a4a|a3b ) */
00110               MP_ADD_CARRY (r1b, a3b, r1b, 0,     carry);
00111               MP_ADD_CARRY (r2a, a4a, r2a, carry, carry);
00112               MP_ADD_CARRY (r2b, a4b, r2b, carry, carry);
00113               MP_ADD_CARRY (r3a, a5a, r3a, carry, carry);
00114               r3b = carry;
00115               MP_ADD_CARRY (r1b, a5b, r1b, 0,     carry);
00116               MP_ADD_CARRY (r2a, a6a, r2a, carry, carry);
00117               MP_ADD_CARRY (r2b, a6b, r2b, carry, carry);
00118               MP_ADD_CARRY (r3a,   0, r3a, carry, carry);
00119               r3b += carry;
00120               MP_SUB_BORROW(r0a, a3b, r0a, 0,     carry);
00121               MP_SUB_BORROW(r0b, a4a, r0b, carry, carry);
00122               MP_SUB_BORROW(r1a, a4b, r1a, carry, carry);
00123               MP_SUB_BORROW(r1b, a5a, r1b, carry, carry);
00124               MP_SUB_BORROW(r2a, a5b, r2a, carry, carry);
00125               MP_SUB_BORROW(r2b, a6a, r2b, carry, carry);
00126               MP_SUB_BORROW(r3a, a6b, r3a, carry, carry);
00127               r3b -= carry;
00128               MP_SUB_BORROW(r0a, a5b, r0a, 0,     carry);
00129               MP_SUB_BORROW(r0b, a6a, r0b, carry, carry);
00130               MP_SUB_BORROW(r1a, a6b, r1a, carry, carry);
00131               if (carry) {
00132                      MP_SUB_BORROW(r1b, 0, r1b, carry, carry);
00133                      MP_SUB_BORROW(r2a, 0, r2a, carry, carry);
00134                      MP_SUB_BORROW(r2b, 0, r2b, carry, carry);
00135                      MP_SUB_BORROW(r3a, 0, r3a, carry, carry);
00136                      r3b -= carry;
00137               }
00138 
00139               while (r3b > 0) {
00140                      int tmp;
00141                      MP_ADD_CARRY(r1b, r3b, r1b, 0,     carry);
00142                      if (carry) {
00143                             MP_ADD_CARRY(r2a,  0, r2a, carry, carry);
00144                             MP_ADD_CARRY(r2b,  0, r2b, carry, carry);
00145                             MP_ADD_CARRY(r3a,  0, r3a, carry, carry);
00146                      }
00147                      tmp = carry;
00148                      MP_SUB_BORROW(r0a, r3b, r0a, 0,     carry);
00149                      if (carry) {
00150                             MP_SUB_BORROW(r0b, 0, r0b, carry, carry);
00151                             MP_SUB_BORROW(r1a, 0, r1a, carry, carry);
00152                             MP_SUB_BORROW(r1b, 0, r1b, carry, carry);
00153                             MP_SUB_BORROW(r2a, 0, r2a, carry, carry);
00154                             MP_SUB_BORROW(r2b, 0, r2b, carry, carry);
00155                             MP_SUB_BORROW(r3a, 0, r3a, carry, carry);
00156                             tmp -= carry;
00157                      }
00158                      r3b = tmp;
00159               }
00160 
00161               while (r3b < 0) {
00162                      mp_digit maxInt = MP_DIGIT_MAX;
00163                      MP_ADD_CARRY (r0a, 1, r0a, 0,     carry);
00164                      MP_ADD_CARRY (r0b, 0, r0b, carry, carry);
00165                      MP_ADD_CARRY (r1a, 0, r1a, carry, carry);
00166                      MP_ADD_CARRY (r1b, maxInt, r1b, carry, carry);
00167                      MP_ADD_CARRY (r2a, maxInt, r2a, carry, carry);
00168                      MP_ADD_CARRY (r2b, maxInt, r2b, carry, carry);
00169                      MP_ADD_CARRY (r3a, maxInt, r3a, carry, carry);
00170                      r3b += carry;
00171               }
00172               /* check for final reduction */
00173               /* now the only way we are over is if the top 4 words are all ones */
00174               if ((r3a == MP_DIGIT_MAX) && (r2b == MP_DIGIT_MAX)
00175                      && (r2a == MP_DIGIT_MAX) && (r1b == MP_DIGIT_MAX) &&
00176                       ((r1a != 0) || (r0b != 0) || (r0a != 0)) ) {
00177                      /* one last subraction */
00178                      MP_SUB_BORROW(r0a, 1, r0a, 0,     carry);
00179                      MP_SUB_BORROW(r0b, 0, r0b, carry, carry);
00180                      MP_SUB_BORROW(r1a, 0, r1a, carry, carry);
00181                      r1b = r2a = r2b = r3a = 0;
00182               }
00183 
00184 
00185               if (a != r) {
00186                      MP_CHECKOK(s_mp_pad(r, 7));
00187               }
00188               /* set the lower words of r */
00189               MP_SIGN(r) = MP_ZPOS;
00190               MP_USED(r) = 7;
00191               MP_DIGIT(r, 6) = r3a;
00192               MP_DIGIT(r, 5) = r2b;
00193               MP_DIGIT(r, 4) = r2a;
00194               MP_DIGIT(r, 3) = r1b;
00195               MP_DIGIT(r, 2) = r1a;
00196               MP_DIGIT(r, 1) = r0b;
00197               MP_DIGIT(r, 0) = r0a;
00198 #else
00199               /* copy out upper words of a */
00200               switch (a_used) {
00201               case 7:
00202                      a6 = MP_DIGIT(a, 6);
00203                      a6b = a6 >> 32;
00204                      a6a_a5b = a6 << 32;
00205               case 6:
00206                      a5 = MP_DIGIT(a, 5);
00207                      a5b = a5 >> 32;
00208                      a6a_a5b |= a5b;
00209                      a5b = a5b << 32;
00210                      a5a_a4b = a5 << 32;
00211                      a5a = a5 & 0xffffffff;
00212               case 5:
00213                      a4 = MP_DIGIT(a, 4);
00214                      a5a_a4b |= a4 >> 32;
00215                      a4a_a3b = a4 << 32;
00216               case 4:
00217                      a3b = MP_DIGIT(a, 3) >> 32;
00218                      a4a_a3b |= a3b;
00219                      a3b = a3b << 32;
00220               }
00221 
00222               r3 = MP_DIGIT(a, 3) & 0xffffffff;
00223               r2 = MP_DIGIT(a, 2);
00224               r1 = MP_DIGIT(a, 1);
00225               r0 = MP_DIGIT(a, 0);
00226 
00227               /* implement r = (a3a,a2,a1,a0)
00228                      +(a5a, a4,a3b,  0)
00229                      +(  0, a6,a5b,  0)
00230                      -(  0   0,    0|a6b, a6a|a5b )
00231                      -(  a6b, a6a|a5b, a5a|a4b, a4a|a3b ) */
00232               MP_ADD_CARRY (r1, a3b, r1, 0,     carry);
00233               MP_ADD_CARRY (r2, a4 , r2, carry, carry);
00234               MP_ADD_CARRY (r3, a5a, r3, carry, carry);
00235               MP_ADD_CARRY (r1, a5b, r1, 0,     carry);
00236               MP_ADD_CARRY (r2, a6 , r2, carry, carry);
00237               MP_ADD_CARRY (r3,   0, r3, carry, carry);
00238 
00239               MP_SUB_BORROW(r0, a4a_a3b, r0, 0,     carry);
00240               MP_SUB_BORROW(r1, a5a_a4b, r1, carry, carry);
00241               MP_SUB_BORROW(r2, a6a_a5b, r2, carry, carry);
00242               MP_SUB_BORROW(r3, a6b    , r3, carry, carry);
00243               MP_SUB_BORROW(r0, a6a_a5b, r0, 0,     carry);
00244               MP_SUB_BORROW(r1, a6b    , r1, carry, carry);
00245               if (carry) {
00246                      MP_SUB_BORROW(r2, 0, r2, carry, carry);
00247                      MP_SUB_BORROW(r3, 0, r3, carry, carry);
00248               }
00249 
00250 
00251               /* if the value is negative, r3 has a 2's complement 
00252                * high value */
00253               r3b = (int)(r3 >>32);
00254               while (r3b > 0) {
00255                      r3 &= 0xffffffff;
00256                      MP_ADD_CARRY(r1,((mp_digit)r3b) << 32, r1, 0, carry);
00257                      if (carry) {
00258                             MP_ADD_CARRY(r2,  0, r2, carry, carry);
00259                             MP_ADD_CARRY(r3,  0, r3, carry, carry);
00260                      }
00261                      MP_SUB_BORROW(r0, r3b, r0, 0, carry);
00262                      if (carry) {
00263                             MP_SUB_BORROW(r1, 0, r1, carry, carry);
00264                             MP_SUB_BORROW(r2, 0, r2, carry, carry);
00265                             MP_SUB_BORROW(r3, 0, r3, carry, carry);
00266                      }
00267                      r3b = (int)(r3 >>32);
00268               }
00269 
00270               while (r3b < 0) {
00271                      MP_ADD_CARRY (r0, 1, r0, 0,     carry);
00272                      MP_ADD_CARRY (r1, MP_DIGIT_MAX <<32, r1, carry, carry);
00273                      MP_ADD_CARRY (r2, MP_DIGIT_MAX, r2, carry, carry);
00274                      MP_ADD_CARRY (r3, MP_DIGIT_MAX >> 32, r3, carry, carry);
00275                      r3b = (int)(r3 >>32);
00276               }
00277               /* check for final reduction */
00278               /* now the only way we are over is if the top 4 words are all ones */
00279               if ((r3 == (MP_DIGIT_MAX >> 32)) && (r2 == MP_DIGIT_MAX)
00280                      && ((r1 & MP_DIGIT_MAX << 32)== MP_DIGIT_MAX << 32) &&
00281                       ((r1 != MP_DIGIT_MAX << 32 ) || (r0 != 0)) ) {
00282                      /* one last subraction */
00283                      MP_SUB_BORROW(r0, 1, r0, 0,     carry);
00284                      MP_SUB_BORROW(r1, 0, r1, carry, carry);
00285                      r2 = r3 = 0;
00286               }
00287 
00288 
00289               if (a != r) {
00290                      MP_CHECKOK(s_mp_pad(r, 4));
00291               }
00292               /* set the lower words of r */
00293               MP_SIGN(r) = MP_ZPOS;
00294               MP_USED(r) = 4;
00295               MP_DIGIT(r, 3) = r3;
00296               MP_DIGIT(r, 2) = r2;
00297               MP_DIGIT(r, 1) = r1;
00298               MP_DIGIT(r, 0) = r0;
00299 #endif
00300        }
00301 
00302   CLEANUP:
00303        return res;
00304 }
00305 
00306 /* Compute the square of polynomial a, reduce modulo p224. Store the
00307  * result in r.  r could be a.  Uses optimized modular reduction for p224. 
00308  */
00309 mp_err
00310 ec_GFp_nistp224_sqr(const mp_int *a, mp_int *r, const GFMethod *meth)
00311 {
00312        mp_err res = MP_OKAY;
00313 
00314        MP_CHECKOK(mp_sqr(a, r));
00315        MP_CHECKOK(ec_GFp_nistp224_mod(r, r, meth));
00316   CLEANUP:
00317        return res;
00318 }
00319 
00320 /* Compute the product of two polynomials a and b, reduce modulo p224.
00321  * Store the result in r.  r could be a or b; a could be b.  Uses
00322  * optimized modular reduction for p224. */
00323 mp_err
00324 ec_GFp_nistp224_mul(const mp_int *a, const mp_int *b, mp_int *r,
00325                                    const GFMethod *meth)
00326 {
00327        mp_err res = MP_OKAY;
00328 
00329        MP_CHECKOK(mp_mul(a, b, r));
00330        MP_CHECKOK(ec_GFp_nistp224_mod(r, r, meth));
00331   CLEANUP:
00332        return res;
00333 }
00334 
00335 /* Divides two field elements. If a is NULL, then returns the inverse of
00336  * b. */
00337 mp_err
00338 ec_GFp_nistp224_div(const mp_int *a, const mp_int *b, mp_int *r,
00339                  const GFMethod *meth)
00340 {
00341        mp_err res = MP_OKAY;
00342        mp_int t;
00343 
00344        /* If a is NULL, then return the inverse of b, otherwise return a/b. */
00345        if (a == NULL) {
00346               return  mp_invmod(b, &meth->irr, r);
00347        } else {
00348               /* MPI doesn't support divmod, so we implement it using invmod and 
00349                * mulmod. */
00350               MP_CHECKOK(mp_init(&t));
00351               MP_CHECKOK(mp_invmod(b, &meth->irr, &t));
00352               MP_CHECKOK(mp_mul(a, &t, r));
00353               MP_CHECKOK(ec_GFp_nistp224_mod(r, r, meth));
00354          CLEANUP:
00355               mp_clear(&t);
00356               return res;
00357        }
00358 }
00359 
00360 /* Wire in fast field arithmetic and precomputation of base point for
00361  * named curves. */
00362 mp_err
00363 ec_group_set_gfp224(ECGroup *group, ECCurveName name)
00364 {
00365        if (name == ECCurve_NIST_P224) {
00366               group->meth->field_mod = &ec_GFp_nistp224_mod;
00367               group->meth->field_mul = &ec_GFp_nistp224_mul;
00368               group->meth->field_sqr = &ec_GFp_nistp224_sqr;
00369               group->meth->field_div = &ec_GFp_nistp224_div;
00370        }
00371        return MP_OKAY;
00372 }