Skip to content

Commit

Permalink
Merge bitcoin-core/secp256k1#955: Add random field multiply/square tests
Browse files Browse the repository at this point in the history
bdf19f1 Add random field multiply/square tests (Pieter Wuille)

Pull request description:

ACKs for top commit:
  real-or-random:
    ACK bitcoin-core/secp256k1@bdf19f1
  jonasnick:
    ACK bdf19f1

Tree-SHA512: e78ce25f5440e87ad2cad0d4a87e5d95c983bc0be3a3e53d97f9cf6d8b3c3db9a830cb5f2f8c62f2f6dc9c6703c2a507cc23fa18d60bb624716e024539db5c21
  • Loading branch information
jonasnick committed Jun 30, 2021
2 parents 75ce488 + bdf19f1 commit 4866178
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions src/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -2508,6 +2508,70 @@ void run_field_misc(void) {
}
}

void test_fe_mul(const secp256k1_fe* a, const secp256k1_fe* b, int use_sqr)
{
secp256k1_fe c, an, bn;
/* Variables in BE 32-byte format. */
unsigned char a32[32], b32[32], c32[32];
/* Variables in LE 16x uint16_t format. */
uint16_t a16[16], b16[16], c16[16];
/* Field modulus in LE 16x uint16_t format. */
static const uint16_t m16[16] = {
0xfc2f, 0xffff, 0xfffe, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
};
uint16_t t16[32];
int i;

/* Compute C = A * B in fe format. */
c = *a;
if (use_sqr) {
secp256k1_fe_sqr(&c, &c);
} else {
secp256k1_fe_mul(&c, &c, b);
}

/* Convert A, B, C into LE 16x uint16_t format. */
an = *a;
bn = *b;
secp256k1_fe_normalize_var(&c);
secp256k1_fe_normalize_var(&an);
secp256k1_fe_normalize_var(&bn);
secp256k1_fe_get_b32(a32, &an);
secp256k1_fe_get_b32(b32, &bn);
secp256k1_fe_get_b32(c32, &c);
for (i = 0; i < 16; ++i) {
a16[i] = a32[31 - 2*i] + ((uint16_t)a32[30 - 2*i] << 8);
b16[i] = b32[31 - 2*i] + ((uint16_t)b32[30 - 2*i] << 8);
c16[i] = c32[31 - 2*i] + ((uint16_t)c32[30 - 2*i] << 8);
}
/* Compute T = A * B in LE 16x uint16_t format. */
mulmod256(t16, a16, b16, m16);
/* Compare */
CHECK(secp256k1_memcmp_var(t16, c16, 32) == 0);
}

void run_fe_mul(void) {
int i;
for (i = 0; i < 100 * count; ++i) {
secp256k1_fe a, b, c, d;
random_fe(&a);
random_field_element_magnitude(&a);
random_fe(&b);
random_field_element_magnitude(&b);
random_fe_test(&c);
random_field_element_magnitude(&c);
random_fe_test(&d);
random_field_element_magnitude(&d);
test_fe_mul(&a, &a, 1);
test_fe_mul(&c, &c, 1);
test_fe_mul(&a, &b, 0);
test_fe_mul(&a, &c, 0);
test_fe_mul(&c, &b, 0);
test_fe_mul(&c, &d, 0);
}
}

void run_sqr(void) {
secp256k1_fe x, s;

Expand Down Expand Up @@ -6512,6 +6576,7 @@ int main(int argc, char **argv) {
/* field tests */
run_field_misc();
run_field_convert();
run_fe_mul();
run_sqr();
run_sqrt();

Expand Down

0 comments on commit 4866178

Please sign in to comment.