remove unnecessary calls to EC_POINT_copy()
[openssl.git] / crypto / ec / ectest.c
index ac0f055faf0432e2506c66b2318930bfc0da3452..243cd83fb54aa7d0cb5ebc06b0a8a74dd7e080ed 100644 (file)
@@ -55,6 +55,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <time.h>
 
 
@@ -64,6 +65,7 @@ int main(int argc, char * argv[]) { puts("Elliptic curves are disabled."); retur
 
 
 #include <openssl/ec.h>
+#include <openssl/engine.h>
 #include <openssl/err.h>
 
 #define ABORT do { \
@@ -74,7 +76,7 @@ int main(int argc, char * argv[]) { puts("Elliptic curves are disabled."); retur
 } while (0)
 
 
-void timings(EC_GROUP *group, int simult, BN_CTX *ctx)
+void timings(EC_GROUP *group, int multi, BN_CTX *ctx)
        {
        clock_t clck;
        int i, j;
@@ -99,13 +101,13 @@ void timings(EC_GROUP *group, int simult, BN_CTX *ctx)
        for (i = 0; i < 10; i++)
                {
                if (!BN_pseudo_rand(s, BN_num_bits(s), 0, 0)) ABORT;
-               if (simult)
+               if (multi)
                        {
                        if (!BN_pseudo_rand(s0, BN_num_bits(s), 0, 0)) ABORT;
                        }
                for (j = 0; j < 10; j++)
                        {
-                       if (!EC_POINT_mul(group, P, s, simult ? P : NULL, simult ? s0 : NULL, ctx)) ABORT;
+                       if (!EC_POINT_mul(group, P, s, multi ? P : NULL, multi ? s0 : NULL, ctx)) ABORT;
                        }
                fprintf(stdout, ".");
                fflush(stdout);
@@ -128,7 +130,7 @@ void timings(EC_GROUP *group, int simult, BN_CTX *ctx)
 #endif
 
        fprintf(stdout, "%i %s in %.2f " UNIT "\n", i*j,
-               simult ? "s*P+t*Q operations" : "point multiplications",
+               multi ? "s*P+t*Q operations" : "point multiplications",
                (double)clck/CLOCKS_PER_SEC);
        fprintf(stdout, "average: %.4f " UNIT "\n", (double)clck/(CLOCKS_PER_SEC*i*j));
 
@@ -148,12 +150,18 @@ int main(int argc, char *argv[])
        BIGNUM *x, *y, *z;
        unsigned char buf[100];
        size_t i, len;
+       int k;
        
        /* enable memory leak checking unless explicitly disabled */
        if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL) && (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off"))))
                {
                CRYPTO_malloc_debug_init();
-               CRYPTO_dbg_set_options(V_CRYPTO_MDEBUG_ALL);
+               CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
+               }
+       else
+               {
+               /* OPENSSL_DEBUG_MEMORY=off */
+               CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
                }
        CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
        ERR_load_crypto_strings();
@@ -230,8 +238,11 @@ int main(int argc, char *argv[])
                }
 
        fprintf(stdout, "A cyclic subgroup:\n");
+       k = 100;
        do
                {
+               if (k-- == 0) ABORT;
+
                if (EC_POINT_is_at_infinity(group, P))
                        fprintf(stdout, "     point at infinity\n");
                else
@@ -330,7 +341,7 @@ int main(int argc, char *argv[])
        if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
        fprintf(stdout, ".");
        fflush(stdout);
-       if (!EC_GROUP_precompute(group, ctx)) ABORT;
+       if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
        if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
        if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
        fprintf(stdout, " ok\n");
@@ -370,7 +381,7 @@ int main(int argc, char *argv[])
        if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
        fprintf(stdout, ".");
        fflush(stdout);
-       if (!EC_GROUP_precompute(group, ctx)) ABORT;
+       if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
        if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
        if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
        fprintf(stdout, " ok\n");
@@ -411,7 +422,7 @@ int main(int argc, char *argv[])
        if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
        fprintf(stdout, ".");
        fflush(stdout);
-       if (!EC_GROUP_precompute(group, ctx)) ABORT;
+       if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
        if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
        if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
        fprintf(stdout, " ok\n");
@@ -457,7 +468,7 @@ int main(int argc, char *argv[])
        if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
        fprintf(stdout, ".");
        fflush(stdout);
-       if (!EC_GROUP_precompute(group, ctx)) ABORT;
+       if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
        if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
        if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
        fprintf(stdout, " ok\n");
@@ -509,7 +520,7 @@ int main(int argc, char *argv[])
        if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
        fprintf(stdout, ".");
        fflush(stdout);
-       if (!EC_GROUP_precompute(group, ctx)) ABORT;
+       if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
        if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
        if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
        fprintf(stdout, " ok\n");
@@ -545,7 +556,7 @@ int main(int argc, char *argv[])
                scalars[0] = y; /* (group order + 1)/2,  so  y*Q + y*Q = Q */
                scalars[1] = y;
 
-               fprintf(stdout, "simultaneous multiplication ...");
+               fprintf(stdout, "combined multiplication ...");
                fflush(stdout);
 
                /* z is still the group order */
@@ -558,10 +569,10 @@ int main(int argc, char *argv[])
                fflush(stdout);
 
                if (!BN_pseudo_rand(y, BN_num_bits(y), 0, 0)) ABORT;
-               if (!BN_copy(z, y)) ABORT;
+               if (!BN_add(z, z, y)) ABORT;
                z->neg = 1;
                scalars[0] = y;
-               scalars[1] = z; /* z = -y */
+               scalars[1] = z; /* z = -(order + y) */
 
                if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) ABORT;
                if (!EC_POINT_is_at_infinity(group, P)) ABORT;
@@ -612,6 +623,8 @@ int main(int argc, char *argv[])
        if (P_384) EC_GROUP_free(P_384);
        if (P_521) EC_GROUP_free(P_521);
 
+       ENGINE_cleanup();
+       CRYPTO_cleanup_all_ex_data();
        ERR_free_strings();
        ERR_remove_state(0);
        CRYPTO_mem_leaks_fp(stderr);