Back to index

lightning-sunbird  0.9+nobinonly
ec2_163.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 163-bit curve. Assumes reduction
00049  * polynomial with terms {163, 7, 6, 3, 0}. */
00050 mp_err
00051 ec_GF2m_163_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) < 6) {
00061               MP_CHECKOK(s_mp_pad(r, 6));
00062        }
00063        u = MP_DIGITS(r);
00064        MP_USED(r) = 6;
00065 
00066        /* u[5] only has 6 significant bits */
00067        z = u[5];
00068        u[2] ^= (z << 36) ^ (z << 35) ^ (z << 32) ^ (z << 29);
00069        z = u[4];
00070        u[2] ^= (z >> 28) ^ (z >> 29) ^ (z >> 32) ^ (z >> 35);
00071        u[1] ^= (z << 36) ^ (z << 35) ^ (z << 32) ^ (z << 29);
00072        z = u[3];
00073        u[1] ^= (z >> 28) ^ (z >> 29) ^ (z >> 32) ^ (z >> 35);
00074        u[0] ^= (z << 36) ^ (z << 35) ^ (z << 32) ^ (z << 29);
00075        z = u[2] >> 35;                           /* z only has 29 significant bits */
00076        u[0] ^= (z << 7) ^ (z << 6) ^ (z << 3) ^ z;
00077        /* clear bits above 163 */
00078        u[5] = u[4] = u[3] = 0;
00079        u[2] ^= z << 35;
00080 #else
00081        if (MP_USED(r) < 11) {
00082               MP_CHECKOK(s_mp_pad(r, 11));
00083        }
00084        u = MP_DIGITS(r);
00085        MP_USED(r) = 11;
00086 
00087        /* u[11] only has 6 significant bits */
00088        z = u[10];
00089        u[5] ^= (z << 4) ^ (z << 3) ^ z ^ (z >> 3);
00090        u[4] ^= (z << 29);
00091        z = u[9];
00092        u[5] ^= (z >> 28) ^ (z >> 29);
00093        u[4] ^= (z << 4) ^ (z << 3) ^ z ^ (z >> 3);
00094        u[3] ^= (z << 29);
00095        z = u[8];
00096        u[4] ^= (z >> 28) ^ (z >> 29);
00097        u[3] ^= (z << 4) ^ (z << 3) ^ z ^ (z >> 3);
00098        u[2] ^= (z << 29);
00099        z = u[7];
00100        u[3] ^= (z >> 28) ^ (z >> 29);
00101        u[2] ^= (z << 4) ^ (z << 3) ^ z ^ (z >> 3);
00102        u[1] ^= (z << 29);
00103        z = u[6];
00104        u[2] ^= (z >> 28) ^ (z >> 29);
00105        u[1] ^= (z << 4) ^ (z << 3) ^ z ^ (z >> 3);
00106        u[0] ^= (z << 29);
00107        z = u[5] >> 3;                            /* z only has 29 significant bits */
00108        u[1] ^= (z >> 25) ^ (z >> 26);
00109        u[0] ^= (z << 7) ^ (z << 6) ^ (z << 3) ^ z;
00110        /* clear bits above 163 */
00111        u[11] = u[10] = u[9] = u[8] = u[7] = u[6] = 0;
00112        u[5] ^= z << 3;
00113 #endif
00114        s_mp_clamp(r);
00115 
00116   CLEANUP:
00117        return res;
00118 }
00119 
00120 /* Fast squaring for polynomials over a 163-bit curve. Assumes reduction
00121  * polynomial with terms {163, 7, 6, 3, 0}. */
00122 mp_err
00123 ec_GF2m_163_sqr(const mp_int *a, mp_int *r, const GFMethod *meth)
00124 {
00125        mp_err res = MP_OKAY;
00126        mp_digit *u, *v;
00127 
00128        v = MP_DIGITS(a);
00129 
00130 #ifdef ECL_SIXTY_FOUR_BIT
00131        if (MP_USED(a) < 3) {
00132               return mp_bsqrmod(a, meth->irr_arr, r);
00133        }
00134        if (MP_USED(r) < 6) {
00135               MP_CHECKOK(s_mp_pad(r, 6));
00136        }
00137        MP_USED(r) = 6;
00138 #else
00139        if (MP_USED(a) < 6) {
00140               return mp_bsqrmod(a, meth->irr_arr, r);
00141        }
00142        if (MP_USED(r) < 12) {
00143               MP_CHECKOK(s_mp_pad(r, 12));
00144        }
00145        MP_USED(r) = 12;
00146 #endif
00147        u = MP_DIGITS(r);
00148 
00149 #ifdef ECL_THIRTY_TWO_BIT
00150        u[11] = gf2m_SQR1(v[5]);
00151        u[10] = gf2m_SQR0(v[5]);
00152        u[9] = gf2m_SQR1(v[4]);
00153        u[8] = gf2m_SQR0(v[4]);
00154        u[7] = gf2m_SQR1(v[3]);
00155        u[6] = gf2m_SQR0(v[3]);
00156 #endif
00157        u[5] = gf2m_SQR1(v[2]);
00158        u[4] = gf2m_SQR0(v[2]);
00159        u[3] = gf2m_SQR1(v[1]);
00160        u[2] = gf2m_SQR0(v[1]);
00161        u[1] = gf2m_SQR1(v[0]);
00162        u[0] = gf2m_SQR0(v[0]);
00163        return ec_GF2m_163_mod(r, r, meth);
00164 
00165   CLEANUP:
00166        return res;
00167 }
00168 
00169 /* Fast multiplication for polynomials over a 163-bit curve. Assumes
00170  * reduction polynomial with terms {163, 7, 6, 3, 0}. */
00171 mp_err
00172 ec_GF2m_163_mul(const mp_int *a, const mp_int *b, mp_int *r,
00173                             const GFMethod *meth)
00174 {
00175        mp_err res = MP_OKAY;
00176        mp_digit a2 = 0, a1 = 0, a0, b2 = 0, b1 = 0, b0;
00177 
00178 #ifdef ECL_THIRTY_TWO_BIT
00179        mp_digit a5 = 0, a4 = 0, a3 = 0, b5 = 0, b4 = 0, b3 = 0;
00180        mp_digit rm[6];
00181 #endif
00182 
00183        if (a == b) {
00184               return ec_GF2m_163_sqr(a, r, meth);
00185        } else {
00186               switch (MP_USED(a)) {
00187 #ifdef ECL_THIRTY_TWO_BIT
00188               case 6:
00189                      a5 = MP_DIGIT(a, 5);
00190               case 5:
00191                      a4 = MP_DIGIT(a, 4);
00192               case 4:
00193                      a3 = MP_DIGIT(a, 3);
00194 #endif
00195               case 3:
00196                      a2 = MP_DIGIT(a, 2);
00197               case 2:
00198                      a1 = MP_DIGIT(a, 1);
00199               default:
00200                      a0 = MP_DIGIT(a, 0);
00201               }
00202               switch (MP_USED(b)) {
00203 #ifdef ECL_THIRTY_TWO_BIT
00204               case 6:
00205                      b5 = MP_DIGIT(b, 5);
00206               case 5:
00207                      b4 = MP_DIGIT(b, 4);
00208               case 4:
00209                      b3 = MP_DIGIT(b, 3);
00210 #endif
00211               case 3:
00212                      b2 = MP_DIGIT(b, 2);
00213               case 2:
00214                      b1 = MP_DIGIT(b, 1);
00215               default:
00216                      b0 = MP_DIGIT(b, 0);
00217               }
00218 #ifdef ECL_SIXTY_FOUR_BIT
00219               MP_CHECKOK(s_mp_pad(r, 6));
00220               s_bmul_3x3(MP_DIGITS(r), a2, a1, a0, b2, b1, b0);
00221               MP_USED(r) = 6;
00222               s_mp_clamp(r);
00223 #else
00224               MP_CHECKOK(s_mp_pad(r, 12));
00225               s_bmul_3x3(MP_DIGITS(r) + 6, a5, a4, a3, b5, b4, b3);
00226               s_bmul_3x3(MP_DIGITS(r), a2, a1, a0, b2, b1, b0);
00227               s_bmul_3x3(rm, a5 ^ a2, a4 ^ a1, a3 ^ a0, b5 ^ b2, b4 ^ b1,
00228                                b3 ^ b0);
00229               rm[5] ^= MP_DIGIT(r, 5) ^ MP_DIGIT(r, 11);
00230               rm[4] ^= MP_DIGIT(r, 4) ^ MP_DIGIT(r, 10);
00231               rm[3] ^= MP_DIGIT(r, 3) ^ MP_DIGIT(r, 9);
00232               rm[2] ^= MP_DIGIT(r, 2) ^ MP_DIGIT(r, 8);
00233               rm[1] ^= MP_DIGIT(r, 1) ^ MP_DIGIT(r, 7);
00234               rm[0] ^= MP_DIGIT(r, 0) ^ MP_DIGIT(r, 6);
00235               MP_DIGIT(r, 8) ^= rm[5];
00236               MP_DIGIT(r, 7) ^= rm[4];
00237               MP_DIGIT(r, 6) ^= rm[3];
00238               MP_DIGIT(r, 5) ^= rm[2];
00239               MP_DIGIT(r, 4) ^= rm[1];
00240               MP_DIGIT(r, 3) ^= rm[0];
00241               MP_USED(r) = 12;
00242               s_mp_clamp(r);
00243 #endif
00244               return ec_GF2m_163_mod(r, r, meth);
00245        }
00246 
00247   CLEANUP:
00248        return res;
00249 }
00250 
00251 /* Wire in fast field arithmetic for 163-bit curves. */
00252 mp_err
00253 ec_group_set_gf2m163(ECGroup *group, ECCurveName name)
00254 {
00255        group->meth->field_mod = &ec_GF2m_163_mod;
00256        group->meth->field_mul = &ec_GF2m_163_mul;
00257        group->meth->field_sqr = &ec_GF2m_163_sqr;
00258        return MP_OKAY;
00259 }