Correct signedness
[openssl.git] / crypto / ec / ec2_mult.c
index 41b4c957156a4998461ae39c800b00587d1ca904..a0ee7c152ff6d11eefba780ac055ca938c731e9b 100644 (file)
@@ -9,25 +9,12 @@
  * The ECC Code is licensed pursuant to the OpenSSL open source
  * license provided below.
  *
- * In addition, Sun covenants to all licensees who provide a reciprocal
- * covenant with respect to their own patents if any, not to sue under
- * current and future patent claims necessarily infringed by the making,
- * using, practicing, selling, offering for sale and/or otherwise
- * disposing of the ECC Code as delivered hereunder (or portions thereof),
- * provided that such covenant shall not apply:
- *  1) for code that a licensee deletes from the ECC Code;
- *  2) separates from the ECC Code; or
- *  3) for infringements caused by:
- *       i) the modification of the ECC Code or
- *      ii) the combination of the ECC Code with other software or
- *          devices where such combination causes the infringement.
- *
  * The software is originally written by Sheueling Chang Shantz and
  * Douglas Stebila of Sun Microsystems Laboratories.
  *
  */
 /* ====================================================================
- * Copyright (c) 1998-2002 The OpenSSL Project.  All rights reserved.
+ * Copyright (c) 1998-2003 The OpenSSL Project.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -92,7 +79,7 @@
  *     GF(2^m) without precomputation".
  * modified to not require precomputation of c=b^{2^{m-1}}.
  */
