-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathinteger_ring.rs
292 lines (270 loc) · 9.25 KB
/
integer_ring.rs
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#![feature(rustc_private)]
#[macro_use]
mod common;
use common::*;
// All these tests are marked as #[ignore] unless Verus is compiled with `--features singular`
test_verify_one_file! {
#[test]
#[cfg_attr(not(feature = "singular"), ignore)]
simple_1 verus_code! {
proof fn test(x: int, y: int, z:int, m:int) by(integer_ring)
requires (x-y) % m == 0
ensures (x*z- y*z) % m == 0
{}
} => Ok(())
}
test_verify_one_file! {
#[test]
#[cfg_attr(not(feature = "singular"), ignore)]
simple_2 verus_code! {
proof fn test(x: int, y: int, z:int) by(integer_ring)
ensures
(x+y+z)*(x+y+z) == x*x + y*y + z*z + 2*(x*y + y*z + z*x)
{}
} => Ok(())
}
test_verify_one_file! {
#[test]
#[cfg_attr(not(feature = "singular"), ignore)]
mont_mul_1 verus_code! {
proof fn test(a:int, s:int, R:int, M:int, RR:int, R_INV:int) by(integer_ring)
requires
(a * R - RR * s) % M == 0,
(R_INV * R - 1) % M == 0,
(RR - R * R % M) == 0,
ensures
(a - s*R) % M == 0
{}
} => Ok(())
}
test_verify_one_file! {
#[test]
#[cfg_attr(not(feature = "singular"), ignore)]
mont_mul_2 verus_code! {
proof fn test(p2_full: int, BASE: int, ui: int, m0: int, m0d: int, p1_lh: int, p1_full: int) by(integer_ring)
requires
p2_full == ui * m0 + p1_lh,
(p1_full - p1_lh) % BASE == 0,
(m0d * m0 - (BASE-1)) % BASE == 0,
(ui - p1_full * m0d) % BASE == 0,
ensures
p2_full % BASE == 0
{}
} => Ok(())
}
test_verify_one_file! {
#[test]
#[cfg_attr(not(feature = "singular"), ignore)]
wide_mul verus_code! {
proof fn test (
B: int,
p0: int, p1: int, p2: int, p3: int,
p4: int, p5: int, p6: int, p7: int,
p8: int, p9: int, p10: int, p11: int,
p12: int, p13: int, p14: int, p15: int,
x: int, x_0: int, x_1: int, x_2: int, x_3: int,
y: int, y_0: int, y_1: int,y_2: int, y_3: int) by(integer_ring)
requires
x == x_0 + x_1 * B + x_2 * B * B + x_3 * B * B * B,
y == y_0 + y_1 * B + y_2 * B * B + y_3 * B * B * B,
p0 == x_0 * y_0,
p1 == x_1 * y_0,
p2 == x_0 * y_1,
p3 == x_2 * y_0,
p4 == x_1 * y_1,
p5 == x_0 * y_2,
p6 == x_3 * y_0,
p7 == x_2 * y_1,
p8 == x_1 * y_2,
p9 == x_0 * y_3,
p10 == x_3 * y_1,
p11 == x_2 * y_2,
p12 == x_1 * y_3,
p13 == x_3 * y_2,
p14 == x_2 * y_3,
p15 == x_3 * y_3,
ensures
x * y == p0 + (p1 + p2) * B + (p3 + p4 + p5) * B * B + (p6 + p7 + p8 + p9) * B * B * B + (p10 + p11 + p12) * B * B * B * B + (p13 + p14) * B * B * B * B * B + p15 * B * B * B * B * B * B
{}
} => Ok(())
}
test_verify_one_file! {
#[test]
#[cfg_attr(not(feature = "singular"), ignore)]
uninterpreted_function verus_code! {
spec fn square(a:int) -> int{
a*a
}
spec fn quad(a:int) -> int{
a*a*a*a
}
proof fn test(x:int, y:int, m:int) by(integer_ring)
requires
(square(x) - square(y)) % m == 0,
square(x) == x*x,
square(y) == y*y,
quad(x) == x*x*x*x,
quad(y) == y*y*y*y,
ensures (quad(x) - quad(y)) % m == 0
{}
} => Ok(())
}
// Failing test cases
test_verify_one_file! {
#[test]
#[cfg_attr(not(feature = "singular"), ignore)]
simple_fail_1 verus_code! {
proof fn test(x: int, y: int, z:int, m:int) by(integer_ring)
requires
(x-y) % m == 0
ensures (x*z + y*z) % m == 0 //FAILS
{}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test]
#[cfg_attr(not(feature = "singular"), ignore)]
simple_fail_2 verus_code! {
proof fn test(x: int, y: int, z:int) by(integer_ring)
ensures
(x+y+z)*(x+y+z) == x*x + y*y + z + 2*(x*y + y*z + z*x) // FAILS
{}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test]
#[cfg_attr(not(feature = "singular"), ignore)]
wide_mul_fail verus_code! {
proof fn test(
B: int,
p0: int, p1: int, p2: int, p3: int,
p4: int, p5: int, p6: int, p7: int,
p8: int, p9: int, p10: int, p11: int,
p12: int, p13: int, p14: int, p15: int,
x: int, x_0: int, x_1: int, x_2: int, x_3: int,
y: int, y_0: int, y_1: int,y_2: int, y_3: int) by(integer_ring)
requires
x == x_0 + x_1 * B + x_2 * B * B + x_3 * B * B * B,
y == y_0 + y_1 * B + y_2 * B * B + y_3 * B * B * B,
p0 == x_0 * y_0,
p1 == x_1 * y_0,
p2 == x_0 * y_1,
p3 == x_2 * y_0,
p4 == x_1 * y_1,
p5 == x_0 * y_2,
p6 == x_3 * y_0,
p7 == x_2 * y_2, // originally p7 == x_2 * y_1
p8 == x_1 * y_2,
p9 == x_0 * y_3,
p10 == x_3 * y_1,
p11 == x_2 * y_2,
p12 == x_1 * y_3,
p13 == x_3 * y_2,
p14 == x_2 * y_3,
p15 == x_3 * y_3,
ensures x * y == p0 + (p1 + p2) * B + (p3 + p4 + p5) * B * B + (p6 + p7 + p8 + p9) * B * B * B + (p10 + p11 + p12) * B * B * B * B + (p13 + p14) * B * B * B * B * B + p15 * B * B * B * B * B * B // FAILS
{}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test]
#[cfg_attr(not(feature = "singular"), ignore)]
type_fail verus_code! {
proof fn test(x: u32, y: u32, z:u32, m:int) by(integer_ring) // FAILS (not supported)
requires
(x-y) % m == 0
ensures
(x*z - y*z) % m == 0
{}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test]
#[cfg_attr(not(feature = "singular"), ignore)]
reserved_keyword verus_code! {
proof fn test(singular_tmp_1 : int, y: int, z:int, m:int) by(integer_ring)
requires (singular_tmp_1 - y) % m == 0
ensures (singular_tmp_1 * z- y*z) % m == 0
{}
} => Ok(())
}
test_verify_one_file! {
#[test]
#[cfg_attr(not(feature = "singular"), ignore)]
div_by_zero_fail verus_code! {
proof fn may_div_zero(x : int) by(integer_ring)
ensures x % x == 0
{}
proof fn div_by_zero_fails() {
let a = 0int;
may_div_zero(a);
assert(a % a == 0); // FAILS , `x` shouldn't be zero
}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test]
#[cfg_attr(not(feature = "singular"), ignore)]
mul_mod_noop verus_code! {
pub proof fn test(x: int, y: int, m: int) by(integer_ring)
ensures
((x % m) * y) % m == (x * y) % m,
(x * (y % m)) % m == (x * y) % m,
((x % m) * (y % m)) % m == (x * y) % m
{}
} => Ok(())
}
test_verify_one_file! {
#[test]
#[cfg_attr(not(feature = "singular"), ignore)]
mul_mod_noop_fail_1 verus_code! {
pub proof fn test(x: int, y: int, m: int) by(integer_ring)
ensures
((x % m) * y) % m == (x * y) % m,
((x % m) * (y % m)) % m == (x) % m, // FAILS
(x * (y % m)) % m == (x * y) % m
{}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test]
#[cfg_attr(not(feature = "singular"), ignore)]
mul_mod_noop_fail_2 verus_code! {
pub proof fn test(x: int, y: int, m: int) by(integer_ring)
ensures
((x % m) * y) % m == (x * y) % m,
((x % m) * (y % m)) % m == (x) % m, // FAILS
(x * (y % m)) % m == (x) % m // also FAILS (but should not report this, since we stop at the first failure)
{}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test]
#[cfg_attr(not(feature = "singular"), ignore)]
neq_not_supported verus_code! {
proof fn test(x: int, y: int, z:int, m:int) by(integer_ring)
requires (x-y) % m != 0 //FAILS (not supported)
ensures (x*z + y*z) % m == 0
{}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test]
#[cfg_attr(not(feature = "singular"), ignore)]
gt_not_supported verus_code! {
proof fn test(x: int, y: int, z:int, m:int) by(integer_ring)
requires (x-y) % m > 0 //FAILS (not supported)
ensures (x*z + y*z) % m == 0
{}
} => Err(err) => assert_one_fails(err)
}
test_verify_one_file! {
#[test]
#[cfg_attr(not(feature = "singular"), ignore)]
lt_not_supported verus_code! {
proof fn test(x: int, y: int, z:int, m:int) by(integer_ring)
requires (x-y) % m == 0
ensures (x*z + y*z) % m < 0 //FAILS (not supported)
{}
} => Err(err) => assert_one_fails(err)
}