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: don't panic when shifting too much #7429

Merged
merged 3 commits into from
Feb 20, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/noirc_evaluator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ fxhash.workspace = true
iter-extended.workspace = true
thiserror.workspace = true
num-bigint = "0.4"
num-traits.workspace = true
im.workspace = true
serde.workspace = true
serde_json.workspace = true
Expand Down
22 changes: 17 additions & 5 deletions compiler/noirc_evaluator/src/ssa/ir/instruction/binary.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use acvm::{acir::AcirField, FieldElement};
use num_traits::ToPrimitive as _;
use serde::{Deserialize, Serialize};

use super::{
Expand Down Expand Up @@ -39,9 +40,9 @@
Or,
/// Bitwise xor (^)
Xor,
/// Bitshift left (<<)

Check warning on line 43 in compiler/noirc_evaluator/src/ssa/ir/instruction/binary.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Bitshift)
Shl,
/// Bitshift right (>>)

Check warning on line 45 in compiler/noirc_evaluator/src/ssa/ir/instruction/binary.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Bitshift)
Shr,
}

Expand Down Expand Up @@ -107,7 +108,7 @@
}

let operator = if lhs_type == NumericType::NativeField {
// Unchecked operations between fields or bools don't make sense, so we convert those to non-unchecked

Check warning on line 111 in compiler/noirc_evaluator/src/ssa/ir/instruction/binary.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (bools)
// to reduce noise and confusion in the generated SSA.
match operator {
BinaryOp::Add { unchecked: true } => BinaryOp::Add { unchecked: false },
Expand All @@ -116,7 +117,7 @@
_ => operator,
}
} else if lhs_type == NumericType::bool() {
// Unchecked mul between bools doesn't make sense, so we convert that to non-unchecked

Check warning on line 120 in compiler/noirc_evaluator/src/ssa/ir/instruction/binary.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (bools)
if let BinaryOp::Mul { unchecked: true } = operator {
BinaryOp::Mul { unchecked: false }
} else {
Expand Down Expand Up @@ -289,7 +290,7 @@
}
if lhs_type == NumericType::bool() {
// Boolean AND is equivalent to multiplication, which is a cheaper operation.
// (mul unchecked because these are bools so it doesn't matter really)

Check warning on line 293 in compiler/noirc_evaluator/src/ssa/ir/instruction/binary.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (bools)
let instruction =
Instruction::binary(BinaryOp::Mul { unchecked: true }, lhs, rhs);
return SimplifyResult::SimplifiedToInstruction(instruction);
Expand Down Expand Up @@ -574,8 +575,8 @@
BinaryOp::Xor => |x, y| Some(x ^ y),
BinaryOp::Eq => |x, y| Some((x == y) as u128),
BinaryOp::Lt => |x, y| Some((x < y) as u128),
BinaryOp::Shl => |x, y| Some(x << y),
BinaryOp::Shr => |x, y| Some(x >> y),
BinaryOp::Shl => |x, y| y.to_u32().and_then(|y| x.checked_shl(y)),
BinaryOp::Shr => |x, y| y.to_u32().and_then(|y| x.checked_shr(y)),
}
}

Expand All @@ -591,8 +592,8 @@
BinaryOp::Xor => |x, y| Some(x ^ y),
BinaryOp::Eq => |x, y| Some((x == y) as i128),
BinaryOp::Lt => |x, y| Some((x < y) as i128),
BinaryOp::Shl => |x, y| Some(x << y),
BinaryOp::Shr => |x, y| Some(x >> y),
BinaryOp::Shl => |x, y| y.to_u32().and_then(|y| x.checked_shl(y)),
BinaryOp::Shr => |x, y| y.to_u32().and_then(|y| x.checked_shr(y)),
}
}

Expand Down Expand Up @@ -621,7 +622,7 @@

use super::{
convert_signed_integer_to_field_element, truncate_field,
try_convert_field_element_to_signed_integer,
try_convert_field_element_to_signed_integer, BinaryOp,
};
use acvm::{AcirField, FieldElement};
use num_bigint::BigUint;
Expand All @@ -645,10 +646,21 @@

let integer_modulus = BigUint::from(2_u128).pow(bit_size);
let truncated_as_bigint = BigUint::from(input)
.modpow(&BigUint::one(), &integer_modulus);

Check warning on line 649 in compiler/noirc_evaluator/src/ssa/ir/instruction/binary.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (modpow)
let truncated_as_bigint = FieldElement::from_be_bytes_reduce(&truncated_as_bigint.to_bytes_be());
prop_assert_eq!(truncated_as_field, truncated_as_bigint);
}
}

#[test]
fn get_u128_function_shift_works_with_values_larger_than_127() {
assert!(BinaryOp::Shr.get_u128_function()(1, 128).is_none());
assert!(BinaryOp::Shl.get_u128_function()(1, 128).is_none());
}

#[test]
fn get_i128_function_shift_works_with_values_larger_than_127() {
assert!(BinaryOp::Shr.get_i128_function()(1, 128).is_none());
assert!(BinaryOp::Shl.get_i128_function()(1, 128).is_none());
}
}
Loading