Skip to content

Commit

Permalink
fix: encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
arayikhalatyan committed Dec 13, 2024
1 parent d1c81db commit 61e6fd1
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
18 changes: 13 additions & 5 deletions crates/circuits/primitives/src/encoder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<u32>>,
reserve_invalid: bool,
}

impl Encoder {
Expand All @@ -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 {
Expand Down Expand Up @@ -52,6 +53,7 @@ impl Encoder {
flag_cnt: cnt,
max_flag_degree: max_degree,
pts,
reserve_invalid,
}
}

Expand Down Expand Up @@ -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::<AB>(&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::<AB>(&self.pts[flag_idx + self.reserve_invalid as usize], vars)
}

pub fn get_flag_pt(&self, flag_idx: usize) -> Vec<u32> {
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<AB: InteractionBuilder>(&self, vars: &[AB::Var]) -> AB::Expr {
Expand Down
2 changes: 1 addition & 1 deletion crates/circuits/sha256-air/src/sha256/air.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/vm/src/system/public_values/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion extensions/sha256/circuit/src/sha256_chip/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl<F: PrimeField32> Sha256VmChip<F> {
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,
Expand Down

0 comments on commit 61e6fd1

Please sign in to comment.