Back to index

lightning-sunbird  0.9+nobinonly
ecp_fp160.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 160
00043 #define ECFP_NUMDOUBLES 7
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, but truncates the number 
00050  * of bits. */
00051 void
00052 ecfp160_singleReduce(double *d, const EC_group_fp * group)
00053 {
00054        double q;
00055 
00056        ECFP_ASSERT(group->doubleBitSize == 24);
00057        ECFP_ASSERT(group->primeBitSize == 160);
00058        ECFP_ASSERT(ECFP_NUMDOUBLES == 7);
00059 
00060        q = d[ECFP_NUMDOUBLES - 1] - ecfp_beta_160;
00061        q += group->bitSize_alpha;
00062        q -= group->bitSize_alpha;
00063 
00064        d[ECFP_NUMDOUBLES - 1] -= q;
00065        d[0] += q * ecfp_twom160;
00066        d[1] += q * ecfp_twom129;
00067        ecfp_positiveTidy(d, group);
00068 
00069        /* Assertions for the highest order term */
00070        ECFP_ASSERT(d[ECFP_NUMDOUBLES - 1] / ecfp_exp[ECFP_NUMDOUBLES - 1] ==
00071                             (unsigned long long) (d[ECFP_NUMDOUBLES - 1] /
00072                                                                  ecfp_exp[ECFP_NUMDOUBLES - 1]));
00073        ECFP_ASSERT(d[ECFP_NUMDOUBLES - 1] >= 0);
00074 }
00075 
00076 /* Performs imperfect reduction.  This might leave some negative terms,
00077  * and one more reduction might be required for the result to be between 0 
00078  * and p-1. x should not already be reduced, i.e. should have
00079  * 2*ECFP_NUMDOUBLES significant terms. x and r can be the same, but then
00080  * the upper parts of r are not zeroed */
00081 void
00082 ecfp160_reduce(double *r, double *x, const EC_group_fp * group)
00083 {
00084 
00085        double x7, x8, q;
00086 
00087        ECFP_ASSERT(group->doubleBitSize == 24);
00088        ECFP_ASSERT(group->primeBitSize == 160);
00089        ECFP_ASSERT(ECFP_NUMDOUBLES == 7);
00090 
00091        /* Tidy just the upper bits, the lower bits can wait. */
00092        ecfp_tidyUpper(x, group);
00093 
00094        /* Assume that this is already tidied so that we have enough extra
00095         * bits */
00096        x7 = x[7] + x[13] * ecfp_twom129;  /* adds bits 15-39 */
00097 
00098        /* Tidy x7, or we won't have enough bits later to add it in */
00099        q = x7 + group->alpha[8];
00100        q -= group->alpha[8];
00101        x7 -= q;                                  /* holds bits 0-24 */
00102        x8 = x[8] + q;                            /* holds bits 0-25 */
00103 
00104        r[6] = x[6] + x[13] * ecfp_twom160 + x[12] * ecfp_twom129;     /* adds
00105                                                                                                                  * bits
00106                                                                                                                  * 8-39 */
00107        r[5] = x[5] + x[12] * ecfp_twom160 + x[11] * ecfp_twom129;
00108        r[4] = x[4] + x[11] * ecfp_twom160 + x[10] * ecfp_twom129;
00109        r[3] = x[3] + x[10] * ecfp_twom160 + x[9] * ecfp_twom129;
00110        r[2] = x[2] + x[9] * ecfp_twom160 + x8 * ecfp_twom129;  /* adds bits
00111                                                                                                           * 8-40 */
00112        r[1] = x[1] + x8 * ecfp_twom160 + x7 * ecfp_twom129;    /* adds bits
00113                                                                                                           * 8-39 */
00114        r[0] = x[0] + x7 * ecfp_twom160;
00115 
00116        /* Tidy up just r[ECFP_NUMDOUBLES-2] so that the number of reductions
00117         * is accurate plus or minus one.  (Rather than tidy all to make it
00118         * totally accurate, which is more costly.) */
00119        q = r[ECFP_NUMDOUBLES - 2] + group->alpha[ECFP_NUMDOUBLES - 1];
00120        q -= group->alpha[ECFP_NUMDOUBLES - 1];
00121        r[ECFP_NUMDOUBLES - 2] -= q;
00122        r[ECFP_NUMDOUBLES - 1] += q;
00123 
00124        /* Tidy up the excess bits on r[ECFP_NUMDOUBLES-1] using reduction */
00125        /* Use ecfp_beta so we get a positive result */
00126        q = r[ECFP_NUMDOUBLES - 1] - ecfp_beta_160;
00127        q += group->bitSize_alpha;
00128        q -= group->bitSize_alpha;
00129 
00130        r[ECFP_NUMDOUBLES - 1] -= q;
00131        r[0] += q * ecfp_twom160;
00132        r[1] += q * ecfp_twom129;
00133 
00134        /* Tidy the result */
00135        ecfp_tidyShort(r, group);
00136 }
00137 
00138 /* Sets group to use optimized calculations in this file */
00139 mp_err
00140 ec_group_set_secp160r1_fp(ECGroup *group)
00141 {
00142 
00143        EC_group_fp *fpg = NULL;
00144 
00145        /* Allocate memory for floating point group data */
00146        fpg = (EC_group_fp *) malloc(sizeof(EC_group_fp));
00147        if (fpg == NULL) {
00148               return MP_MEM;
00149        }
00150 
00151        fpg->numDoubles = ECFP_NUMDOUBLES;
00152        fpg->primeBitSize = ECFP_BSIZE;
00153        fpg->orderBitSize = 161;
00154        fpg->doubleBitSize = 24;
00155        fpg->numInts = (ECFP_BSIZE + ECL_BITS - 1) / ECL_BITS;
00156        fpg->aIsM3 = 1;
00157        fpg->ecfp_singleReduce = &ecfp160_singleReduce;
00158        fpg->ecfp_reduce = &ecfp160_reduce;
00159        fpg->ecfp_tidy = &ecfp_tidy;
00160 
00161        fpg->pt_add_jac_aff = &ecfp160_pt_add_jac_aff;
00162        fpg->pt_add_jac = &ecfp160_pt_add_jac;
00163        fpg->pt_add_jm_chud = &ecfp160_pt_add_jm_chud;
00164        fpg->pt_add_chud = &ecfp160_pt_add_chud;
00165        fpg->pt_dbl_jac = &ecfp160_pt_dbl_jac;
00166        fpg->pt_dbl_jm = &ecfp160_pt_dbl_jm;
00167        fpg->pt_dbl_aff2chud = &ecfp160_pt_dbl_aff2chud;
00168        fpg->precompute_chud = &ecfp160_precompute_chud;
00169        fpg->precompute_jac = &ecfp160_precompute_jac;
00170 
00171        group->point_mul = &ec_GFp_point_mul_wNAF_fp;
00172        group->points_mul = &ec_pts_mul_basic;
00173        group->extra1 = fpg;
00174        group->extra_free = &ec_GFp_extra_free_fp;
00175 
00176        ec_set_fp_precision(fpg);
00177        fpg->bitSize_alpha = ECFP_TWO160 * fpg->alpha[0];
00178        return MP_OKAY;
00179 }