Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Euclidean div/mod from num-bigint #766

Merged
merged 1 commit into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions source/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions source/rust_verify/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ serde = "1"
serde_json = { version = ">=1.0.95", features = ["preserve_order"] }
bincode = "1.0.1"
sise = "0.6.0"
num-bigint = "0.4.3"
num-bigint = "0.4.4"
num-format = "0.4.0"
getopts = { git = "https://github.com/utaal/getopts.git", branch = "parse-partial" }
regex = "1"
Expand All @@ -30,4 +30,4 @@ rust_verify_test_macros = { path = "../rust_verify_test_macros" }
singular = ["vir/singular", "air/singular"]

[package.metadata.rust-analyzer]
rustc_private = true
rustc_private = true
4 changes: 2 additions & 2 deletions source/vir/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ edition = "2018"
air = { path = "../air" }
vir-macros = { path = "../vir_macros" }
sise = "0.6.0"
num-bigint = { version = "0.4.3", features = ["serde"] }
num-traits= "0.2.15"
num-bigint = { version = "0.4.4", features = ["serde"] }
num-traits= "0.2.16"
im = "15.1.0"
sha2 = "0.10.2"
serde = { version = "1", features = ["derive", "rc"] }
Expand Down
39 changes: 4 additions & 35 deletions source/vir/src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ use air::ast::{Binder, BinderX, Binders, Span};
use air::messages::{warning, Diagnostics, Message};
use air::scope_map::ScopeMap;
use im::Vector;
use num_bigint::{BigInt, Sign};
use num_bigint::BigInt;
use num_traits::identities::Zero;
use num_traits::{FromPrimitive, One, Signed, ToPrimitive};
use num_traits::{Euclid, FromPrimitive, One, ToPrimitive};
use std::collections::HashMap;
use std::fs::File;
use std::hash::{Hash, Hasher};
Expand Down Expand Up @@ -511,37 +511,6 @@ fn hash_exp<H: Hasher>(state: &mut H, exp: &Exp) {
* Utility functions *
**********************/

// Based on Dafny's C# implementation:
// https://github.com/dafny-lang/dafny/blob/08744a797296897f4efd486083579e484f57b9dc/Source/DafnyRuntime/DafnyRuntime.cs#L1383
/// Proper Euclidean division on BigInt
fn euclidean_div(i1: &BigInt, i2: &BigInt) -> BigInt {
// Note: Can be replaced with an inbuilt method on BigInts once
// https://github.com/rust-num/num-bigint/pull/245 is merged.
use Sign::*;
match (i1.sign(), i2.sign()) {
(Plus | NoSign, Plus | NoSign) => i1 / i2,
(Plus | NoSign, Minus) => -(i1 / (-i2)),
(Minus, Plus | NoSign) => -(((-i1) - BigInt::one()) / i2) - BigInt::one(),
(Minus, Minus) => (((-i1) - BigInt::one()) / (-i2)) + 1,
}
}

// Based on Dafny's C# implementation:
// https://github.com/dafny-lang/dafny/blob/08744a797296897f4efd486083579e484f57b9dc/Source/DafnyRuntime/DafnyRuntime.cs#L1436
/// Proper Euclidean mod on BigInt
fn euclidean_mod(i1: &BigInt, i2: &BigInt) -> BigInt {
// Note: Can be replaced with an inbuilt method on BigInts once
// https://github.com/rust-num/num-bigint/pull/245 is merged.
use Sign::*;
match i1.sign() {
Plus | NoSign => i1 % i2.abs(),
Minus => {
let c = (-i1) % i2.abs();
if c.is_zero() { BigInt::zero() } else { i2.abs() - c }
}
}
}

/// Truncate a u128 to a fixed width BigInt
fn u128_to_fixed_width(u: u128, width: u32) -> BigInt {
match width {
Expand Down Expand Up @@ -1221,14 +1190,14 @@ fn eval_expr_internal(ctx: &Ctx, state: &mut State, exp: &Exp) -> Result<Exp, Vi
if i2.is_zero() {
ok_e2(e2) // Treat as symbolic instead of erroring
} else {
int_new(euclidean_div(i1, i2))
int_new(i1.div_euclid(i2))
}
}
EuclideanMod => {
if i2.is_zero() {
ok_e2(e2) // Treat as symbolic instead of erroring
} else {
int_new(euclidean_mod(i1, i2))
int_new(i1.rem_euclid(i2))
}
}
}
Expand Down