Skip to content

Commit

Permalink
Full flow, but verification fails
Browse files Browse the repository at this point in the history
  • Loading branch information
ed255 committed Dec 29, 2023
1 parent a8a2009 commit c272378
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 85 deletions.
84 changes: 15 additions & 69 deletions halo2_proofs/src/plonk/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1754,7 +1754,8 @@ impl<'a, F: Field, ConcreteCircuit: Circuit<F>> WitnessCalculator<'a, F, Concret
self.circuit,
self.config.clone(),
self.cs.constants.clone(),
)?;
)
.expect("todo");

let column_indices = self
.cs
Expand Down Expand Up @@ -1786,74 +1787,6 @@ impl<'a, F: Field, ConcreteCircuit: Circuit<F>> WitnessCalculator<'a, F, Concret
}
}

/// Calculate witness at phase. Frontend function
pub fn calc_witness<F: Field, ConcreteCircuit: Circuit<F>>(
k: u32,
circuit: &ConcreteCircuit,
config: &ConcreteCircuit::Config,
cs: &ConstraintSystem<F>,
instances: &[&[F]],
phase: u8,
challenges: &HashMap<usize, F>,
) -> Result<Vec<Option<Polynomial<Assigned<F>, LagrangeCoeff>>>, Error> {
let n = 2usize.pow(k);
let unusable_rows_start = n - (cs.blinding_factors() + 1);
let phase = match phase {
0 => FirstPhase.to_sealed(),
1 => SecondPhase.to_sealed(),
2 => ThirdPhase.to_sealed(),
_ => unreachable!("only phase [0..2] supported"),
};
let mut witness = WitnessCollection {
k,
current_phase: phase,
advice: vec![Polynomial::new_empty(n, F::ZERO.into()); cs.num_advice_columns],
unblinded_advice: HashSet::from_iter(cs.unblinded_advice_columns.clone()),
instances,
challenges,
// The prover will not be allowed to assign values to advice
// cells that exist within inactive rows, which include some
// number of blinding factors and an extra row for use in the
// permutation argument.
usable_rows: ..unusable_rows_start,
_marker: std::marker::PhantomData,
};

// Synthesize the circuit to obtain the witness and other information.
ConcreteCircuit::FloorPlanner::synthesize(
&mut witness,
circuit,
config.clone(),
cs.constants.clone(),
)?;

let column_indices = cs
.advice_column_phase
.iter()
.enumerate()
.filter_map(|(column_index, phase)| {
if witness.current_phase == *phase {
Some(column_index)
} else {
None
}
})
.collect::<BTreeSet<_>>();

Ok(witness
.advice
.into_iter()
.enumerate()
.map(|(column_index, advice)| {
if column_indices.contains(&column_index) {
Some(advice)
} else {
None
}
})
.collect())
}

/// TODO: Document. Frontend function
pub fn compile_circuit<F: Field, ConcreteCircuit: Circuit<F>>(
k: u32,
Expand Down Expand Up @@ -2033,6 +1966,19 @@ impl<F: Field> ConstraintSystemV2Backend<F> {
collect_queries(expr, &mut queries);
}
}
for column in self.permutation.get_columns() {
match column.column_type {
Any::Instance => queries
.instance
.insert((Column::new(column.index(), Instance), Rotation::cur())),
Any::Fixed => queries
.fixed
.insert((Column::new(column.index(), Fixed), Rotation::cur())),
Any::Advice(advice) => queries
.advice
.insert((Column::new(column.index(), advice), Rotation::cur())),
};
}

