Skip to content

Commit

Permalink
Do not panic on wire set twice or generator not run issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Nashtare committed Aug 6, 2024
1 parent 0e363e1 commit 3c32490
Show file tree
Hide file tree
Showing 45 changed files with 462 additions and 257 deletions.
8 changes: 4 additions & 4 deletions plonky2/examples/bench_recursion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ fn dummy_lookup_proof<F: RichField + Extendable<D>, C: GenericConfig<D, F = F>,

let data = builder.build::<C>();
let mut inputs = PartialWitness::<F>::new();
inputs.set_target(initial_a, F::ONE);
inputs.set_target(initial_a, F::ONE)?;
let mut timing = TimingTree::new("prove with one lookup", Level::Debug);
let proof = prove(&data.prover_only, &data.common, inputs, &mut timing)?;
timing.print();
Expand Down Expand Up @@ -189,7 +189,7 @@ fn dummy_many_rows_proof<
builder.register_public_input(output);

let mut pw = PartialWitness::new();
pw.set_target(initial_a, F::ONE);
pw.set_target(initial_a, F::ONE)?;
let data = builder.build::<C>();
let mut timing = TimingTree::new("prove with many lookups", Level::Debug);
let proof = prove(&data.prover_only, &data.common, pw, &mut timing)?;
Expand Down Expand Up @@ -235,8 +235,8 @@ where
let data = builder.build::<C>();

let mut pw = PartialWitness::new();
pw.set_proof_with_pis_target(&pt, inner_proof);
pw.set_verifier_data_target(&inner_data, inner_vd);
pw.set_proof_with_pis_target(&pt, inner_proof)?;
pw.set_verifier_data_target(&inner_data, inner_vd)?;

