1#![allow(unused_imports)]
2use super::super::arithmetic::div_mod::{rust_div, rust_rem};
3use super::super::prelude::*;
4use super::super::wrapping::*;
5
6use core::cmp::Ordering;
7
8verus! {
9
10pub open spec fn next_multiple_of(x: int, y: int) -> int
13 recommends
14 y > 0,
15{
16 if x % y == 0 {
17 x
18 } else {
19 x + (y - x % y)
20 }
21}
22
23} macro_rules! num_specs {
25 ($uN: ty, $iN: ty, $mod_u_tmp:ident, $mod_i_tmp:ident, $mod_u:ident, $mod_i:ident, $range:expr) => {
26 verus! {
27
28 mod $mod_u_tmp {
33 use super::*;
34
35 pub assume_specification[<$uN as Clone>::clone](x: &$uN) -> (res: $uN)
36 ensures res == x;
37
38 impl super::super::cmp::PartialEqSpecImpl for $uN {
39 open spec fn obeys_eq_spec() -> bool {
40 true
41 }
42
43 open spec fn eq_spec(&self, other: &$uN) -> bool {
44 *self == *other
45 }
46 }
47
48 impl super::super::cmp::PartialOrdSpecImpl for $uN {
49 open spec fn obeys_partial_cmp_spec() -> bool {
50 true
51 }
52
53 open spec fn partial_cmp_spec(&self, other: &$uN) -> Option<Ordering> {
54 if *self < *other {
55 Some(Ordering::Less)
56 } else if *self > *other {
57 Some(Ordering::Greater)
58 } else {
59 Some(Ordering::Equal)
60 }
61 }
62 }
63
64 impl super::super::cmp::OrdSpecImpl for $uN {
65 open spec fn obeys_cmp_spec() -> bool {
66 true
67 }
68
69 open spec fn cmp_spec(&self, other: &$uN) -> Ordering {
70 if *self < *other {
71 Ordering::Less
72 } else if *self > *other {
73 Ordering::Greater
74 } else {
75 Ordering::Equal
76 }
77 }
78 }
79
80 pub assume_specification[<$uN as PartialEq<$uN>>::eq](x: &$uN, y: &$uN) -> bool;
81
82 pub assume_specification[<$uN as PartialEq<$uN>>::ne](x: &$uN, y: &$uN) -> bool;
83
84 pub assume_specification[<$uN as Ord>::cmp](x: &$uN, y: &$uN) -> Ordering;
85
86 pub assume_specification[<$uN as PartialOrd<$uN>>::partial_cmp](x: &$uN, y: &$uN) -> Option<Ordering>;
87
88 pub assume_specification[<$uN as PartialOrd<$uN>>::lt](x: &$uN, y: &$uN) -> bool;
89
90 pub assume_specification[<$uN as PartialOrd<$uN>>::le](x: &$uN, y: &$uN) -> bool;
91
92 pub assume_specification[<$uN as PartialOrd<$uN>>::gt](x: &$uN, y: &$uN) -> bool;
93
94 pub assume_specification[<$uN as PartialOrd<$uN>>::ge](x: &$uN, y: &$uN) -> bool;
95
96 #[verifier::allow_in_spec]
97 #[cfg(not(verus_verify_core))]
98 pub assume_specification[<$uN>::wrapping_add](x: $uN, y: $uN) -> $uN
99 returns $mod_u::wrapping_add(x, y)
100 opens_invariants none
101 no_unwind;
102
103 #[verifier::allow_in_spec]
104 #[cfg(not(verus_verify_core))]
105 pub assume_specification[<$uN>::wrapping_add_signed](x: $uN, y: $iN) -> $uN
106 returns $mod_u::wrapping_add_signed(x, y)
107 opens_invariants none
108 no_unwind;
109
110 #[verifier::allow_in_spec]
111 #[cfg(not(verus_verify_core))]
112 pub assume_specification[<$uN>::wrapping_sub](x: $uN, y: $uN) -> $uN
113 returns $mod_u::wrapping_sub(x, y)
114 opens_invariants none
115 no_unwind;
116
117 #[verifier::allow_in_spec]
118 #[cfg(not(verus_verify_core))]
119 pub assume_specification[<$uN>::wrapping_mul](x: $uN, y: $uN) -> $uN
120 returns $mod_u::wrapping_mul(x, y)
121 opens_invariants none
122 no_unwind;
123
124 #[verifier::allow_in_spec]
125 #[cfg(not(verus_verify_core))]
126 pub assume_specification[<$uN>::wrapping_shl](x: $uN, rhs: u32) -> $uN
127 returns $mod_u::wrapping_shl(x, rhs)
128 opens_invariants none
129 no_unwind;
130
131 #[verifier::allow_in_spec]
132 #[cfg(not(verus_verify_core))]
133 pub assume_specification[<$uN>::wrapping_shr](x: $uN, rhs: u32) -> $uN
134 returns $mod_u::wrapping_shr(x, rhs)
135 opens_invariants none
136 no_unwind;
137
138 #[verifier::allow_in_spec]
139 #[cfg(not(verus_verify_core))]
140 pub assume_specification[<$uN>::rotate_left](x: $uN, n: u32) -> $uN
141 returns $mod_u::rotate_left(x, n)
142 opens_invariants none
143 no_unwind;
144
145 #[verifier::allow_in_spec]
146 #[cfg(not(verus_verify_core))]
147 pub assume_specification[<$uN>::rotate_right](x: $uN, n: u32) -> $uN
148 returns $mod_u::rotate_right(x, n)
149 opens_invariants none
150 no_unwind;
151
152 #[verifier::allow_in_spec]
153 #[cfg(not(verus_verify_core))]
154 pub assume_specification[<$uN>::checked_add](x: $uN, y: $uN) -> Option<$uN>
155 returns (
156 if x + y > <$uN>::MAX {
157 None
158 } else {
159 Some((x + y) as $uN)
160 }
161 )
162 no_unwind;
163
164 #[verifier::allow_in_spec]
165 #[cfg(not(verus_verify_core))]
166 pub assume_specification[<$uN>::checked_add_signed](x: $uN, y: $iN) -> Option<$uN>
167 returns (
168 if x + y > <$uN>::MAX || x + y < 0 {
169 None
170 } else {
171 Some((x + y) as $uN)
172 }
173 )
174 no_unwind;
175
176 #[verifier::allow_in_spec]
177 #[cfg(not(verus_verify_core))]
178 pub assume_specification[<$uN>::checked_sub](x: $uN, y: $uN) -> Option<$uN>
179 returns (
180 if x - y < 0 {
181 None
182 } else {
183 Some((x - y) as $uN)
184 }
185 )
186 no_unwind;
187
188 #[verifier::allow_in_spec]
189 #[cfg(not(verus_verify_core))]
190 pub assume_specification[<$uN>::checked_mul](x: $uN, y: $uN) -> Option<$uN>
191 returns (
192 if x * y > <$uN>::MAX {
193 None
194 } else {
195 Some((x * y) as $uN)
196 }
197 )
198 no_unwind;
199
200 #[verifier::allow_in_spec]
201 #[cfg(not(verus_verify_core))]
202 pub assume_specification[<$uN>::checked_next_multiple_of](x: $uN, rhs: $uN) -> Option<$uN>
203 returns (
204 if rhs == 0 {
205 None
206 } else if next_multiple_of(x as int, rhs as int) > <$uN>::MAX {
207 None
208 } else {
209 Some(next_multiple_of(x as int, rhs as int) as $uN)
210 }
211 )
212 no_unwind;
213
214 pub open spec fn checked_div(x: $uN, y: $uN) -> Option<$uN> {
215 if y == 0 {
216 None
217 } else {
218 Some(x / y)
219 }
220 }
221
222 #[verifier::when_used_as_spec(checked_div)]
223 #[cfg(not(verus_verify_core))]
224 pub assume_specification[<$uN>::checked_div](lhs: $uN, rhs: $uN) -> (result: Option<$uN>)
225 ensures
226 result == checked_div(lhs, rhs),
227 no_unwind;
228
229 #[verifier::when_used_as_spec(checked_div)]
230 #[cfg(not(verus_verify_core))]
231 pub assume_specification[<$uN>::checked_div_euclid](lhs: $uN, rhs: $uN) -> (result: Option<$uN>)
232 ensures
233 result == checked_div(lhs, rhs),
235 no_unwind;
236
237 #[verifier::allow_in_spec]
238 #[cfg(not(verus_verify_core))]
239 pub assume_specification[<$uN>::checked_rem](lhs: $uN, rhs: $uN) -> Option<$uN>
240 returns (
241 if rhs == 0 {
242 None
243 }
244 else {
245 Some((lhs % rhs) as $uN)
246 }
247 )
248 no_unwind;
249
250 #[verifier::allow_in_spec]
251 #[cfg(not(verus_verify_core))]
252 pub assume_specification[<$uN>::checked_rem_euclid](lhs: $uN, rhs: $uN) -> Option<$uN>
253 returns (
254 if rhs == 0 {
255 None
256 }
257 else {
258 Some((lhs % rhs) as $uN)
259 }
260 )
261 no_unwind;
262
263 #[verifier::allow_in_spec]
264 #[cfg(not(verus_verify_core))]
265 pub assume_specification[<$uN>::saturating_add](x: $uN, y: $uN) -> $uN
266 returns (
267 if x + y > <$uN>::MAX {
268 <$uN>::MAX
269 } else {
270 (x + y) as $uN
271 }
272 )
273 no_unwind;
274
275 #[verifier::allow_in_spec]
276 #[cfg(not(verus_verify_core))]
277 pub assume_specification[<$uN>::saturating_sub](x: $uN, y: $uN) -> $uN
278 returns (
279 if x - y < <$uN>::MIN {
280 <$uN>::MIN
281 } else {
282 (x - y) as $uN
283 }
284 )
285 no_unwind;
286
287 #[verifier::allow_in_spec]
288 #[cfg(not(verus_verify_core))]
289 pub assume_specification[<$uN>::saturating_mul](x: $uN, y: $uN) -> $uN
290 returns (
291 if x * y > <$uN>::MAX {
292 <$uN>::MAX
293 } else {
294 (x * y) as $uN
295 }
296 )
297 no_unwind;
298
299 #[verifier::allow_in_spec]
300 #[cfg(not(verus_verify_core))]
301 pub assume_specification[<$uN>::is_multiple_of](x: $uN, y: $uN) -> bool
302 returns (
303 if y == 0 { x == 0 } else { x % y == 0 }
304 )
305 no_unwind;
306 }
307
308 mod $mod_i_tmp {
311 use super::*;
312
313 pub assume_specification[<$iN as Clone>::clone](x: &$iN) -> (res: $iN)
314 ensures res == x;
315
316 impl super::super::cmp::PartialEqSpecImpl for $iN {
317 open spec fn obeys_eq_spec() -> bool {
318 true
319 }
320
321 open spec fn eq_spec(&self, other: &$iN) -> bool {
322 *self == *other
323 }
324 }
325
326 impl super::super::cmp::PartialOrdSpecImpl for $iN {
327 open spec fn obeys_partial_cmp_spec() -> bool {
328 true
329 }
330
331 open spec fn partial_cmp_spec(&self, other: &$iN) -> Option<Ordering> {
332 if *self < *other {
333 Some(Ordering::Less)
334 } else if *self > *other {
335 Some(Ordering::Greater)
336 } else {
337 Some(Ordering::Equal)
338 }
339 }
340 }
341
342 impl super::super::cmp::OrdSpecImpl for $iN {
343 open spec fn obeys_cmp_spec() -> bool {
344 true
345 }
346
347 open spec fn cmp_spec(&self, other: &$iN) -> Ordering {
348 if *self < *other {
349 Ordering::Less
350 } else if *self > *other {
351 Ordering::Greater
352 } else {
353 Ordering::Equal
354 }
355 }
356 }
357
358 pub assume_specification[<$iN as PartialEq<$iN>>::eq](x: &$iN, y: &$iN) -> bool;
359
360 pub assume_specification[<$iN as PartialEq<$iN>>::ne](x: &$iN, y: &$iN) -> bool;
361
362 pub assume_specification[<$iN as Ord>::cmp](x: &$iN, y: &$iN) -> Ordering;
363
364 pub assume_specification[<$iN as PartialOrd<$iN>>::partial_cmp](x: &$iN, y: &$iN) -> Option<Ordering>;
365
366 pub assume_specification[<$iN as PartialOrd<$iN>>::lt](x: &$iN, y: &$iN) -> bool;
367
368 pub assume_specification[<$iN as PartialOrd<$iN>>::le](x: &$iN, y: &$iN) -> bool;
369
370 pub assume_specification[<$iN as PartialOrd<$iN>>::gt](x: &$iN, y: &$iN) -> bool;
371
372 pub assume_specification[<$iN as PartialOrd<$iN>>::ge](x: &$iN, y: &$iN) -> bool;
373
374 #[verifier::allow_in_spec]
375 #[cfg(not(verus_verify_core))]
376 pub assume_specification[<$iN>::wrapping_add](x: $iN, y: $iN) -> $iN
377 returns $mod_i::wrapping_add(x, y)
378 opens_invariants none
379 no_unwind;
380
381 #[verifier::allow_in_spec]
382 #[cfg(not(verus_verify_core))]
383 pub assume_specification[<$iN>::wrapping_add_unsigned](x: $iN, y: $uN) -> $iN
384 returns $mod_i::wrapping_add_unsigned(x, y)
385 opens_invariants none
386 no_unwind;
387
388 #[verifier::allow_in_spec]
389 #[cfg(not(verus_verify_core))]
390 pub assume_specification[<$iN>::wrapping_sub](x: $iN, y: $iN) -> (res: $iN)
391 returns $mod_i::wrapping_sub(x, y)
392 opens_invariants none
393 no_unwind;
394
395 #[verifier::allow_in_spec]
396 #[cfg(not(verus_verify_core))]
397 pub assume_specification[<$iN>::wrapping_mul](x: $iN, y: $iN) -> $iN
398 returns $mod_i::wrapping_mul(x, y)
399 opens_invariants none
400 no_unwind;
401
402 #[verifier::allow_in_spec]
403 #[cfg(not(verus_verify_core))]
404 pub assume_specification[<$iN>::wrapping_shl](x: $iN, rhs: u32) -> $iN
405 returns $mod_i::wrapping_shl(x, rhs)
406 opens_invariants none
407 no_unwind;
408
409 #[verifier::allow_in_spec]
410 #[cfg(not(verus_verify_core))]
411 pub assume_specification[<$iN>::wrapping_shr](x: $iN, rhs: u32) -> $iN
412 returns $mod_i::wrapping_shr(x, rhs)
413 opens_invariants none
414 no_unwind;
415
416 #[verifier::allow_in_spec]
417 #[cfg(not(verus_verify_core))]
418 pub assume_specification[<$iN>::rotate_left](x: $iN, n: u32) -> $iN
419 returns $mod_i::rotate_left(x, n)
420 opens_invariants none
421 no_unwind;
422
423 #[verifier::allow_in_spec]
424 #[cfg(not(verus_verify_core))]
425 pub assume_specification[<$iN>::rotate_right](x: $iN, n: u32) -> $iN
426 returns $mod_i::rotate_right(x, n)
427 opens_invariants none
428 no_unwind;
429
430 #[verifier::allow_in_spec]
431 #[cfg(not(verus_verify_core))]
432 pub assume_specification[<$iN>::checked_add](x: $iN, y: $iN) -> Option<$iN>
433 returns (
434 if x + y > <$iN>::MAX || x + y < <$iN>::MIN {
435 None
436 } else {
437 Some((x + y) as $iN)
438 }
439 )
440 no_unwind;
441
442 #[verifier::allow_in_spec]
443 #[cfg(not(verus_verify_core))]
444 pub assume_specification[<$iN>::checked_add_unsigned](x: $iN, y: $uN) -> Option<$iN>
445 returns (
446 if x + y > <$iN>::MAX {
447 None
448 } else {
449 Some((x + y) as $iN)
450 }
451 )
452 no_unwind;
453
454 #[verifier::allow_in_spec]
455 #[cfg(not(verus_verify_core))]
456 pub assume_specification[<$iN>::checked_sub](x: $iN, y: $iN) -> Option<$iN>
457 returns (
458 if x - y > <$iN>::MAX || x - y < <$iN>::MIN {
459 None
460 } else {
461 Some((x - y) as $iN)
462 }
463 )
464 no_unwind;
465
466 #[verifier::allow_in_spec]
467 #[cfg(not(verus_verify_core))]
468 pub assume_specification[<$iN>::checked_sub_unsigned](x: $iN, y: $uN) -> Option<$iN>
469 returns (
470 if x - y < <$iN>::MIN {
471 None
472 } else {
473 Some((x - y) as $iN)
474 }
475 )
476 no_unwind;
477
478 #[verifier::allow_in_spec]
479 #[cfg(not(verus_verify_core))]
480 pub assume_specification[<$iN>::checked_mul](x: $iN, y: $iN) -> Option<$iN>
481 returns (
482 if x * y > <$iN>::MAX || x * y < <$iN>::MIN {
483 None
484 } else {
485 Some((x * y) as $iN)
486 }
487 )
488 no_unwind;
489
490 #[verifier::allow_in_spec]
491 #[cfg(not(verus_verify_core))]
492 pub assume_specification[<$iN>::checked_div](lhs: $iN, rhs: $iN) -> Option<$iN>
493 returns (
494 if rhs == 0 || (lhs == <$iN>::MIN && rhs == -1) {
495 None
496 } else {
497 Some(rust_div(lhs as int, rhs as int) as $iN)
498 }
499 )
500 no_unwind;
501
502 #[verifier::allow_in_spec]
503 #[cfg(not(verus_verify_core))]
504 pub assume_specification[<$iN>::checked_div_euclid](lhs: $iN, rhs: $iN) -> Option<$iN>
505 returns (
506 if rhs == 0 || (lhs == <$iN>::MIN && rhs == -1) {
507 None
508 } else {
509 Some((lhs / rhs) as $iN)
510 }
511 )
512 no_unwind;
513
514 #[verifier::allow_in_spec]
515 #[cfg(not(verus_verify_core))]
516 pub assume_specification[<$iN>::checked_rem](lhs: $iN, rhs: $iN) -> Option<$iN>
517 returns (
518 if rhs == 0 || (lhs == <$iN>::MIN && rhs == -1) {
519 None
520 } else {
521 Some(rust_rem(lhs as int, rhs as int) as $iN)
522 }
523 )
524 no_unwind;
525
526 #[verifier::allow_in_spec]
527 #[cfg(not(verus_verify_core))]
528 pub assume_specification[<$iN>::checked_rem_euclid](lhs: $iN, rhs: $iN) -> Option<$iN>
529 returns (
530 if rhs == 0 || (lhs == <$iN>::MIN && rhs == -1) {
531 None
532 } else {
533 Some((lhs % rhs) as $iN)
534 }
535 )
536 no_unwind;
537 }
538
539 }
540 };
541}
542
543num_specs!(u8, i8, u8_specs_tmp, i8_specs_tmp, u8_specs, i8_specs, 0x100);
544num_specs!(u16, i16, u16_specs_tmp, i16_specs_tmp, u16_specs, i16_specs, 0x1_0000);
545num_specs!(u32, i32, u32_specs_tmp, i32_specs_tmp, u32_specs, i32_specs, 0x1_0000_0000);
546num_specs!(u64, i64, u64_specs_tmp, i64_specs_tmp, u64_specs, i64_specs, 0x1_0000_0000_0000_0000);
547num_specs!(
548 u128,
549 i128,
550 u128_specs_tmp,
551 i128_specs_tmp,
552 u128_specs,
553 i128_specs,
554 0x1_0000_0000_0000_0000_0000_0000_0000_0000
555);
556num_specs!(
557 usize,
558 isize,
559 usize_specs_tmp,
560 isize_specs_tmp,
561 usize_specs,
562 isize_specs,
563 (usize::MAX - usize::MIN + 1)
564);