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

Use vectors instead of slices for PI #353

Merged
Merged
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
18 changes: 5 additions & 13 deletions halo2_backend/src/plonk/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@
engine: PlonkEngine<Scheme::Curve, M>,
params: &'params Scheme::ParamsProver,
pk: &'a ProvingKey<Scheme::Curve>,
// TODO: If this was a vector the usage would be simpler
// https://github.com/privacy-scaling-explorations/halo2/issues/265
instance: &[&[Scheme::Scalar]],
instance: Vec<Vec<Scheme::Scalar>>,
rng: R,
transcript: &'a mut T,
) -> Result<Self, Error>
Expand All @@ -90,9 +88,7 @@
pub fn new(
params: &'params Scheme::ParamsProver,
pk: &'a ProvingKey<Scheme::Curve>,
// TODO: If this was a vector the usage would be simpler
// https://github.com/privacy-scaling-explorations/halo2/issues/265
instance: &[&[Scheme::Scalar]],
instance: Vec<Vec<Scheme::Scalar>>,
rng: R,
transcript: &'a mut T,
) -> Result<ProverSingle<'a, 'params, Scheme, P, E, R, T, H2cEngine>, Error>
Expand Down Expand Up @@ -175,9 +171,7 @@
engine: PlonkEngine<Scheme::Curve, M>,
params: &'params Scheme::ParamsProver,
pk: &'a ProvingKey<Scheme::Curve>,
// TODO: If this was a vector the usage would be simpler.
// https://github.com/privacy-scaling-explorations/halo2/issues/265
circuits_instances: &[&[&[Scheme::Scalar]]],
circuits_instances: &[Vec<Vec<Scheme::Scalar>>],
rng: R,
transcript: &'a mut T,
) -> Result<Self, Error>
Expand All @@ -201,7 +195,7 @@
// commit_instance_fn is a helper function to return the polynomials (and its commitments) of
// instance columns while updating the transcript.
let mut commit_instance_fn =
|instance: &[&[Scheme::Scalar]]| -> Result<InstanceSingle<Scheme::Curve>, Error> {
|instance: &[Vec<Scheme::Scalar>]| -> Result<InstanceSingle<Scheme::Curve>, Error> {
// Create a lagrange polynomial for each instance column

let instance_values = instance
Expand Down Expand Up @@ -507,7 +501,7 @@
/// - 10. Compute and hash instance evals for the circuit instance
/// - 11. Compute and hash fixed evals
/// - 12. Evaluate permutation, lookups and shuffles at x
/// - 13. Generate all queries ([`ProverQuery`])

Check warning on line 504 in halo2_backend/src/plonk/prover.rs

View workflow job for this annotation

GitHub Actions / Intra-doc links

public documentation for `create_proof` links to private item `ProverQuery`
/// - 14. Send the queries to the [`Prover`]
pub fn create_proof(mut self) -> Result<(), Error>
where
Expand Down Expand Up @@ -905,9 +899,7 @@
pub fn new(
params: &'params Scheme::ParamsProver,
pk: &'a ProvingKey<Scheme::Curve>,
// TODO: If this was a vector the usage would be simpler.
// https://github.com/privacy-scaling-explorations/halo2/issues/265
circuits_instances: &[&[&[Scheme::Scalar]]],
circuits_instances: &[Vec<Vec<Scheme::Scalar>>],
rng: R,
transcript: &'a mut T,
) -> Result<Prover<'a, 'params, Scheme, P, E, R, T, H2cEngine>, Error>
Expand Down
11 changes: 7 additions & 4 deletions halo2_backend/src/plonk/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn verify_proof_single<'params, Scheme, V, E, T, Strategy>(
params: &'params Scheme::ParamsVerifier,
vk: &VerifyingKey<Scheme::Curve>,
strategy: Strategy,
instance: &[&[Scheme::Scalar]],
instance: Vec<Vec<Scheme::Scalar>>,
transcript: &mut T,
) -> Result<Strategy::Output, Error>
where
Expand All @@ -60,7 +60,7 @@ pub fn verify_proof<
params: &'params Scheme::ParamsVerifier,
vk: &VerifyingKey<Scheme::Curve>,
strategy: Strategy,
instances: &[&[&[Scheme::Scalar]]],
instances: &[Vec<Vec<Scheme::Scalar>>],
transcript: &mut T,
) -> Result<Strategy::Output, Error>
where
Expand Down Expand Up @@ -301,9 +301,12 @@ where
.instance_queries
.iter()
.map(|(column, rotation)| {
let instances = instances[column.index];
let instances = &instances[column.index];
let offset = (max_rotation - rotation.0) as usize;
compute_inner_product(instances, &l_i_s[offset..offset + instances.len()])
compute_inner_product(
instances.as_slice(),
&l_i_s[offset..offset + instances.len()],
)
})
.collect::<Vec<_>>()
})
Expand Down
10 changes: 1 addition & 9 deletions halo2_backend/src/plonk/verifier/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ where
// `is_zero() == false` then this argument won't be able to interfere with it
// to make it true, with high probability.
acc.scale(C::Scalar::random(OsRng));