let mut timing = TimingTree::new("prove", Level::Debug);
let proof = prove::<F, C, D>(&data.prover_only, &data.common, pw, &mut timing)?;
Expand Down
2 changes: 1 addition & 1 deletion plonky2/examples/factorial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn main() -> Result<()> {
builder.register_public_input(cur_target);

let mut pw = PartialWitness::new();
pw.set_target(initial, F::ONE);
pw.set_target(initial, F::ONE)?;

let data = builder.build::<C>();
let proof = data.prove(pw)?;
Expand Down
4 changes: 2 additions & 2 deletions plonky2/examples/fibonacci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ fn main() -> Result<()> {

// Provide initial values.
let mut pw = PartialWitness::new();
pw.set_target(initial_a, F::ZERO);
pw.set_target(initial_b, F::ONE);
pw.set_target(initial_a, F::ZERO)?;
pw.set_target(initial_b, F::ONE)?;

let data = builder.build::<C>();
let proof = data.prove(pw)?;
Expand Down
4 changes: 2 additions & 2 deletions plonky2/examples/fibonacci_serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ fn main() -> Result<()> {

// Provide initial values.
let mut pw = PartialWitness::new();
pw.set_target(initial_a, F::ZERO);
pw.set_target(initial_b, F::ONE);
pw.set_target(initial_a, F::ZERO)?;
pw.set_target(initial_b, F::ONE)?;

let data = builder.build::<C>();

Expand Down
2 changes: 1 addition & 1 deletion plonky2/examples/range_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn main() -> Result<()> {
builder.range_check(value, log_max);

let mut pw = PartialWitness::new();
pw.set_target(value, F::from_canonical_usize(42));
pw.set_target(value, F::from_canonical_usize(42))?;

let data = builder.build::<C>();
let proof = data.prove(pw)?;
Expand Down
10 changes: 7 additions & 3 deletions plonky2/examples/square_root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,17 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F, D>
vec![self.x_squared]
}

fn run_once(&self, witness: &PartitionWitness<F>, out_buffer: &mut GeneratedValues<F>) {
fn run_once(
&self,
witness: &PartitionWitness<F>,
out_buffer: &mut GeneratedValues<F>,
) -> Result<()> {
let x_squared = witness.get_target(self.x_squared);
let x = x_squared.sqrt().unwrap();

println!("Square root: {x}");

out_buffer.set_target(self.x, x);
out_buffer.set_target(self.x, x)
}

fn serialize(&self, dst: &mut Vec<u8>, _common_data: &CommonCircuitData<F, D>) -> IoResult<()> {
Expand Down Expand Up @@ -121,7 +125,7 @@ fn main() -> Result<()> {
};

let mut pw = PartialWitness::new();
pw.set_target(x_squared, x_squared_value);
pw.set_target(x_squared, x_squared_value)?;

let data = builder.build::<C>();
let proof = data.prove(pw.clone())?;
Expand Down
2 changes: 1 addition & 1 deletion plonky2/src/batch_fri/oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ mod test {
);

let mut pw = PartialWitness::new();
set_fri_proof_target(&mut pw, &fri_proof_target, &proof);
set_fri_proof_target(&mut pw, &fri_proof_target, &proof)?;

let data = builder.build::<C>();
let proof = prove::<F, C, D>(&data.prover_only, &data.common, pw, &mut timing)?;
Expand Down
20 changes: 12 additions & 8 deletions plonky2/src/fri/witness_util.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::Result;
use itertools::Itertools;

use crate::field::extension::Extendable;
Expand All @@ -11,28 +12,29 @@ pub fn set_fri_proof_target<F, W, H, const D: usize>(
witness: &mut W,
fri_proof_target: &FriProofTarget<D>,
fri_proof: &FriProof<F, H, D>,
) where
) -> Result<()>
where
F: RichField + Extendable<D>,
W: WitnessWrite<F> + ?Sized,
H: AlgebraicHasher<F>,
{
witness.set_target(fri_proof_target.pow_witness, fri_proof.pow_witness);
witness.set_target(fri_proof_target.pow_witness, fri_proof.pow_witness)?;

for (&t, &x) in fri_proof_target
.final_poly
.0
.iter()
.zip_eq(&fri_proof.final_poly.coeffs)
{
witness.set_extension_target(t, x);
witness.set_extension_target(t, x)?;
}

for (t, x) in fri_proof_target
.commit_phase_merkle_caps
.iter()
.zip_eq(&fri_proof.commit_phase_merkle_caps)
{
witness.set_cap_target(t, x);
witness.set_cap_target(t, x)?;
}

for (qt, q) in fri_proof_target
Expand All @@ -47,25 +49,27 @@ pub fn set_fri_proof_target<F, W, H, const D: usize>(
.zip_eq(&q.initial_trees_proof.evals_proofs)
{
for (&t, &x) in at.0.iter().zip_eq(&a.0) {
witness.set_target(t, x);
witness.set_target(t, x)?;
}
for (&t, &x) in at.1.siblings.iter().zip_eq(&a.1.siblings) {
witness.set_hash_target(t, x);
witness.set_hash_target(t, x)?;
}
}

for (st, s) in qt.steps.iter().zip_eq(&q.steps) {
for (&t, &x) in st.evals.iter().zip_eq(&s.evals) {
witness.set_extension_target(t, x);
witness.set_extension_target(t, x)?;
}
for (&t, &x) in st
.merkle_proof
.siblings
.iter()
.zip_eq(&s.merkle_proof.siblings)
{
witness.set_hash_target(t, x);
witness.set_hash_target(t, x)?;
}
}
}

Ok(())
}
12 changes: 9 additions & 3 deletions plonky2/src/gadgets/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use alloc::{
};
use core::borrow::Borrow;

use anyhow::Result;

use crate::field::extension::Extendable;
use crate::field::types::Field64;
use crate::gates::arithmetic_base::ArithmeticGate;
Expand Down Expand Up @@ -397,14 +399,18 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F, D> for Equ
vec![self.x, self.y]
}

fn run_once(&self, witness: &PartitionWitness<F>, out_buffer: &mut GeneratedValues<F>) {
fn run_once(
&self,
witness: &PartitionWitness<F>,
out_buffer: &mut GeneratedValues<F>,
) -> Result<()> {
let x = witness.get_target(self.x);
let y = witness.get_target(self.y);

let inv = if x != y { (x - y).inverse() } else { F::ZERO };

out_buffer.set_bool_target(self.equal, x == y);
out_buffer.set_target(self.inv, inv);
out_buffer.set_bool_target(self.equal, x == y)?;
out_buffer.set_target(self.inv, inv)
}

fn serialize(&self, dst: &mut Vec<u8>, _common_data: &CommonCircuitData<F, D>) -> IoResult<()> {
Expand Down
16 changes: 11 additions & 5 deletions plonky2/src/gadgets/arithmetic_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use alloc::{
};
use core::borrow::Borrow;

use anyhow::Result;

use crate::field::extension::{Extendable, FieldExtension, OEF};
use crate::field::types::{Field, Field64};
use crate::gates::arithmetic_extension::ArithmeticExtensionGate;
Expand Down Expand Up @@ -519,7 +521,11 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F, D>
deps
}

fn run_once(&self, witness: &PartitionWitness<F>, out_buffer: &mut GeneratedValues<F>) {
fn run_once(
&self,
witness: &PartitionWitness<F>,
out_buffer: &mut GeneratedValues<F>,
) -> Result<()> {
let num = witness.get_extension_target(self.numerator);
let dem = witness.get_extension_target(self.denominator);
let quotient = num / dem;
Expand Down Expand Up @@ -621,7 +627,7 @@ mod tests {
let vs = FF::rand_vec(3);
let ts = builder.add_virtual_extension_targets(3);
for (&v, &t) in vs.iter().zip(&ts) {
pw.set_extension_target(t, v);
pw.set_extension_target(t, v)?;
}
let mul0 = builder.mul_many_extension(&ts);
let mul1 = {
Expand Down Expand Up @@ -696,9 +702,9 @@ mod tests {
let y = ExtensionAlgebra::<FF, D>(FF::rand_array());
let z = x * y;
for i in 0..D {
pw.set_extension_target(xt.0[i], x.0[i]);
pw.set_extension_target(yt.0[i], y.0[i]);
pw.set_extension_target(zt.0[i], z.0[i]);
pw.set_extension_target(xt.0[i], x.0[i])?;
pw.set_extension_target(yt.0[i], y.0[i])?;
pw.set_extension_target(zt.0[i], z.0[i])?;
}

let data = builder.build::<C>();
Expand Down
12 changes: 9 additions & 3 deletions plonky2/src/gadgets/range_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use alloc::{
vec::Vec,
};

use anyhow::Result;

use crate::field::extension::Extendable;
use crate::hash::hash_types::RichField;
use crate::iop::generator::{GeneratedValues, SimpleGenerator};
Expand Down Expand Up @@ -74,13 +76,17 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F, D> for Low
vec![self.integer]
}

fn run_once(&self, witness: &PartitionWitness<F>, out_buffer: &mut GeneratedValues<F>) {
fn run_once(
&self,
witness: &PartitionWitness<F>,
out_buffer: &mut GeneratedValues<F>,
) -> Result<()> {
let integer_value = witness.get_target(self.integer).to_canonical_u64();
let low = integer_value & ((1 << self.n_log) - 1);
let high = integer_value >> self.n_log;

out_buffer.set_target(self.low, F::from_canonical_u64(low));
out_buffer.set_target(self.high, F::from_canonical_u64(high));
out_buffer.set_target(self.low, F::from_canonical_u64(low))?;
out_buffer.set_target(self.high, F::from_canonical_u64(high))
}

fn serialize(&self, dst: &mut Vec<u8>, _common_data: &CommonCircuitData<F, D>) -> IoResult<()> {
Expand Down
4 changes: 2 additions & 2 deletions plonky2/src/gadgets/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ mod tests {
let truet = builder._true();
let falset = builder._false();

pw.set_extension_target(xt, x);
pw.set_extension_target(yt, y);
pw.set_extension_target(xt, x)?;
pw.set_extension_target(yt, y)?;

let should_be_x = builder.select_ext(truet, xt, yt);
let should_be_y = builder.select_ext(falset, xt, yt);
Expand Down
9 changes: 7 additions & 2 deletions plonky2/src/gadgets/split_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use alloc::{format, string::String, vec, vec::Vec};
use core::borrow::Borrow;

use anyhow::Result;
use itertools::Itertools;

use crate::field::extension::Extendable;
Expand Down Expand Up @@ -97,7 +98,11 @@ impl<F: RichField + Extendable<D>, const B: usize, const D: usize> SimpleGenerat
self.limbs.iter().map(|b| b.target).collect()
}

fn run_once(&self, witness: &PartitionWitness<F>, out_buffer: &mut GeneratedValues<F>) {
fn run_once(
&self,
witness: &PartitionWitness<F>,
out_buffer: &mut GeneratedValues<F>,
) -> Result<()> {
let sum = self
.limbs
.iter()
Expand All @@ -107,7 +112,7 @@ impl<F: RichField + Extendable<D>, const B: usize, const D: usize> SimpleGenerat
acc * F::from_canonical_usize(B) + F::from_bool(limb)
});

out_buffer.set_target(Target::wire(self.row, BaseSumGate::<B>::WIRE_SUM), sum);
out_buffer.set_target(Target::wire(self.row, BaseSumGate::<B>::WIRE_SUM), sum)
}

fn serialize(&self, dst: &mut Vec<u8>, _common_data: &CommonCircuitData<F, D>) -> IoResult<()> {
Expand Down
22 changes: 18 additions & 4 deletions plonky2/src/gadgets/split_join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use alloc::{
vec::Vec,
};

use anyhow::Result;

use crate::field::extension::Extendable;
use crate::gates::base_sum::BaseSumGate;
use crate::hash::hash_types::RichField;
Expand Down Expand Up @@ -75,19 +77,25 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F, D> for Spl
vec![self.integer]
}

fn run_once(&self, witness: &PartitionWitness<F>, out_buffer: &mut GeneratedValues<F>) {
fn run_once(
&self,
witness: &PartitionWitness<F>,
out_buffer: &mut GeneratedValues<F>,
) -> Result<()> {
let mut integer_value = witness.get_target(self.integer).to_canonical_u64();

for &b in &self.bits {
let b_value = integer_value & 1;
out_buffer.set_target(b, F::from_canonical_u64(b_value));
out_buffer.set_target(b, F::from_canonical_u64(b_value))?;
integer_value >>= 1;
}

debug_assert_eq!(
integer_value, 0,
"Integer too large to fit in given number of bits"
);

Ok(())
}

fn serialize(&self, dst: &mut Vec<u8>, _common_data: &CommonCircuitData<F, D>) -> IoResult<()> {
Expand Down Expand Up @@ -118,7 +126,11 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F, D> for Wir
vec![self.integer]
}

fn run_once(&self, witness: &PartitionWitness<F>, out_buffer: &mut GeneratedValues<F>) {
fn run_once(
&self,
witness: &PartitionWitness<F>,
out_buffer: &mut GeneratedValues<F>,
) -> Result<()> {
let mut integer_value = witness.get_target(self.integer).to_canonical_u64();

for &gate in &self.gates {
Expand All @@ -134,7 +146,7 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F, D> for Wir
integer_value = 0;
};

out_buffer.set_target(sum, F::from_canonical_u64(truncated_value));
out_buffer.set_target(sum, F::from_canonical_u64(truncated_value))?;
}

debug_assert_eq!(
Expand All @@ -143,6 +155,8 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F, D> for Wir
"Integer too large to fit in {} many `BaseSumGate`s",
self.gates.len()
);

Ok(())
}

fn serialize(&self, dst: &mut Vec<u8>, _common_data: &CommonCircuitData<F, D>) -> IoResult<()> {
Expand Down
Loading

0 comments on commit 3c32490

Please sign in to comment.