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

fix: used signed division for signed modulo #6635

Merged
merged 5 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions compiler/noirc_evaluator/src/acir/acir_variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1084,11 +1084,22 @@ impl<F: AcirField, B: BlackBoxFunctionSolver<F>> AcirContext<F, B> {
&mut self,
lhs: AcirVar,
rhs: AcirVar,
typ: AcirType,
bit_size: u32,
predicate: AcirVar,
) -> Result<AcirVar, RuntimeError> {
let (_, remainder) = self.euclidean_division_var(lhs, rhs, bit_size, predicate)?;
Ok(remainder)
let numeric_type = match typ {
AcirType::NumericType(numeric_type) => numeric_type,
AcirType::Array(_, _) => {
todo!("cannot modulo arrays. This should have been caught by the frontend")
guipublic marked this conversation as resolved.
Show resolved Hide resolved
}
};

let (_, remainder_var) = match numeric_type {
NumericType::Signed { bit_size } => self.signed_division_var(lhs, rhs, bit_size)?,
_ => self.euclidean_division_var(lhs, rhs, bit_size, predicate)?,
};
Ok(remainder_var)
}

/// Constrains the `AcirVar` variable to be of type `NumericType`.
Expand Down
1 change: 1 addition & 0 deletions compiler/noirc_evaluator/src/acir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1968,6 +1968,7 @@ impl<'a> Context<'a> {
BinaryOp::Mod => self.acir_context.modulo_var(
lhs,
rhs,
binary_type.clone(),
bit_count,
self.current_side_effects_enabled_var,
),
Expand Down
2 changes: 2 additions & 0 deletions test_programs/execution_success/regression/Prover.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
x = [0x3f, 0x1c, 0xb8, 0x99, 0xab]
z = 3
u = "169"
v = "-13"
6 changes: 5 additions & 1 deletion test_programs/execution_success/regression/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ fn bitshift_variable(idx: u8) -> u64 {
bits
}

fn main(x: [u8; 5], z: Field) {
fn main(x: [u8; 5], z: Field, u: i16, v: i16) {
//Issue 1144
let (nib, len) = compact_decode(x, z);
assert(len == 5);
Expand Down Expand Up @@ -130,4 +130,8 @@ fn main(x: [u8; 5], z: Field) {
assert(result_0 == 1);
let result_4 = bitshift_variable(4);
assert(result_4 == 16);

// Issue 6609
assert(u % -13 == 0);
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved
assert(u % v == 0);
guipublic marked this conversation as resolved.
Show resolved Hide resolved
}
Loading