acc.add_msm(&msm);
acc
}
Expand All @@ -109,16 +108,9 @@ where
.into_par_iter()
.enumerate()
.map(|(i, item)| {
let instances: Vec<Vec<_>> = item
.instances
.iter()
.map(|i| i.iter().map(|c| &c[..]).collect())
.collect();
let instances: Vec<_> = instances.iter().map(|i| &i[..]).collect();

let strategy = BatchStrategy::new(params);
let mut transcript = Blake2bRead::init(&item.proof[..]);
verify_proof(params, vk, strategy, &instances, &mut transcript).map_err(|e| {
verify_proof(params, vk, strategy, &item.instances, &mut transcript).map_err(|e| {
tracing::debug!("Batch item {} failed verification: {}", i, e);
e
})
Expand Down
6 changes: 3 additions & 3 deletions halo2_frontend/src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ struct WitnessCollection<'a, F: Field> {
advice_column_phase: &'a Vec<sealed::Phase>,
advice: Vec<Vec<Assigned<F>>>,
challenges: &'a HashMap<usize, F>,
instances: &'a [&'a [F]],
instances: &'a [Vec<F>],
usable_rows: RangeTo<usize>,
}

Expand Down Expand Up @@ -259,7 +259,7 @@ pub struct WitnessCalculator<'a, F: Field, ConcreteCircuit: Circuit<F>> {
circuit: &'a ConcreteCircuit,
config: &'a ConcreteCircuit::Config,
cs: &'a ConstraintSystem<F>,
instances: &'a [&'a [F]],
instances: &'a [Vec<F>],
next_phase: u8,
}

