diff --git a/crates/circuits/primitives/src/encoder/mod.rs b/crates/circuits/primitives/src/encoder/mod.rs index 2c44b27eda..b15b0e9384 100644 --- a/crates/circuits/primitives/src/encoder/mod.rs +++ b/crates/circuits/primitives/src/encoder/mod.rs @@ -13,6 +13,7 @@ pub struct Encoder { /// The maximal degree of the equalities in the AIR, however, **is one higher:** that is, `max_flag_degree + 1`. max_flag_degree: u32, pts: Vec>, + reserve_invalid: bool, } impl Encoder { @@ -21,7 +22,7 @@ impl Encoder { /// The zero point is reserved for the dummy row. /// `max_degree` is the upper bound for the flag expressions, but the `eval` function /// of the encoder itself will use some constraints of degree `max_degree + 1`. - pub fn new(cnt: usize, max_degree: u32) -> Self { + pub fn new(cnt: usize, max_degree: u32, reserve_invalid: bool) -> Self { let binomial = |x: u32| { let mut res = 1; for i in 1..=max_degree { @@ -52,6 +53,7 @@ impl Encoder { flag_cnt: cnt, max_flag_degree: max_degree, pts, + reserve_invalid, } } @@ -86,13 +88,19 @@ impl Encoder { flag_idx: usize, vars: &[AB::Var], ) -> AB::Expr { - assert!(flag_idx <= self.flag_cnt, "flag index out of range"); - self.expression_for_point::(&self.pts[flag_idx], vars) + assert!( + flag_idx + self.reserve_invalid as usize <= self.flag_cnt, + "flag index out of range" + ); + self.expression_for_point::(&self.pts[flag_idx + self.reserve_invalid as usize], vars) } pub fn get_flag_pt(&self, flag_idx: usize) -> Vec { - assert!(flag_idx <= self.flag_cnt, "flag index out of range"); - self.pts[flag_idx].clone() + assert!( + flag_idx + self.reserve_invalid as usize <= self.flag_cnt, + "flag index out of range" + ); + self.pts[flag_idx + self.reserve_invalid as usize].clone() } pub fn is_valid(&self, vars: &[AB::Var]) -> AB::Expr { diff --git a/crates/circuits/sha256-air/src/sha256/air.rs b/crates/circuits/sha256-air/src/sha256/air.rs index 451f8ad03e..fd0590a907 100644 --- a/crates/circuits/sha256-air/src/sha256/air.rs +++ b/crates/circuits/sha256-air/src/sha256/air.rs @@ -31,7 +31,7 @@ impl Sha256Air { pub fn new(bitwise_lookup_bus: BitwiseOperationLookupBus, self_bus_idx: usize) -> Self { Self { bitwise_lookup_bus, - row_idx_encoder: Encoder::new(17, 2), + row_idx_encoder: Encoder::new(17, 2, false), bus_idx: self_bus_idx, } } diff --git a/crates/vm/src/system/public_values/core.rs b/crates/vm/src/system/public_values/core.rs index aa2e7866bb..3b6b98a821 100644 --- a/crates/vm/src/system/public_values/core.rs +++ b/crates/vm/src/system/public_values/core.rs @@ -34,7 +34,7 @@ impl PublicValuesCoreAir { Self { num_custom_pvs, offset, - encoder: Encoder::new(num_custom_pvs, max_degree), + encoder: Encoder::new(num_custom_pvs, max_degree, true), } } } diff --git a/extensions/sha256/circuit/src/sha256_chip/mod.rs b/extensions/sha256/circuit/src/sha256_chip/mod.rs index 1256099cf3..200abd02b4 100644 --- a/extensions/sha256/circuit/src/sha256_chip/mod.rs +++ b/extensions/sha256/circuit/src/sha256_chip/mod.rs @@ -87,7 +87,7 @@ impl Sha256VmChip { ptr_max_bits, offset, Sha256Air::new(bitwise_lookup_chip.bus(), SHA256_CHIP_BUS_IDX), - Encoder::new(PaddingFlags::COUNT, 2), + Encoder::new(PaddingFlags::COUNT, 2, false), ), memory_controller, bitwise_lookup_chip,