Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
nulltea committed Feb 1, 2024
1 parent d453dd8 commit 12f9cc4
Show file tree
Hide file tree
Showing 11 changed files with 228 additions and 199 deletions.
3 changes: 2 additions & 1 deletion lightclient-circuits/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ getset = "0.1.2"

[dev-dependencies]
rstest = "0.18.2"
test-utils = { workspace = true }
# test-utils = { workspace = true }


[features]
default = []
3 changes: 2 additions & 1 deletion lightclient-circuits/src/committee_update_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,8 @@ impl<S: Spec> AppCircuit for CommitteeUpdateCircuit<S, bn256::Fr> {
let mut builder = Eth2CircuitBuilder::<ShaBitGateManager<bn256::Fr>>::from_stage(stage)
.use_k(k)
.use_instance_columns(1);
let range = builder.range_chip(lookup_bits);
builder.set_lookup_bits(lookup_bits);
let range = builder.range_chip();
let fp_chip = FpChip::new(&range, LIMB_BITS, NUM_LIMBS);

let assigned_instances = Self::synthesize(&mut builder, &fp_chip, witness)?;
Expand Down
16 changes: 12 additions & 4 deletions lightclient-circuits/src/gadget/crypto/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::env::set_var;

use crate::util::{CommonGateManager, Eth2ConfigPinning, GateBuilderConfig, PinnableCircuit};
use eth_types::Field;
use getset::Getters;
use getset::{Getters, MutGetters};
use halo2_base::{
gates::{
circuit::{
Expand Down Expand Up @@ -44,7 +44,7 @@ impl<F: Field, GateConfig: GateBuilderConfig<F>> SHAConfig<F, GateConfig> {
}
}

#[derive(Getters)]
#[derive(Clone, Debug, Getters, MutGetters)]
pub struct ShaCircuitBuilder<F: Field, ThreadBuilder: CommonGateManager<F>> {
#[getset(get = "pub", get_mut = "pub")]
pub(crate) sha: ThreadBuilder,
Expand Down Expand Up @@ -151,6 +151,10 @@ impl<F: Field, GateManager: CommonGateManager<F>> ShaCircuitBuilder<F, GateManag
self
}

pub fn break_points(&self) -> MultiPhaseThreadBreakPoints {
self.base.break_points()
}

/// Sets the break points of the circuit.
pub fn set_break_points(&mut self, break_points: MultiPhaseThreadBreakPoints) {
self.base.set_break_points(break_points);
Expand Down Expand Up @@ -180,10 +184,14 @@ impl<F: Field, GateManager: CommonGateManager<F>> ShaCircuitBuilder<F, GateManag
(self.base.main(0), self.sha.custom_context())
}

pub fn range_chip(&mut self, lookup_bits: usize) -> RangeChip<F> {
self.base.set_lookup_bits(lookup_bits);
pub fn range_chip(&mut self) -> RangeChip<F> {
self.base.range_chip()
}

pub fn clear(&mut self) {
self.base.clear();
self.sha.clear();
}
}

impl<F: Field, GateManager: CommonGateManager<F>> CommonCircuitBuilder<F>
Expand Down
2 changes: 1 addition & 1 deletion lightclient-circuits/src/gadget/crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use halo2_ecc::{
bls12_381::{Fp2Chip, Fp2Point, FpChip},
ecc::{EcPoint, EccChip},
};
pub use sha256_flex::{Sha256Chip, ShaContexts, ShaFlexGateManager};
pub use sha256_flex::{Sha256Chip, ShaContexts, ShaFlexGateManager, SpreadConfig};
pub use sha256_wide::{Sha256ChipWide, ShaBitGateManager};

pub use ecc::calculate_ysquared;
Expand Down
7 changes: 5 additions & 2 deletions lightclient-circuits/src/gadget/crypto/sha256_flex/gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// Code: https://github.com/ChainSafe/Spectre
// SPDX-License-Identifier: LGPL-3.0-only

use std::any::TypeId;

use eth_types::Field;
use getset::CopyGetters;
use halo2_base::{
Expand Down Expand Up @@ -113,6 +111,11 @@ impl<F: Field> CommonGateManager<F> for ShaFlexGateManager<F> {
self.use_unknown = use_unknown;
self
}

fn clear(&mut self) {
self.threads_dense.clear();
self.threads_spread.clear();
}
}

impl<F: Field> VirtualRegionManager<F> for ShaFlexGateManager<F> {
Expand Down
5 changes: 5 additions & 0 deletions lightclient-circuits/src/gadget/crypto/sha256_wide/gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ impl<F: Field> CommonGateManager<F> for ShaBitGateManager<F> {
self.use_unknown = use_unknown;
self
}

fn clear(&mut self) {
self.virtual_rows.clear();
self.loaded_blocks.clear();
}
}

impl<F: Field> VirtualRegionManager<F> for ShaBitGateManager<F> {
Expand Down
2 changes: 1 addition & 1 deletion lightclient-circuits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub mod committee_update_circuit;
pub mod sync_step_circuit;

pub mod poseidon;
mod ssz_merkle;
pub mod ssz_merkle;

pub use halo2_base;
pub use halo2_base::halo2_proofs;
Expand Down
21 changes: 15 additions & 6 deletions lightclient-circuits/src/sync_step_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl<S: Spec, F: Field> StepCircuit<S, F> {
builder: &mut ShaCircuitBuilder<F, ShaFlexGateManager<F>>,
fp_chip: &FpChip<F>,
args: &witness::SyncStepArgs<S>,
) -> Result<Vec<AssignedValue<F>>, Error> {
) -> Result<(Vec<AssignedValue<F>>, Vec<AssignedValue<F>>), Error> {
assert!(!args.signature_compressed.is_empty(), "signature expected");

let range = fp_chip.range();
Expand All @@ -76,8 +76,13 @@ impl<S: Spec, F: Field> StepCircuit<S, F> {
let bls_chip = BlsSignatureChip::new(fp_chip, &pairing_chip);
let h2c_chip = HashToCurveChip::new(&sha256_chip, &fp2_chip);

let execution_payload_root: HashInputChunk<QuantumCell<F>> =
args.execution_payload_root.clone().into_witness();
let execution_payload_root_bytes = args
.execution_payload_root
.clone()
.iter()
.map(|v| builder.main().load_witness(F::from(*v as u64)))
.collect_vec();
let execution_payload_root: HashInputChunk<_> = execution_payload_root_bytes.clone().into();

let pubkey_affines = args
.pubkeys_uncompressed
Expand Down Expand Up @@ -217,7 +222,10 @@ impl<S: Spec, F: Field> StepCircuit<S, F> {
truncate_sha256_into_single_elem(builder.main(), range, pub_inputs_bytes)
};

Ok(vec![pub_inputs_commit, poseidon_commit])
Ok((
vec![pub_inputs_commit, poseidon_commit],
execution_payload_root_bytes,
))
}

// Computes public inputs to `StepCircuit` matching the in-circuit logic from `synthesise` method.
Expand Down Expand Up @@ -409,10 +417,11 @@ impl<S: Spec> AppCircuit for StepCircuit<S, bn256::Fr> {
let mut builder = Eth2CircuitBuilder::<ShaFlexGateManager<bn256::Fr>>::from_stage(stage)
.use_k(k)
.use_instance_columns(1);
let range = builder.range_chip(lookup_bits);
builder.set_lookup_bits(lookup_bits);
let range = builder.range_chip();
let fp_chip = FpChip::new(&range, LIMB_BITS, NUM_LIMBS);

let assigned_instances = Self::synthesize(&mut builder, &fp_chip, args)?;
let (assigned_instances, _) = Self::synthesize(&mut builder, &fp_chip, args)?;
builder.set_instances(0, assigned_instances);

match stage {
Expand Down
2 changes: 2 additions & 0 deletions lightclient-circuits/src/util/gates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ pub trait CommonGateManager<F: Field>: VirtualRegionManager<F> + Clone {
fn use_copy_manager(self, copy_manager: SharedCopyConstraintManager<F>) -> Self;

fn unknown(self, use_unknown: bool) -> Self;

fn clear(&mut self);
}
2 changes: 1 addition & 1 deletion lightclient-circuits/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod gates;
pub use gates::*;

mod conversion;
pub(crate) use conversion::*;
pub use conversion::*;

mod circuit;
pub use circuit::*;
Loading

0 comments on commit 12f9cc4

Please sign in to comment.