Expand All @@ -270,7 +270,7 @@ impl<'a, F: Field, ConcreteCircuit: Circuit<F>> WitnessCalculator<'a, F, Concret
circuit: &'a ConcreteCircuit,
config: &'a ConcreteCircuit::Config,
cs: &'a ConstraintSystem<F>,
instances: &'a [&'a [F]],
instances: &'a [Vec<F>],
) -> Self {
let n = 2usize.pow(k);
let unusable_rows_start = n - (cs.blinding_factors() + 1);
Expand Down
4 changes: 2 additions & 2 deletions halo2_proofs/benches/plonk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ fn criterion_benchmark(c: &mut Criterion) {
params,
pk,
&[circuit],
&[&[]],
&[vec![vec![]]],
rng,
&mut transcript,
)
Expand All @@ -302,7 +302,7 @@ fn criterion_benchmark(c: &mut Criterion) {
fn verifier(params: &ParamsIPA<EqAffine>, vk: &VerifyingKey<EqAffine>, proof: &[u8]) {
let strategy = SingleStrategy::new(params);
let mut transcript = Blake2bRead::<_, _, Challenge255<_>>::init(proof);
assert!(verify_proof(params, vk, strategy, &[&[]], &mut transcript).is_ok());
assert!(verify_proof(params, vk, strategy, &[vec![vec![]]], &mut transcript).is_ok());
}

let k_range = 8..=16;
Expand Down
12 changes: 7 additions & 5 deletions halo2_proofs/src/plonk/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub fn create_proof_with_engine<
params: &'params Scheme::ParamsProver,
pk: &ProvingKey<Scheme::Curve>,
circuits: &[ConcreteCircuit],
instances: &[&[&[Scheme::Scalar]]],
instances: &[Vec<Vec<Scheme::Scalar>>],
rng: R,
transcript: &mut T,
) -> Result<(), Error>
Expand All @@ -51,7 +51,9 @@ where
let mut witness_calcs: Vec<_> = circuits
.iter()
.enumerate()
.map(|(i, circuit)| WitnessCalculator::new(params.k(), circuit, &config, &cs, instances[i]))
.map(|(i, circuit)| {
WitnessCalculator::new(params.k(), circuit, &config, &cs, instances[i].as_slice())
})
.collect();
let mut prover = Prover::<Scheme, P, _, _, _, _>::new_with_engine(
engine, params, pk, instances, rng, transcript,
Expand Down Expand Up @@ -84,7 +86,7 @@ pub fn create_proof<
params: &'params Scheme::ParamsProver,
pk: &ProvingKey<Scheme::Curve>,
circuits: &[ConcreteCircuit],
instances: &[&[&[Scheme::Scalar]]],
instances: &[Vec<Vec<Scheme::Scalar>>],
rng: R,
transcript: &mut T,
) -> Result<(), Error>
Expand Down Expand Up @@ -160,7 +162,7 @@ fn test_create_proof() {
&params,
&pk,
&[MyCircuit, MyCircuit],
&[&[], &[]],
&[vec![], vec![]],
OsRng,
&mut transcript,
)
Expand Down Expand Up @@ -220,7 +222,7 @@ fn test_create_proof_custom() {
&params,
&pk,
&[MyCircuit, MyCircuit],
&[&[], &[]],
&[vec![], vec![]],
OsRng,
&mut transcript,
)
Expand Down
10 changes: 3 additions & 7 deletions halo2_proofs/tests/compress_selectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,19 +365,15 @@ fn test_mycircuit(

// Proving
#[allow(clippy::useless_vec)]
let instances = vec![vec![Fr::one(), Fr::from_u128(3)]];
let instances_slice: &[&[Fr]] = &(instances
.iter()
.map(|instance| instance.as_slice())
.collect::<Vec<_>>());
let instances = vec![vec![vec![Fr::one(), Fr::from_u128(3)]]];

let mut transcript = Blake2bWrite::<_, G1Affine, Challenge255<_>>::init(vec![]);
create_proof_with_engine::<KZGCommitmentScheme<Bn256>, ProverSHPLONK<'_, Bn256>, _, _, _, _, _>(
engine,
&params,
&pk,
&[circuit],
&[instances_slice],
instances.as_slice(),
&mut rng,
&mut transcript,
)?;
Expand All @@ -392,7 +388,7 @@ fn test_mycircuit(
&verifier_params,
&vk,
strategy,
&[instances_slice],
instances.as_slice(),
&mut verifier_transcript,
)
.map_err(halo2_proofs::plonk::Error::Backend)
Expand Down
23 changes: 7 additions & 16 deletions halo2_proofs/tests/frontend_backend_split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,19 +523,15 @@ fn test_mycircuit_full_legacy() {
println!("Keygen: {:?}", start.elapsed());

// Proving
let instances = circuit.instances();
let instances_slice: &[&[Fr]] = &(instances
.iter()
.map(|instance| instance.as_slice())
.collect::<Vec<_>>());
let instances = vec![circuit.instances()];

let start = Instant::now();
let mut transcript = Blake2bWrite::<_, G1Affine, Challenge255<_>>::init(vec![]);
create_proof::<KZGCommitmentScheme<Bn256>, ProverSHPLONK<'_, Bn256>, _, _, _, _>(
&params,
&pk,
&[circuit],
&[instances_slice],
instances.as_slice(),
&mut rng,
&mut transcript,
)
Expand All @@ -554,7 +550,7 @@ fn test_mycircuit_full_legacy() {
&verifier_params,
&vk,
strategy,
&[instances_slice],
instances.as_slice(),
&mut verifier_transcript,
)
.expect("verify succeeds");
Expand Down Expand Up @@ -585,16 +581,11 @@ fn test_mycircuit_full_split() {
println!("Keygen: {:?}", start.elapsed());
drop(compiled_circuit);

let instances = circuit.instances();
// Proving
println!("Proving...");
let instances = circuit.instances();
let instances_slice: &[&[Fr]] = &(instances
.iter()
.map(|instance| instance.as_slice())
.collect::<Vec<_>>());

let start = Instant::now();
let mut witness_calc = WitnessCalculator::new(k, &circuit, &config, &cs, instances_slice);
let mut witness_calc = WitnessCalculator::new(k, &circuit, &config, &cs, &instances);
let mut transcript = Blake2bWrite::<_, G1Affine, Challenge255<_>>::init(vec![]);
let mut prover = ProverSingle::<
KZGCommitmentScheme<Bn256>,
Expand All @@ -607,7 +598,7 @@ fn test_mycircuit_full_split() {
engine,
&params,
&pk,
instances_slice,
instances.clone(),
&mut rng,
&mut transcript,
)
Expand All @@ -634,7 +625,7 @@ fn test_mycircuit_full_split() {
&verifier_params,
&vk,
strategy,
instances_slice,
instances,
&mut verifier_transcript,
)
.expect("verify succeeds");
Expand Down
21 changes: 8 additions & 13 deletions halo2_proofs/tests/plonk_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ fn plonk_api() {
where
Scheme::Scalar: Ord + WithSmallOrderMulGroup<3> + FromUniformBytes<64>,
{
let (a, instance, lookup_table) = common!(Scheme);
let (a, instance_val, lookup_table) = common!(Scheme);

let circuit: MyCircuit<Scheme::Scalar> = MyCircuit {
a: Value::known(a),
Expand All @@ -498,19 +498,20 @@ fn plonk_api() {

let mut transcript = T::init(vec![]);

let instance = [vec![vec![instance_val]], vec![vec![instance_val]]];
create_plonk_proof_with_engine::<Scheme, P, _, _, _, _, _>(
engine,
params,
pk,
&[circuit.clone(), circuit.clone()],
&[&[&[instance]], &[&[instance]]],
&instance,
rng,
&mut transcript,
)
.expect("proof generation should not fail");

// Check this circuit is satisfied.
let prover = match MockProver::run(K, &circuit, vec![vec![instance]]) {
let prover = match MockProver::run(K, &circuit, vec![vec![instance_val]]) {
Ok(prover) => prover,
Err(e) => panic!("{e:?}"),
};
Expand Down Expand Up @@ -553,20 +554,14 @@ fn plonk_api() {
) where
Scheme::Scalar: Ord + WithSmallOrderMulGroup<3> + FromUniformBytes<64>,
{
let (_, instance, _) = common!(Scheme);
let pubinputs = [instance];
let (_, instance_val, _) = common!(Scheme);

let mut transcript = T::init(proof);
let instance = [vec![vec![instance_val]], vec![vec![instance_val]]];

let strategy = Strategy::new(params_verifier);
let strategy = verify_plonk_proof(
params_verifier,
vk,
strategy,
&[&[&pubinputs[..]], &[&pubinputs[..]]],
&mut transcript,
)
.unwrap();
let strategy =
verify_plonk_proof(params_verifier, vk, strategy, &instance, &mut transcript).unwrap();

assert!(strategy.finalize());
}
Expand Down
6 changes: 3 additions & 3 deletions halo2_proofs/tests/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ fn test_serialization() {

std::fs::remove_file("serialization-test.pk").unwrap();

let instances: &[&[Fr]] = &[&[circuit.0]];
let instances: Vec<Vec<Vec<Fr>>> = vec![vec![vec![circuit.0]]];
let mut transcript = Blake2bWrite::<_, _, Challenge255<_>>::init(vec![]);
create_proof::<
KZGCommitmentScheme<Bn256>,
Expand All @@ -169,7 +169,7 @@ fn test_serialization() {
&params,
&pk,
&[circuit],
&[instances],
instances.as_slice(),
OsRng,
&mut transcript,
)
Expand All @@ -189,7 +189,7 @@ fn test_serialization() {
&verifier_params,
pk.get_vk(),
strategy,
&[instances],
instances.as_slice(),
&mut transcript
)
.is_ok());
Expand Down
Loading
Loading