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: fix serializer for u32 gates and get validator via dynamic index #108

Merged
merged 13 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 4 additions & 4 deletions plonky2x/src/backend/circuit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ use plonky2::iop::witness::PartialWitness;
use plonky2::plonk::circuit_data::CircuitData;
use plonky2::plonk::config::{AlgebraicHasher, GenericConfig};
use plonky2::plonk::proof::ProofWithPublicInputs;
use plonky2::util::serialization::{Buffer, DefaultGateSerializer, IoResult, Read, Write};
use plonky2::util::serialization::{Buffer, IoResult, Read, Write};

use self::io::{CircuitInput, CircuitOutput};
use self::utils::CustomGeneratorSerializer;
use self::utils::{CustomGateSerializer, CustomGeneratorSerializer};
use crate::frontend::builder::io::{EvmIO, FieldIO};
use crate::frontend::builder::CircuitIO;
use crate::prelude::{ByteVariable, CircuitVariable, Variable};
Expand Down Expand Up @@ -97,8 +97,8 @@ where
self.data.verify(proof.clone()).unwrap();
}

fn serializers() -> (DefaultGateSerializer, CustomGeneratorSerializer<C, D>) {
let gate_serializer = DefaultGateSerializer;
fn serializers() -> (CustomGateSerializer, CustomGeneratorSerializer<C, D>) {
let gate_serializer = CustomGateSerializer;
let generator_serializer = CustomGeneratorSerializer::<C, D> {
_phantom: PhantomData,
};
Expand Down
74 changes: 56 additions & 18 deletions plonky2x/src/backend/circuit/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,43 @@ use plonky2::gadgets::arithmetic_extension::QuotientGeneratorExtension;
use plonky2::gadgets::range_check::LowHighGenerator;
use plonky2::gadgets::split_base::BaseSumGenerator;
use plonky2::gadgets::split_join::{SplitGenerator, WireSplitGenerator};
use plonky2::gates::arithmetic_base::ArithmeticBaseGenerator;
use plonky2::gates::arithmetic_extension::ArithmeticExtensionGenerator;
use plonky2::gates::base_sum::BaseSplitGenerator;
use plonky2::gates::coset_interpolation::InterpolationGenerator;
use plonky2::gates::exponentiation::ExponentiationGenerator;
use plonky2::gates::lookup::LookupGenerator;
use plonky2::gates::lookup_table::LookupTableGenerator;
use plonky2::gates::multiplication_extension::MulExtensionGenerator;
use plonky2::gates::poseidon::PoseidonGenerator;
use plonky2::gates::poseidon_mds::PoseidonMdsGenerator;
use plonky2::gates::random_access::RandomAccessGenerator;
use plonky2::gates::reducing::ReducingGenerator;
use plonky2::gates::reducing_extension::ReducingGenerator as ReducingExtensionGenerator;
use plonky2::gates::arithmetic_base::{ArithmeticBaseGenerator, ArithmeticGate};
use plonky2::gates::arithmetic_extension::{ArithmeticExtensionGate, ArithmeticExtensionGenerator};
use plonky2::gates::base_sum::{BaseSplitGenerator, BaseSumGate};
use plonky2::gates::constant::ConstantGate;
use plonky2::gates::coset_interpolation::{CosetInterpolationGate, InterpolationGenerator};
use plonky2::gates::exponentiation::{ExponentiationGate, ExponentiationGenerator};
use plonky2::gates::lookup::{LookupGate, LookupGenerator};
use plonky2::gates::lookup_table::{LookupTableGate, LookupTableGenerator};
use plonky2::gates::multiplication_extension::{MulExtensionGate, MulExtensionGenerator};
use plonky2::gates::noop::NoopGate;
use plonky2::gates::poseidon::{PoseidonGate, PoseidonGenerator};
use plonky2::gates::poseidon_mds::{PoseidonMdsGate, PoseidonMdsGenerator};
use plonky2::gates::public_input::PublicInputGate;
use plonky2::gates::random_access::{RandomAccessGate, RandomAccessGenerator};
use plonky2::gates::reducing::{ReducingGate, ReducingGenerator};
use plonky2::gates::reducing_extension::{
ReducingExtensionGate, ReducingGenerator as ReducingExtensionGenerator,
};
use plonky2::hash::hash_types::RichField;
use plonky2::iop::generator::{
ConstantGenerator, CopyGenerator, NonzeroTestGenerator, RandomValueGenerator,
};
use plonky2::plonk::circuit_data::CommonCircuitData;
use plonky2::plonk::config::{AlgebraicHasher, GenericConfig};
use plonky2::recursion::dummy_circuit::DummyProofGenerator;
use plonky2::util::serialization::{Buffer, IoResult, WitnessGeneratorSerializer};
use plonky2::util::serialization::{Buffer, GateSerializer, IoResult, WitnessGeneratorSerializer};
use plonky2::{get_gate_tag_impl, impl_gate_serializer, read_gate_impl};

use crate::frontend::eth::beacon::generators::balance::BeaconValidatorBalanceGenerator;
use crate::frontend::eth::beacon::generators::validator::BeaconValidatorGenerator;
use crate::frontend::eth::beacon::generators::validators::BeaconValidatorsRootGenerator;
use crate::frontend::eth::storage::generators::block::EthBlockGenerator;
use crate::frontend::eth::storage::generators::storage::{
EthLogGenerator, EthStorageKeyGenerator, EthStorageProofGenerator,
};
use crate::frontend::hash::keccak::keccak256::Keccack256Generator;
use crate::frontend::hash::keccak::keccak256::Keccak256Generator;
use crate::frontend::num::u32::gates::add_many_u32::{U32AddManyGate, U32AddManyGenerator};

#[macro_export]
macro_rules! impl_generator_serializer {
Expand Down Expand Up @@ -76,8 +86,7 @@ macro_rules! get_generator_tag_impl {
Ok(tag)
} else)*
{
log::log!(log::Level::Error, "attempted to serialize generator with id {} which is unsupported by this generator serializer", $generator.0.id());
Err(plonky2::util::serialization::IoError)
panic!("attempted to serialize generator with id {} which is unsupported by this generator serializer", $generator.0.id());
}
}};
}
Expand Down Expand Up @@ -147,6 +156,35 @@ where
EthLogGenerator<F, D>, "EthLogGenerator",
EthBlockGenerator<F, D>, "EthBlockGenerator",
EthStorageKeyGenerator<F, D>, "EthStorageKeyGenerator",
Keccack256Generator<F, D>, "Keccak256Generator"
Keccak256Generator<F, D>, "Keccak256Generator",
BeaconValidatorBalanceGenerator<F, D>, "BeaconValidatorBalanceGenerator",
BeaconValidatorGenerator<F, D>, "BeaconValidatorGenerator",
BeaconValidatorsRootGenerator<F, D>, "BeaconValidatorsGenerator",
U32AddManyGenerator<F, D>, "U32AddManyGenerator",
}
}

