Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
nulltea committed Mar 6, 2024
1 parent 4cc22ed commit da2207f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 73 deletions.
43 changes: 14 additions & 29 deletions lightclient-circuits/src/aggregation_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use halo2_base::{
gates::{circuit::CircuitBuilderStage, flex_gate::MultiPhaseThreadBreakPoints},
halo2_proofs::{
halo2curves::bn256::{Bn256, Fr},
plonk::Error,
plonk::{Circuit, Error},
poly::{commitment::Params, kzg::commitment::ParamsKZG},
},
};
Expand All @@ -16,11 +16,7 @@ use snark_verifier_sdk::{
halo2::aggregation::{AggregationCircuit, AggregationConfigParams},
Snark, SHPLONK,
};
use std::{
env::{set_var, var},
fs::File,
path::Path,
};
use std::{env::set_var, fs::File, path::Path};

/// Configuration for the aggregation circuit.
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
Expand All @@ -43,39 +39,28 @@ impl AggregationConfigPinning {
}

impl Halo2ConfigPinning for AggregationConfigPinning {
type CircuitParams = AggregationConfigParams;
type BreakPoints = MultiPhaseThreadBreakPoints;

fn new(params: Self::CircuitParams, break_points: Self::BreakPoints) -> Self {
Self {
params,
break_points,
}
}

fn from_path<P: AsRef<Path>>(path: P) -> Self {
let pinning: Self = serde_json::from_reader(
serde_json::from_reader(
File::open(&path)
.unwrap_or_else(|e| panic!("{:?} does not exist: {e:?}", path.as_ref())),
)
.unwrap();
pinning.set_var();
pinning
}

fn set_var(&self) {
set_var(
"AGG_CONFIG_PARAMS",
serde_json::to_string(&self.params).unwrap(),
);
set_var("LOOKUP_BITS", (self.params.degree - 1).to_string());
.unwrap()
}

fn break_points(self) -> MultiPhaseThreadBreakPoints {
self.break_points
}

fn from_var(break_points: MultiPhaseThreadBreakPoints) -> Self {
let params: AggregationConfigParams =
serde_json::from_str(&var("AGG_CONFIG_PARAMS").unwrap()).unwrap();
Self {
params,
break_points,
}
}

fn degree(&self) -> u32 {
self.params.degree
}
Expand All @@ -84,8 +69,8 @@ impl Halo2ConfigPinning for AggregationConfigPinning {
impl PinnableCircuit<Fr> for AggregationCircuit {
type Pinning = AggregationConfigPinning;

fn break_points(&self) -> MultiPhaseThreadBreakPoints {
AggregationCircuit::break_points(self)
fn pinning(&self) -> Self::Pinning {
<AggregationConfigPinning as Halo2ConfigPinning>::new(self.params(), self.break_points())
}
}

Expand Down
6 changes: 3 additions & 3 deletions lightclient-circuits/src/gadget/crypto/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use std::env::set_var;

use crate::util::{CommonGateManager, Eth2ConfigPinning, GateBuilderConfig, PinnableCircuit};
use crate::util::{CommonGateManager, Eth2ConfigPinning, GateBuilderConfig, Halo2ConfigPinning, PinnableCircuit};
use eth_types::Field;
use getset::Getters;
use halo2_base::{
Expand Down Expand Up @@ -280,7 +280,7 @@ where
{
type Pinning = Eth2ConfigPinning;

fn break_points(&self) -> MultiPhaseThreadBreakPoints {
self.base.break_points()
fn pinning(&self) -> Self::Pinning {
Eth2ConfigPinning::new(self.params(), self.base.break_points())
}
}
70 changes: 29 additions & 41 deletions lightclient-circuits/src/util/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Code: https://github.com/ChainSafe/Spectre
// SPDX-License-Identifier: LGPL-3.0-only

use std::env::{set_var, var};
use std::fs;
use std::{fs::File, path::Path};

Expand All @@ -24,18 +23,32 @@ use snark_verifier_sdk::{gen_pk, halo2::gen_snark_shplonk, read_pk};
use snark_verifier_sdk::{CircuitExt, Snark};

/// Halo2 circuit configuration parameters.
pub trait Halo2ConfigPinning: Serialize {
pub trait Halo2ConfigPinning: Serialize + Sized + for<'de> Deserialize<'de> {
type CircuitParams;

type BreakPoints;
/// Loads configuration parameters from a file and sets environmental variables.
fn from_path<P: AsRef<Path>>(path: P) -> Self;
/// Loads configuration parameters into environment variables.
fn set_var(&self);

fn new(params: Self::CircuitParams, break_points: Self::BreakPoints) -> Self;
/// Returns break points
fn break_points(self) -> Self::BreakPoints;
/// Constructs `Self` from environmental variables and break points
fn from_var(break_points: Self::BreakPoints) -> Self;

/// Degree of the circuit, log_2(number of rows)
fn degree(&self) -> u32;

/// Loads configuration parameters from a file and sets environmental variables.
fn from_path<P: AsRef<Path>>(path: P) -> Self {
serde_json::from_reader(
File::open(&path)
.unwrap_or_else(|e| panic!("{:?} does not exist: {e:?}", path.as_ref())),
)
.unwrap()
}

/// Writes to file
fn write<P: AsRef<Path>>(&self, path: P) {
serde_json::to_writer_pretty(File::create(path).unwrap(), self)
.expect("failed to serialize to file");
}
}

#[derive(Clone, Debug, Serialize, Deserialize)]
Expand All @@ -45,39 +58,20 @@ pub struct Eth2ConfigPinning {
}

impl Halo2ConfigPinning for Eth2ConfigPinning {
type CircuitParams = BaseCircuitParams;
type BreakPoints = MultiPhaseThreadBreakPoints;

fn from_path<P: AsRef<Path>>(path: P) -> Self {
let pinning: Self = serde_json::from_reader(
File::open(&path)
.unwrap_or_else(|e| panic!("{:?} does not exist: {e:?}", path.as_ref())),
)
.unwrap();
pinning.set_var();
pinning
}

fn set_var(&self) {
set_var(
"GATE_CONFIG_PARAMS",
serde_json::to_string(&self.params).unwrap(),
);
set_var("LOOKUP_BITS", (self.params.k - 1).to_string());
}

fn break_points(self) -> MultiPhaseThreadBreakPoints {
self.break_points
}

fn from_var(break_points: MultiPhaseThreadBreakPoints) -> Self {
let params: BaseCircuitParams =
serde_json::from_str(&var("GATE_CONFIG_PARAMS").unwrap()).unwrap();
fn new(params: Self::CircuitParams, break_points: Self::BreakPoints) -> Self {
Self {
params,
break_points,
}
}

fn break_points(self) -> MultiPhaseThreadBreakPoints {
self.break_points
}

fn degree(&self) -> u32 {
self.params.k as u32
}
Expand All @@ -86,13 +80,7 @@ impl Halo2ConfigPinning for Eth2ConfigPinning {
pub trait PinnableCircuit<F: Field>: CircuitExt<F> {
type Pinning: Halo2ConfigPinning;

fn break_points(&self) -> <Self::Pinning as Halo2ConfigPinning>::BreakPoints;

fn write_pinning(&self, path: impl AsRef<Path>) {
let break_points = self.break_points();
let pinning: Self::Pinning = Halo2ConfigPinning::from_var(break_points);
serde_json::to_writer_pretty(File::create(path).unwrap(), &pinning).unwrap();
}
fn pinning(&self) -> Self::Pinning;
}

pub trait AppCircuit {
Expand Down Expand Up @@ -143,7 +131,7 @@ pub trait AppCircuit {
let pk = gen_pk(params, &circuit, Some(pk_path.as_ref()));
if !pk_exists {
// should only write pinning data if we created a new pkey
circuit.write_pinning(pinning_path);
circuit.pinning().write(pinning_path);
}
pk
}
Expand Down

0 comments on commit da2207f

Please sign in to comment.