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

feat(gadgets): impl bn::from_assigned_value_to_limbs #409

Open
wants to merge 1 commit into
base: ecc-fix
Choose a base branch
from
Open
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
32 changes: 21 additions & 11 deletions src/gadgets/nonnative/bn/big_uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,21 @@ pub struct BigUint<F> {
}

impl<F: PrimeField> BigUint<F> {
pub fn zero(limb_width: NonZeroUsize) -> Self {
pub fn zero(limb_width: NonZeroUsize, limbs_count: NonZeroUsize) -> Self {
Self {
limbs: vec![F::ZERO],
limbs: vec![F::ZERO; limbs_count.get()],
width: limb_width,
}
}

pub fn one(limb_width: NonZeroUsize) -> Self {
pub fn one(limb_width: NonZeroUsize, limbs_count: NonZeroUsize) -> Self {
let limbs = iter::once(F::ONE)
.chain(iter::repeat(F::ZERO))
.take(limbs_count.get())
.collect();

Self {
limbs: vec![F::ONE],
limbs,
width: limb_width,
}
}
Expand Down Expand Up @@ -92,6 +97,8 @@ impl<F: PrimeField> BigUint<F> {
.take(limbs_count.get())
.collect::<Vec<_>>();

assert_eq!(limbs.len(), limbs_count.get());

let tail = limbs_input.collect::<Box<[_]>>();
if tail.len() == 0 {
Ok(Self {
Expand Down Expand Up @@ -193,16 +200,19 @@ impl<F: PrimeField> BigUint<F> {

let mut nat = input.clone();
let limb_mask = get_big_int_with_n_ones(limb_width.get());

Self::from_limbs(
iter::repeat_with(|| {
nat.is_zero().not().then(|| {
let r = &nat & &limb_mask;
nat >>= limb_width.get() as u32;
nat_to_f(&r).expect("TODO: Check safety")
})
nat.is_zero()
.not()
.then(|| {
let r = &nat & &limb_mask;
nat >>= limb_width.get() as u32;
nat_to_f(&r).expect("TODO: Check safety")
})
.unwrap_or(F::ZERO)
})
.take(max_limbs_count)
.map_while(|mut o| o.take()),
.take(max_limbs_count),
limb_width,
limbs_count,
)
Expand Down
27 changes: 26 additions & 1 deletion src/gadgets/nonnative/bn/big_uint_mul_mod_chip/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1036,7 +1036,7 @@ impl<F: PrimeField> BigUintMulModChip<F> {
/// - `s_k` partial sum of [0..k] limbs
///
/// The `s_n` it's equal with original input and check by copy constraint
pub fn from_assigned_cell_to_limbs(
pub fn from_assigned_value_to_limbs(
&self,
ctx: &mut RegionCtx<'_, F>,
input: &AssignedCell<F, F>,
Expand Down Expand Up @@ -1154,6 +1154,31 @@ impl<F: PrimeField> BigUintMulModChip<F> {
Ok(limbs)
}

pub fn from_assigned_limbs_to_value(
&self,
ctx: &mut RegionCtx<'_, F>,
input_limbs: &[AssignedCell<F, F>],
) -> Result<AssignedCell<F, F>, Error> {
let value = match big_uint::BigUint::from_assigned_cells(
input_limbs,
self.limb_width,
self.limbs_count,
)? {
Some(value) => Value::known(value.into_f().unwrap()),
_ => Value::unknown(),
};

let assigned_value = ctx.assign_advice(|| "", self.config().state[0], value)?;
ctx.next();

self.from_assigned_value_to_limbs(ctx, &assigned_value)?
.iter()
.zip_eq(input_limbs)
.try_for_each(|(lhs, rhs)| ctx.constrain_equal(lhs.cell(), rhs.cell()))?;

Ok(assigned_value)
}

/// Performs the multiplication of `lhs` and `rhs` taking into account the `modulus`.
///
/// This method serves as an implementation of modular multiplication in the context
Expand Down
6 changes: 4 additions & 2 deletions src/gadgets/nonnative/bn/big_uint_mul_mod_chip/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,9 @@ mod decompose_tests {

region.next();

let limbs = chip.from_assigned_cell_to_limbs(&mut region, &val).unwrap();
let limbs = chip
.from_assigned_value_to_limbs(&mut region, &val)
.unwrap();

for val in [
F::from_u128(0u128),
Expand All @@ -890,7 +892,7 @@ mod decompose_tests {

region.next();

chip.from_assigned_cell_to_limbs(&mut region, &new_val)
chip.from_assigned_value_to_limbs(&mut region, &new_val)
.unwrap();
}

Expand Down
4 changes: 2 additions & 2 deletions src/ivc/cyclefold/sfc/input/assigned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -667,15 +667,15 @@ impl<const A: usize, F: PrimeField> Input<A, F> {

let expected_l0 = mg.conditional_select(region, &zero, &poly_L_values[0], &is_zero_term)?;
let expected_l0 = bn_chip
.from_assigned_cell_to_limbs(region, &expected_l0)
.from_assigned_value_to_limbs(region, &expected_l0)
.map_err(|err| {
error!("while make from L0 biguint form: {err:?}");
Halo2PlonkError::Synthesis
})?;

let expected_l1 = mg.conditional_select(region, &zero, &poly_L_values[1], &is_zero_term)?;
let expected_l1 = bn_chip
.from_assigned_cell_to_limbs(region, &expected_l1)
.from_assigned_value_to_limbs(region, &expected_l1)
.map_err(|err| {
error!("while make from L1 biguint form: {err:?}");
Halo2PlonkError::Synthesis
Expand Down
21 changes: 17 additions & 4 deletions src/ivc/cyclefold/sfc/input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,10 +638,15 @@ impl<const ARITY: usize, F: PrimeField> Input<ARITY, F> {
.ins
.instances
.iter()
.map(|v| vec![BigUint::zero(DEFAULT_LIMB_WIDTH); v.len()])
.map(|v| {
vec![
BigUint::zero(DEFAULT_LIMB_WIDTH, DEFAULT_LIMBS_COUNT_LIMIT);
v.len()
]
})
.collect(),
challenges: vec![
BigUint::zero(DEFAULT_LIMB_WIDTH);
BigUint::zero(DEFAULT_LIMB_WIDTH, DEFAULT_LIMBS_COUNT_LIMIT);
self.paired_trace.input_accumulator.ins.challenges.len()
],
},
Expand All @@ -662,10 +667,18 @@ impl<const ARITY: usize, F: PrimeField> Input<ARITY, F> {
.instance
.instances
.iter()
.map(|v| vec![BigUint::zero(DEFAULT_LIMB_WIDTH); v.len()])
.map(|v| {
vec![
BigUint::zero(DEFAULT_LIMB_WIDTH, DEFAULT_LIMBS_COUNT_LIMIT);
v.len()
]
})
.collect(),
challenges: vec![
BigUint::zero(DEFAULT_LIMB_WIDTH);
BigUint::zero(
DEFAULT_LIMB_WIDTH,
DEFAULT_LIMBS_COUNT_LIMIT
);
incoming.instance.challenges.len()
],
},
Expand Down
2 changes: 1 addition & 1 deletion src/ivc/cyclefold/sfc/sangria_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ where
.absorb_iter(input.iter_wrap_values())
.squeeze(region)?;
let r_bits = mg.le_num_to_bits(region, r.clone(), MAX_BITS)?;
let r_as_bn = bn_chip.from_assigned_cell_to_limbs(region, &r).unwrap();
let r_as_bn = bn_chip.from_assigned_value_to_limbs(region, &r).unwrap();

let m_bn = module_as_bn::<CMain::ScalarExt, CMain::Base>().unwrap();

Expand Down
8 changes: 6 additions & 2 deletions src/ivc/protogalaxy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,12 @@ pub mod verify_chip {
impl<F: PrimeField> BigUintPoint<F> {
pub fn identity() -> Self {
Self {
x: big_uint::BigUint::zero(DEFAULT_LIMB_WIDTH).limbs().to_vec(),
y: big_uint::BigUint::zero(DEFAULT_LIMB_WIDTH).limbs().to_vec(),
x: big_uint::BigUint::zero(DEFAULT_LIMB_WIDTH, DEFAULT_LIMBS_COUNT_LIMIT)
.limbs()
.to_vec(),
y: big_uint::BigUint::zero(DEFAULT_LIMB_WIDTH, DEFAULT_LIMBS_COUNT_LIMIT)
.limbs()
.to_vec(),
}
}
}
Expand Down
14 changes: 8 additions & 6 deletions src/ivc/sangria/fold_relaxed_plonk_instance_chip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,9 @@ where

let r_value = gate.le_bits_to_num(region, &r)?;
let r = BigUintView {
as_bn_limbs: self.bn_chip.from_assigned_cell_to_limbs(region, &r_value)?,
as_bn_limbs: self
.bn_chip
.from_assigned_value_to_limbs(region, &r_value)?,
as_bits: r.clone(),
};

Expand Down Expand Up @@ -736,7 +738,7 @@ where

let assigned_bn = self
.bn_chip
.from_assigned_cell_to_limbs(region, &assigned_cell)?;
.from_assigned_value_to_limbs(region, &assigned_cell)?;

Result::<_, Error>::Ok(assigned_bn)
}};
Expand Down Expand Up @@ -835,7 +837,7 @@ where
region.next();
let assigned_bn = self
.bn_chip
.from_assigned_cell_to_limbs(region, &assigned_cell)?;
.from_assigned_value_to_limbs(region, &assigned_cell)?;

Result::<_, Error>::Ok((assigned_cell, assigned_bn))
}};
Expand Down Expand Up @@ -1479,7 +1481,7 @@ mod tests {
let r_vv = BigUintView {
as_bn_limbs: chip
.bn_chip
.from_assigned_cell_to_limbs(&mut ctx, &assigned_r)
.from_assigned_value_to_limbs(&mut ctx, &assigned_r)
.unwrap(),
as_bits: r,
};
Expand Down Expand Up @@ -1605,7 +1607,7 @@ mod tests {
ctx.next();

let r_as_bn = bn_chip
.from_assigned_cell_to_limbs(&mut ctx, &assigned_r)
.from_assigned_value_to_limbs(&mut ctx, &assigned_r)
.unwrap();

Ok(FoldRelaxedPlonkInstanceChip::<
Expand Down Expand Up @@ -1749,7 +1751,7 @@ mod tests {
ctx.next();

let r_as_bn = bn_chip
.from_assigned_cell_to_limbs(&mut ctx, &assigned_r)
.from_assigned_value_to_limbs(&mut ctx, &assigned_r)
.unwrap();

let m_bn = scalar_module_as_bn::<C1>(LIMB_WIDTH, LIMBS_COUNT).unwrap();
Expand Down
Loading