New implementation of Curve25519 (using multiplications of words of 15 bits or so...
[BearSSL] / test / test_speed.c
index 4304cd7..48c2fdb 100644 (file)
@@ -287,6 +287,79 @@ test_speed_ghash_ctmul64(void)
        test_speed_ghash_inner("GHASH (ctmul64)", &br_ghash_ctmul64);
 }
 
+static uint32_t
+fake_chacha20(const void *key, const void *iv,
+       uint32_t cc, void *data, size_t len)
+{
+       (void)key;
+       (void)iv;
+       (void)data;
+       (void)len;
+       return cc + (uint32_t)((len + 63) >> 6);
+}
+
+/*
+ * To speed-test Poly1305, we run it with a do-nothing stub instead of
+ * ChaCha20.
+ */
+static void
+test_speed_poly1305_inner(char *name, br_poly1305_run pl)
+{
+       unsigned char buf[8192], key[32], iv[12], aad[13], tag[16];
+       int i;
+       long num;
+
+       memset(key, 'K', sizeof key);
+       memset(iv, 'I', sizeof iv);
+       memset(aad, 'A', sizeof aad);
+       memset(buf, 'T', sizeof buf);
+       for (i = 0; i < 10; i ++) {
+               pl(key, iv, buf, sizeof buf,
+                       aad, sizeof aad, tag, &fake_chacha20, 0);
+       }
+       num = 10;
+       for (;;) {
+               clock_t begin, end;
+               double tt;
+               long k;
+
+               begin = clock();
+               for (k = num; k > 0; k --) {
+                       pl(key, iv, buf, sizeof buf,
+                               aad, sizeof aad, tag, &fake_chacha20, 0);
+               }
+               end = clock();
+               tt = (double)(end - begin) / CLOCKS_PER_SEC;
+               if (tt >= 2.0) {
+                       printf("%-30s %8.2f MB/s\n", name,
+                               ((double)sizeof buf) * (double)num
+                               / (tt * 1000000.0));
+                       fflush(stdout);
+                       return;
+               }
+               num <<= 1;
+       }
+}
+
+static void
+test_speed_poly1305_ctmul(void)
+{
+       test_speed_poly1305_inner("Poly1305 (ctmul)", &br_poly1305_ctmul_run);
+}
+
+static void
+test_speed_poly1305_ctmul32(void)
+{
+       test_speed_poly1305_inner("Poly1305 (ctmul32)",
+               &br_poly1305_ctmul32_run);
+}
+
+static void
+test_speed_poly1305_i15(void)
+{
+       test_speed_poly1305_inner("Poly1305 (i15)", &br_poly1305_i15_run);
+}
+
 static const unsigned char RSA_N[] = {
        0xE9, 0xF2, 0x4A, 0x2F, 0x96, 0xDF, 0x0A, 0x23,
        0x01, 0x85, 0xF1, 0x2C, 0xB2, 0xA8, 0xEF, 0x23,
@@ -497,6 +570,13 @@ test_speed_rsa_inner(char *name,
        }
 }
 
+static void
+test_speed_rsa_i15(void)
+{
+       test_speed_rsa_inner("RSA i15",
+               &br_rsa_i15_public, &br_rsa_i15_private);
+}
+
 static void
 test_speed_rsa_i31(void)
 {
@@ -512,7 +592,7 @@ test_speed_rsa_i32(void)
 }
 
 static void
