-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram1.c
More file actions
68 lines (51 loc) · 1.37 KB
/
program1.c
File metadata and controls
68 lines (51 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*----------------------------------------------------------------------------*\
*
* program1.c
* lucas@pamorana.net
*
* Implements \p main, using the contrived double-and-add algorithm to
* perform a scalar multiplication of an elliptic curve point. The curve
* used is secp256k1.
*
\*----------------------------------------------------------------------------*/
#include <stdlib.h>
#include "dbl_and_add.h"
#define P \
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F"
#define Q \
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141"
#define Gx \
"79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798"
#define Gy \
"483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8"
#define ELEN(A) (sizeof(A[0]))
#define ALEN(A) (sizeof(A)/sizeof(A[0]))
int main(int argc, char *argv[])
{
EC C;
PT R;
mpz_t k;
unsigned char kbuf[256/8];
point_init(&R);
curve_init(&C);
mpz_init(k);
// secp256k1
mpz_set_str(C.p, P, 16);
mpz_set_str(C.q, Q, 16);
mpz_set_ui (C.a, 0);
mpz_set_ui (C.b, 7);
mpz_set_str(C.g.x, Gx, 16);
mpz_set_str(C.g.y, Gy, 16);
do
{
arc4random_buf(kbuf, sizeof(kbuf));
mpz_import(k, ALEN(kbuf), 1, ELEN(kbuf), 1, 0, kbuf);
}
while (mpz_cmp(C.q, k) <= 0);
dbl_and_add(&C, k, &C.g, &R);
gmp_printf("truth: %Zd\n", k);
mpz_clear(k);
curve_free(&C);
point_free(&R);
return EXIT_SUCCESS;
}