Skip to content

Commit

Permalink
Use cloned structs
Browse files Browse the repository at this point in the history
  • Loading branch information
zpokendev committed Mar 27, 2024
1 parent 53c5bc3 commit 2244a9d
Show file tree
Hide file tree
Showing 21 changed files with 32 additions and 34 deletions.
11 changes: 2 additions & 9 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
# Cargo build
/target
Cargo.lock

# Profile-guided optimization
/tmp
pgo-data.profdata

# MacOS nuisances
.DS_Store
target/
Cargo.lock
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

This repository was originally for Plonky2, a SNARK implementation based on techniques from PLONK and FRI. It has since expanded to include tools such as Starky, a highly performant STARK implementation.

The main change made to the above version of plonky2 is the addition of the Clone attribute to enable deep cloning in WitnessGenerator, including the following structures: SquareRootGenerator, PolynomialBatch, EqualityGenerator, QuotientGeneratorExtension, LowHighGenerator, BaseSumGenerator, SplitGenerator, WireSplitGenerator, BaseSplitGenerator, InterpolationGenerator, ExponentiationGenerator, PoseidonGenerator, RandomAccessGenerator, ReducingGenerator, WitnessGeneratorRef, SimpleGeneratorAdapter, CopyGenerator, RandomValueGenerator, NonzeroTestGenerator, CircuitData, ProverOnlyCircuitData, DummyProofGenerator.

## Documentation

Expand Down
1 change: 1 addition & 0 deletions plonky2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ serde = { workspace = true, features = ["rc"] }
static_assertions = { workspace = true }
unroll = { workspace = true }
web-time = { version = "1.0.0", optional = true }
dyn-clonable = { version = "0.9.0" }

# Local dependencies
plonky2_field = { version = "0.2.1", path = "../field", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion plonky2/examples/square_root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use plonky2_field::extension::Extendable;

/// A generator used by the prover to calculate the square root (`x`) of a given value
/// (`x_squared`), outside of the circuit, in order to supply it as an additional public input.
#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
struct SquareRootGenerator<F: RichField + Extendable<D>, const D: usize> {
x: Target,
x_squared: Target,
Expand Down
2 changes: 1 addition & 1 deletion plonky2/src/fri/oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::util::{log2_strict, reverse_bits, reverse_index_bits_in_place, transp
pub const SALT_SIZE: usize = 4;

/// Represents a FRI oracle, i.e. a batch of polynomials which have been Merklized.
#[derive(Eq, PartialEq, Debug)]
#[derive(Eq, PartialEq, Debug, Clone)]
pub struct PolynomialBatch<F: RichField + Extendable<D>, C: GenericConfig<D, F = F>, const D: usize>
{
pub polynomials: Vec<PolynomialCoeffs<F>>,
Expand Down
2 changes: 1 addition & 1 deletion plonky2/src/gadgets/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
}
}

#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub struct EqualityGenerator {
x: Target,
y: Target,
Expand Down
2 changes: 1 addition & 1 deletion plonky2/src/gadgets/arithmetic_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
}
}

#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub struct QuotientGeneratorExtension<const D: usize> {
numerator: ExtensionTarget<D>,
denominator: ExtensionTarget<D>,
Expand Down
2 changes: 1 addition & 1 deletion plonky2/src/gadgets/range_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
}
}

#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub struct LowHighGenerator {
integer: Target,
n_log: usize,
Expand Down
2 changes: 1 addition & 1 deletion plonky2/src/gadgets/split_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
}
}

#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub struct BaseSumGenerator<const B: usize> {
row: usize,
limbs: Vec<BoolTarget>,
Expand Down
4 changes: 2 additions & 2 deletions plonky2/src/gadgets/split_join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
}
}

#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub struct SplitGenerator {
integer: Target,
bits: Vec<Target>,
Expand Down Expand Up @@ -103,7 +103,7 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F, D> for Spl
}
}

#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub struct WireSplitGenerator {
integer: Target,
gates: Vec<usize>,
Expand Down
2 changes: 1 addition & 1 deletion plonky2/src/gates/base_sum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ impl<F: RichField + Extendable<D>, const D: usize, const B: usize> PackedEvaluab
}
}

#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub struct BaseSplitGenerator<const B: usize> {
row: usize,
num_limbs: usize,
Expand Down
2 changes: 1 addition & 1 deletion plonky2/src/gates/coset_interpolation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ impl<F: RichField + Extendable<D>, const D: usize> Gate<F, D> for CosetInterpola
}
}

#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub struct InterpolationGenerator<F: RichField + Extendable<D>, const D: usize> {
row: usize,
gate: CosetInterpolationGate<F, D>,
Expand Down
2 changes: 1 addition & 1 deletion plonky2/src/gates/exponentiation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ impl<F: RichField + Extendable<D>, const D: usize> PackedEvaluableBase<F, D>
}
}

#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub struct ExponentiationGenerator<F: RichField + Extendable<D>, const D: usize> {
row: usize,
gate: ExponentiationGate<F, D>,
Expand Down
2 changes: 1 addition & 1 deletion plonky2/src/gates/poseidon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ impl<F: RichField + Extendable<D>, const D: usize> Gate<F, D> for PoseidonGate<F
}
}

#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub struct PoseidonGenerator<F: RichField + Extendable<D> + Poseidon, const D: usize> {
row: usize,
_phantom: PhantomData<F>,
Expand Down
2 changes: 1 addition & 1 deletion plonky2/src/gates/random_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ impl<F: RichField + Extendable<D>, const D: usize> PackedEvaluableBase<F, D>
}
}

