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

Implement Clone trait for CircuitBuilder #26

Merged
merged 7 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions plonky2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ plonky2_field = { version = "0.2.0", path = "../field", default-features = false
plonky2_maybe_rayon = { version = "0.2.0", path = "../maybe_rayon", default-features = false }
plonky2_util = { version = "0.2.0", path = "../util", default-features = false }
cryptography_cuda ={git="ssh://[email protected]/okx/cryptography_cuda.git", rev="d44b861cf241d27688525e7a8f082199a2abcbfa", optional=true, features=["no_cuda"]}
dyn-clone = "1.0.17"

[target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dependencies]
getrandom = { version = "0.2", default-features = false, features = ["js"] }
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/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 @@ -238,7 +238,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 @@ -400,7 +400,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 @@ -312,7 +312,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/high_degree_interpolation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ impl<F: RichField + Extendable<D>, const D: usize> Gate<F, D>
}
}

#[derive(Debug)]
#[derive(Debug, Clone)]
struct InterpolationGenerator<F: RichField + Extendable<D>, const D: usize> {
row: usize,
gate: HighDegreeInterpolationGate<F, D>,
Expand Down
2 changes: 1 addition & 1 deletion plonky2/src/gates/low_degree_interpolation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ function two_adic_subgroup(i) {{
}
}

#[derive(Debug)]
#[derive(Debug, Clone)]
struct InterpolationGenerator<F: RichField + Extendable<D>, const D: usize> {
row: usize,
gate: LowDegreeInterpolationGate<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 @@ -717,7 +717,7 @@ function MDS_MATRIX_DIAG(i) {{
}
}

#[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 @@ -467,7 +467,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 @@ -257,7 +257,7 @@ function r_wires_accs_start(i, num_coeffs) {{
}
}

#[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 @@ -254,7 +254,7 @@ function re_wires_accs_start(i, num_coeffs) {{
}
}

#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub struct ReducingGenerator<const D: usize> {
row: usize,
gate: ReducingExtensionGate<D>,
Expand Down
24 changes: 17 additions & 7 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_clone::DynClone;

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.
pub trait WitnessGenerator<F: RichField + Extendable<D>, const D: usize>:
'static + Send + Sync + Debug
pub trait WitnessGenerator<F, const D: usize>: 'static + Send + Sync + Debug + DynClone
where
F: RichField + Extendable<D>,
Comment on lines +108 to +110
Copy link
Collaborator Author

@doutv doutv Apr 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add DynClone trait requirement

{
fn id(&self) -> String;

Expand All @@ -123,13 +125,21 @@ pub trait WitnessGenerator<F: RichField + Extendable<D>, const D: usize>:
where
Self: Sized;
}
dyn_clone::clone_trait_object!(<F, const D: usize> WitnessGenerator<F, D> where F: RichField + Extendable<D>);

/// 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>>,
);
Comment on lines +129 to 136
Copy link
Collaborator Author

@doutv doutv Apr 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dyn_clone::clone_trait_object! Implement Clone trait for WitnessGenerator
Then WitnessGeneratorRef can automatically derive Clone trait


// impl<F: RichField + Extendable<D>, const D: usize> Clone for WitnessGeneratorRef<F, D> {
// fn clone(&self) -> Self {
// Self(dyn_clone::clone_box(&self.0))
// }
// }

impl<F: RichField + Extendable<D>, const D: usize> WitnessGeneratorRef<F, D> {
pub fn new<G: WitnessGenerator<F, D>>(generator: G) -> WitnessGeneratorRef<F, D> {
WitnessGeneratorRef(Box::new(generator))
Expand Down Expand Up @@ -200,7 +210,7 @@ impl<F: Field> GeneratedValues<F> {

/// A generator which runs once after a list of dependencies is present in the witness.
pub trait SimpleGenerator<F: RichField + Extendable<D>, const D: usize>:
'static + Send + Sync + Debug
'static + Send + Sync + Debug + Clone
{
fn id(&self) -> String;

Expand All @@ -225,7 +235,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 Down Expand Up @@ -268,7 +278,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 +311,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 +341,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 @@ -136,7 +136,7 @@ pub struct LookupWire {
/// // Verify the proof
/// assert!(circuit_data.verify(proof).is_ok());
/// ```
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct CircuitBuilder<F: RichField + Extendable<D>, const D: usize> {
/// Circuit configuration to be used by this [`CircuitBuilder`].
pub config: CircuitConfig,
Expand Down
2 changes: 1 addition & 1 deletion plonky2/src/plonk/copy_constraint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use alloc::string::String;
use crate::iop::target::Target;

/// A named copy constraint.
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct CopyConstraint {
pub pair: (Target, Target),
pub name: String,
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
2 changes: 1 addition & 1 deletion plonky2/src/util/context_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use alloc::{
use log::{log, Level};

/// The hierarchy of contexts, and the gate count contributed by each one. Useful for debugging.
#[derive(Debug)]
#[derive(Debug, Clone)]
pub(crate) struct ContextTree {
/// The name of this scope.
name: String,
Expand Down