Skip to content

Commit c45b7c4

Browse files
committed
refactor: introduce testutil.h (deduplicate random_fe_, ge_equals_ helpers)
1 parent dc55141 commit c45b7c4

File tree

4 files changed

+58
-86
lines changed

4 files changed

+58
-86
lines changed

Makefile.am

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ noinst_HEADERS += src/precomputed_ecmult.h
4646
noinst_HEADERS += src/precomputed_ecmult_gen.h
4747
noinst_HEADERS += src/assumptions.h
4848
noinst_HEADERS += src/checkmem.h
49+
noinst_HEADERS += src/testutil.h
4950
noinst_HEADERS += src/util.h
5051
noinst_HEADERS += src/int128.h
5152
noinst_HEADERS += src/int128_impl.h

src/tests.c

+1-42
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "../include/secp256k1_preallocated.h"
2424
#include "testrand_impl.h"
2525
#include "checkmem.h"
26+
#include "testutil.h"
2627
#include "util.h"
2728

2829
#include "../contrib/lax_der_parsing.c"
@@ -2956,22 +2957,6 @@ static void run_scalar_tests(void) {
29562957

29572958
/***** FIELD TESTS *****/
29582959

2959-
static void random_fe(secp256k1_fe *x) {
2960-
unsigned char bin[32];
2961-
do {
2962-
secp256k1_testrand256(bin);
2963-
if (secp256k1_fe_set_b32_limit(x, bin)) {
2964-
return;
2965-
}
2966-
} while(1);
2967-
}
2968-
2969-
static void random_fe_non_zero(secp256k1_fe *nz) {
2970-
do {
2971-
random_fe(nz);
2972-
} while (secp256k1_fe_is_zero(nz));
2973-
}
2974-
29752960
static void random_fe_non_square(secp256k1_fe *ns) {
29762961
secp256k1_fe r;
29772962
random_fe_non_zero(ns);
@@ -3691,15 +3676,6 @@ static void run_inverse_tests(void)
36913676

36923677
/***** GROUP TESTS *****/
36933678

3694-
static void ge_equals_ge(const secp256k1_ge *a, const secp256k1_ge *b) {
3695-
CHECK(a->infinity == b->infinity);
3696-
if (a->infinity) {
3697-
return;
3698-
}
3699-
CHECK(secp256k1_fe_equal(&a->x, &b->x));
3700-
CHECK(secp256k1_fe_equal(&a->y, &b->y));
3701-
}
3702-
37033679
/* This compares jacobian points including their Z, not just their geometric meaning. */
37043680
static int gej_xyz_equals_gej(const secp256k1_gej *a, const secp256k1_gej *b) {
37053681
secp256k1_gej a2;
@@ -3722,23 +3698,6 @@ static int gej_xyz_equals_gej(const secp256k1_gej *a, const secp256k1_gej *b) {
37223698
return ret;
37233699
}
37243700

3725-
static void ge_equals_gej(const secp256k1_ge *a, const secp256k1_gej *b) {
3726-
secp256k1_fe z2s;
3727-
secp256k1_fe u1, u2, s1, s2;
3728-
CHECK(a->infinity == b->infinity);
3729-
if (a->infinity) {
3730-
return;
3731-
}
3732-
/* Check a.x * b.z^2 == b.x && a.y * b.z^3 == b.y, to avoid inverses. */
3733-
secp256k1_fe_sqr(&z2s, &b->z);
3734-
secp256k1_fe_mul(&u1, &a->x, &z2s);
3735-
u2 = b->x;
3736-
secp256k1_fe_mul(&s1, &a->y, &z2s); secp256k1_fe_mul(&s1, &s1, &b->z);
3737-
s2 = b->y;
3738-
CHECK(secp256k1_fe_equal(&u1, &u2));
3739-
CHECK(secp256k1_fe_equal(&s1, &s2));
3740-
}
3741-
37423701
static void test_ge(void) {
37433702
int i, i1;
37443703
int runs = 6;

src/tests_exhaustive.c

+1-44
Original file line numberDiff line numberDiff line change
@@ -28,54 +28,11 @@
2828
#include "testrand_impl.h"
2929
#include "ecmult_compute_table_impl.h"
3030
#include "ecmult_gen_compute_table_impl.h"
31+
#include "testutil.h"
3132
#include "util.h"
3233

3334
static int count = 2;
3435

35-
/** stolen from tests.c */
36-
static void ge_equals_ge(const secp256k1_ge *a, const secp256k1_ge *b) {
37-
CHECK(a->infinity == b->infinity);
38-
if (a->infinity) {
39-
return;
40-
}
41-
CHECK(secp256k1_fe_equal(&a->x, &b->x));
42-
CHECK(secp256k1_fe_equal(&a->y, &b->y));
43-
}
44-
45-
static void ge_equals_gej(const secp256k1_ge *a, const secp256k1_gej *b) {
46-
secp256k1_fe z2s;
47-
secp256k1_fe u1, u2, s1, s2;
48-
CHECK(a->infinity == b->infinity);
49-
if (a->infinity) {
50-
return;
51-
}
52-
/* Check a.x * b.z^2 == b.x && a.y * b.z^3 == b.y, to avoid inverses. */
53-
secp256k1_fe_sqr(&z2s, &b->z);
54-
secp256k1_fe_mul(&u1, &a->x, &z2s);
55-
u2 = b->x;
56-
secp256k1_fe_mul(&s1, &a->y, &z2s); secp256k1_fe_mul(&s1, &s1, &b->z);
57-
s2 = b->y;
58-
CHECK(secp256k1_fe_equal(&u1, &u2));
59-
CHECK(secp256k1_fe_equal(&s1, &s2));
60-
}
61-
62-
static void random_fe(secp256k1_fe *x) {
63-
unsigned char bin[32];
64-
do {
65-
secp256k1_testrand256(bin);
66-
if (secp256k1_fe_set_b32_limit(x, bin)) {
67-
return;
68-
}
69-
} while(1);
70-
}
71-
72-
static void random_fe_non_zero(secp256k1_fe *nz) {
73-
do {
74-
random_fe(nz);
75-
} while (secp256k1_fe_is_zero(nz));
76-
}
77-
/** END stolen from tests.c */
78-
7936
static uint32_t num_cores = 1;
8037
static uint32_t this_core = 0;
8138

src/testutil.h

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/***********************************************************************
2+
* Distributed under the MIT software license, see the accompanying *
3+
* file COPYING or https://www.opensource.org/licenses/mit-license.php.*
4+
***********************************************************************/
5+
6+
#ifndef SECP256K1_TESTUTIL_H
7+
#define SECP256K1_TESTUTIL_H
8+
9+
#include "field.h"
10+
#include "testrand.h"
11+
#include "util.h"
12+
13+
static void random_fe(secp256k1_fe *x) {
14+
unsigned char bin[32];
15+
do {
16+
secp256k1_testrand256(bin);
17+
if (secp256k1_fe_set_b32_limit(x, bin)) {
18+
return;
19+
}
20+
} while(1);
21+
}
22+
23+
static void random_fe_non_zero(secp256k1_fe *nz) {
24+
do {
25+
random_fe(nz);
26+
} while (secp256k1_fe_is_zero(nz));
27+
}
28+
29+
static void ge_equals_ge(const secp256k1_ge *a, const secp256k1_ge *b) {
30+
CHECK(a->infinity == b->infinity);
31+
if (a->infinity) {
32+
return;
33+
}
34+
CHECK(secp256k1_fe_equal(&a->x, &b->x));
35+
CHECK(secp256k1_fe_equal(&a->y, &b->y));
36+
}
37+
38+
static void ge_equals_gej(const secp256k1_ge *a, const secp256k1_gej *b) {
39+
secp256k1_fe z2s;
40+
secp256k1_fe u1, u2, s1, s2;
41+
CHECK(a->infinity == b->infinity);
42+
if (a->infinity) {
43+
return;
44+
}
45+
/* Check a.x * b.z^2 == b.x && a.y * b.z^3 == b.y, to avoid inverses. */
46+
secp256k1_fe_sqr(&z2s, &b->z);
47+
secp256k1_fe_mul(&u1, &a->x, &z2s);
48+
u2 = b->x;
49+
secp256k1_fe_mul(&s1, &a->y, &z2s); secp256k1_fe_mul(&s1, &s1, &b->z);
50+
s2 = b->y;
51+
CHECK(secp256k1_fe_equal(&u1, &u2));
52+
CHECK(secp256k1_fe_equal(&s1, &s2));
53+
}
54+
55+
#endif /* SECP256K1_TESTUTIL_H */

0 commit comments

Comments
 (0)