#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub struct RandomAccessGenerator<F: RichField + Extendable<D>, const D: usize> {
row: usize,
gate: RandomAccessGate<F, D>,
Expand Down
2 changes: 1 addition & 1 deletion plonky2/src/gates/reducing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ impl<F: RichField + Extendable<D>, const D: usize> Gate<F, D> for ReducingGate<D
}
}

#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub struct ReducingGenerator<const D: usize> {
row: usize,
gate: ReducingGate<D>,
Expand Down
2 changes: 1 addition & 1 deletion plonky2/src/gates/reducing_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ impl<F: RichField + Extendable<D>, const D: usize> Gate<F, D> for ReducingExtens
}
}

#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub struct ReducingGenerator<const D: usize> {
row: usize,
gate: ReducingExtensionGate<D>,
Expand Down
15 changes: 9 additions & 6 deletions plonky2/src/iop/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use alloc::{
};
use core::fmt::Debug;
use core::marker::PhantomData;
use dyn_clonable::clonable;

use crate::field::extension::Extendable;
use crate::field::types::Field;
Expand Down Expand Up @@ -103,8 +104,9 @@ pub fn generate_partial_witness<
}

/// A generator participates in the generation of the witness.
#[clonable]
pub trait WitnessGenerator<F: RichField + Extendable<D>, const D: usize>:
'static + Send + Sync + Debug
'static + Send + Sync + Debug + Clone
{
fn id(&self) -> String;

Expand All @@ -126,6 +128,7 @@ pub trait WitnessGenerator<F: RichField + Extendable<D>, const D: usize>:

/// A wrapper around an `Box<WitnessGenerator>` which implements `PartialEq`
/// and `Eq` based on generator IDs.
#[derive(Clone)]
pub struct WitnessGeneratorRef<F: RichField + Extendable<D>, const D: usize>(
pub Box<dyn WitnessGenerator<F, D>>,
);
Expand Down Expand Up @@ -225,7 +228,7 @@ pub trait SimpleGenerator<F: RichField + Extendable<D>, const D: usize>:
Self: Sized;
}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct SimpleGeneratorAdapter<
F: RichField + Extendable<D>,
SG: SimpleGenerator<F, D> + ?Sized,
Expand All @@ -235,7 +238,7 @@ pub struct SimpleGeneratorAdapter<
inner: SG,
}

impl<F: RichField + Extendable<D>, SG: SimpleGenerator<F, D>, const D: usize> WitnessGenerator<F, D>
impl<F: RichField + Extendable<D>, SG: SimpleGenerator<F, D> + Clone, const D: usize> WitnessGenerator<F, D>
for SimpleGeneratorAdapter<F, SG, D>
{
fn id(&self) -> String {
Expand Down Expand Up @@ -268,7 +271,7 @@ impl<F: RichField + Extendable<D>, SG: SimpleGenerator<F, D>, const D: usize> Wi
}

/// A generator which copies one wire to another.
#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub struct CopyGenerator {
pub(crate) src: Target,
pub(crate) dst: Target,
Expand Down Expand Up @@ -301,7 +304,7 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F, D> for Cop
}

/// A generator for including a random value
#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub struct RandomValueGenerator {
pub(crate) target: Target,
}
Expand Down Expand Up @@ -331,7 +334,7 @@ impl<F: RichField + Extendable<D>, const D: usize> SimpleGenerator<F, D> for Ran
}

/// A generator for testing if a value equals zero
#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub struct NonzeroTestGenerator {
pub(crate) to_test: Target,
pub(crate) dummy: Target,
Expand Down
2 changes: 1 addition & 1 deletion plonky2/src/plonk/circuit_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
self.generators.extend(generators);
}

pub fn add_simple_generator<G: SimpleGenerator<F, D>>(&mut self, generator: G) {
pub fn add_simple_generator<G: SimpleGenerator<F, D> + Clone>(&mut self, generator: G) {
self.generators
.push(WitnessGeneratorRef::new(generator.adapter()));
}
Expand Down
4 changes: 2 additions & 2 deletions plonky2/src/plonk/circuit_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ impl<F: RichField + Extendable<D>, C: GenericConfig<D, F = F>, const D: usize>
}

/// Circuit data required by the prover or the verifier.
#[derive(Eq, PartialEq, Debug)]
#[derive(Eq, PartialEq, Debug, Clone)]
pub struct CircuitData<F: RichField + Extendable<D>, C: GenericConfig<D, F = F>, const D: usize> {
pub prover_only: ProverOnlyCircuitData<F, C, D>,
pub verifier_only: VerifierOnlyCircuitData<C, D>,
Expand Down Expand Up @@ -335,7 +335,7 @@ impl<F: RichField + Extendable<D>, C: GenericConfig<D, F = F>, const D: usize>
}

/// Circuit data required by the prover, but not the verifier.
#[derive(Eq, PartialEq, Debug)]
#[derive(Eq, PartialEq, Debug, Clone)]
pub struct ProverOnlyCircuitData<
F: RichField + Extendable<D>,
C: GenericConfig<D, F = F>,
Expand Down
2 changes: 1 addition & 1 deletion plonky2/src/recursion/dummy_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
}
}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct DummyProofGenerator<F, C, const D: usize>
where
F: RichField + Extendable<D>,
Expand Down

0 comments on commit 2244a9d

Please sign in to comment.