-test_speed_ec_inner(const char *name,
+test_speed_ec_inner_1(const char *name,
        const br_ec_impl *impl, const br_ec_curve_def *cd)
 {
        unsigned char bx[80], U[160];
@@ -553,6 +633,72 @@ test_speed_ec_inner(const char *name,
        }
 }
 
+static void
+test_speed_ec_inner_2(const char *name,
+       const br_ec_impl *impl, const br_ec_curve_def *cd)
+{
+       unsigned char bx[80], U[160];
+       uint32_t x[22], n[22];
+       size_t nlen;
+       int i;
+       long num;
+
+       nlen = cd->order_len;
+       br_i31_decode(n, cd->order, nlen);
+       memset(bx, 'T', sizeof bx);
+       br_i31_decode_reduce(x, bx, sizeof bx, n);
+       br_i31_encode(bx, nlen, x);
+       for (i = 0; i < 10; i ++) {
+               impl->mulgen(U, bx, nlen, cd->curve);
+       }
+       num = 10;
+       for (;;) {
+               clock_t begin, end;
+               double tt;
+               long k;
+
+               begin = clock();
+               for (k = num; k > 0; k --) {
+                       impl->mulgen(U, bx, nlen, cd->curve);
+               }
+               end = clock();
+               tt = (double)(end - begin) / CLOCKS_PER_SEC;
+               if (tt >= 2.0) {
+                       printf("%-30s %8.2f mul/s\n", name,
+                               (double)num / tt);
+                       fflush(stdout);
+                       break;
+               }
+               num <<= 1;
+       }
+}
+
+static void
+test_speed_ec_inner(const char *name,
+       const br_ec_impl *impl, const br_ec_curve_def *cd)
+{
+       char tmp[50];
+
+       test_speed_ec_inner_1(name, impl, cd);
+       sprintf(tmp, "%s (FP)", name);
+       test_speed_ec_inner_2(tmp, impl, cd);
+}
+
+static void
+test_speed_ec_p256_m15(void)
+{
+       test_speed_ec_inner("EC m15 P-256",
+               &br_ec_p256_m15, &br_secp256r1);
+}
+
+static void
+test_speed_ec_prime_i15(void)
+{
+       test_speed_ec_inner("EC i15 P-256", &br_ec_prime_i15, &br_secp256r1);
+       test_speed_ec_inner("EC i15 P-384", &br_ec_prime_i15, &br_secp384r1);
+       test_speed_ec_inner("EC i15 P-521", &br_ec_prime_i15, &br_secp521r1);
+}
+
 static void
 test_speed_ec_prime_i31(void)
 {
@@ -561,6 +707,20 @@ test_speed_ec_prime_i31(void)
        test_speed_ec_inner("EC i31 P-521", &br_ec_prime_i31, &br_secp521r1);
 }
 
+static void
+test_speed_ec_c25519_i15(void)
+{
+       test_speed_ec_inner("EC i15 C25519",
+               &br_ec_c25519_i15, &br_curve25519);
+}
+
+static void
+test_speed_ec_c25519_m15(void)
+{
+       test_speed_ec_inner("EC m15 C25519",
+               &br_ec_c25519_m15, &br_curve25519);
+}
+
 static void
 test_speed_ecdsa_inner(const char *name,
        const br_ec_impl *impl, const br_ec_curve_def *cd,
@@ -646,6 +806,32 @@ test_speed_ecdsa_inner(const char *name,
        }
 }
 
+static void
+test_speed_ecdsa_p256_m15(void)
+{
+       test_speed_ecdsa_inner("ECDSA m15 P-256",
+               &br_ec_p256_m15, &br_secp256r1,
+               &br_ecdsa_i15_sign_asn1,
+               &br_ecdsa_i15_vrfy_asn1);
+}
+
+static void
+test_speed_ecdsa_i15(void)
+{
+       test_speed_ecdsa_inner("ECDSA i15 P-256",
+               &br_ec_prime_i15, &br_secp256r1,
+               &br_ecdsa_i15_sign_asn1,
+               &br_ecdsa_i15_vrfy_asn1);
+       test_speed_ecdsa_inner("ECDSA i15 P-384",
+               &br_ec_prime_i15, &br_secp384r1,
+               &br_ecdsa_i15_sign_asn1,
+               &br_ecdsa_i15_vrfy_asn1);
+       test_speed_ecdsa_inner("ECDSA i15 P-521",
+               &br_ec_prime_i15, &br_secp521r1,
+               &br_ecdsa_i15_sign_asn1,
+               &br_ecdsa_i15_vrfy_asn1);
+}
+
 static void
 test_speed_ecdsa_i31(void)
 {
@@ -1066,9 +1252,20 @@ static const struct {
        STU(ghash_ctmul32),
        STU(ghash_ctmul64),
 
+       STU(poly1305_ctmul),
+       STU(poly1305_ctmul32),
+       STU(poly1305_i15),
+
+       STU(rsa_i15),
        STU(rsa_i31),
        STU(rsa_i32),
+       STU(ec_p256_m15),
+       STU(ec_prime_i15),
        STU(ec_prime_i31),
+       STU(ec_c25519_i15),
+       STU(ec_c25519_m15),
+       STU(ecdsa_p256_m15),
+       STU(ecdsa_i15),
        STU(ecdsa_i31),
 
        STU(i31)