-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
174 lines (132 loc) · 5.23 KB
/
test.c
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "bitcoin.h"
static int test_hexstr(void)
{
int err = 0;
char *expected = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad";
uint64_t buflen;
uint8_t *buf = hextobuf(expected, &buflen);
char *actual = buftohex(buf, buflen);
free(buf);
err = err || strcmp(expected, actual);
free(actual);
return err;
}
static int test_sha256(void)
{
int err = 0;
uint8_t sha256[SHA256_SIZE];
char *str;
sha256_encode((uint8_t *)"abc", 3, sha256);
str = buftohex(sha256, sizeof(sha256));
err = err || strcmp(str, "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
free(str);
sha256_encode((uint8_t *)"qwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwerty", 56, sha256);
str = buftohex(sha256, sizeof(sha256));
err = err || strcmp(str, "45af198c730b5bbb42a997cd2b14c1364995a8d3210a4ce91ca7a3cac451c9f0");
free(str);
sha256_encode((uint8_t *)"iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii", 63, sha256);
str = buftohex(sha256, sizeof(sha256));
err = err || strcmp(str, "95791dbc15f57efffe740116d876928bbd383aef1186bb88b46d849a7066ef9b");
free(str);
sha256_encode((uint8_t *)"iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii", 64, sha256);
str = buftohex(sha256, sizeof(sha256));
err = err || strcmp(str, "a343b617ce1070a37251a5e66b409947ec3d3ff7d89b9de482d7df84402778d2");
free(str);
sha256_encode((uint8_t *)
"qwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwerty"
"qwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwerty"
"qwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwerty"
, 56 * 3, sha256);
str = buftohex(sha256, sizeof(sha256));
err = err || strcmp(str, "b3b92da0d8c10cf435676e3cfd7d5992caf3fb53fc1418d11f2b20c000eb7872");
free(str);
sha256_encode((uint8_t *)
"qwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwerty"
"qwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwerty"
"qwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwerty"
"qwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwerty"
"qwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwerty"
"qwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwerty"
"qwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwerty"
"qwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwerty"
, 56 * 8, sha256);
str = buftohex(sha256, sizeof(sha256));
err = err || strcmp(str, "06289543043e585ec04f756f971ef8ceda1577ca5344b69a30c829a81a626cfb");
free(str);
return err;
}
static int test_base58(void)
{
int err = 0;
char *res;
res = base58_encode((uint8_t *)"a", 1);
err = err || strcmp(res, "2g");
free(res);
res = base58_encode((uint8_t *)"abc", 3);
err = err || strcmp(res, "ZiCa");
free(res);
res = base58_encode((uint8_t *)"qwertyuiopqwertyuiop", 20);
err = err || strcmp(res, "2agjfoWbNPGS3tMXRsKAK3PkDCh5");
free(res);
return err;
}
static int test_base58check(void)
{
int err = 0;
char *res;
uint8_t sha256[SHA256_SIZE];
sha256_encode((uint8_t *)"aaaaaaaaaaaaaaaa", 16, sha256);
res = base58check_encode(0x80, sha256, sizeof(sha256));
err = err || strcmp(res, "5HubJ2xgcnt244PH7gs1G63yWrLetPGjVoELTMahmPL6yw9jnEu");
free(res);
sha256_encode((uint8_t *)"abcdefghijklmnopqrstuvwxyz", 26, sha256);
res = base58check_encode(0x80, sha256, sizeof(sha256));
err = err || strcmp(res, "5JgPd2TjTgsfUBqhwJS53Q8nKyYhxvprFEUpDtXk67waaRWeYKq");
free(res);
sha256_encode((uint8_t *)
"abcdefghijklmnopqrstuvwxyz"
"abcdefghijklmnopqrstuvwxyz"
"abcdefghijklmnopqrstuvwxyz"
"abcdefghijklmnopqrstuvwxyz"
, 26 * 4, sha256);
res = base58check_encode(0x80, sha256, sizeof(sha256));
err = err || strcmp(res, "5JwWiKvNTV8MWtEUXSKVb2wqXpEKXi2TXbc2bgC8yisQutkbUtC");
free(res);
return err;
}
int test_secp256k1(void)
{
int err = 0;
uint8_t privkey[SHA256_SIZE];
uint8_t pubkey[SECP256K1_PUB_SIZE];
sha256_encode((uint8_t *)"aaaaaaaaaaaaaaaa", 16, privkey);
secp256k1_privtopub(privkey, sizeof(privkey), pubkey);
char *expected = "04"
"1d06fb5e9bfdfd9ea1ff1eb97be76f7dcb88c23ce8e0f4ab57394572445a847b"
"ca075746d686ec4b622362b01080c3585b32c5f1742ffb326073984349456419";
char *actual = buftohex(pubkey, sizeof(pubkey));
err = err || strcmp(expected, actual);
free(actual);
return err;
}
int test(void)
{
int err = 0;
int hexstrerr = test_hexstr();
printf("hexstr:\t\t%s\n", hexstrerr ? "failed" : "ok");
int sha256err = test_sha256();
printf("sha256:\t\t%s\n", sha256err ? "failed" : "ok");
err = err || sha256err;
int base58err = test_base58();
printf("base58:\t\t%s\n", base58err ? "failed" : "ok");
err = err || base58err;
int base58checkerr = test_base58check();
printf("base58check:\t%s\n", base58checkerr ? "failed" : "ok");
err = err || base58checkerr;
int secp256k1err = test_secp256k1();
printf("secp256k1:\t%s\n", secp256k1err ? "failed" : "ok");
err = err || secp256k1err;
if (err)
return 1;
return 0;
}