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

Feat/build all casper circuits #271

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use plonky2::field::goldilocks_field::GoldilocksField;
use plonky2::field::types::Field;
use plonky2::iop::witness::{PartialWitness, WitnessWrite};
use plonky2::plonk::circuit_builder::CircuitBuilder;
use plonky2::plonk::circuit_data::{CircuitConfig};
use plonky2::plonk::circuit_data::CircuitConfig;
use plonky2::plonk::config::PoseidonGoldilocksConfig;
use serde::Deserialize;
use std::fs::File;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ use plonky2::{
};

use crate::{
biguint::{CircuitBuilderBiguint},
build_validator_balance_circuit::{
set_public_variables, ValidatorBalanceProofTargetsExt,
},
biguint::CircuitBuilderBiguint,
build_validator_balance_circuit::{set_public_variables, ValidatorBalanceProofTargetsExt},
sha256::make_circuits,
targets_serialization::{ReadTargets, WriteTargets},
utils::{ETH_SHA256_BIT_SIZE},
utils::ETH_SHA256_BIT_SIZE,
};

pub struct BalanceInnerCircuitTargets {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use plonky2::{
hash::{poseidon::PoseidonHash},
hash::poseidon::PoseidonHash,
iop::target::{BoolTarget, Target},
plonk::{
circuit_builder::CircuitBuilder,
Expand All @@ -11,12 +11,10 @@ use plonky2::{
};

use crate::{
build_commitment_mapper_first_level_circuit::{
CommitmentMapperProofTargetExt,
},
build_commitment_mapper_first_level_circuit::CommitmentMapperProofTargetExt,
sha256::make_circuits,
targets_serialization::{ReadTargets, WriteTargets},
utils::{ETH_SHA256_BIT_SIZE},
utils::ETH_SHA256_BIT_SIZE,
};

pub struct CommitmentMapperInnerCircuitTargets {
Expand Down
8 changes: 4 additions & 4 deletions beacon-light-client/plonky2/circuits/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
pub mod biguint;
pub mod build_balance_inner_level_circuit;
pub mod build_commitment_mapper_first_level_circuit;
pub mod build_commitment_mapper_inner_level_circuit;
pub mod build_final_circuit;
pub mod build_validator_balance_circuit;
pub mod generator_serializer;
pub mod hash_tree_root;
pub mod hash_tree_root_poseidon;
pub mod is_active_validator;
pub mod is_valid_merkle_branch;
pub mod sha256;
pub mod targets_serialization;
pub mod utils;
pub mod validator_balance_circuit;
pub mod validator_commitment_mapper;
pub mod validator_hash_tree_root;
pub mod validator_hash_tree_root_poseidon;
pub mod build_balance_inner_level_circuit;
pub mod is_active_validator;
pub mod build_final_circuit;
pub mod targets_serialization;
2 changes: 1 addition & 1 deletion beacon-light-client/plonky2/circuits/src/sha256.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use plonky2::field::extension::Extendable;
use plonky2::hash::hash_types::RichField;
use plonky2::iop::target::BoolTarget;
use plonky2::plonk::circuit_builder::CircuitBuilder;
use plonky2::field::extension::Extendable;
use plonky2_u32::gadgets::arithmetic_u32::{CircuitBuilderU32, U32Target};

#[rustfmt::skip]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use plonky2::{
hash_types::{HashOutTarget, RichField},
poseidon::PoseidonHash,
},
iop::target::{BoolTarget},
iop::target::BoolTarget,
plonk::circuit_builder::CircuitBuilder,
util::serialization::{Buffer, IoResult, Read, Write},
};
Expand Down
4 changes: 4 additions & 0 deletions casper-finality-proofs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ path = "bin/weigh_justification_and_finalization.rs"
name = "compute_shuffled_index"
path = "bin/compute_shuffled_index.rs"

[[bin]]
name = "build_circuits"
path = "bin/build_circuits.rs"

[[bin]]
name = "test_engine"
path = "src/test_engine/bin/main.rs"
Expand Down
102 changes: 102 additions & 0 deletions casper-finality-proofs/bin/build_circuits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
use casper_finality_proofs::test_engine::wrappers::{
compute_shuffled_index::wrapper_mainnet::MAINNET_CIRCUIT as circuit_mainnet,
compute_shuffled_index::wrapper_minimal::MINIMAL_CIRCUIT as circuit_minimal,
wrapper_weigh_justification_and_finalization::CIRCUIT as circuit_weigh_justification_and_finalization,
};
use clap::Parser;
use once_cell::sync::Lazy;
use plonky2x::{
backend::circuit::{DefaultParameters, GateRegistry, HintRegistry},
frontend::builder::CircuitBuilder,
};
use std::env::args;
use strum::{Display, EnumIter, EnumString, IntoEnumIterator};
#[derive(Debug, Eq, Hash, PartialEq, Copy, Clone, EnumString, Display, EnumIter)]
#[allow(non_camel_case_types)]
enum Circuits {
compute_shuffled_index_mainnet,
Copy link
Contributor

Choose a reason for hiding this comment

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

Rename these to Pascal case

compute_shuffled_index_minimal,
weigh_justification_and_finalization,
all,
}

impl Circuits {
fn from_str(circuit_as_str: &str) -> Option<Circuits> {
Copy link
Contributor

Choose a reason for hiding this comment

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

EnumString already creates this function for you so this implementation is redundant. Moreover, you can provide casing in it to match the Pascal case of the enum corectly

match circuit_as_str {
"compute_shuffled_index_mainnet" => Some(Circuits::compute_shuffled_index_mainnet),
"compute_shuffled_index_minimal" => Some(Circuits::compute_shuffled_index_minimal),
"weigh_justification_and_finalization" => {
Some(Circuits::weigh_justification_and_finalization)
}
_ => None,
}
}
}

enum OneOrAllCircuits {
OneCircuit(Box<dyn Fn() -> () + Send + Sync>),
AllCircuits(Vec<Box<dyn Fn() -> () + Send + Sync>>),
}

#[derive(Parser, Debug)]
struct CommandLineCircuit {
/// Enter name of circuit
#[clap(value_delimiter = ' ', num_args = 0..)]
name: Circuits,
}

fn build_circuit(circuit: Circuits) -> OneOrAllCircuits {
match circuit {
Circuits::compute_shuffled_index_mainnet => OneOrAllCircuits::OneCircuit(Box::new(|| {
Lazy::force(&circuit_mainnet);
})),
Circuits::compute_shuffled_index_minimal => OneOrAllCircuits::OneCircuit(Box::new(|| {
Lazy::force(&circuit_minimal);
})),
Circuits::weigh_justification_and_finalization => {
OneOrAllCircuits::OneCircuit(Box::new(|| {
Lazy::force(&circuit_weigh_justification_and_finalization);
}))
}
Comment on lines +48 to +60
Copy link
Contributor

Choose a reason for hiding this comment

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

Return type of build_circuit is never used so it can be removed. Then OneOrAllCircuits becomes obsolete and the Box::new expression can be removed. Simply match the enum value and Lazy::force in the body of the matched value

Circuits::all => OneOrAllCircuits::AllCircuits(vec![
Copy link
Contributor

Choose a reason for hiding this comment

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

This is never called

Box::new(|| {
Lazy::force(&circuit_mainnet);
}),
Box::new(|| {
Lazy::force(&circuit_minimal);
}),
Box::new(|| {
Lazy::force(&circuit_weigh_justification_and_finalization);
}),
]),
}
}

fn main() {
type L = DefaultParameters;
const D: usize = 2;
let builder = CircuitBuilder::<DefaultParameters, D>::new();

let circuit = builder.build();
let hint_serializer = HintRegistry::<L, D>::new();
let gate_serializer = GateRegistry::<L, D>::new();
let command_line_circuit: CommandLineCircuit = CommandLineCircuit::parse();
Copy link
Contributor

Choose a reason for hiding this comment

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

Rename CommandLineCircuit to CommandLineOptions

let command_line_arguments: Vec<String> = args().skip(1).collect();

if command_line_circuit.name != Circuits::all {
Comment on lines +83 to +86
Copy link
Contributor

Choose a reason for hiding this comment

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

You are only using command_line_circuit to check if it's all and are not using it to determine which circuits you should build. You could just as well check if args()[1] is "all"

for (_, arg) in command_line_arguments.iter().enumerate() {
Copy link
Contributor

Choose a reason for hiding this comment

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

enumerate is useless if you are not using the index

let arg_as_circuit = Circuits::from_str(&arg);
build_circuit(arg_as_circuit.unwrap());
let path = format!("build/{}", arg_as_circuit.unwrap().to_string());
Copy link
Contributor

Choose a reason for hiding this comment

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

handle the None case

circuit.save(&path, &gate_serializer, &hint_serializer);
}
} else {
for _circuit in Circuits::iter() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove underscore

if _circuit != Circuits::all {
build_circuit(_circuit);
let path = format!("build/{}", _circuit.to_string());
circuit.save(&path, &gate_serializer, &hint_serializer);
}
}
}
}
15 changes: 6 additions & 9 deletions casper-finality-proofs/bin/compute_shuffled_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,18 @@ use casper_finality_proofs::compute_shuffled_index::circuit::define;
use plonky2x::prelude::{
bytes, ArrayVariable, ByteVariable, CircuitBuilder, DefaultParameters, U64Variable,
};
use plonky2x::utils;

fn main() {
utils::setup_logger();
const D: usize = 2;
let mut builder = CircuitBuilder::<DefaultParameters, D>::new();
const SHUFFLE_ROUND_COUNT: u8 = 90;
define(&mut builder, SHUFFLE_ROUND_COUNT);

let seed_bytes: Vec<u8> =
bytes!("0x4ac96f664a6cafd300b161720809b9e17905d4d8fed7a97ff89cf0080a953fe7");

let seed_bytes_fixed_size: [u8; 32] = seed_bytes.try_into().unwrap();

const SHUFFLE_ROUND_COUNT: u8 = 90;
let mut builder = CircuitBuilder::<DefaultParameters, 2>::new();
define(&mut builder, SHUFFLE_ROUND_COUNT);

let circuit = builder.mock_build();
let circuit = builder.build();

const START_IDX: u64 = 0;
const COUNT: u64 = 100;
Expand All @@ -34,7 +31,7 @@ fn main() {
input.write::<U64Variable>(COUNT);
input.write::<ArrayVariable<ByteVariable, 32>>(seed_bytes_fixed_size.to_vec());

let (_witness, mut _output) = circuit.mock_prove(&input);
let (_witness, mut _output) = circuit.prove(&input);
let shuffled_index_res = _output.read::<U64Variable>();

println!("{} {}", mapping[i as usize], shuffled_index_res);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ fn main() {
let mut builder = CircuitBuilder::<L, D>::new();
WeighJustificationAndFinalization::define(&mut builder);
let circuit = builder.build();

let mut input = circuit.input();

let beacon_state_root =
Expand Down
2 changes: 1 addition & 1 deletion casper-finality-proofs/src/compute_shuffled_index/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod circuit;

mod helpers;
mod helpers;