Skip to content

Commit

Permalink
fix bug in range chip
Browse files Browse the repository at this point in the history
  • Loading branch information
kitounliu committed Oct 27, 2023
1 parent d7f5a22 commit b717aa7
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions maingate/src/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,11 @@ impl<F: PrimeField> RangeInstructions<F> for RangeChip<F> {
.map(|unassigned| decompose(unassigned, number_of_limbs, limb_bit_len))
.transpose_vec(number_of_limbs);

let bases = self.bases(limb_bit_len);
assert!(decomposed.len() <= bases.len());
let terms: Vec<Term<F>> = decomposed
.into_iter()
.zip(self.bases(limb_bit_len))
.zip(bases)
.map(|(limb, base)| Term::Unassigned(limb, *base))
.collect();

Expand Down Expand Up @@ -213,7 +215,11 @@ impl<F: PrimeField> RangeChip<F> {
if bit_len == 0 {
None
} else {
let bases = (0..F::NUM_BITS as usize / bit_len)
let mut base_len = F::NUM_BITS as usize / bit_len;
if F::NUM_BITS as usize % bit_len != 0 {
base_len += 1;
}
let bases = (0..base_len)
.map(|i| F::from(2).pow(&[(bit_len * i) as u64, 0, 0, 0]))
.collect();
Some((bit_len, bases))
Expand Down Expand Up @@ -383,10 +389,10 @@ impl<F: PrimeField> RangeChip<F> {

#[cfg(test)]
mod tests {

use halo2wrong::halo2::arithmetic::Field;
use halo2wrong::halo2::circuit::Value;
use halo2wrong::RegionCtx;
use rand_core::OsRng;

use super::{RangeChip, RangeConfig, RangeInstructions};
use crate::curves::{ff::PrimeField, pasta::Fp};
Expand Down Expand Up @@ -445,7 +451,7 @@ mod tests {
}

fn overflow_bit_lens() -> Vec<usize> {
vec![3]
vec![3, 7]
}
}

Expand Down Expand Up @@ -517,7 +523,7 @@ mod tests {
const OVERFLOW_BIT_LEN: usize = 3;
let k: u32 = (LIMB_BIT_LEN + 1) as u32;

let inputs = (2..20)
let mut inputs: Vec<_> = (2..20)
.map(|number_of_limbs| {
let bit_len = LIMB_BIT_LEN * number_of_limbs + OVERFLOW_BIT_LEN;
let value = Fp::from(2).pow(&[bit_len as u64, 0, 0, 0]) - Fp::one();
Expand All @@ -529,6 +535,13 @@ mod tests {
})
.collect();

let mut rng = OsRng;
inputs.push(Input {
bit_len: Fp::NUM_BITS as usize,
limb_bit_len: LIMB_BIT_LEN,
value: Value::known(Fp::random(&mut rng)),
});

let circuit = TestCircuit::<Fp> { inputs };
let public_inputs = vec![vec![]];
let prover = match MockProver::run(k, &circuit, public_inputs) {
Expand Down

0 comments on commit b717aa7

Please sign in to comment.