annotate src/share/native/sun/security/ec/impl/ecp_jac.c @ 3909:272483f6650b

7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
author ohair
date Wed, 06 Apr 2011 22:06:11 -0700
parents 1b5c838b8db8
children b49a0af85821
rev   line source
vinnie@1674 1 /* *********************************************************************
vinnie@1674 2 *
vinnie@1674 3 * Sun elects to have this file available under and governed by the
vinnie@1674 4 * Mozilla Public License Version 1.1 ("MPL") (see
vinnie@1674 5 * http://www.mozilla.org/MPL/ for full license text). For the avoidance
vinnie@1674 6 * of doubt and subject to the following, Sun also elects to allow
vinnie@1674 7 * licensees to use this file under the MPL, the GNU General Public
vinnie@1674 8 * License version 2 only or the Lesser General Public License version
vinnie@1674 9 * 2.1 only. Any references to the "GNU General Public License version 2
vinnie@1674 10 * or later" or "GPL" in the following shall be construed to mean the
vinnie@1674 11 * GNU General Public License version 2 only. Any references to the "GNU
vinnie@1674 12 * Lesser General Public License version 2.1 or later" or "LGPL" in the
vinnie@1674 13 * following shall be construed to mean the GNU Lesser General Public
vinnie@1674 14 * License version 2.1 only. However, the following notice accompanied
vinnie@1674 15 * the original version of this file:
vinnie@1674 16 *
vinnie@1674 17 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
vinnie@1674 18 *
vinnie@1674 19 * The contents of this file are subject to the Mozilla Public License Version
vinnie@1674 20 * 1.1 (the "License"); you may not use this file except in compliance with
vinnie@1674 21 * the License. You may obtain a copy of the License at
vinnie@1674 22 * http://www.mozilla.org/MPL/
vinnie@1674 23 *
vinnie@1674 24 * Software distributed under the License is distributed on an "AS IS" basis,
vinnie@1674 25 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
vinnie@1674 26 * for the specific language governing rights and limitations under the
vinnie@1674 27 * License.
vinnie@1674 28 *
vinnie@1674 29 * The Original Code is the elliptic curve math library for prime field curves.
vinnie@1674 30 *
vinnie@1674 31 * The Initial Developer of the Original Code is
vinnie@1674 32 * Sun Microsystems, Inc.
vinnie@1674 33 * Portions created by the Initial Developer are Copyright (C) 2003
vinnie@1674 34 * the Initial Developer. All Rights Reserved.
vinnie@1674 35 *
vinnie@1674 36 * Contributor(s):
vinnie@1674 37 * Sheueling Chang-Shantz <sheueling.chang@sun.com>,
vinnie@1674 38 * Stephen Fung <fungstep@hotmail.com>, and
vinnie@1674 39 * Douglas Stebila <douglas@stebila.ca>, Sun Microsystems Laboratories.
vinnie@1674 40 * Bodo Moeller <moeller@cdc.informatik.tu-darmstadt.de>,
vinnie@1674 41 * Nils Larsch <nla@trustcenter.de>, and
vinnie@1674 42 * Lenka Fibikova <fibikova@exp-math.uni-essen.de>, the OpenSSL Project
vinnie@1674 43 *
vinnie@1674 44 * Alternatively, the contents of this file may be used under the terms of
vinnie@1674 45 * either the GNU General Public License Version 2 or later (the "GPL"), or
vinnie@1674 46 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
vinnie@1674 47 * in which case the provisions of the GPL or the LGPL are applicable instead
vinnie@1674 48 * of those above. If you wish to allow use of your version of this file only
vinnie@1674 49 * under the terms of either the GPL or the LGPL, and not to allow others to
vinnie@1674 50 * use your version of this file under the terms of the MPL, indicate your
vinnie@1674 51 * decision by deleting the provisions above and replace them with the notice
vinnie@1674 52 * and other provisions required by the GPL or the LGPL. If you do not delete
vinnie@1674 53 * the provisions above, a recipient may use your version of this file under
vinnie@1674 54 * the terms of any one of the MPL, the GPL or the LGPL.
vinnie@1674 55 *
vinnie@1674 56 *********************************************************************** */
vinnie@1674 57 /*
ohair@3909 58 * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
vinnie@1674 59 * Use is subject to license terms.
vinnie@1674 60 */
vinnie@1674 61
vinnie@1674 62 #include "ecp.h"
vinnie@1674 63 #include "mplogic.h"
vinnie@1674 64 #ifndef _KERNEL
vinnie@1674 65 #include <stdlib.h>
vinnie@1674 66 #endif
vinnie@1674 67 #ifdef ECL_DEBUG
vinnie@1674 68 #include <assert.h>
vinnie@1674 69 #endif
vinnie@1674 70
vinnie@1674 71 /* Converts a point P(px, py) from affine coordinates to Jacobian
vinnie@1674 72 * projective coordinates R(rx, ry, rz). Assumes input is already
vinnie@1674 73 * field-encoded using field_enc, and returns output that is still
vinnie@1674 74 * field-encoded. */
vinnie@1674 75 mp_err
vinnie@1674 76 ec_GFp_pt_aff2jac(const mp_int *px, const mp_int *py, mp_int *rx,
vinnie@1674 77 mp_int *ry, mp_int *rz, const ECGroup *group)
vinnie@1674 78 {
vinnie@1674 79 mp_err res = MP_OKAY;
vinnie@1674 80
vinnie@1674 81 if (ec_GFp_pt_is_inf_aff(px, py) == MP_YES) {
vinnie@1674 82 MP_CHECKOK(ec_GFp_pt_set_inf_jac(rx, ry, rz));
vinnie@1674 83 } else {
vinnie@1674 84 MP_CHECKOK(mp_copy(px, rx));
vinnie@1674 85 MP_CHECKOK(mp_copy(py, ry));
vinnie@1674 86 MP_CHECKOK(mp_set_int(rz, 1));
vinnie@1674 87 if (group->meth->field_enc) {
vinnie@1674 88 MP_CHECKOK(group->meth->field_enc(rz, rz, group->meth));
vinnie@1674 89 }
vinnie@1674 90 }
vinnie@1674 91 CLEANUP:
vinnie@1674 92 return res;
vinnie@1674 93 }
vinnie@1674 94
vinnie@1674 95 /* Converts a point P(px, py, pz) from Jacobian projective coordinates to
vinnie@1674 96 * affine coordinates R(rx, ry). P and R can share x and y coordinates.
vinnie@1674 97 * Assumes input is already field-encoded using field_enc, and returns
vinnie@1674 98 * output that is still field-encoded. */
vinnie@1674 99 mp_err
vinnie@1674 100 ec_GFp_pt_jac2aff(const mp_int *px, const mp_int *py, const mp_int *pz,
vinnie@1674 101 mp_int *rx, mp_int *ry, const ECGroup *group)
vinnie@1674 102 {
vinnie@1674 103 mp_err res = MP_OKAY;
vinnie@1674 104 mp_int z1, z2, z3;
vinnie@1674 105
vinnie@1674 106 MP_DIGITS(&z1) = 0;
vinnie@1674 107 MP_DIGITS(&z2) = 0;
vinnie@1674 108 MP_DIGITS(&z3) = 0;
vinnie@1674 109 MP_CHECKOK(mp_init(&z1, FLAG(px)));
vinnie@1674 110 MP_CHECKOK(mp_init(&z2, FLAG(px)));
vinnie@1674 111 MP_CHECKOK(mp_init(&z3, FLAG(px)));
vinnie@1674 112
vinnie@1674 113 /* if point at infinity, then set point at infinity and exit */
vinnie@1674 114 if (ec_GFp_pt_is_inf_jac(px, py, pz) == MP_YES) {
vinnie@1674 115 MP_CHECKOK(ec_GFp_pt_set_inf_aff(rx, ry));
vinnie@1674 116 goto CLEANUP;
vinnie@1674 117 }
vinnie@1674 118
vinnie@1674 119 /* transform (px, py, pz) into (px / pz^2, py / pz^3) */
vinnie@1674 120 if (mp_cmp_d(pz, 1) == 0) {
vinnie@1674 121 MP_CHECKOK(mp_copy(px, rx));
vinnie@1674 122 MP_CHECKOK(mp_copy(py, ry));
vinnie@1674 123 } else {
vinnie@1674 124 MP_CHECKOK(group->meth->field_div(NULL, pz, &z1, group->meth));
vinnie@1674 125 MP_CHECKOK(group->meth->field_sqr(&z1, &z2, group->meth));
vinnie@1674 126 MP_CHECKOK(group->meth->field_mul(&z1, &z2, &z3, group->meth));
vinnie@1674 127 MP_CHECKOK(group->meth->field_mul(px, &z2, rx, group->meth));
vinnie@1674 128 MP_CHECKOK(group->meth->field_mul(py, &z3, ry, group->meth));
vinnie@1674 129 }
vinnie@1674 130
vinnie@1674 131 CLEANUP:
vinnie@1674 132 mp_clear(&z1);
vinnie@1674 133 mp_clear(&z2);
vinnie@1674 134 mp_clear(&z3);
vinnie@1674 135 return res;
vinnie@1674 136 }
vinnie@1674 137
vinnie@1674 138 /* Checks if point P(px, py, pz) is at infinity. Uses Jacobian
vinnie@1674 139 * coordinates. */
vinnie@1674 140 mp_err
vinnie@1674 141 ec_GFp_pt_is_inf_jac(const mp_int *px, const mp_int *py, const mp_int *pz)
vinnie@1674 142 {
vinnie@1674 143 return mp_cmp_z(pz);
vinnie@1674 144 }
vinnie@1674 145
vinnie@1674 146 /* Sets P(px, py, pz) to be the point at infinity. Uses Jacobian
vinnie@1674 147 * coordinates. */
vinnie@1674 148 mp_err
vinnie@1674 149 ec_GFp_pt_set_inf_jac(mp_int *px, mp_int *py, mp_int *pz)
vinnie@1674 150 {
vinnie@1674 151 mp_zero(pz);
vinnie@1674 152 return MP_OKAY;
vinnie@1674 153 }
vinnie@1674 154
vinnie@1674 155 /* Computes R = P + Q where R is (rx, ry, rz), P is (px, py, pz) and Q is
vinnie@1674 156 * (qx, qy, 1). Elliptic curve points P, Q, and R can all be identical.
vinnie@1674 157 * Uses mixed Jacobian-affine coordinates. Assumes input is already
vinnie@1674 158 * field-encoded using field_enc, and returns output that is still
vinnie@1674 159 * field-encoded. Uses equation (2) from Brown, Hankerson, Lopez, and
vinnie@1674 160 * Menezes. Software Implementation of the NIST Elliptic Curves Over Prime
vinnie@1674 161 * Fields. */
vinnie@1674 162 mp_err
vinnie@1674 163 ec_GFp_pt_add_jac_aff(const mp_int *px, const mp_int *py, const mp_int *pz,
vinnie@1674 164 const mp_int *qx, const mp_int *qy, mp_int *rx,
vinnie@1674 165 mp_int *ry, mp_int *rz, const ECGroup *group)
vinnie@1674 166 {
vinnie@1674 167 mp_err res = MP_OKAY;
vinnie@1674 168 mp_int A, B, C, D, C2, C3;
vinnie@1674 169
vinnie@1674 170 MP_DIGITS(&A) = 0;
vinnie@1674 171 MP_DIGITS(&B) = 0;
vinnie@1674 172 MP_DIGITS(&C) = 0;
vinnie@1674 173 MP_DIGITS(&D) = 0;
vinnie@1674 174 MP_DIGITS(&C2) = 0;
vinnie@1674 175 MP_DIGITS(&C3) = 0;
vinnie@1674 176 MP_CHECKOK(mp_init(&A, FLAG(px)));
vinnie@1674 177 MP_CHECKOK(mp_init(&B, FLAG(px)));
vinnie@1674 178 MP_CHECKOK(mp_init(&C, FLAG(px)));
vinnie@1674 179 MP_CHECKOK(mp_init(&D, FLAG(px)));
vinnie@1674 180 MP_CHECKOK(mp_init(&C2, FLAG(px)));
vinnie@1674 181 MP_CHECKOK(mp_init(&C3, FLAG(px)));
vinnie@1674 182
vinnie@1674 183 /* If either P or Q is the point at infinity, then return the other
vinnie@1674 184 * point */
vinnie@1674 185 if (ec_GFp_pt_is_inf_jac(px, py, pz) == MP_YES) {
vinnie@1674 186 MP_CHECKOK(ec_GFp_pt_aff2jac(qx, qy, rx, ry, rz, group));
vinnie@1674 187 goto CLEANUP;
vinnie@1674 188 }
vinnie@1674 189 if (ec_GFp_pt_is_inf_aff(qx, qy) == MP_YES) {
vinnie@1674 190 MP_CHECKOK(mp_copy(px, rx));
vinnie@1674 191 MP_CHECKOK(mp_copy(py, ry));
vinnie@1674 192 MP_CHECKOK(mp_copy(pz, rz));
vinnie@1674 193 goto CLEANUP;
vinnie@1674 194 }
vinnie@1674 195
vinnie@1674 196 /* A = qx * pz^2, B = qy * pz^3 */
vinnie@1674 197 MP_CHECKOK(group->meth->field_sqr(pz, &A, group->meth));
vinnie@1674 198 MP_CHECKOK(group->meth->field_mul(&A, pz, &B, group->meth));
vinnie@1674 199 MP_CHECKOK(group->meth->field_mul(&A, qx, &A, group->meth));
vinnie@1674 200 MP_CHECKOK(group->meth->field_mul(&B, qy, &B, group->meth));
vinnie@1674 201
vinnie@1674 202 /* C = A - px, D = B - py */
vinnie@1674 203 MP_CHECKOK(group->meth->field_sub(&A, px, &C, group->meth));
vinnie@1674 204 MP_CHECKOK(group->meth->field_sub(&B, py, &D, group->meth));
vinnie@1674 205
vinnie@1674 206 /* C2 = C^2, C3 = C^3 */
vinnie@1674 207 MP_CHECKOK(group->meth->field_sqr(&C, &C2, group->meth));
vinnie@1674 208 MP_CHECKOK(group->meth->field_mul(&C, &C2, &C3, group->meth));
vinnie@1674 209
vinnie@1674 210 /* rz = pz * C */
vinnie@1674 211 MP_CHECKOK(group->meth->field_mul(pz, &C, rz, group->meth));
vinnie@1674 212
vinnie@1674 213 /* C = px * C^2 */
vinnie@1674 214 MP_CHECKOK(group->meth->field_mul(px, &C2, &C, group->meth));
vinnie@1674 215 /* A = D^2 */
vinnie@1674 216 MP_CHECKOK(group->meth->field_sqr(&D, &A, group->meth));
vinnie@1674 217
vinnie@1674 218 /* rx = D^2 - (C^3 + 2 * (px * C^2)) */
vinnie@1674 219 MP_CHECKOK(group->meth->field_add(&C, &C, rx, group->meth));
vinnie@1674 220 MP_CHECKOK(group->meth->field_add(&C3, rx, rx, group->meth));
vinnie@1674 221 MP_CHECKOK(group->meth->field_sub(&A, rx, rx, group->meth));
vinnie@1674 222
vinnie@1674 223 /* C3 = py * C^3 */
vinnie@1674 224 MP_CHECKOK(group->meth->field_mul(py, &C3, &C3, group->meth));
vinnie@1674 225
vinnie@1674 226 /* ry = D * (px * C^2 - rx) - py * C^3 */
vinnie@1674 227 MP_CHECKOK(group->meth->field_sub(&C, rx, ry, group->meth));
vinnie@1674 228 MP_CHECKOK(group->meth->field_mul(&D, ry, ry, group->meth));
vinnie@1674 229 MP_CHECKOK(group->meth->field_sub(ry, &C3, ry, group->meth));
vinnie@1674 230
vinnie@1674 231 CLEANUP:
vinnie@1674 232 mp_clear(&A);
vinnie@1674 233 mp_clear(&B);
vinnie@1674 234 mp_clear(&C);
vinnie@1674 235 mp_clear(&D);
vinnie@1674 236 mp_clear(&C2);
vinnie@1674 237 mp_clear(&C3);
vinnie@1674 238 return res;
vinnie@1674 239 }
vinnie@1674 240
vinnie@1674 241 /* Computes R = 2P. Elliptic curve points P and R can be identical. Uses
vinnie@1674 242 * Jacobian coordinates.
vinnie@1674 243 *
vinnie@1674 244 * Assumes input is already field-encoded using field_enc, and returns
vinnie@1674 245 * output that is still field-encoded.
vinnie@1674 246 *
vinnie@1674 247 * This routine implements Point Doubling in the Jacobian Projective
vinnie@1674 248 * space as described in the paper "Efficient elliptic curve exponentiation
vinnie@1674 249 * using mixed coordinates", by H. Cohen, A Miyaji, T. Ono.
vinnie@1674 250 */
vinnie@1674 251 mp_err
vinnie@1674 252 ec_GFp_pt_dbl_jac(const mp_int *px, const mp_int *py, const mp_int *pz,
vinnie@1674 253 mp_int *rx, mp_int *ry, mp_int *rz, const ECGroup *group)
vinnie@1674 254 {
vinnie@1674 255 mp_err res = MP_OKAY;
vinnie@1674 256 mp_int t0, t1, M, S;
vinnie@1674 257
vinnie@1674 258 MP_DIGITS(&t0) = 0;
vinnie@1674 259 MP_DIGITS(&t1) = 0;
vinnie@1674 260 MP_DIGITS(&M) = 0;
vinnie@1674 261 MP_DIGITS(&S) = 0;
vinnie@1674 262 MP_CHECKOK(mp_init(&t0, FLAG(px)));
vinnie@1674 263 MP_CHECKOK(mp_init(&t1, FLAG(px)));
vinnie@1674 264 MP_CHECKOK(mp_init(&M, FLAG(px)));
vinnie@1674 265 MP_CHECKOK(mp_init(&S, FLAG(px)));
vinnie@1674 266
vinnie@1674 267 if (ec_GFp_pt_is_inf_jac(px, py, pz) == MP_YES) {
vinnie@1674 268 MP_CHECKOK(ec_GFp_pt_set_inf_jac(rx, ry, rz));
vinnie@1674 269 goto CLEANUP;
vinnie@1674 270 }
vinnie@1674 271
vinnie@1674 272 if (mp_cmp_d(pz, 1) == 0) {
vinnie@1674 273 /* M = 3 * px^2 + a */
vinnie@1674 274 MP_CHECKOK(group->meth->field_sqr(px, &t0, group->meth));
vinnie@1674 275 MP_CHECKOK(group->meth->field_add(&t0, &t0, &M, group->meth));
vinnie@1674 276 MP_CHECKOK(group->meth->field_add(&t0, &M, &t0, group->meth));
vinnie@1674 277 MP_CHECKOK(group->meth->
vinnie@1674 278 field_add(&t0, &group->curvea, &M, group->meth));
vinnie@1674 279 } else if (mp_cmp_int(&group->curvea, -3, FLAG(px)) == 0) {
vinnie@1674 280 /* M = 3 * (px + pz^2) * (px - pz^2) */
vinnie@1674 281 MP_CHECKOK(group->meth->field_sqr(pz, &M, group->meth));
vinnie@1674 282 MP_CHECKOK(group->meth->field_add(px, &M, &t0, group->meth));
vinnie@1674 283 MP_CHECKOK(group->meth->field_sub(px, &M, &t1, group->meth));
vinnie@1674 284 MP_CHECKOK(group->meth->field_mul(&t0, &t1, &M, group->meth));
vinnie@1674 285 MP_CHECKOK(group->meth->field_add(&M, &M, &t0, group->meth));
vinnie@1674 286 MP_CHECKOK(group->meth->field_add(&t0, &M, &M, group->meth));
vinnie@1674 287 } else {
vinnie@1674 288 /* M = 3 * (px^2) + a * (pz^4) */
vinnie@1674 289 MP_CHECKOK(group->meth->field_sqr(px, &t0, group->meth));
vinnie@1674 290 MP_CHECKOK(group->meth->field_add(&t0, &t0, &M, group->meth));
vinnie@1674 291 MP_CHECKOK(group->meth->field_add(&t0, &M, &t0, group->meth));
vinnie@1674 292 MP_CHECKOK(group->meth->field_sqr(pz, &M, group->meth));
vinnie@1674 293 MP_CHECKOK(group->meth->field_sqr(&M, &M, group->meth));
vinnie@1674 294 MP_CHECKOK(group->meth->
vinnie@1674 295 field_mul(&M, &group->curvea, &M, group->meth));
vinnie@1674 296 MP_CHECKOK(group->meth->field_add(&M, &t0, &M, group->meth));
vinnie@1674 297 }
vinnie@1674 298
vinnie@1674 299 /* rz = 2 * py * pz */
vinnie@1674 300 /* t0 = 4 * py^2 */
vinnie@1674 301 if (mp_cmp_d(pz, 1) == 0) {
vinnie@1674 302 MP_CHECKOK(group->meth->field_add(py, py, rz, group->meth));
vinnie@1674 303 MP_CHECKOK(group->meth->field_sqr(rz, &t0, group->meth));
vinnie@1674 304 } else {
vinnie@1674 305 MP_CHECKOK(group->meth->field_add(py, py, &t0, group->meth));
vinnie@1674 306 MP_CHECKOK(group->meth->field_mul(&t0, pz, rz, group->meth));
vinnie@1674 307 MP_CHECKOK(group->meth->field_sqr(&t0, &t0, group->meth));
vinnie@1674 308 }
vinnie@1674 309
vinnie@1674 310 /* S = 4 * px * py^2 = px * (2 * py)^2 */
vinnie@1674 311 MP_CHECKOK(group->meth->field_mul(px, &t0, &S, group->meth));
vinnie@1674 312
vinnie@1674 313 /* rx = M^2 - 2 * S */
vinnie@1674 314 MP_CHECKOK(group->meth->field_add(&S, &S, &t1, group->meth));
vinnie@1674 315 MP_CHECKOK(group->meth->field_sqr(&M, rx, group->meth));
vinnie@1674 316 MP_CHECKOK(group->meth->field_sub(rx, &t1, rx, group->meth));
vinnie@1674 317
vinnie@1674 318 /* ry = M * (S - rx) - 8 * py^4 */
vinnie@1674 319 MP_CHECKOK(group->meth->field_sqr(&t0, &t1, group->meth));
vinnie@1674 320 if (mp_isodd(&t1)) {
vinnie@1674 321 MP_CHECKOK(mp_add(&t1, &group->meth->irr, &t1));
vinnie@1674 322 }
vinnie@1674 323 MP_CHECKOK(mp_div_2(&t1, &t1));
vinnie@1674 324 MP_CHECKOK(group->meth->field_sub(&S, rx, &S, group->meth));
vinnie@1674 325 MP_CHECKOK(group->meth->field_mul(&M, &S, &M, group->meth));
vinnie@1674 326 MP_CHECKOK(group->meth->field_sub(&M, &t1, ry, group->meth));
vinnie@1674 327
vinnie@1674 328 CLEANUP:
vinnie@1674 329 mp_clear(&t0);
vinnie@1674 330 mp_clear(&t1);
vinnie@1674 331 mp_clear(&M);
vinnie@1674 332 mp_clear(&S);
vinnie@1674 333 return res;
vinnie@1674 334 }
vinnie@1674 335
vinnie@1674 336 /* by default, this routine is unused and thus doesn't need to be compiled */
vinnie@1674 337 #ifdef ECL_ENABLE_GFP_PT_MUL_JAC
vinnie@1674 338 /* Computes R = nP where R is (rx, ry) and P is (px, py). The parameters
vinnie@1674 339 * a, b and p are the elliptic curve coefficients and the prime that
vinnie@1674 340 * determines the field GFp. Elliptic curve points P and R can be
vinnie@1674 341 * identical. Uses mixed Jacobian-affine coordinates. Assumes input is
vinnie@1674 342 * already field-encoded using field_enc, and returns output that is still
vinnie@1674 343 * field-encoded. Uses 4-bit window method. */
vinnie@1674 344 mp_err
vinnie@1674 345 ec_GFp_pt_mul_jac(const mp_int *n, const mp_int *px, const mp_int *py,
vinnie@1674 346 mp_int *rx, mp_int *ry, const ECGroup *group)
vinnie@1674 347 {
vinnie@1674 348 mp_err res = MP_OKAY;
vinnie@1674 349 mp_int precomp[16][2], rz;
vinnie@1674 350 int i, ni, d;
vinnie@1674 351
vinnie@1674 352 MP_DIGITS(&rz) = 0;
vinnie@1674 353 for (i = 0; i < 16; i++) {
vinnie@1674 354 MP_DIGITS(&precomp[i][0]) = 0;
vinnie@1674 355 MP_DIGITS(&precomp[i][1]) = 0;
vinnie@1674 356 }
vinnie@1674 357
vinnie@1674 358 ARGCHK(group != NULL, MP_BADARG);
vinnie@1674 359 ARGCHK((n != NULL) && (px != NULL) && (py != NULL), MP_BADARG);
vinnie@1674 360
vinnie@1674 361 /* initialize precomputation table */
vinnie@1674 362 for (i = 0; i < 16; i++) {
vinnie@1674 363 MP_CHECKOK(mp_init(&precomp[i][0]));
vinnie@1674 364 MP_CHECKOK(mp_init(&precomp[i][1]));
vinnie@1674 365 }
vinnie@1674 366
vinnie@1674 367 /* fill precomputation table */
vinnie@1674 368 mp_zero(&precomp[0][0]);
vinnie@1674 369 mp_zero(&precomp[0][1]);
vinnie@1674 370 MP_CHECKOK(mp_copy(px, &precomp[1][0]));
vinnie@1674 371 MP_CHECKOK(mp_copy(py, &precomp[1][1]));
vinnie@1674 372 for (i = 2; i < 16; i++) {
vinnie@1674 373 MP_CHECKOK(group->
vinnie@1674 374 point_add(&precomp[1][0], &precomp[1][1],
vinnie@1674 375 &precomp[i - 1][0], &precomp[i - 1][1],
vinnie@1674 376 &precomp[i][0], &precomp[i][1], group));
vinnie@1674 377 }
vinnie@1674 378
vinnie@1674 379 d = (mpl_significant_bits(n) + 3) / 4;
vinnie@1674 380
vinnie@1674 381 /* R = inf */
vinnie@1674 382 MP_CHECKOK(mp_init(&rz));
vinnie@1674 383 MP_CHECKOK(ec_GFp_pt_set_inf_jac(rx, ry, &rz));
vinnie@1674 384
vinnie@1674 385 for (i = d - 1; i >= 0; i--) {
vinnie@1674 386 /* compute window ni */
vinnie@1674 387 ni = MP_GET_BIT(n, 4 * i + 3);
vinnie@1674 388 ni <<= 1;
vinnie@1674 389 ni |= MP_GET_BIT(n, 4 * i + 2);
vinnie@1674 390 ni <<= 1;
vinnie@1674 391 ni |= MP_GET_BIT(n, 4 * i + 1);
vinnie@1674 392 ni <<= 1;
vinnie@1674 393 ni |= MP_GET_BIT(n, 4 * i);
vinnie@1674 394 /* R = 2^4 * R */
vinnie@1674 395 MP_CHECKOK(ec_GFp_pt_dbl_jac(rx, ry, &rz, rx, ry, &rz, group));
vinnie@1674 396 MP_CHECKOK(ec_GFp_pt_dbl_jac(rx, ry, &rz, rx, ry, &rz, group));
vinnie@1674 397 MP_CHECKOK(ec_GFp_pt_dbl_jac(rx, ry, &rz, rx, ry, &rz, group));
vinnie@1674 398 MP_CHECKOK(ec_GFp_pt_dbl_jac(rx, ry, &rz, rx, ry, &rz, group));
vinnie@1674 399 /* R = R + (ni * P) */
vinnie@1674 400 MP_CHECKOK(ec_GFp_pt_add_jac_aff
vinnie@1674 401 (rx, ry, &rz, &precomp[ni][0], &precomp[ni][1], rx, ry,
vinnie@1674 402 &rz, group));
vinnie@1674 403 }
vinnie@1674 404
vinnie@1674 405 /* convert result S to affine coordinates */
vinnie@1674 406 MP_CHECKOK(ec_GFp_pt_jac2aff(rx, ry, &rz, rx, ry, group));
vinnie@1674 407
vinnie@1674 408 CLEANUP:
vinnie@1674 409 mp_clear(&rz);
vinnie@1674 410 for (i = 0; i < 16; i++) {
vinnie@1674 411 mp_clear(&precomp[i][0]);
vinnie@1674 412 mp_clear(&precomp[i][1]);
vinnie@1674 413 }
vinnie@1674 414 return res;
vinnie@1674 415 }
vinnie@1674 416 #endif
vinnie@1674 417
vinnie@1674 418 /* Elliptic curve scalar-point multiplication. Computes R(x, y) = k1 * G +
vinnie@1674 419 * k2 * P(x, y), where G is the generator (base point) of the group of
vinnie@1674 420 * points on the elliptic curve. Allows k1 = NULL or { k2, P } = NULL.
vinnie@1674 421 * Uses mixed Jacobian-affine coordinates. Input and output values are
vinnie@1674 422 * assumed to be NOT field-encoded. Uses algorithm 15 (simultaneous
vinnie@1674 423 * multiple point multiplication) from Brown, Hankerson, Lopez, Menezes.
vinnie@1674 424 * Software Implementation of the NIST Elliptic Curves over Prime Fields. */
vinnie@1674 425 mp_err
vinnie@1674 426 ec_GFp_pts_mul_jac(const mp_int *k1, const mp_int *k2, const mp_int *px,
vinnie@1674 427 const mp_int *py, mp_int *rx, mp_int *ry,
vinnie@1674 428 const ECGroup *group)
vinnie@1674 429 {
vinnie@1674 430 mp_err res = MP_OKAY;
vinnie@1674 431 mp_int precomp[4][4][2];
vinnie@1674 432 mp_int rz;
vinnie@1674 433 const mp_int *a, *b;
vinnie@1674 434 int i, j;
vinnie@1674 435 int ai, bi, d;
vinnie@1674 436
vinnie@1674 437 for (i = 0; i < 4; i++) {
vinnie@1674 438 for (j = 0; j < 4; j++) {
vinnie@1674 439 MP_DIGITS(&precomp[i][j][0]) = 0;
vinnie@1674 440 MP_DIGITS(&precomp[i][j][1]) = 0;
vinnie@1674 441 }
vinnie@1674 442 }
vinnie@1674 443 MP_DIGITS(&rz) = 0;
vinnie@1674 444
vinnie@1674 445 ARGCHK(group != NULL, MP_BADARG);
vinnie@1674 446 ARGCHK(!((k1 == NULL)
vinnie@1674 447 && ((k2 == NULL) || (px == NULL)
vinnie@1674 448 || (py == NULL))), MP_BADARG);
vinnie@1674 449
vinnie@1674 450 /* if some arguments are not defined used ECPoint_mul */
vinnie@1674 451 if (k1 == NULL) {
vinnie@1674 452 return ECPoint_mul(group, k2, px, py, rx, ry);
vinnie@1674 453 } else if ((k2 == NULL) || (px == NULL) || (py == NULL)) {
vinnie@1674 454 return ECPoint_mul(group, k1, NULL, NULL, rx, ry);
vinnie@1674 455 }
vinnie@1674 456
vinnie@1674 457 /* initialize precomputation table */
vinnie@1674 458 for (i = 0; i < 4; i++) {
vinnie@1674 459 for (j = 0; j < 4; j++) {
vinnie@1674 460 MP_CHECKOK(mp_init(&precomp[i][j][0], FLAG(k1)));
vinnie@1674 461 MP_CHECKOK(mp_init(&precomp[i][j][1], FLAG(k1)));
vinnie@1674 462 }
vinnie@1674 463 }
vinnie@1674 464
vinnie@1674 465 /* fill precomputation table */
vinnie@1674 466 /* assign {k1, k2} = {a, b} such that len(a) >= len(b) */
vinnie@1674 467 if (mpl_significant_bits(k1) < mpl_significant_bits(k2)) {
vinnie@1674 468 a = k2;
vinnie@1674 469 b = k1;
vinnie@1674 470 if (group->meth->field_enc) {
vinnie@1674 471 MP_CHECKOK(group->meth->
vinnie@1674 472 field_enc(px, &precomp[1][0][0], group->meth));
vinnie@1674 473 MP_CHECKOK(group->meth->
vinnie@1674 474 field_enc(py, &precomp[1][0][1], group->meth));
vinnie@1674 475 } else {
vinnie@1674 476 MP_CHECKOK(mp_copy(px, &precomp[1][0][0]));
vinnie@1674 477 MP_CHECKOK(mp_copy(py, &precomp[1][0][1]));
vinnie@1674 478 }
vinnie@1674 479 MP_CHECKOK(mp_copy(&group->genx, &precomp[0][1][0]));
vinnie@1674 480 MP_CHECKOK(mp_copy(&group->geny, &precomp[0][1][1]));
vinnie@1674 481 } else {
vinnie@1674 482 a = k1;
vinnie@1674 483 b = k2;
vinnie@1674 484 MP_CHECKOK(mp_copy(&group->genx, &precomp[1][0][0]));
vinnie@1674 485 MP_CHECKOK(mp_copy(&group->geny, &precomp[1][0][1]));
vinnie@1674 486 if (group->meth->field_enc) {
vinnie@1674 487 MP_CHECKOK(group->meth->
vinnie@1674 488 field_enc(px, &precomp[0][1][0], group->meth));
vinnie@1674 489 MP_CHECKOK(group->meth->
vinnie@1674 490 field_enc(py, &precomp[0][1][1], group->meth));
vinnie@1674 491 } else {
vinnie@1674 492 MP_CHECKOK(mp_copy(px, &precomp[0][1][0]));
vinnie@1674 493 MP_CHECKOK(mp_copy(py, &precomp[0][1][1]));
vinnie@1674 494 }
vinnie@1674 495 }
vinnie@1674 496 /* precompute [*][0][*] */
vinnie@1674 497 mp_zero(&precomp[0][0][0]);
vinnie@1674 498 mp_zero(&precomp[0][0][1]);
vinnie@1674 499 MP_CHECKOK(group->
vinnie@1674 500 point_dbl(&precomp[1][0][0], &precomp[1][0][1],
vinnie@1674 501 &precomp[2][0][0], &precomp[2][0][1], group));
vinnie@1674 502 MP_CHECKOK(group->
vinnie@1674 503 point_add(&precomp[1][0][0], &precomp[1][0][1],
vinnie@1674 504 &precomp[2][0][0], &precomp[2][0][1],
vinnie@1674 505 &precomp[3][0][0], &precomp[3][0][1], group));
vinnie@1674 506 /* precompute [*][1][*] */
vinnie@1674 507 for (i = 1; i < 4; i++) {
vinnie@1674 508 MP_CHECKOK(group->
vinnie@1674 509 point_add(&precomp[0][1][0], &precomp[0][1][1],
vinnie@1674 510 &precomp[i][0][0], &precomp[i][0][1],
vinnie@1674 511 &precomp[i][1][0], &precomp[i][1][1], group));
vinnie@1674 512 }
vinnie@1674 513 /* precompute [*][2][*] */
vinnie@1674 514 MP_CHECKOK(group->
vinnie@1674 515 point_dbl(&precomp[0][1][0], &precomp[0][1][1],
vinnie@1674 516 &precomp[0][2][0], &precomp[0][2][1], group));
vinnie@1674 517 for (i = 1; i < 4; i++) {
vinnie@1674 518 MP_CHECKOK(group->
vinnie@1674 519 point_add(&precomp[0][2][0], &precomp[0][2][1],
vinnie@1674 520 &precomp[i][0][0], &precomp[i][0][1],
vinnie@1674 521 &precomp[i][2][0], &precomp[i][2][1], group));
vinnie@1674 522 }
vinnie@1674 523 /* precompute [*][3][*] */
vinnie@1674 524 MP_CHECKOK(group->
vinnie@1674 525 point_add(&precomp[0][1][0], &precomp[0][1][1],
vinnie@1674 526 &precomp[0][2][0], &precomp[0][2][1],
vinnie@1674 527 &precomp[0][3][0], &precomp[0][3][1], group));
vinnie@1674 528 for (i = 1; i < 4; i++) {
vinnie@1674 529 MP_CHECKOK(group->
vinnie@1674 530 point_add(&precomp[0][3][0], &precomp[0][3][1],
vinnie@1674 531 &precomp[i][0][0], &precomp[i][0][1],
vinnie@1674 532 &precomp[i][3][0], &precomp[i][3][1], group));
vinnie@1674 533 }
vinnie@1674 534
vinnie@1674 535 d = (mpl_significant_bits(a) + 1) / 2;
vinnie@1674 536
vinnie@1674 537 /* R = inf */
vinnie@1674 538 MP_CHECKOK(mp_init(&rz, FLAG(k1)));
vinnie@1674 539 MP_CHECKOK(ec_GFp_pt_set_inf_jac(rx, ry, &rz));
vinnie@1674 540
vinnie@1674 541 for (i = d - 1; i >= 0; i--) {
vinnie@1674 542 ai = MP_GET_BIT(a, 2 * i + 1);
vinnie@1674 543 ai <<= 1;
vinnie@1674 544 ai |= MP_GET_BIT(a, 2 * i);
vinnie@1674 545 bi = MP_GET_BIT(b, 2 * i + 1);
vinnie@1674 546 bi <<= 1;
vinnie@1674 547 bi |= MP_GET_BIT(b, 2 * i);
vinnie@1674 548 /* R = 2^2 * R */
vinnie@1674 549 MP_CHECKOK(ec_GFp_pt_dbl_jac(rx, ry, &rz, rx, ry, &rz, group));
vinnie@1674 550 MP_CHECKOK(ec_GFp_pt_dbl_jac(rx, ry, &rz, rx, ry, &rz, group));
vinnie@1674 551 /* R = R + (ai * A + bi * B) */
vinnie@1674 552 MP_CHECKOK(ec_GFp_pt_add_jac_aff
vinnie@1674 553 (rx, ry, &rz, &precomp[ai][bi][0], &precomp[ai][bi][1],
vinnie@1674 554 rx, ry, &rz, group));
vinnie@1674 555 }
vinnie@1674 556
vinnie@1674 557 MP_CHECKOK(ec_GFp_pt_jac2aff(rx, ry, &rz, rx, ry, group));
vinnie@1674 558
vinnie@1674 559 if (group->meth->field_dec) {
vinnie@1674 560 MP_CHECKOK(group->meth->field_dec(rx, rx, group->meth));
vinnie@1674 561 MP_CHECKOK(group->meth->field_dec(ry, ry, group->meth));
vinnie@1674 562 }
vinnie@1674 563
vinnie@1674 564 CLEANUP:
vinnie@1674 565 mp_clear(&rz);
vinnie@1674 566 for (i = 0; i < 4; i++) {
vinnie@1674 567 for (j = 0; j < 4; j++) {
vinnie@1674 568 mp_clear(&precomp[i][j][0]);
vinnie@1674 569 mp_clear(&precomp[i][j][1]);
vinnie@1674 570 }
vinnie@1674 571 }
vinnie@1674 572 return res;
vinnie@1674 573 }