pub struct CustomGateSerializer;

impl<F: RichField + Extendable<D>, const D: usize> GateSerializer<F, D> for CustomGateSerializer {
impl_gate_serializer! {
CustomGateSerializer,
ArithmeticGate,
ArithmeticExtensionGate<D>,
BaseSumGate<2>,
ConstantGate,
CosetInterpolationGate<F, D>,
ExponentiationGate<F, D>,
LookupGate,
LookupTableGate,
MulExtensionGate<D>,
NoopGate,
PoseidonMdsGate<F, D>,
PoseidonGate<F, D>,
PublicInputGate,
RandomAccessGate<F, D>,
ReducingExtensionGate<D>,
ReducingGate<D>,
U32AddManyGate<F, D>
}
}
2 changes: 1 addition & 1 deletion plonky2x/src/frontend/builder/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl<F: RichField + Extendable<D>, V: CircuitVariable, const D: usize> SimpleGen
for WatchGenerator<V>
{
fn id(&self) -> String {
todo!()
"WatchGenerator".to_string()
}

fn dependencies(&self) -> Vec<Target> {
Expand Down
65 changes: 53 additions & 12 deletions plonky2x/src/frontend/eth/beacon/builder.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use plonky2::field::extension::Extendable;
use plonky2::hash::hash_types::RichField;

use super::generators::balance::BeaconValidatorBalanceGenerator;
use super::generators::validator::BeaconValidatorGenerator;
use super::vars::{BeaconValidatorVariable, BeaconValidatorsVariable};
use crate::frontend::builder::CircuitBuilder;
use crate::frontend::eth::beacon::generators::validators::BeaconValidatorsRootGenerator;
use crate::frontend::uint::uint256::U256Variable;
use crate::frontend::vars::Bytes32Variable;
use crate::prelude::Variable;

impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
/// Get the validators for a given block root.
Expand All @@ -25,7 +28,24 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
}
}