-static int Mdouble(const EC_GROUP *group, BIGNUM *x, BIGNUM *z, BN_CTX *ctx)
+static int gf2m_Mdouble(const EC_GROUP *group, BIGNUM *x, BIGNUM *z, BN_CTX *ctx)
        {
        BIGNUM *t1;
        int ret = 0;
@@ -123,7 +110,7 @@ static int Mdouble(const EC_GROUP *group, BIGNUM *x, BIGNUM *z, BN_CTX *ctx)
  *     Lopex, J. and Dahab, R.  "Fast multiplication on elliptic curves over 
  *     GF(2^m) without precomputation".
  */
-static int Madd(const EC_GROUP *group, const BIGNUM *x, BIGNUM *x1, BIGNUM *z1, 
+static int gf2m_Madd(const EC_GROUP *group, const BIGNUM *x, BIGNUM *x1, BIGNUM *z1, 
        const BIGNUM *x2, const BIGNUM *z2, BN_CTX *ctx)
        {
        BIGNUM *t1, *t2;
@@ -151,9 +138,8 @@ static int Madd(const EC_GROUP *group, const BIGNUM *x, BIGNUM *x1, BIGNUM *z1,
        return ret;
        }
 
-/* Compute the affine coordinates x2, y2=z2 for the point (x1/z1) and (x2/x2) in
- * Montgomery projective coordinates.
- * Uses algorithm Mxy in appendix of 
+/* Compute the x, y affine coordinates from the point (x1, z1) (x2, z2) 
+ * using Montgomery point multiplication algorithm Mxy() in appendix of 
  *     Lopex, J. and Dahab, R.  "Fast multiplication on elliptic curves over 
  *     GF(2^m) without precomputation".
  * Returns:
@@ -161,7 +147,7 @@ static int Madd(const EC_GROUP *group, const BIGNUM *x, BIGNUM *x1, BIGNUM *z1,
  *     1 if return value should be the point at infinity
  *     2 otherwise
  */
-static int Mxy(const EC_GROUP *group, const BIGNUM *x, const BIGNUM *y, BIGNUM *x1, 
+static int gf2m_Mxy(const EC_GROUP *group, const BIGNUM *x, const BIGNUM *y, BIGNUM *x1, 
        BIGNUM *z1, BIGNUM *x2, BIGNUM *z2, BN_CTX *ctx)
        {
        BIGNUM *t3, *t4, *t5;
@@ -226,7 +212,7 @@ static int Mxy(const EC_GROUP *group, const BIGNUM *x, const BIGNUM *y, BIGNUM *
  *     Lopex, J. and Dahab, R.  "Fast multiplication on elliptic curves over 
  *     GF(2^m) without precomputation".
  */
-static int point_multiply(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
+static int ec_GF2m_montgomery_point_multiply(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
        const EC_POINT *point, BN_CTX *ctx)
        {
        BIGNUM *x1, *x2, *z1, *z2;
@@ -282,13 +268,13 @@ static int point_multiply(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scal
                        {
                        if (scalar->d[i] & mask)
                                {
-                               if (!Madd(group, &point->X, x1, z1, x2, z2, ctx)) goto err;
-                               if (!Mdouble(group, x2, z2, ctx)) goto err;
+                               if (!gf2m_Madd(group, &point->X, x1, z1, x2, z2, ctx)) goto err;
+                               if (!gf2m_Mdouble(group, x2, z2, ctx)) goto err;
                                }
                        else
                                {
-                               if (!Madd(group, &point->X, x2, z2, x1, z1, ctx)) goto err;
-                               if (!Mdouble(group, x1, z1, ctx)) goto err;
+                               if (!gf2m_Madd(group, &point->X, x2, z2, x1, z1, ctx)) goto err;
+                               if (!gf2m_Mdouble(group, x1, z1, ctx)) goto err;
                                }
                        mask >>= 1;
                        }
@@ -297,7 +283,7 @@ static int point_multiply(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scal
                }
 
        /* convert out of "projective" coordinates */
-       i = Mxy(group, &point->X, &point->Y, x1, z1, x2, z2, ctx);
+       i = gf2m_Mxy(group, &point->X, &point->Y, x1, z1, x2, z2, ctx);
        if (i == 0) goto err;
        else if (i == 1) 
                {
@@ -310,8 +296,8 @@ static int point_multiply(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scal
                }
 
        /* GF(2^m) field elements should always have BIGNUM::neg = 0 */
-       r->X.neg = 0;
-       r->Y.neg = 0;
+       BN_set_sign(&r->X, 0);
+       BN_set_sign(&r->Y, 0);
 
        ret = 1;
 
@@ -325,11 +311,12 @@ static int point_multiply(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scal
  *     scalar*group->generator + scalars[0]*points[0] + ... + scalars[num-1]*points[num-1]
  * gracefully ignoring NULL scalar values.
  */
-int ec_GF2m_mont_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
+int ec_GF2m_simple_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
        size_t num, const EC_POINT *points[], const BIGNUM *scalars[], BN_CTX *ctx)
        {
        BN_CTX *new_ctx = NULL;
-       int ret = 0, i;
+       int ret = 0;
+       size_t i;
        EC_POINT *p=NULL;
 
        if (ctx == NULL)
@@ -340,9 +327,10 @@ int ec_GF2m_mont_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
                }
 
        /* This implementation is more efficient than the wNAF implementation for 2
-        * or fewer points.  Use the ec_wNAF_mul implementation for 3 or more points.
+        * or fewer points.  Use the ec_wNAF_mul implementation for 3 or more points,
+        * or if we can perform a fast multiplication based on precomputation.
         */
-       if ((scalar && (num > 1)) || (num > 2))
+       if ((scalar && (num > 1)) || (num > 2) || (num == 0 && EC_GROUP_have_precompute_mult(group)))
                {
                ret = ec_wNAF_mul(group, r, scalar, num, points, scalars, ctx);
                goto err;
@@ -354,15 +342,17 @@ int ec_GF2m_mont_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
 
        if (scalar)
                {
-               if (!point_multiply(group, p, scalar, group->generator, ctx)) goto err;
-               if (scalar->neg) if (!group->meth->invert(group, p, ctx)) goto err;
+               if (!ec_GF2m_montgomery_point_multiply(group, p, scalar, group->generator, ctx)) goto err;
+               if (BN_get_sign(scalar)) 
+                       if (!group->meth->invert(group, p, ctx)) goto err;
                if (!group->meth->add(group, r, r, p, ctx)) goto err;
                }
 
        for (i = 0; i < num; i++)
                {
-               if (!point_multiply(group, p, scalars[i], points[i], ctx)) goto err;
-               if (scalars[i]->neg) if (!group->meth->invert(group, p, ctx)) goto err;
+               if (!ec_GF2m_montgomery_point_multiply(group, p, scalars[i], points[i], ctx)) goto err;
+               if (BN_get_sign(scalars[i]))
+                       if (!group->meth->invert(group, p, ctx)) goto err;
                if (!group->meth->add(group, r, r, p, ctx)) goto err;
                }
 
@@ -376,12 +366,15 @@ int ec_GF2m_mont_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
        }
 
 
-/* Precomputation for point multiplication. */ 
-int ec_GF2m_mont_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
+/* Precomputation for point multiplication: fall back to wNAF methods
+ * because ec_GF2m_simple_mul() uses ec_wNAF_mul() if appropriate */
+
+int ec_GF2m_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
        {
-       /* There is no precomputation to do for Montgomery scalar multiplication but
-        * since this implementation falls back to the wNAF multiplication for more than
-        * two points, call the wNAF implementation's precompute.
-        */
        return ec_wNAF_precompute_mult(group, ctx);
-       }
+       }
+
+int ec_GF2m_have_precompute_mult(const EC_GROUP *group)
+       {
+       return ec_wNAF_have_precompute_mult(group);
+       }