]> Git Repo - secp256k1.git/blob - src/tests.cpp
0d5ca874b45d6b86f14392d063e8faedb17cc796
[secp256k1.git] / src / tests.cpp
1 #include <assert.h>
2
3 #include "num.cpp"
4 #include "field.cpp"
5 #include "group.cpp"
6 #include "ecmult.cpp"
7 #include "ecdsa.cpp"
8
9 // #define COUNT 2
10 #define COUNT 100
11
12 using namespace secp256k1;
13
14 void test_run_ecmult_chain() {
15     // random starting point A (on the curve)
16     secp256k1_fe_t ax; secp256k1_fe_set_hex(&ax, "8b30bbe9ae2a990696b22f670709dff3727fd8bc04d3362c6c7bf458e2846004", 64);
17     secp256k1_fe_t ay; secp256k1_fe_set_hex(&ay, "a357ae915c4a65281309edf20504740f0eb3343990216b4f81063cb65f2f7e0f", 64);
18     GroupElemJac a(ax,ay);
19     // two random initial factors xn and gn
20     secp256k1_num_t xn;
21     secp256k1_num_init(&xn);
22     secp256k1_num_set_hex(&xn, "84cc5452f7fde1edb4d38a8ce9b1b84ccef31f146e569be9705d357a42985407", 64);
23     secp256k1_num_t gn;
24     secp256k1_num_init(&gn);
25     secp256k1_num_set_hex(&gn, "a1e58d22553dcd42b23980625d4c57a96e9323d42b3152e5ca2c3990edc7c9de", 64);
26     // two small multipliers to be applied to xn and gn in every iteration:
27     secp256k1_num_t xf;
28     secp256k1_num_init(&xf);
29     secp256k1_num_set_hex(&xf, "1337", 4);
30     secp256k1_num_t gf;
31     secp256k1_num_init(&gf);
32     secp256k1_num_set_hex(&gf, "7113", 4);
33     // accumulators with the resulting coefficients to A and G
34     secp256k1_num_t ae;
35     secp256k1_num_init(&ae);
36     secp256k1_num_set_int(&ae, 1);
37     secp256k1_num_t ge;
38     secp256k1_num_init(&ge);
39     secp256k1_num_set_int(&ge, 0);
40     // the point being computed
41     GroupElemJac x = a;
42     const secp256k1_num_t &order = GetGroupConst().order;
43     for (int i=0; i<200*COUNT; i++) {
44         // in each iteration, compute X = xn*X + gn*G;
45         ECMult(x, x, xn, gn);
46         // also compute ae and ge: the actual accumulated factors for A and G
47         // if X was (ae*A+ge*G), xn*X + gn*G results in (xn*ae*A + (xn*ge+gn)*G)
48         secp256k1_num_mod_mul(&ae, &ae, &xn, &order);
49         secp256k1_num_mod_mul(&ge, &ge, &xn, &order);
50         secp256k1_num_add(&ge, &ge, &gn);
51         secp256k1_num_mod(&ge, &ge, &order);
52         // modify xn and gn
53         secp256k1_num_mod_mul(&xn, &xn, &xf, &order);
54         secp256k1_num_mod_mul(&gn, &gn, &gf, &order);
55     }
56     std::string res = x.ToString();
57     if (COUNT == 100) {
58       assert(res == "(D6E96687F9B10D092A6F35439D86CEBEA4535D0D409F53586440BD74B933E830,B95CBCA2C77DA786539BE8FD53354D2D3B4F566AE658045407ED6015EE1B2A88)");
59     }
60     // redo the computation, but directly with the resulting ae and ge coefficients:
61     GroupElemJac x2; ECMult(x2, a, ae, ge);
62     std::string res2 = x2.ToString();
63     assert(res == res2);
64     secp256k1_num_free(&xn);
65     secp256k1_num_free(&gn);
66     secp256k1_num_free(&xf);
67     secp256k1_num_free(&gf);
68     secp256k1_num_free(&ae);
69     secp256k1_num_free(&ge);
70 }
71
72 void test_point_times_order(const GroupElemJac &point) {
73     // either the point is not on the curve, or multiplying it by the order results in O
74     if (!point.IsValid())
75         return;
76
77     const GroupConstants &c = GetGroupConst();
78     secp256k1_num_t zero;
79     secp256k1_num_init(&zero);
80     secp256k1_num_set_int(&zero, 0);
81     GroupElemJac res;
82     ECMult(res, point, c.order, zero); // calc res = order * point + 0 * G;
83     assert(res.IsInfinity());
84     secp256k1_num_free(&zero);
85 }
86
87 void test_run_point_times_order() {
88     secp256k1_fe_t x; secp256k1_fe_set_hex(&x, "02", 2);
89     for (int i=0; i<500; i++) {
90         GroupElemJac j; j.SetCompressed(x, true);
91         test_point_times_order(j);
92         secp256k1_fe_sqr(&x, &x);
93     }
94     char c[65]; int cl=65;
95     secp256k1_fe_get_hex(c, &cl, &x);
96     assert(strcmp(c, "7603CB59B0EF6C63FE6084792A0C378CDB3233A80F8A9A09A877DEAD31B38C45") == 0);
97 }
98
99 void test_wnaf(const secp256k1_num_t &number, int w) {
100     secp256k1_num_t x, two, t;
101     secp256k1_num_init(&x);
102     secp256k1_num_init(&two);
103     secp256k1_num_init(&t);
104     secp256k1_num_set_int(&x, 0);
105     secp256k1_num_set_int(&two, 2);
106     WNAF<1023> wnaf(number, w);
107     int zeroes = -1;
108     for (int i=wnaf.GetSize()-1; i>=0; i--) {
109         secp256k1_num_mul(&x, &x, &two);
110         int v = wnaf.Get(i);
111         if (v) {
112             assert(zeroes == -1 || zeroes >= w-1); // check that distance between non-zero elements is at least w-1
113             zeroes=0;
114             assert((v & 1) == 1); // check non-zero elements are odd
115             assert(v <= (1 << (w-1)) - 1); // check range below
116             assert(v >= -(1 << (w-1)) - 1); // check range above
117         } else {
118             assert(zeroes != -1); // check that no unnecessary zero padding exists
119             zeroes++;
120         }
121         secp256k1_num_set_int(&t, v);
122         secp256k1_num_add(&x, &x, &t);
123     }
124     assert(secp256k1_num_cmp(&x, &number) == 0); // check that wnaf represents number
125     secp256k1_num_free(&x);
126     secp256k1_num_free(&two);
127     secp256k1_num_free(&t);
128 }
129
130 void test_run_wnaf() {
131     secp256k1_num_t range, min, n;
132     secp256k1_num_init(&range);
133     secp256k1_num_init(&min);
134     secp256k1_num_init(&n);
135     secp256k1_num_set_hex(&range, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 256);
136     secp256k1_num_copy(&min, &range);
137     secp256k1_num_shift(&min, 1);
138     secp256k1_num_negate(&min);
139     for (int i=0; i<COUNT; i++) {
140         secp256k1_num_set_rand(&n, &range);
141         secp256k1_num_add(&n, &n, &min);
142         test_wnaf(n, 4+(i%10));
143     }
144     secp256k1_num_free(&range);
145     secp256k1_num_free(&min);
146     secp256k1_num_free(&n);
147 }
148
149 void test_ecdsa_sign_verify() {
150     const GroupConstants &c = GetGroupConst();
151     secp256k1_num_t msg, key, nonce;
152     secp256k1_num_init(&msg);
153     secp256k1_num_set_rand(&msg, &c.order);
154     secp256k1_num_init(&key);
155     secp256k1_num_set_rand(&key, &c.order);
156     secp256k1_num_init(&nonce);
157     GroupElemJac pub; ECMultBase(pub, key);
158     Signature sig;
159     do {
160         secp256k1_num_set_rand(&nonce, &c.order);
161     } while(!sig.Sign(key, msg, nonce));
162     assert(sig.Verify(pub, msg));
163     secp256k1_num_inc(&msg);
164     assert(!sig.Verify(pub, msg));
165     secp256k1_num_free(&msg);
166     secp256k1_num_free(&key);
167     secp256k1_num_free(&nonce);
168 }
169
170 void test_run_ecdsa_sign_verify() {
171     for (int i=0; i<10*COUNT; i++) {
172         test_ecdsa_sign_verify();
173     }
174 }
175
176 int main(void) {
177     secp256k1_num_start();
178     secp256k1_fe_start();
179
180     test_run_wnaf();
181     test_run_point_times_order();
182     test_run_ecmult_chain();
183     test_run_ecdsa_sign_verify();
184
185     secp256k1_fe_stop();
186     return 0;
187 }
This page took 0.025362 seconds and 2 git commands to generate.