Skip to content

Commit c58f24f

Browse files
guipublicSakapoi
authored andcommitted
fix: finer bit size in bound constrain (noir-lang#2869)
1 parent b3c12a9 commit c58f24f

4 files changed

Lines changed: 15 additions & 3 deletions

File tree

compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/generated_acir.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -596,18 +596,19 @@ impl GeneratedAcir {
596596
// we now have lhs+offset <= rhs <=> lhs_offset <= rhs_offset
597597

598598
let bit_size = bit_size_u128(rhs_offset);
599-
// r = 2^bit_size - rhs_offset
599+
// r = 2^bit_size - rhs_offset -1, is of bit size 'bit_size' by construtction
600600
let r = (1_u128 << bit_size) - rhs_offset - 1;
601+
// however, since it is a constant, we can compute it's actual bit size
602+
let r_bit_size = bit_size_u128(r);
601603
// witness = lhs_offset + r
602-
assert!(bits + bit_size < FieldElement::max_num_bits()); //we need to ensure lhs_offset + r does not overflow
604+
assert!(bits + r_bit_size < FieldElement::max_num_bits()); //we need to ensure lhs_offset + r does not overflow
603605
let mut aor = lhs_offset;
604606
aor.q_c += FieldElement::from(r);
605607
let witness = self.get_or_create_witness(&aor);
606608
// lhs_offset<=rhs_offset <=> lhs_offset + r < rhs_offset + r = 2^bit_size <=> witness < 2^bit_size
607609
self.range_constraint(witness, bit_size)?;
608610
return Ok(());
609611
}
610-
611612
// General case: lhs_offset<=rhs <=> rhs-lhs_offset>=0 <=> rhs-lhs_offset is a 'bits' bit integer
612613
let sub_expression = rhs - &lhs_offset; //rhs-lhs_offset
613614
let w = self.create_witness_for_expression(&sub_expression);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "regression_2854"
3+
type = "bin"
4+
authors = [""]
5+
compiler_version = "0.1"
6+
7+
[dependencies]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
x = "3"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main(x: Field) -> pub i127 {
2+
x as i127
3+
}

0 commit comments

Comments
 (0)