/// Get a validator from a given index.
/// Get a beacon validator from a given dynamic index.
pub fn get_beacon_validator(
&mut self,
validators: BeaconValidatorsVariable,
index: Variable,
) -> BeaconValidatorVariable {
let generator = BeaconValidatorGenerator::new(
self,
validators.block_root,
validators.validators_root,
None,
Some(index),
);
self.add_simple_generator(&generator);
generator.validator
}

/// Get a validator from a given deterministic index.
pub fn get_beacon_validator_from_u64(
&mut self,
validators: BeaconValidatorsVariable,
Expand All @@ -35,31 +55,50 @@ impl<F: RichField + Extendable<D>, const D: usize> CircuitBuilder<F, D> {
self,
validators.block_root,
validators.validators_root,
index,
Some(index),
None,
);
self.add_simple_generator(&generator);
generator.validator
}

/// Get a validator balance from a given deterministic index.
pub fn get_beacon_validator_balance(
&mut self,
validators: BeaconValidatorsVariable,
index: Variable,
) -> U256Variable {
let generator = BeaconValidatorBalanceGenerator::new(
self,
validators.block_root,
validators.validators_root,
None,
Some(index),
);
self.add_simple_generator(&generator);
generator.balance
}
}

#[cfg(test)]
pub(crate) mod tests {
use std::env;

use itertools::Itertools;
use curta::math::prelude::Field;
use plonky2::field::goldilocks_field::GoldilocksField;
use plonky2::iop::witness::PartialWitness;
use plonky2::plonk::config::PoseidonGoldilocksConfig;

use crate::frontend::builder::CircuitBuilder;
use crate::frontend::vars::Bytes32Variable;
use crate::prelude::Variable;
use crate::utils::bytes32;
use crate::utils::eth::beacon::BeaconClient;
use crate::utils::{bytes32, setup_logger};

#[test]
#[cfg_attr(feature = "ci", ignore)]
fn test_get_validator_generator() {
setup_logger();
env_logger::init();
dotenv::dotenv().ok();

type F = GoldilocksField;
Expand All @@ -78,13 +117,15 @@ pub(crate) mod tests {
));
let validators = builder.get_beacon_validators(block_root);

let balances = (0..4)
.map(|i| {
let validator = builder.get_beacon_validator_from_u64(validators, i);
validator.effective_balance
})
.collect_vec();
println!("balances: {:?}", balances);
(0..1).for_each(|i| {
builder.get_beacon_validator_from_u64(validators, i);
});

(0..1).for_each(|i| {
let idx = builder.constant::<Variable>(F::from_canonical_u64(i));
builder.get_beacon_validator(validators, idx);
builder.get_beacon_validator_balance(validators, idx);
});

let circuit = builder.build::<C>();
let pw = PartialWitness::new();
Expand Down
Loading
Loading