Back to index

lightning-sunbird  0.9+nobinonly
ecp_fp192.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 using floating point operations.
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_fp.h"
00040 #include <stdlib.h>
00041 
00042 #define ECFP_BSIZE 192
00043 #define ECFP_NUMDOUBLES 8
00044 
00045 #include "ecp_fpinc.c"
00046 
00047 /* Performs a single step of reduction, just on the uppermost float
00048  * (assumes already tidied), and then retidies. Note, this does not
00049  * guarantee that the result will be less than p. */
00050 void
00051 ecfp192_singleReduce(double *d, const EC_group_fp * group)
00052 {
00053        double q;
00054 
00055        ECFP_ASSERT(group->doubleBitSize == 24);
00056        ECFP_ASSERT(group->primeBitSize == 192);
00057        ECFP_ASSERT(group->numDoubles == 8);
00058 
00059        q = d[ECFP_NUMDOUBLES - 1] - ecfp_beta_192;
00060        q += group->bitSize_alpha;
00061        q -= group->bitSize_alpha;
00062 
00063        d[ECFP_NUMDOUBLES - 1] -= q;
00064        d[0] += q * ecfp_twom192;
00065        d[2] += q * ecfp_twom128;
00066        ecfp_positiveTidy(d, group);
00067 }
00068 
00069 /* 
00070  * Performs imperfect reduction.  This might leave some negative terms,
00071  * and one more reduction might be required for the result to be between 0 
00072  * and p-1. x should be be an array of at least 16, and r at least 8 x and 
00073  * r can be the same, but then the upper parts of r are not zeroed */
00074 void
00075 ecfp_reduce_192(double *r, double *x, const EC_group_fp * group)
00076 {
00077        double x8, x9, x10, q;
00078 
00079        ECFP_ASSERT(group->doubleBitSize == 24);
00080        ECFP_ASSERT(group->primeBitSize == 192);
00081        ECFP_ASSERT(group->numDoubles == 8);
00082 
00083        /* Tidy just the upper portion, the lower part can wait */
00084        ecfp_tidyUpper(x, group);
00085 
00086        x8 = x[8] + x[14] * ecfp_twom128;  /* adds bits 16-40 */
00087        x9 = x[9] + x[15] * ecfp_twom128;  /* adds bits 16-40 */
00088 
00089        /* Tidy up, or we won't have enough bits later to add it in */
00090 
00091        q = x8 + group->alpha[9];
00092        q -= group->alpha[9];
00093        x8 -= q;
00094        x9 += q;
00095 
00096        q = x9 + group->alpha[10];
00097        q -= group->alpha[10];
00098        x9 -= q;
00099        x10 = x[10] + q;
00100 
00101        r[7] = x[7] + x[15] * ecfp_twom192 + x[13] * ecfp_twom128;     /* adds
00102                                                                                                                  * bits
00103                                                                                                                  * 0-40 */
00104        r[6] = x[6] + x[14] * ecfp_twom192 + x[12] * ecfp_twom128;
00105        r[5] = x[5] + x[13] * ecfp_twom192 + x[11] * ecfp_twom128;
00106        r[4] = x[4] + x[12] * ecfp_twom192 + x10 * ecfp_twom128;
00107        r[3] = x[3] + x[11] * ecfp_twom192 + x9 * ecfp_twom128; /* adds bits
00108                                                                                                           * 0-40 */
00109        r[2] = x[2] + x10 * ecfp_twom192 + x8 * ecfp_twom128;
00110        r[1] = x[1] + x9 * ecfp_twom192;   /* adds bits 16-40 */
00111        r[0] = x[0] + x8 * ecfp_twom192;
00112 
00113        /* 
00114         * Tidy up just r[group->numDoubles-2] so that the number of
00115         * reductions is accurate plus or minus one.  (Rather than tidy all to 
00116         * make it totally accurate) */
00117        q = r[ECFP_NUMDOUBLES - 2] + group->alpha[ECFP_NUMDOUBLES - 1];
00118        q -= group->alpha[ECFP_NUMDOUBLES - 1];
00119        r[ECFP_NUMDOUBLES - 2] -= q;
00120        r[ECFP_NUMDOUBLES - 1] += q;
00121 
00122        /* Tidy up the excess bits on r[group->numDoubles-1] using reduction */
00123        /* Use ecfp_beta so we get a positive res */
00124        q = r[ECFP_NUMDOUBLES - 1] - ecfp_beta_192;
00125        q += group->bitSize_alpha;
00126        q -= group->bitSize_alpha;
00127 
00128        r[ECFP_NUMDOUBLES - 1] -= q;
00129        r[0] += q * ecfp_twom192;
00130        r[2] += q * ecfp_twom128;
00131 
00132        /* Tidy the result */
00133        ecfp_tidyShort(r, group);
00134 }
00135 
00136 /* Sets group to use optimized calculations in this file */
00137 mp_err
00138 ec_group_set_nistp192_fp(ECGroup *group)
00139 {
00140        EC_group_fp *fpg;
00141 
00142        /* Allocate memory for floating point group data */
00143        fpg = (EC_group_fp *) malloc(sizeof(EC_group_fp));
00144        if (fpg == NULL) {
00145               return MP_MEM;
00146        }
00147 
00148        fpg->numDoubles = ECFP_NUMDOUBLES;
00149        fpg->primeBitSize = ECFP_BSIZE;
00150        fpg->orderBitSize = 192;
00151        fpg->doubleBitSize = 24;
00152        fpg->numInts = (ECFP_BSIZE + ECL_BITS - 1) / ECL_BITS;
00153        fpg->aIsM3 = 1;
00154        fpg->ecfp_singleReduce = &ecfp192_singleReduce;
00155        fpg->ecfp_reduce = &ecfp_reduce_192;
00156        fpg->ecfp_tidy = &ecfp_tidy;
00157 
00158        fpg->pt_add_jac_aff = &ecfp192_pt_add_jac_aff;
00159        fpg->pt_add_jac = &ecfp192_pt_add_jac;
00160        fpg->pt_add_jm_chud = &ecfp192_pt_add_jm_chud;
00161        fpg->pt_add_chud = &ecfp192_pt_add_chud;
00162        fpg->pt_dbl_jac = &ecfp192_pt_dbl_jac;
00163        fpg->pt_dbl_jm = &ecfp192_pt_dbl_jm;
00164        fpg->pt_dbl_aff2chud = &ecfp192_pt_dbl_aff2chud;
00165        fpg->precompute_chud = &ecfp192_precompute_chud;
00166        fpg->precompute_jac = &ecfp192_precompute_jac;
00167 
00168        group->point_mul = &ec_GFp_point_mul_wNAF_fp;
00169        group->points_mul = &ec_pts_mul_basic;
00170        group->extra1 = fpg;
00171        group->extra_free = &ec_GFp_extra_free_fp;
00172 
00173        ec_set_fp_precision(fpg);
00174        fpg->bitSize_alpha = ECFP_TWO192 * fpg->alpha[0];
00175 
00176        return MP_OKAY;
00177 }