for (column, _) in queries.advice.iter() {
num_advice_queries[column.index()] += 1;
Expand Down
19 changes: 16 additions & 3 deletions halo2_proofs/src/plonk/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,25 @@ impl<
if witness.len() != advice.len() {
return Err(Error::Other(format!("witness.len() != advice.len()")));

Check warning on line 240 in halo2_proofs/src/plonk/prover.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

useless use of `format!`

warning: useless use of `format!` --> halo2_proofs/src/plonk/prover.rs:240:37 | 240 | return Err(Error::Other(format!("witness.len() != advice.len()"))); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `"witness.len() != advice.len()".to_string()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_format = note: `-W clippy::useless-format` implied by `-W clippy::all`

Check warning on line 240 in halo2_proofs/src/plonk/prover.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

useless use of `format!`

warning: useless use of `format!` --> halo2_proofs/src/plonk/prover.rs:240:37 | 240 | return Err(Error::Other(format!("witness.len() != advice.len()"))); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `"witness.len() != advice.len()".to_string()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_format = note: `-W clippy::useless-format` implied by `-W clippy::all`
}
for witness in witness.iter() {
if witness.len() != self.params.n() as usize {
for witness_circuit in witness.iter() {
if witness_circuit.len() != meta.num_advice_columns {
return Err(Error::Other(format!(
"unexpected length in witness columns"
"unexpected length in witness_circuitk. Got {}, expected {}",
witness_circuit.len(),
meta.num_advice_columns,
)));
}
for witness_column in witness_circuit {
if let Some(witness_column) = witness_column {
if witness_column.len() != self.params.n() as usize {
return Err(Error::Other(format!(
"unexpected length in witness_column. Got {}, expected {}",
witness_column.len(),
self.params.n()
)));
}
}
}

Check warning on line 260 in halo2_proofs/src/plonk/prover.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

unnecessary `if let` since only the `Some` variant of the iterator element is used

warning: unnecessary `if let` since only the `Some` variant of the iterator element is used --> halo2_proofs/src/plonk/prover.rs:250:13 | 250 | for witness_column in witness_circuit { | ^ --------------- help: try: `witness_circuit.iter().flatten()` | _____________| | | 251 | | if let Some(witness_column) = witness_column { 252 | | if witness_column.len() != self.params.n() as usize { 253 | | return Err(Error::Other(format!( ... | 259 | | } 260 | | } | |_____________^ | help: ...and remove the `if let` statement in the for loop --> halo2_proofs/src/plonk/prover.rs:251:17 | 251 | / if let Some(witness_column) = witness_column { 252 | | if witness_column.len() != self.params.n() as usize { 253 | | return Err(Error::Other(format!( 254 | | "unexpected length in witness_column. Got {}, expected {}", ... | 258 | | } 259 | | } | |_________________^ = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_flatten = note: `-W clippy::manual-flatten` implied by `-W clippy::all`

Check warning on line 260 in halo2_proofs/src/plonk/prover.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

unnecessary `if let` since only the `Some` variant of the iterator element is used

warning: unnecessary `if let` since only the `Some` variant of the iterator element is used --> halo2_proofs/src/plonk/prover.rs:250:13 | 250 | for witness_column in witness_circuit { | ^ --------------- help: try: `witness_circuit.iter().flatten()` | _____________| | | 251 | | if let Some(witness_column) = witness_column { 252 | | if witness_column.len() != self.params.n() as usize { 253 | | return Err(Error::Other(format!( ... | 259 | | } 260 | | } | |_____________^ | help: ...and remove the `if let` statement in the for loop --> halo2_proofs/src/plonk/prover.rs:251:17 | 251 | / if let Some(witness_column) = witness_column { 252 | | if witness_column.len() != self.params.n() as usize { 253 | | return Err(Error::Other(format!( 254 | | "unexpected length in witness_column. Got {}, expected {}", ... | 258 | | } 259 | | } | |_________________^ = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_flatten = note: `-W clippy::manual-flatten` implied by `-W clippy::all`
}

// Check that all current_phase advice columns are Some
Expand Down
13 changes: 8 additions & 5 deletions halo2_proofs/src/plonk/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,11 +434,14 @@ where
// polynomial commitments open to the correct values.

let verifier = V::new(params);
strategy.process(|msm| {
verifier
.verify_proof(transcript, queries, msm)
.map_err(|_| Error::Opening)
})
Ok(strategy
.process(|msm| {
println!("ONE");
verifier
.verify_proof(transcript, queries, msm)
.map_err(|_| Error::Opening)
})
.expect("todo"))
}

// TODO: Remove
Expand Down
3 changes: 2 additions & 1 deletion halo2_proofs/src/poly/kzg/strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ where
self.msm_accumulator.scale(E::Scalar::random(OsRng));

// Guard is updated with new msm contributions
let guard = f(self.msm_accumulator)?;
let guard = f(self.msm_accumulator).expect("todo");
Ok(Self {
msm_accumulator: guard.msm_accumulator,
})
Expand Down Expand Up @@ -144,6 +144,7 @@ where
if msm.check() {
Ok(())
} else {
println!("OH NO");
Err(Error::ConstraintSystemFailure)
}
}
Expand Down
16 changes: 9 additions & 7 deletions halo2_proofs/tests/frontend_backend_split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use halo2_proofs::arithmetic::Field;
use halo2_proofs::circuit::{AssignedCell, Cell, Layouter, Region, SimpleFloorPlanner, Value};

Check warning on line 7 in halo2_proofs/tests/frontend_backend_split.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

unused import: `Cell`

warning: unused import: `Cell` --> halo2_proofs/tests/frontend_backend_split.rs:7:43 | 7 | use halo2_proofs::circuit::{AssignedCell, Cell, Layouter, Region, SimpleFloorPlanner, Value}; | ^^^^
use halo2_proofs::dev::MockProver;
use halo2_proofs::plonk::{
calc_witness, compile_circuit, create_proof, keygen_pk, keygen_pk_v2, keygen_vk, keygen_vk_v2,
verify_proof, verify_proof_v2, Advice, Assigned, Challenge, Circuit, Column, CompiledCircuitV2,
compile_circuit, create_proof, keygen_pk, keygen_pk_v2, keygen_vk, keygen_vk_v2, verify_proof,
verify_proof_v2, Advice, Assigned, Challenge, Circuit, Column, CompiledCircuitV2,

Check warning on line 11 in halo2_proofs/tests/frontend_backend_split.rs

View workflow job for this annotation

GitHub Actions / Clippy (beta)

unused imports: `Assigned`, `CompiledCircuitV2`, `ConstraintSystemV2Backend`, `ProvingKey`, `TableColumn`, `VerifyingKey`

warning: unused imports: `Assigned`, `CompiledCircuitV2`, `ConstraintSystemV2Backend`, `ProvingKey`, `TableColumn`, `VerifyingKey` --> halo2_proofs/tests/frontend_backend_split.rs:11:30 | 11 | verify_proof_v2, Advice, Assigned, Challenge, Circuit, Column, CompiledCircuitV2, | ^^^^^^^^ ^^^^^^^^^^^^^^^^^ 12 | ConstraintSystem, ConstraintSystemV2Backend, Error, Expression, FirstPhase, Fixed, Instance, | ^^^^^^^^^^^^^^^^^^^^^^^^^ 13 | ProverV2, ProvingKey, SecondPhase, Selector, TableColumn, VerifyingKey, WitnessCalculator, | ^^^^^^^^^^ ^^^^^^^^^^^ ^^^^^^^^^^^^
ConstraintSystem, ConstraintSystemV2Backend, Error, Expression, FirstPhase, Fixed, Instance,
ProverV2, ProvingKey, SecondPhase, Selector, TableColumn, VerifyingKey, WitnessCalculator,
};
Expand Down Expand Up @@ -316,10 +316,11 @@ impl<F: Field + From<u64>, const WIDTH_FACTOR: usize> MyCircuit<F, WIDTH_FACTOR>
// Enable RLC gate 3 times
for abcd in [[3, 5, 3, 5], [8, 9, 8, 9], [111, 222, 111, 222]] {
config.s_rlc.enable(&mut region, offset)?;
let (_, abcd1) = config.assign_gate(&mut region, &mut offset, None, abcd)?;
let rlc = challenge
.zip(abcd1[0].value().zip(abcd1[1].value()))
.map(|(ch, (a, b))| *a + ch * b);
let (_, _) = config.assign_gate(&mut region, &mut offset, None, abcd)?;
let rlc = challenge.map(|ch| {
let [a, b, ..] = abcd;
F::from(a) + ch * F::from(b)
});
region.assign_advice(|| "", config.e, offset - 1, || rlc)?;
offset += 1;
}
Expand Down Expand Up @@ -381,7 +382,8 @@ impl<F: Field + From<u64>, const WIDTH_FACTOR: usize> Circuit<F> for MyCircuit<F
for config in &config {
let mut total_rows = 0;
loop {
let (rows, instance_copy) = self.synthesize_unit(config, &mut layouter)?;
let (rows, instance_copy) =
self.synthesize_unit(config, &mut layouter).expect("todo");
if total_rows == 0 {
for (i, instance) in instance_copy.iter().enumerate() {
layouter.constrain_instance(instance.cell(), config.instance, 1 + i)?;
Expand Down

0 comments on commit c272378

Please sign in to comment.