regorus 0.13.0

A fast, lightweight Rego (OPA policy language) interpreter
Documentation
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use vstd::prelude::*;

verus! {

use core::cmp::Ordering;
use crate::number::Number;
use num_bigint::BigInt;
use super::bigint_assumptions::*;
use super::number_specs::NumberView;
use vstd::arithmetic::div_mod::{
    lemma_div_of0,
    lemma_fundamental_div_mod,
    rust_div,
    rust_rem,
};
use vstd::arithmetic::mul::{
    lemma_mul_cancels_negatives,
    lemma_mul_increases,
    lemma_mul_strictly_increases,
    lemma_mul_unary_negation,
};
use vstd::float::*;
use vstd::std_specs::cmp::*;
use vstd::std_specs::convert::*;
use vstd::std_specs::ops::*;

/// Spec trait implementations

// For various `T`, we implement `From<T>` for `Number`. This means
// that Verus demands a proof that it implements `FromSpecImpl<T>`.
// The easiest (and most obviously correct) way to do this is to just
// define `obeys_from_spec()` as always returning `false`. We also
// need to define a `from_spec`, but it's meaningless since
// `obeys_from_spec()` always returns false. So we may as well leave
// it uninterpreted.

impl FromSpecImpl<BigInt> for Number {
    open spec fn obeys_from_spec() -> bool
    {
        false
    }

    uninterp spec fn from_spec(v: BigInt) -> Number;
}

impl FromSpecImpl<u64> for Number {
    open spec fn obeys_from_spec() -> bool
    {
        false
    }

    uninterp spec fn from_spec(v: u64) -> Number;
}

impl FromSpecImpl<usize> for Number {
    open spec fn obeys_from_spec() -> bool
    {
        false
    }

    uninterp spec fn from_spec(v: usize) -> Number;
}

impl FromSpecImpl<u128> for Number {
    open spec fn obeys_from_spec() -> bool
    {
        false
    }

    uninterp spec fn from_spec(v: u128) -> Number;
}

impl FromSpecImpl<i64> for Number {
    open spec fn obeys_from_spec() -> bool
    {
        false
    }

    uninterp spec fn from_spec(v: i64) -> Number;
}

impl FromSpecImpl<i128> for Number {
    open spec fn obeys_from_spec() -> bool
    {
        false
    }

    uninterp spec fn from_spec(v: i128) -> Number;
}

impl FromSpecImpl<f64> for Number {
    open spec fn obeys_from_spec() -> bool
    {
        false
    }

    uninterp spec fn from_spec(v: f64) -> Number;
}

// We implement `PartialEq` for `Number`. This means that Verus
// demands a proof that it implements `PartialEqSpecImpl`. The
// simplest, and most obviously correct, way to do this is to just say
// that `obeys_eq_spec()` always returns `false`. This makes all the
// postconditions trivial. We also need to define an `eq_spec`, but
// it's meaningless since `obeys_from_spec()` always returns false. So
// we may as well leave it uninterpreted.

impl PartialEqSpecImpl for Number {
    open spec fn obeys_eq_spec() -> bool
    {
        false
    }

    uninterp spec fn eq_spec(&self, other: &Self) -> bool;
}

// We implement `Ord` for `Number`. This means that Verus demands a
// proof that it implements `OrdSpecImpl`. The simplest, and most
// obviously correct, way to do this is to just say that
// `obeys_eq_spec()` always returns `false`. This makes all the
// postconditions trivial. We also need to define a `cmp_spec`, but
// it's meaningless since `obeys_from_spec()` always returns false. So
// we may as well leave it uninterpreted.

impl OrdSpecImpl for Number {
    open spec fn obeys_cmp_spec() -> bool
    {
        false
    }

    uninterp spec fn cmp_spec(&self, other: &Self) -> Ordering;
}

/// View implementation (internal to crate)

impl View for Number
{
    type V = NumberView;

    open(crate) spec fn view(&self) -> NumberView
    {
        match self {
            Number::UInt(n) => NumberView::Integer(n as int),
            Number::Int(n) => NumberView::Integer(n as int),
            Number::Float(f) => NumberView::Float(*f),
            Number::BigInt(b) => NumberView::Integer(b@),
        }
    }
}

/// Helpful lemmas

pub proof fn lemma_div_ensures_cases(lhs: NumberView, rhs: NumberView)
    ensures
        match (lhs, rhs) {
            (NumberView::Integer(integer_lhs), NumberView::Integer(divisor)) => {
                divisor != 0 && rust_rem(integer_lhs, divisor) == 0
                ==> lhs.div_ensures(
                    rhs,
                    NumberView::Integer(rust_div(integer_lhs, divisor)),
                )
            },
            _ => true,
        },
        forall|lhs_float: f64, rhs_float: f64|
            lhs is Integer && rhs is Integer && rhs->Integer_0 != 0 && rust_rem(
                lhs->Integer_0,
                rhs->Integer_0,
            ) != 0 && lhs.to_f64_lossy_ensures(lhs_float) && rhs.to_f64_lossy_ensures(rhs_float)
            ==> #[trigger] lhs.div_ensures(rhs, NumberView::Float(lhs_float / rhs_float)),
        forall|lhs_float: f64, rhs_float: f64|
            (lhs is Float || rhs is Float) && !rhs.is_zero() && lhs.to_f64_lossy_ensures(lhs_float)
                && rhs.to_f64_lossy_ensures(rhs_float)
            ==> #[trigger] lhs.div_ensures(rhs, NumberView::Float(lhs_float / rhs_float)),
{
    reveal(NumberView::is_zero);
    reveal(NumberView::div_ensures);
}

proof fn lemma_rust_div_rem_identity(lhs: int, rhs: int)
    requires
        rhs != 0,
    ensures
        lhs == rhs * rust_div(lhs, rhs) + rust_rem(lhs, rhs),
{
    reveal(rust_div);
    reveal(rust_rem);
    if lhs == 0 {
        lemma_div_of0(rhs);
        lemma_fundamental_div_mod(lhs, rhs);
    } else if lhs > 0 {
        lemma_fundamental_div_mod(lhs, rhs);
    } else {
        let positive_lhs = -lhs;
        let quotient = positive_lhs / rhs;
        let remainder = positive_lhs % rhs;
        lemma_fundamental_div_mod(positive_lhs, rhs);
        lemma_mul_unary_negation(rhs, quotient);
        assert(positive_lhs == rhs * quotient + remainder);
        assert(rhs * (-quotient) == -(rhs * quotient));
        assert(lhs == rhs * (-quotient) + (-remainder));
        assert(rust_div(lhs, rhs) == -quotient);
        assert(rust_rem(lhs, rhs) == -remainder);
    }
}

proof fn lemma_exact_rust_div_fits(
    dividend: int,
    divisor: int,
    minimum: int,
    maximum: int,
)
    requires
        minimum == -maximum - 1,
        minimum <= dividend <= maximum,
        minimum <= divisor <= maximum,
        divisor != 0,
        !(dividend == minimum && divisor == -1),
        rust_rem(dividend, divisor) == 0,
    ensures
        minimum <= rust_div(dividend, divisor) <= maximum,
{
    let quotient = rust_div(dividend, divisor);
    lemma_rust_div_rem_identity(dividend, divisor);
    assert(dividend == divisor * quotient);

    if quotient < minimum {
        assert(quotient <= minimum - 1);
        assert(-quotient > maximum);
        if divisor > 0 {
            lemma_mul_increases(divisor, -quotient);
            lemma_mul_unary_negation(divisor, quotient);
            assert(divisor * quotient <= quotient);
            assert(dividend < minimum);
        } else {
            assert(divisor <= -1);
            lemma_mul_increases(-divisor, -quotient);
            lemma_mul_cancels_negatives(divisor, quotient);
            assert(dividend > maximum);
        }
    } else if quotient > maximum {
        assert(quotient >= maximum + 1);
        assert(quotient >= -minimum);
        if divisor > 0 {
            lemma_mul_increases(divisor, quotient);
            assert(dividend > maximum);
        } else {
            assert(divisor <= -1);
            lemma_mul_increases(-divisor, quotient);
            lemma_mul_unary_negation(divisor, quotient);
            assert(dividend <= minimum);
            assert(dividend == minimum);
            if divisor < -1 {
                lemma_mul_strictly_increases(-divisor, quotient);
                assert(quotient < (-divisor) * quotient);
                assert((-divisor) * quotient == -dividend);
                assert(false);
            }
            assert(divisor == -1);
        }
    }
}

pub proof fn lemma_rust_div_fits_i64(lhs: i64, rhs: i64)
    requires
        rhs != 0,
        !(lhs == i64::MIN && rhs == -1),
        rust_rem(lhs as int, rhs as int) == 0,
    ensures
        i64::MIN as int <= rust_div(lhs as int, rhs as int) <= i64::MAX as int,
{
    lemma_exact_rust_div_fits(
        lhs as int,
        rhs as int,
        i64::MIN as int,
        i64::MAX as int,
    );
}

pub proof fn lemma_checked_div_matches_rust_i64(lhs: i64, rhs: i64, quotient: i64)
    requires
        rhs != 0,
        !(lhs == i64::MIN && rhs == -1),
        rust_rem(lhs as int, rhs as int) == 0,
        i64::checked_div(lhs, rhs) == Some(quotient),
    ensures
        quotient as int == rust_div(lhs as int, rhs as int),
{
    let mathematical_quotient = rust_div(lhs as int, rhs as int);
    lemma_rust_div_fits_i64(lhs, rhs);
    assert(i64::MIN as int <= mathematical_quotient <= i64::MAX as int);
    assert(quotient == mathematical_quotient as i64);
}

pub proof fn lemma_remainder_matches_rust_i64(lhs: i64, rhs: i64)
    requires
        rhs != 0,
        !(lhs == i64::MIN && rhs == -1),
    ensures
        <i64 as RemSpec>::obeys_rem_spec(),
        <i64 as RemSpec>::rem_req(lhs, rhs),
        <i64 as RemSpec>::rem_spec(lhs, rhs) as int == rust_rem(lhs as int, rhs as int),
{
    assert(<i64 as RemSpec>::obeys_rem_spec());
    assert(<i64 as RemSpec>::rem_req(lhs, rhs));
    reveal(rust_rem);
}

pub proof fn lemma_remainder_matches_rust_i128(lhs: i128, rhs: i128)
    requires
        rhs != 0,
        !(lhs == i128::MIN && rhs == -1),
    ensures
        <i128 as RemSpec>::obeys_rem_spec(),
        <i128 as RemSpec>::rem_req(lhs, rhs),
        <i128 as RemSpec>::rem_spec(lhs, rhs) as int == rust_rem(lhs as int, rhs as int),
{
    assert(<i128 as RemSpec>::obeys_rem_spec());
    assert(<i128 as RemSpec>::rem_req(lhs, rhs));
    reveal(rust_rem);
}

pub proof fn lemma_remainder_matches_rust_u64(lhs: u64, rhs: u64)
    requires
        rhs != 0,
    ensures
        <u64 as RemSpec>::obeys_rem_spec(),
        <u64 as RemSpec>::rem_req(lhs, rhs),
        rust_rem(lhs as int, rhs as int) == 0 ==>
            <u64 as RemSpec>::rem_spec(lhs, rhs) == 0,
        <u64 as RemSpec>::rem_spec(lhs, rhs) != 0 ==>
            rust_rem(lhs as int, rhs as int) != 0,
{
    assert(<u64 as RemSpec>::obeys_rem_spec());
    assert(<u64 as RemSpec>::rem_req(lhs, rhs));
    reveal(rust_rem);
    if rust_rem(lhs as int, rhs as int) == 0 {
        if lhs == 0 {
            lemma_div_of0(rhs as int);
        } else {
            assert(lhs > 0);
        }
        assert((lhs as int) % (rhs as int) == 0);
        assert(<u64 as RemSpec>::rem_spec(lhs, rhs)
            == ((lhs as int) % (rhs as int)) as u64);
    }
}

pub proof fn lemma_number_primitive_division_facts(lhs: &Number, rhs: &Number)
    ensures
        match (lhs, rhs) {
            (Number::Int(lhs), Number::Int(rhs)) =>
                *rhs != 0 && !(*lhs == i64::MIN && *rhs == -1) ==>
                    <i64 as RemSpec>::obeys_rem_spec()
                        && <i64 as RemSpec>::rem_req(*lhs, *rhs)
                        && <i64 as RemSpec>::rem_spec(*lhs, *rhs) as int
                            == rust_rem(*lhs as int, *rhs as int),
            (Number::UInt(lhs), Number::UInt(rhs)) =>
                *rhs != 0 ==>
                    <u64 as RemSpec>::obeys_rem_spec()
                        && <u64 as RemSpec>::rem_req(*lhs, *rhs)
                        && (rust_rem(*lhs as int, *rhs as int) == 0 ==>
                            <u64 as RemSpec>::rem_spec(*lhs, *rhs) == 0)
                        && (<u64 as RemSpec>::rem_spec(*lhs, *rhs) != 0 ==>
                            rust_rem(*lhs as int, *rhs as int) != 0),
            (Number::Int(lhs), Number::UInt(rhs)) =>
                *rhs != 0 ==>
                    *rhs as i128 > 0
                        && <i128 as RemSpec>::obeys_rem_spec()
                        && <i128 as RemSpec>::rem_req(*lhs as i128, *rhs as i128)
                        && <i128 as RemSpec>::rem_spec(*lhs as i128, *rhs as i128) as int
                            == rust_rem(*lhs as int, *rhs as int)
                        && (rust_rem(*lhs as int, *rhs as int) == 0 ==>
                            i128::MIN as int <= rust_div(*lhs as int, *rhs as int)
                                <= i128::MAX as int),
            (Number::UInt(lhs), Number::Int(rhs)) =>
                *rhs != 0 ==>
                    *lhs as i128 >= 0
                        && *rhs as i128 != 0
                        && rust_div(*lhs as int, *rhs as int)
                            == (*lhs as int) / (*rhs as int)
                        && <i128 as RemSpec>::obeys_rem_spec()
                        && <i128 as RemSpec>::rem_req(*lhs as i128, *rhs as i128)
                        && (rust_rem(*lhs as int, *rhs as int) == 0 ==>
                            <i128 as RemSpec>::rem_spec(*lhs as i128, *rhs as i128) == 0)
                        && (<i128 as RemSpec>::rem_spec(*lhs as i128, *rhs as i128) != 0 ==>
                            rust_rem(*lhs as int, *rhs as int) != 0)
                        && (rust_rem(*lhs as int, *rhs as int) == 0 ==>
                            i128::MIN as int <= rust_div(*lhs as int, *rhs as int)
                                <= i128::MAX as int),
            _ => true,
        },
{
    match (lhs, rhs) {
        (Number::Int(lhs), Number::Int(rhs)) => {
            if *rhs != 0 && !(*lhs == i64::MIN && *rhs == -1) {
                lemma_remainder_matches_rust_i64(*lhs, *rhs);
            }
        },
        (Number::UInt(lhs), Number::UInt(rhs)) => {
            if *rhs != 0 {
                lemma_remainder_matches_rust_u64(*lhs, *rhs);
            }
        },
        (Number::Int(lhs), Number::UInt(rhs)) => {
            if *rhs != 0 {
                lemma_remainder_matches_rust_i128(*lhs as i128, *rhs as i128);
                if rust_rem(*lhs as int, *rhs as int) == 0 {
                    lemma_rust_div_fits_i128(*lhs as i128, *rhs as i128);
                }
            }
        },
        (Number::UInt(lhs), Number::Int(rhs)) => {
            if *rhs != 0 {
                lemma_nonnegative_div_matches_rust(*lhs as i128, *rhs as i128);
                lemma_nonnegative_remainder_matches_rust(*lhs as i128, *rhs as i128);
                if rust_rem(*lhs as int, *rhs as int) == 0 {
                    lemma_rust_div_fits_i128(*lhs as i128, *rhs as i128);
                }
            }
        },
        _ => {},
    }
}

pub proof fn lemma_rust_div_fits_i128(lhs: i128, rhs: i128)
    requires
        rhs != 0,
        !(lhs == i128::MIN && rhs == -1),
        rust_rem(lhs as int, rhs as int) == 0,
    ensures
        i128::MIN as int <= rust_div(lhs as int, rhs as int) <= i128::MAX as int,
{
    lemma_exact_rust_div_fits(
        lhs as int,
        rhs as int,
        i128::MIN as int,
        i128::MAX as int,
    );
}

pub proof fn lemma_nonnegative_div_matches_rust(lhs: i128, rhs: i128)
    requires
        lhs >= 0,
        rhs != 0,
    ensures
        rust_div(lhs as int, rhs as int) == (lhs as int) / (rhs as int),
{
    reveal(rust_div);
    if lhs == 0 {
        lemma_div_of0(rhs as int);
    } else {
        assert(lhs > 0);
    }
}

pub proof fn lemma_nonnegative_remainder_matches_rust(lhs: i128, rhs: i128)
    requires
        lhs >= 0,
        rhs != 0,
    ensures
        <i128 as RemSpec>::obeys_rem_spec(),
        <i128 as RemSpec>::rem_req(lhs, rhs),
        rust_rem(lhs as int, rhs as int) == 0 ==>
            <i128 as RemSpec>::rem_spec(lhs, rhs) == 0,
        <i128 as RemSpec>::rem_spec(lhs, rhs) != 0 ==>
            rust_rem(lhs as int, rhs as int) != 0,
{
    assert(<i128 as RemSpec>::obeys_rem_spec());
    assert(<i128 as RemSpec>::rem_req(lhs, rhs));
    reveal(rust_rem);
    if rust_rem(lhs as int, rhs as int) == 0 {
        if lhs == 0 {
            lemma_div_of0(rhs as int);
        } else {
            assert(lhs > 0);
        }
        assert((lhs as int) % (rhs as int) == 0);
        assert(<i128 as RemSpec>::rem_spec(lhs, rhs)
            == ((lhs as int) % (rhs as int)) as i128);
    }
}

}