forked from Election-Tech-Initiative/electionguard-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbignum.saw
272 lines (211 loc) · 8.85 KB
/
bignum.saw
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
include "mpz.saw";
let {{
p_array = [
0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF,
0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF,
0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF,
0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF,
0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF,
0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF,
0xFFFFFFFFFF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FE1284E702BB, 0x9B8C78E03E697F37,
0x8D25BCBCB94FEFD1, 0x2B7F97047F634232, 0x68881C3B96B389E1,
0x34CB3162CB73ED80, 0x52F7946C7E72907F, 0xD8B96862D443B5C2,
0x6F7B0E3FDC9F035C, 0xBF0F5AAB670B7901, 0x1A8BCDEBCF421CC9,
0xCBBE12C788E50328, 0x041EB59D81079497, 0xB667B96049DA04C7,
0x9D60F527B1C02F7E, 0xCBA66849179CB5CF, 0xBE7C990CD888B69C,
0x44171E4F54C21A8C, 0xFE9D821F195F7553, 0xB73A705707263EAE,
0xA3B7AFA7DED79ACF, 0x5A64F3BFB939B815, 0xC52085F40714F4C6,
0x460B0B0C3598E317, 0x46A06C2A3457676C, 0xB345C8A390EBB942,
0x8CEECEFA6FCB1C27, 0xA9E527A6C55B8D6B, 0x2B1868D6EC719E18,
0x9A799605C540F864, 0x1F135D5DC7FB62D5, 0x8E0DE0B6AE3AB90E,
0x91FB996505D7D928, 0x3DA833FF0CB6CC8C, 0xA7BAFA0E90BB1ADB,
0x81545A801F0016DC, 0x7088A4DF2CFB7D6D, 0xD876A2A5807BDAA4,
0x000DAFA2DFB6FBB0, 0xED9D775589156DDB, 0xFC24FF2203FFF9C5,
0xCF7C85C68F66DE94, 0xC98331F50FEF59CF, 0x8E7CE9D95FA008F7,
0xC1672D269C163751, 0x012826C4C8F5B5F4, 0xC11EDB62550F3CF9,
0x3D86F3CC6E22B0E7, 0x69AC659157F40383, 0xB5DF9DB9F8414F6C,
0xB5FA7D17BDDD3BC9, 0x0DC7BDC39BAF3BE6, 0x02A99E2A37CE3A5C,
0x098A8C1EFD3CD28A, 0x6B79306CA2C20C55, 0x174218A3935F697E,
0x813628D2D861BE54] : [64][64]
q_array = zero
# [0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF,
0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFF43] : [64][64]
}};
//common global [1 x %struct.__mpz_struct]
let p_global (rw : Term) = do {
global_alloc "p";
p <- mpz_t' CONST 4096 "p" (element (global "p") 0);
let pt = p.t;
precond {{ pt.value == p_array }};
};
//common global [1 x %struct.__mpz_struct]
let generator_global (rw : Term) = do {
global_alloc "generator";
g <- mpz_t' CONST 4096 "generator" (element (global "generator") 0);
let gt = g.t;
precond {{ gt.value == g_array }};
};
//common global [1 x %struct.__mpz_struct]
let q_global (rw : Term) = do {
global_alloc "q";
q <- mpz_t' CONST 4096 "q" (element (global "q") 0);
let qt = q.t;
precond {{ qt.value == q_array }};
};
bignum_bc <- llvm_load_module "obj/bignum.bc";
let bignum_verify function_name overrides spec solver = crucible_llvm_verify bignum_bc function_name overrides true spec solver;
//void Crypto_parameters_new()
let Crypto_parameters_new_spec = do {
global_init "g_array";
global_init "p_array";
global_init "q_array";
global_init "p";
global_init "q";
global_init "generator";
global_init "bignum_one";
execute [];
generator <- mpz_t' WRITE 4096 "generator" (element (global "generator") 0);
let generatort = generator.t;
postcond {{ from_mpz`{b=4096} generatort.value == join g_array }};
bignum_one <- mpz_t' WRITE 4096 "bignum_one" (element (global "bignum_one") 0);
let bignum_onet = bignum_one.t;
postcond {{ from_mpz`{b=4096} bignum_onet.value == 1 }};
p <- mpz_t' WRITE 4096 "p" (element (global "p") 0);
let pt = p.t;
postcond {{ from_mpz`{b=4096} pt.value == join p_array }};
q <- mpz_t' WRITE 256 "q" (element (global "q") 0);
let qt = q.t;
postcond {{ from_mpz`{b=256} qt.value == join (drop q_array) }};
};
//Create overrides
mpz_init_4096_ov <- mpz_init_ov bignum_bc 4096;
mpz_init_256_ov <- mpz_init_ov bignum_bc 256;
mpz_import_64_8_ov <- mpz_import_ov bignum_bc 64 8;
mpz_import_4_8_ov <- mpz_import_ov bignum_bc 4 8;
mpz_set_ui_4096_ov <- mpz_set_ui_ov bignum_bc 4096;
//Verify function
Crypto_parameters_new_ov <- bignum_verify
"Crypto_parameters_new"
[mpz_init_4096_ov, mpz_import_64_8_ov, mpz_import_4_8_ov, mpz_set_ui_4096_ov]
Crypto_parameters_new_spec
z3;
//void print_base16(const mpz_t z)
let print_base16_spec = do {
zp <- pointer CONST (struct_t "__mpz_struct");
execute [zp];
};
print_base16_ov <- crucible_llvm_unsafe_assume_spec bignum_bc "print_base16" print_base16_spec;
//void pow_mod_p(mpz_t res, const mpz_t base, const mpz_t exp)
let pow_mod_p_spec = do {
p_global CONST;
resp <- pointer WRITE (struct_t "__mpz_struct");
base <- mpz_t CONST 4096 "base";
exp <- mpz_t CONST 4096 "exp";
execute [resp, base.p, exp.p];
res' <- mpz_t' WRITE 4096 "res'" resp;
let (rest', baset, expt) = (res'.t, base.t, exp.t);
equal (from_cryptol {{ from_mpz`{b=4096} rest'.value }})
(from_cryptol {{ mod_pow (from_mpz`{b=4096} baset.value)
(from_mpz`{b=4096} expt.value)
(join p_array) }});
};
mpz_powm_4096_ov <- mpz_powm_ov bignum_bc 4096;
pow_mod_p_test_ov <- bignum_verify
"pow_mod_p"
[print_base16_ov, mpz_powm_4096_ov]
pow_mod_p_spec
(unint_z3 ["mod_pow"]);
//void pow_mod_q(mpz_t res, const mpz_t base, const mpz_t exp)
let pow_mod_q_spec = do {
q_global CONST;
resp <- pointer WRITE (struct_t "__mpz_struct");
base <- mpz_t CONST 4096 "base";
exp <- mpz_t CONST 4096 "exp";
execute [resp, base.p, exp.p];
res' <- mpz_t' WRITE 4096 "res'" resp;
let (rest', baset, expt) = (res'.t, base.t, exp.t);
equal (from_cryptol {{ from_mpz`{b=4096} rest'.value }})
(from_cryptol {{ mod_pow (from_mpz`{b=4096} baset.value)
(from_mpz`{b=4096} expt.value)
(join q_array) }});
};
pow_mod_q_test_ov <- bignum_verify
"pow_mod_q"
[print_base16_ov, mpz_powm_4096_ov]
pow_mod_q_spec
(unint_z3 ["mod_pow"]);
//void mul_mod_p(mpz_t res, const mpz_t a, const mpz_t b)
let mul_mod_p_spec = do {
p_global CONST;
resp <- pointer WRITE (struct_t "__mpz_struct");
a <- mpz_t CONST 4096 "a";
b <- mpz_t CONST 4096 "b";
execute [resp, a.p, b.p];
res' <- mpz_t' WRITE 4096 "resp'" resp;
let (rest', at, bt) = (res'.t, a.t, b.t);
equal (from_cryptol {{ from_mpz`{b=4096} rest'.value }})
(from_cryptol {{ mod_mul (from_mpz`{b=4096} at.value) (from_mpz`{b=4096} bt.value) (from_mpz`{b=4096} p_array) }});
};
mpz_mul_4096_ov <- mpz_mul_ov bignum_bc 4096;
mpz_mod_r_r_8192_4096_ov <- mpz_mod_r_r_ov bignum_bc 8192 4096;
mul_mod_p_ov <- bignum_verify
"mul_mod_p"
[mpz_mul_4096_ov, mpz_mod_r_r_8192_4096_ov]
mul_mod_p_spec
(unint_z3 ["safe_product", "safe_mod"]);
//void mul_mod_p(mpz_t res, const mpz_t res, const mpz_t b)
let mul_mod_p_r_r_spec = do {
p_global CONST;
res <- mpz_t WRITE 4096 "res";
b <- mpz_t CONST 4096 "b";
execute [res.p, res.p, b.p];
res' <- mpz_t' WRITE 4096 "resp'" res.p;
let (rest', rest, bt) = (res'.t, res.t, b.t);
equal (from_cryptol {{ from_mpz`{b=4096} rest'.value }})
(from_cryptol {{ mod_mul (from_mpz`{b=4096} rest.value) (from_mpz`{b=4096} bt.value) (from_mpz`{b=4096} p_array) }});
};
mpz_mul_r_r_4096_ov <- mpz_mul_r_r_ov bignum_bc 4096;
mul_mod_p_ov <- bignum_verify
"mul_mod_p"
[mpz_mul_4096_ov, mpz_mod_r_r_8192_4096_ov]
mul_mod_p_spec
(unint_z3 ["safe_product", "safe_mod"]);
mul_mod_p_r_r_ov <- bignum_verify
"mul_mod_p"
[mpz_mul_r_r_4096_ov, mpz_mod_r_r_8192_4096_ov]
mul_mod_p_r_r_spec
(unint_z3 ["safe_product", "safe_mod"]);
//bool log_generator_mod_p(mpz_t result, mpz_t a)
let log_generator_mod_p_spec = do {
p_global CONST;
generator_global CONST;
result <- mpz_t WRITE 4096 "result";
a <- mpz_t CONST 4096 "a";
execute [result.p, a.p];
};
mpz_add_ui_r_r_4096_ov <- mpz_add_ui_r_r_ov bignum_bc 4096;
mpz_cmp_4096_ov <- mpz_cmp_ov bignum_bc 4096;
log_generator_mod_p_ov <- bignum_verify
"log_generator_mod_p"
[print_base16_ov, mpz_init_4096_ov, mpz_set_ui_4096_ov, mpz_add_ui_r_r_4096_ov, mpz_mul_r_r_4096_ov, mpz_mod_r_r_8192_4096_ov /*mul_mod_p_r_r_ov*/, mpz_cmp_4096_ov]
log_generator_mod_p_spec
z3;