Skip to content

Commit

Permalink
fix(nifs): protogalaxy initial acc
Browse files Browse the repository at this point in the history
**Motivation**
ProtoGalaxy accumulator was created immediately by `!is_sat`, which prevents folding in #373

**Overview**
- The protogalaxy accumulator logic has been changed
- AssignedBigUint within ivc::protogalaxy is now an array. This generated a certain number of try_into().unwrap(), however, these will be cleaned up soon
- Changed the order of absorption in ro, for consistency with cyclefold
  • Loading branch information
cyphersnake committed Jan 8, 2025
1 parent 4ad26ad commit 6070ee5
Show file tree
Hide file tree
Showing 10 changed files with 280 additions and 320 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,152 +69,16 @@ where
CSup::Scalar: PrimeFieldBits + FromUniformBytes<64>,
{
pub fn new(
primary_sc: &SC,
ck1: CommitmentKey<CMain>,
ck2: CommitmentKey<CSup>,
k_table_size: u32,
_primary_sc: &SC,
_ck1: CommitmentKey<CMain>,
_ck2: CommitmentKey<CSup>,
_k_table_size: u32,
) -> Self
where
CMain::ScalarExt: Serialize,
CSup::ScalarExt: Serialize,
{
// Trace in C1::Base or C2::Scalar
let (support_S, support_initial_trace): (
PlonkStructure<CMain::Base>,
FoldablePlonkTrace<CSup>,
) = {
// Since I want to scalar_multiply points for main::sfc, I take `CMain` as the main curve here
// CMain::Base or CSupport::Scalar (native for suppport_circuit)
let support_circuit_instances: Vec<Vec<CMain::Base>> = support_circuit::InstanceInput {
p0: CMain::identity(),
l0: CMain::Base::ZERO,
p1: CMain::identity(),
l1: CMain::Base::ZERO,
}
.into_instance();

let support_cr = CircuitRunner::<CMain::Base, _>::new(
SupportCircuit::<CMain>::MIN_K_TABLE_SIZE,
SupportCircuit::<CMain>::default(),
support_circuit_instances.clone(),
);
let S = support_cr.try_collect_plonk_structure().unwrap();

// The trace is generated for `CSup`, since all result types use `C::ScalarExt` in our
// case it will be `CSup::ScalarExt` or `CMain::Base`
(
S,
VanillaFS::<CSup>::generate_plonk_trace(
&ck2,
&support_circuit_instances,
&support_cr.try_collect_witness().unwrap(),
&nifs::sangria::ProverParam {
S: support_cr.try_collect_plonk_structure().unwrap(),
pp_digest: (CSup::Base::ZERO, CSup::Base::ZERO),
},
&mut ro(),
)
.unwrap(),
)
};

let (primary_S, primary_initial_trace) = {
let mut mock_sfc = StepFoldingCircuit::<A1, CMain, CSup, SC> {
sc: primary_sc,
input: sfc::Input::<A1, CMain::ScalarExt>::new_initial::<CMain, CSup>(
&PlonkStructure {
k: k_table_size as usize,
..Default::default()
},
&support_S,
&support_initial_trace.u,
),
_p: PhantomData,
};

let mock_instances = mock_sfc.initial_instances();

// Correct `num_io`
mock_sfc.input = sfc::Input::<A1, CMain::ScalarExt>::new_initial::<CMain, CSup>(
&PlonkStructure {
k: k_table_size as usize,
num_io: mock_instances.iter().map(|col| col.len()).collect(),
..Default::default()
},
&support_S,
&support_initial_trace.u,
);

let mock_S = CircuitRunner::new(k_table_size, mock_sfc, mock_instances)
.try_collect_plonk_structure()
.unwrap();

let sfc = StepFoldingCircuit::<A1, CMain, CSup, SC> {
sc: primary_sc,
input: sfc::Input::<A1, CMain::ScalarExt>::new_initial::<CMain, CSup>(
&mock_S,
&support_S,
&support_initial_trace.u,
),
_p: PhantomData,
};

let primary_instances = sfc.initial_instances();
let primary_cr = CircuitRunner::new(k_table_size, sfc, primary_instances.clone());

(
primary_cr.try_collect_plonk_structure().unwrap(),
ProtoGalaxy::<CMain, 1>::generate_plonk_trace(
&ck1,
&primary_instances,
&primary_cr.try_collect_witness().unwrap(),
&nifs::protogalaxy::ProverParam {
S: primary_cr.try_collect_plonk_structure().unwrap(),
pp_digest: CMain::identity(),
},
&mut ro(),
)
.unwrap(),
)
};

let digest_bytes = {
use serde::Serialize;

#[derive(Serialize)]
struct Meaningful<'link, CMainScalar: PrimeField, CSupScalar: PrimeField>
where
CMainScalar: Serialize,
CSupScalar: Serialize,
{
primary_S: &'link PlonkStructure<CMainScalar>,
primary_k_table_size: &'link u32,
support_S: &'link PlonkStructure<CSupScalar>,
}

digest::DefaultHasher::digest_to_bits(&Meaningful {
primary_S: &primary_S,
primary_k_table_size: &k_table_size,
support_S: &support_S,
})
.unwrap()
};

Self {
primary_ck: ck1,
support_ck: ck2,
primary_k_table_size: k_table_size,

primary_initial_trace,
support_initial_trace,

primary_S,
support_S,

digest_bytes,

_p: PhantomData,
}
todo!("temporarily removed for the purposes of a simple merge into main")
}

pub fn cmain_pp_digest(&self) -> CMain {
Expand Down Expand Up @@ -242,7 +106,7 @@ where
pub fn protogalaxy_prover_params(&self) -> nifs::protogalaxy::ProverParam<CMain> {
nifs::protogalaxy::ProverParam {
S: self.primary_S.clone(),
pp_digest: self.cmain_pp_digest(),
pp_digest: self.cmain_pp_digest_coordinates(),
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/ivc/cyclefold/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub const R_P: usize = 10;
pub const DEFAULT_LIMB_WIDTH: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked(64) };

/// Safety: because 10 != 0
pub const DEFAULT_LIMBS_COUNT_LIMIT: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked(20) };
pub const DEFAULT_LIMBS_COUNT: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked(20) };

pub fn ro_const<F: PrimeFieldBits + FromUniformBytes<64>>() -> Spec<F, T, RATE> {
Spec::<F, T, RATE>::new(R_F, R_P)
Expand Down
26 changes: 20 additions & 6 deletions src/ivc/cyclefold/sfc/input/assigned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
halo2_proofs::plonk::Error as Halo2PlonkError,
ivc::{
self,
cyclefold::{ro_chip, DEFAULT_LIMBS_COUNT_LIMIT, DEFAULT_LIMB_WIDTH},
cyclefold::{ro_chip, DEFAULT_LIMBS_COUNT, DEFAULT_LIMB_WIDTH},
},
main_gate::{self, AdviceCyclicAssignor, AssignedValue, MainGate, RegionCtx, WrapValue},
poseidon::ROCircuitTrait,
Expand Down Expand Up @@ -656,7 +656,7 @@ impl<const A: usize, F: PrimeField> Input<A, F> {
let bn_chip = BigUintMulModChip::new(
main_gate_config.into_smaller_size().unwrap(),
DEFAULT_LIMB_WIDTH,
DEFAULT_LIMBS_COUNT_LIMIT,
DEFAULT_LIMBS_COUNT,
);

let mg = MainGate::new(main_gate_config.clone());
Expand Down Expand Up @@ -703,12 +703,26 @@ impl<const A: usize, F: PrimeField> Input<A, F> {
.zip_eq(expected_l1.iter())
.try_for_each(|(l, r)| region.constrain_equal(l.cell(), r.cell()))?;

BigUintPoint::constrain_equal(region, acc_W, &BigUintPoint { x: x0, y: y0 })?;
BigUintPoint::constrain_equal(region, incoming_W, &BigUintPoint { x: x1, y: y1 })?;
BigUintPoint::constrain_equal(
region,
acc_W,
&BigUintPoint {
x: x0.try_into().unwrap(),
y: y0.try_into().unwrap(),
},
)?;
BigUintPoint::constrain_equal(
region,
incoming_W,
&BigUintPoint {
x: x1.try_into().unwrap(),
y: y1.try_into().unwrap(),
},
)?;

*new_acc_W = BigUintPoint {
x: expected_x,
y: expected_y,
x: expected_x.try_into().unwrap(),
y: expected_y.try_into().unwrap(),
};
}

Expand Down
63 changes: 23 additions & 40 deletions src/ivc/cyclefold/sfc/input/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{array, ops::Deref};

use super::super::{DEFAULT_LIMBS_COUNT_LIMIT, DEFAULT_LIMB_WIDTH};
use super::super::{DEFAULT_LIMBS_COUNT, DEFAULT_LIMB_WIDTH};
pub use crate::ivc::protogalaxy::verify_chip::BigUintPoint;
use crate::{
gadgets::nonnative::bn::big_uint::BigUint,
Expand Down Expand Up @@ -85,7 +85,7 @@ impl<F: PrimeField> PairedPlonkInstance<F> {
BigUint::from_different_field(
value,
DEFAULT_LIMB_WIDTH,
DEFAULT_LIMBS_COUNT_LIMIT,
DEFAULT_LIMBS_COUNT,
)
.unwrap()
})
Expand All @@ -95,12 +95,8 @@ impl<F: PrimeField> PairedPlonkInstance<F> {
challenges: challenges
.iter()
.map(|cha| {
BigUint::from_different_field(
cha,
DEFAULT_LIMB_WIDTH,
DEFAULT_LIMBS_COUNT_LIMIT,
)
.unwrap()
BigUint::from_different_field(cha, DEFAULT_LIMB_WIDTH, DEFAULT_LIMBS_COUNT)
.unwrap()
})
.collect(),
}
Expand Down Expand Up @@ -213,9 +209,10 @@ impl<F: PrimeField> SelfTrace<F> {
.collect(),
challenges: vec![F::ZERO; challenges_len],
};
let betas_len = nifs::protogalaxy::poly::get_count_of_valuation(native_plonk_structure)
.unwrap()
.get();
let betas_len =
nifs::protogalaxy::poly::get_count_of_valuation_with_padding(native_plonk_structure)
.unwrap()
.get();
let proof_len = betas_len.ilog2() as usize;

SelfTrace {
Expand Down Expand Up @@ -273,20 +270,16 @@ impl<F: PrimeField> SangriaAccumulatorInstance<F> {
BigUint::from_different_field(
instance,
DEFAULT_LIMB_WIDTH,
DEFAULT_LIMBS_COUNT_LIMIT,
DEFAULT_LIMBS_COUNT,
)
.unwrap()
})
.collect()],
challenges: challenges
.iter()
.map(|cha| {
BigUint::from_different_field(
cha,
DEFAULT_LIMB_WIDTH,
DEFAULT_LIMBS_COUNT_LIMIT,
)
.unwrap()
BigUint::from_different_field(cha, DEFAULT_LIMB_WIDTH, DEFAULT_LIMBS_COUNT)
.unwrap()
})
.collect(),
},
Expand Down Expand Up @@ -390,7 +383,7 @@ impl<F: PrimeField> PairedTrace<F> {
BigUint::from_different_field(
value,
DEFAULT_LIMB_WIDTH,
DEFAULT_LIMBS_COUNT_LIMIT,
DEFAULT_LIMBS_COUNT,
)
.unwrap()
})
Expand All @@ -401,12 +394,8 @@ impl<F: PrimeField> PairedTrace<F> {
.challenges
.iter()
.map(|value| {
BigUint::from_different_field(
value,
DEFAULT_LIMB_WIDTH,
DEFAULT_LIMBS_COUNT_LIMIT,
)
.unwrap()
BigUint::from_different_field(value, DEFAULT_LIMB_WIDTH, DEFAULT_LIMBS_COUNT)
.unwrap()
})
.collect(),
};
Expand Down Expand Up @@ -467,7 +456,7 @@ impl<const ARITY: usize, F: PrimeField> Input<ARITY, F> {
BigUint::from_f(
&gen.next().unwrap(),
DEFAULT_LIMB_WIDTH,
DEFAULT_LIMBS_COUNT_LIMIT,
DEFAULT_LIMBS_COUNT,
)
.expect("Failed to create BigUint from limbs")
}
Expand All @@ -480,8 +469,8 @@ impl<const ARITY: usize, F: PrimeField> Input<ARITY, F> {
input_accumulator: ProtoGalaxyAccumulatorInstance {
ins: NativePlonkInstance {
W_commitments: vec![BigUintPoint {
x: random_big_uint(&mut gen).limbs().to_vec(),
y: random_big_uint(&mut gen).limbs().to_vec(),
x: random_big_uint(&mut gen).limbs().try_into().unwrap(),
y: random_big_uint(&mut gen).limbs().try_into().unwrap(),
}],
instances: vec![
vec![gen.next().unwrap(); 10]; // 5 instances each with 10 field elements
Expand All @@ -494,8 +483,8 @@ impl<const ARITY: usize, F: PrimeField> Input<ARITY, F> {
},
incoming: NativePlonkInstance {
W_commitments: vec![BigUintPoint {
x: random_big_uint(&mut gen).limbs().to_vec(),
y: random_big_uint(&mut gen).limbs().to_vec(),
x: random_big_uint(&mut gen).limbs().try_into().unwrap(),
y: random_big_uint(&mut gen).limbs().try_into().unwrap(),
}],
instances: vec![
vec![gen.next().unwrap(); 10]; // 10 instances each with 10 field elements
Expand Down Expand Up @@ -639,14 +628,11 @@ impl<const ARITY: usize, F: PrimeField> Input<ARITY, F> {
.instances
.iter()
.map(|v| {
vec![
BigUint::zero(DEFAULT_LIMB_WIDTH, DEFAULT_LIMBS_COUNT_LIMIT);
v.len()
]
vec![BigUint::zero(DEFAULT_LIMB_WIDTH, DEFAULT_LIMBS_COUNT); v.len()]
})
.collect(),
challenges: vec![
BigUint::zero(DEFAULT_LIMB_WIDTH, DEFAULT_LIMBS_COUNT_LIMIT);
BigUint::zero(DEFAULT_LIMB_WIDTH, DEFAULT_LIMBS_COUNT);
self.paired_trace.input_accumulator.ins.challenges.len()
],
},
Expand All @@ -669,16 +655,13 @@ impl<const ARITY: usize, F: PrimeField> Input<ARITY, F> {
.iter()
.map(|v| {
vec![
BigUint::zero(DEFAULT_LIMB_WIDTH, DEFAULT_LIMBS_COUNT_LIMIT);
BigUint::zero(DEFAULT_LIMB_WIDTH, DEFAULT_LIMBS_COUNT);
v.len()
]
})
.collect(),
challenges: vec![
BigUint::zero(
DEFAULT_LIMB_WIDTH,
DEFAULT_LIMBS_COUNT_LIMIT
);
BigUint::zero(DEFAULT_LIMB_WIDTH, DEFAULT_LIMBS_COUNT);
incoming.instance.challenges.len()
],
},
Expand Down
Loading

0 comments on commit 6070ee5

Please sign in to comment.