-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.c
142 lines (103 loc) · 2.82 KB
/
tests.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
#include "lib/cryptoolz.h"
#define RESET "\033[0m"
#define RED "\033[31m"
#define GREEN "\033[32m"
int test1()
{
secretKey sk;
sk.sk = "00112233445566778899";
publicKey pk;
generate_keypair(&pk, &sk);
char *message = "1234";
signature sig;
sign_message(&sig, message, &sk, &pk);
int verified = verify_signature(&sig, message, &pk);
return verified;
}
int test2()
{
secretKey sk;
sk.sk = "00112233445566778899";
publicKey pk;
generate_keypair(&pk, &sk);
char *message = "1234";
signature sig;
sign_message(&sig, message, &sk, &pk);
char *fakeMessage = "5678";
int verified = !verify_signature(&sig, fakeMessage, &pk);
return verified;
}
int test3()
{
secretKey sk;
sk.sk = "00112233445566778899";
publicKey pk;
generate_keypair(&pk, &sk);
char *message = "1234";
signature sig;
sign_message(&sig, message, &sk, &pk);
mpz_set_str(sig.S, "123456789", 10);
int verified = !verify_signature(&sig, message, &pk);
return verified;
}
int test4()
{
secretKey sk;
sk.sk = "00112233445566778899";
publicKey pk;
generate_keypair(&pk, &sk);
char *message = "1234";
signature sig;
sign_message(&sig, message, &sk, &pk);
mpz_set_str(pk.A1, "123456789", 10);
int verified = !verify_signature(&sig, message, &pk);
return verified;
}
int test5()
{
mpz_t hash, x_in, k;
mpz_inits(hash, x_in, k, NULL);
mpz_set_str(x_in, "0011223344", 10);
mpz_set_str(k, "1234", 10);
mimc7(&hash, &x_in, &k);
return 1;
}
int test6()
{
int size = 4;
mpz_t hash, arr[size];
mpz_init(hash);
for (int i = 0; i < size; i++)
{
mpz_init(arr[i]);
mpz_set_str(arr[i], "0011223344", 10);
}
multi_mimc7(&hash, arr, &size);
return 1;
}
int main()
{
// we initialize the public BN128 and Baby JubJub parameters
init_public_parameters();
printf("Test 1: EdDSA - Generate keys, sign a message and verify: ");
if (test1()) printf(GREEN "PASSED\n" RESET);
else printf(RED "NOT PASSED\n" RESET);
printf("Test 2: EdDSA - Generate keys, sign a message and verify a fake message: ");
if (test2()) printf(GREEN "PASSED\n" RESET);
else printf(RED "NOT PASSED\n" RESET);
printf("Test 3: EdDSA - Generate keys, sign a message and verify a fake signature: ");
if (test3()) printf(GREEN "PASSED\n" RESET);
else printf(RED "NOT PASSED\n" RESET);
printf("Test 4: EdDSA - Generate keys, sign a message and verify using a fake public key: ");
if (test4()) printf(GREEN "PASSED\n" RESET);
else printf(RED "NOT PASSED\n" RESET);
printf("Test 5: MiMC7 - Hash a value with no exceptions: ");
if (test5()) printf(GREEN "PASSED\n" RESET);
else printf(RED "NOT PASSED\n" RESET);
printf("Test 6: MiMC7 - Hash an array of values with no exceptions: ");
if (test6()) printf(GREEN "PASSED\n" RESET);
else printf(RED "NOT PASSED\n" RESET);
// we clear the public parameters to avoid memory leaks
clear_public_parameters();
return 0;
}