12 using namespace secp256k1;
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
21 secp256k1_num_init(&xn);
22 secp256k1_num_set_hex(&xn, "84cc5452f7fde1edb4d38a8ce9b1b84ccef31f146e569be9705d357a42985407", 64);
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:
28 secp256k1_num_init(&xf);
29 secp256k1_num_set_hex(&xf, "1337", 4);
31 secp256k1_num_init(&gf);
32 secp256k1_num_set_hex(&gf, "7113", 4);
33 // accumulators with the resulting coefficients to A and G
35 secp256k1_num_init(&ae);
36 secp256k1_num_set_int(&ae, 1);
38 secp256k1_num_init(&ge);
39 secp256k1_num_set_int(&ge, 0);
40 // the point being computed
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;
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);
53 secp256k1_num_mod_mul(&xn, &xn, &xf, &order);
54 secp256k1_num_mod_mul(&gn, &gn, &gf, &order);
56 std::string res = x.ToString();
58 assert(res == "(D6E96687F9B10D092A6F35439D86CEBEA4535D0D409F53586440BD74B933E830,B95CBCA2C77DA786539BE8FD53354D2D3B4F566AE658045407ED6015EE1B2A88)");
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();
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);
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
77 const GroupConstants &c = GetGroupConst();
79 secp256k1_num_init(&zero);
80 secp256k1_num_set_int(&zero, 0);
82 ECMult(res, point, c.order, zero); // calc res = order * point + 0 * G;
83 assert(res.IsInfinity());
84 secp256k1_num_free(&zero);
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);
94 char c[65]; int cl=65;
95 secp256k1_fe_get_hex(c, &cl, &x);
96 assert(strcmp(c, "7603CB59B0EF6C63FE6084792A0C378CDB3233A80F8A9A09A877DEAD31B38C45") == 0);
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);
108 for (int i=wnaf.GetSize()-1; i>=0; i--) {
109 secp256k1_num_mul(&x, &x, &two);
112 assert(zeroes == -1 || zeroes >= w-1); // check that distance between non-zero elements is at least w-1
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
118 assert(zeroes != -1); // check that no unnecessary zero padding exists
121 secp256k1_num_set_int(&t, v);
122 secp256k1_num_add(&x, &x, &t);
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);
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));
144 secp256k1_num_free(&range);
145 secp256k1_num_free(&min);
146 secp256k1_num_free(&n);
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);
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);
170 void test_run_ecdsa_sign_verify() {
171 for (int i=0; i<10*COUNT; i++) {
172 test_ecdsa_sign_verify();
177 secp256k1_num_start();
178 secp256k1_fe_start();
181 test_run_point_times_order();
182 test_run_ecmult_chain();
183 test_run_ecdsa_sign_verify();