Back to index

lightning-sunbird  0.9+nobinonly
ec2_233.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  *   Sheueling Chang-Shantz <sheueling.chang@sun.com>,
00024  *   Stephen Fung <fungstep@hotmail.com>, and
00025  *   Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories.
00026  *
00027  * Alternatively, the contents of this file may be used under the terms of
00028  * either the GNU General Public License Version 2 or later (the "GPL"), or
00029  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00030  * in which case the provisions of the GPL or the LGPL are applicable instead
00031  * of those above. If you wish to allow use of your version of this file only
00032  * under the terms of either the GPL or the LGPL, and not to allow others to
00033  * use your version of this file under the terms of the MPL, indicate your
00034  * decision by deleting the provisions above and replace them with the notice
00035  * and other provisions required by the GPL or the LGPL. If you do not delete
00036  * the provisions above, a recipient may use your version of this file under
00037  * the terms of any one of the MPL, the GPL or the LGPL.
00038  *
00039  * ***** END LICENSE BLOCK ***** */
00040 
00041 #include "ec2.h"
00042 #include "mp_gf2m.h"
00043 #include "mp_gf2m-priv.h"
00044 #include "mpi.h"
00045 #include "mpi-priv.h"
00046 #include <stdlib.h>
00047 
00048 /* Fast reduction for polynomials over a 233-bit curve. Assumes reduction
00049  * polynomial with terms {233, 74, 0}. */
00050 mp_err
00051 ec_GF2m_233_mod(const mp_int *a, mp_int *r, const GFMethod *meth)
00052 {
00053        mp_err res = MP_OKAY;
00054        mp_digit *u, z;
00055 
00056        if (a != r) {
00057               MP_CHECKOK(mp_copy(a, r));
00058        }
00059 #ifdef ECL_SIXTY_FOUR_BIT
00060        if (MP_USED(r) < 8) {
00061               MP_CHECKOK(s_mp_pad(r, 8));
00062        }
00063        u = MP_DIGITS(r);
00064        MP_USED(r) = 8;
00065 
00066        /* u[7] only has 18 significant bits */
00067        z = u[7];
00068        u[4] ^= (z << 33) ^ (z >> 41);
00069        u[3] ^= (z << 23);
00070        z = u[6];
00071        u[4] ^= (z >> 31);
00072        u[3] ^= (z << 33) ^ (z >> 41);
00073        u[2] ^= (z << 23);
00074        z = u[5];
00075        u[3] ^= (z >> 31);
00076        u[2] ^= (z << 33) ^ (z >> 41);
00077        u[1] ^= (z << 23);
00078        z = u[4];
00079        u[2] ^= (z >> 31);
00080        u[1] ^= (z << 33) ^ (z >> 41);
00081        u[0] ^= (z << 23);
00082        z = u[3] >> 41;                           /* z only has 23 significant bits */
00083        u[1] ^= (z << 10);
00084        u[0] ^= z;
00085        /* clear bits above 233 */
00086        u[7] = u[6] = u[5] = u[4] = 0;
00087        u[3] ^= z << 41;
00088 #else
00089        if (MP_USED(r) < 15) {
00090               MP_CHECKOK(s_mp_pad(r, 15));
00091        }
00092        u = MP_DIGITS(r);
00093        MP_USED(r) = 15;
00094 
00095        /* u[14] only has 18 significant bits */
00096        z = u[14];
00097        u[9] ^= (z << 1);
00098        u[7] ^= (z >> 9);
00099        u[6] ^= (z << 23);
00100        z = u[13];
00101        u[9] ^= (z >> 31);
00102        u[8] ^= (z << 1);
00103        u[6] ^= (z >> 9);
00104        u[5] ^= (z << 23);
00105        z = u[12];
00106        u[8] ^= (z >> 31);
00107        u[7] ^= (z << 1);
00108        u[5] ^= (z >> 9);
00109        u[4] ^= (z << 23);
00110        z = u[11];
00111        u[7] ^= (z >> 31);
00112        u[6] ^= (z << 1);
00113        u[4] ^= (z >> 9);
00114        u[3] ^= (z << 23);
00115        z = u[10];
00116        u[6] ^= (z >> 31);
00117        u[5] ^= (z << 1);
00118        u[3] ^= (z >> 9);
00119        u[2] ^= (z << 23);
00120        z = u[9];
00121        u[5] ^= (z >> 31);
00122        u[4] ^= (z << 1);
00123        u[2] ^= (z >> 9);
00124        u[1] ^= (z << 23);
00125        z = u[8];
00126        u[4] ^= (z >> 31);
00127        u[3] ^= (z << 1);
00128        u[1] ^= (z >> 9);
00129        u[0] ^= (z << 23);
00130        z = u[7] >> 9;                            /* z only has 23 significant bits */
00131        u[3] ^= (z >> 22);
00132        u[2] ^= (z << 10);
00133        u[0] ^= z;
00134        /* clear bits above 233 */
00135        u[14] = u[13] = u[12] = u[11] = u[10] = u[9] = u[8] = 0;
00136        u[7] ^= z << 9;
00137 #endif
00138        s_mp_clamp(r);
00139 
00140   CLEANUP:
00141        return res;
00142 }
00143 
00144 /* Fast squaring for polynomials over a 233-bit curve. Assumes reduction
00145  * polynomial with terms {233, 74, 0}. */
00146 mp_err
00147 ec_GF2m_233_sqr(const mp_int *a, mp_int *r, const GFMethod *meth)
00148 {
00149        mp_err res = MP_OKAY;
00150        mp_digit *u, *v;
00151 
00152        v = MP_DIGITS(a);
00153 
00154 #ifdef ECL_SIXTY_FOUR_BIT
00155        if (MP_USED(a) < 4) {
00156               return mp_bsqrmod(a, meth->irr_arr, r);
00157        }
00158        if (MP_USED(r) < 8) {
00159               MP_CHECKOK(s_mp_pad(r, 8));
00160        }
00161        MP_USED(r) = 8;
00162 #else
00163        if (MP_USED(a) < 8) {
00164               return mp_bsqrmod(a, meth->irr_arr, r);
00165        }
00166        if (MP_USED(r) < 15) {
00167               MP_CHECKOK(s_mp_pad(r, 15));
00168        }
00169        MP_USED(r) = 15;
00170 #endif
00171        u = MP_DIGITS(r);
00172 
00173 #ifdef ECL_THIRTY_TWO_BIT
00174        u[14] = gf2m_SQR0(v[7]);
00175        u[13] = gf2m_SQR1(v[6]);
00176        u[12] = gf2m_SQR0(v[6]);
00177        u[11] = gf2m_SQR1(v[5]);
00178        u[10] = gf2m_SQR0(v[5]);
00179        u[9] = gf2m_SQR1(v[4]);
00180        u[8] = gf2m_SQR0(v[4]);
00181 #endif
00182        u[7] = gf2m_SQR1(v[3]);
00183        u[6] = gf2m_SQR0(v[3]);
00184        u[5] = gf2m_SQR1(v[2]);
00185        u[4] = gf2m_SQR0(v[2]);
00186        u[3] = gf2m_SQR1(v[1]);
00187        u[2] = gf2m_SQR0(v[1]);
00188        u[1] = gf2m_SQR1(v[0]);
00189        u[0] = gf2m_SQR0(v[0]);
00190        return ec_GF2m_233_mod(r, r, meth);
00191 
00192   CLEANUP:
00193        return res;
00194 }
00195 
00196 /* Fast multiplication for polynomials over a 233-bit curve. Assumes
00197  * reduction polynomial with terms {233, 74, 0}. */
00198 mp_err
00199 ec_GF2m_233_mul(const mp_int *a, const mp_int *b, mp_int *r,
00200                             const GFMethod *meth)
00201 {
00202        mp_err res = MP_OKAY;
00203        mp_digit a3 = 0, a2 = 0, a1 = 0, a0, b3 = 0, b2 = 0, b1 = 0, b0;
00204 
00205 #ifdef ECL_THIRTY_TWO_BIT
00206        mp_digit a7 = 0, a6 = 0, a5 = 0, a4 = 0, b7 = 0, b6 = 0, b5 = 0, b4 =
00207               0;
00208        mp_digit rm[8];
00209 #endif
00210 
00211        if (a == b) {
00212               return ec_GF2m_233_sqr(a, r, meth);
00213        } else {
00214               switch (MP_USED(a)) {
00215 #ifdef ECL_THIRTY_TWO_BIT
00216               case 8:
00217                      a7 = MP_DIGIT(a, 7);
00218               case 7:
00219                      a6 = MP_DIGIT(a, 6);
00220               case 6:
00221                      a5 = MP_DIGIT(a, 5);
00222               case 5:
00223                      a4 = MP_DIGIT(a, 4);
00224 #endif
00225               case 4:
00226                      a3 = MP_DIGIT(a, 3);
00227               case 3:
00228                      a2 = MP_DIGIT(a, 2);
00229               case 2:
00230                      a1 = MP_DIGIT(a, 1);
00231               default:
00232                      a0 = MP_DIGIT(a, 0);
00233               }
00234               switch (MP_USED(b)) {
00235 #ifdef ECL_THIRTY_TWO_BIT
00236               case 8:
00237                      b7 = MP_DIGIT(b, 7);
00238               case 7:
00239                      b6 = MP_DIGIT(b, 6);
00240               case 6:
00241                      b5 = MP_DIGIT(b, 5);
00242               case 5:
00243                      b4 = MP_DIGIT(b, 4);
00244 #endif
00245               case 4:
00246                      b3 = MP_DIGIT(b, 3);
00247               case 3:
00248                      b2 = MP_DIGIT(b, 2);
00249               case 2:
00250                      b1 = MP_DIGIT(b, 1);
00251               default:
00252                      b0 = MP_DIGIT(b, 0);
00253               }
00254 #ifdef ECL_SIXTY_FOUR_BIT
00255               MP_CHECKOK(s_mp_pad(r, 8));
00256               s_bmul_4x4(MP_DIGITS(r), a3, a2, a1, a0, b3, b2, b1, b0);
00257               MP_USED(r) = 8;
00258               s_mp_clamp(r);
00259 #else
00260               MP_CHECKOK(s_mp_pad(r, 16));
00261               s_bmul_4x4(MP_DIGITS(r) + 8, a7, a6, a5, a4, b7, b6, b5, b4);
00262               s_bmul_4x4(MP_DIGITS(r), a3, a2, a1, a0, b3, b2, b1, b0);
00263               s_bmul_4x4(rm, a7 ^ a3, a6 ^ a2, a5 ^ a1, a4 ^ a0, b7 ^ b3,
00264                                b6 ^ b2, b5 ^ b1, b4 ^ b0);
00265               rm[7] ^= MP_DIGIT(r, 7) ^ MP_DIGIT(r, 15);
00266               rm[6] ^= MP_DIGIT(r, 6) ^ MP_DIGIT(r, 14);
00267               rm[5] ^= MP_DIGIT(r, 5) ^ MP_DIGIT(r, 13);
00268               rm[4] ^= MP_DIGIT(r, 4) ^ MP_DIGIT(r, 12);
00269               rm[3] ^= MP_DIGIT(r, 3) ^ MP_DIGIT(r, 11);
00270               rm[2] ^= MP_DIGIT(r, 2) ^ MP_DIGIT(r, 10);
00271               rm[1] ^= MP_DIGIT(r, 1) ^ MP_DIGIT(r, 9);
00272               rm[0] ^= MP_DIGIT(r, 0) ^ MP_DIGIT(r, 8);
00273               MP_DIGIT(r, 11) ^= rm[7];
00274               MP_DIGIT(r, 10) ^= rm[6];
00275               MP_DIGIT(r, 9) ^= rm[5];
00276               MP_DIGIT(r, 8) ^= rm[4];
00277               MP_DIGIT(r, 7) ^= rm[3];
00278               MP_DIGIT(r, 6) ^= rm[2];
00279               MP_DIGIT(r, 5) ^= rm[1];
00280               MP_DIGIT(r, 4) ^= rm[0];
00281               MP_USED(r) = 16;
00282               s_mp_clamp(r);
00283 #endif
00284               return ec_GF2m_233_mod(r, r, meth);
00285        }
00286 
00287   CLEANUP:
00288        return res;
00289 }
00290 
00291 /* Wire in fast field arithmetic for 233-bit curves. */
00292 mp_err
00293 ec_group_set_gf2m233(ECGroup *group, ECCurveName name)
00294 {
00295        group->meth->field_mod = &ec_GF2m_233_mod;
00296        group->meth->field_mul = &ec_GF2m_233_mul;
00297        group->meth->field_sqr = &ec_GF2m_233_sqr;
00298        return MP_OKAY;
00299 }