Skip to content

Commit da2207f

Browse files
committed
fix
1 parent 4cc22ed commit da2207f

File tree

3 files changed

+46
-73
lines changed

3 files changed

+46
-73
lines changed

lightclient-circuits/src/aggregation_circuit.rs

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use halo2_base::{
77
gates::{circuit::CircuitBuilderStage, flex_gate::MultiPhaseThreadBreakPoints},
88
halo2_proofs::{
99
halo2curves::bn256::{Bn256, Fr},
10-
plonk::Error,
10+
plonk::{Circuit, Error},
1111
poly::{commitment::Params, kzg::commitment::ParamsKZG},
1212
},
1313
};
@@ -16,11 +16,7 @@ use snark_verifier_sdk::{
1616
halo2::aggregation::{AggregationCircuit, AggregationConfigParams},
1717
Snark, SHPLONK,
1818
};
19-
use std::{
20-
env::{set_var, var},
21-
fs::File,
22-
path::Path,
23-
};
19+
use std::{env::set_var, fs::File, path::Path};
2420

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

4541
impl Halo2ConfigPinning for AggregationConfigPinning {
42+
type CircuitParams = AggregationConfigParams;
4643
type BreakPoints = MultiPhaseThreadBreakPoints;
4744

45+
fn new(params: Self::CircuitParams, break_points: Self::BreakPoints) -> Self {
46+
Self {
47+
params,
48+
break_points,
49+
}
50+
}
51+
4852
fn from_path<P: AsRef<Path>>(path: P) -> Self {
49-
let pinning: Self = serde_json::from_reader(
53+
serde_json::from_reader(
5054
File::open(&path)
5155
.unwrap_or_else(|e| panic!("{:?} does not exist: {e:?}", path.as_ref())),
5256
)
53-
.unwrap();
54-
pinning.set_var();
55-
pinning
56-
}
57-
58-
fn set_var(&self) {
59-
set_var(
60-
"AGG_CONFIG_PARAMS",
61-
serde_json::to_string(&self.params).unwrap(),
62-
);
63-
set_var("LOOKUP_BITS", (self.params.degree - 1).to_string());
57+
.unwrap()
6458
}
6559

6660
fn break_points(self) -> MultiPhaseThreadBreakPoints {
6761
self.break_points
6862
}
6963

70-
fn from_var(break_points: MultiPhaseThreadBreakPoints) -> Self {
71-
let params: AggregationConfigParams =
72-
serde_json::from_str(&var("AGG_CONFIG_PARAMS").unwrap()).unwrap();
73-
Self {
74-
params,
75-
break_points,
76-
}
77-
}
78-
7964
fn degree(&self) -> u32 {
8065
self.params.degree
8166
}
@@ -84,8 +69,8 @@ impl Halo2ConfigPinning for AggregationConfigPinning {
8469
impl PinnableCircuit<Fr> for AggregationCircuit {
8570
type Pinning = AggregationConfigPinning;
8671

87-
fn break_points(&self) -> MultiPhaseThreadBreakPoints {
88-
AggregationCircuit::break_points(self)
72+
fn pinning(&self) -> Self::Pinning {
73+
<AggregationConfigPinning as Halo2ConfigPinning>::new(self.params(), self.break_points())
8974
}
9075
}
9176

lightclient-circuits/src/gadget/crypto/builder.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use std::env::set_var;
66

7-
use crate::util::{CommonGateManager, Eth2ConfigPinning, GateBuilderConfig, PinnableCircuit};
7+
use crate::util::{CommonGateManager, Eth2ConfigPinning, GateBuilderConfig, Halo2ConfigPinning, PinnableCircuit};
88
use eth_types::Field;
99
use getset::Getters;
1010
use halo2_base::{
@@ -280,7 +280,7 @@ where
280280
{
281281
type Pinning = Eth2ConfigPinning;
282282

283-
fn break_points(&self) -> MultiPhaseThreadBreakPoints {
284-
self.base.break_points()
283+
fn pinning(&self) -> Self::Pinning {
284+
Eth2ConfigPinning::new(self.params(), self.base.break_points())
285285
}
286286
}

lightclient-circuits/src/util/circuit.rs

Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Code: https://github.com/ChainSafe/Spectre
33
// SPDX-License-Identifier: LGPL-3.0-only
44

5-
use std::env::{set_var, var};
65
use std::fs;
76
use std::{fs::File, path::Path};
87

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

2625
/// Halo2 circuit configuration parameters.
27-
pub trait Halo2ConfigPinning: Serialize {
26+
pub trait Halo2ConfigPinning: Serialize + Sized + for<'de> Deserialize<'de> {
27+
type CircuitParams;
28+
2829
type BreakPoints;
29-
/// Loads configuration parameters from a file and sets environmental variables.
30-
fn from_path<P: AsRef<Path>>(path: P) -> Self;
31-
/// Loads configuration parameters into environment variables.
32-
fn set_var(&self);
30+
31+
fn new(params: Self::CircuitParams, break_points: Self::BreakPoints) -> Self;
3332
/// Returns break points
3433
fn break_points(self) -> Self::BreakPoints;
35-
/// Constructs `Self` from environmental variables and break points
36-
fn from_var(break_points: Self::BreakPoints) -> Self;
34+
3735
/// Degree of the circuit, log_2(number of rows)
3836
fn degree(&self) -> u32;
37+
38+
/// Loads configuration parameters from a file and sets environmental variables.
39+
fn from_path<P: AsRef<Path>>(path: P) -> Self {
40+
serde_json::from_reader(
41+
File::open(&path)
42+
.unwrap_or_else(|e| panic!("{:?} does not exist: {e:?}", path.as_ref())),
43+
)
44+
.unwrap()
45+
}
46+
47+
/// Writes to file
48+
fn write<P: AsRef<Path>>(&self, path: P) {
49+
serde_json::to_writer_pretty(File::create(path).unwrap(), self)
50+
.expect("failed to serialize to file");
51+
}
3952
}
4053

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

4760
impl Halo2ConfigPinning for Eth2ConfigPinning {
61+
type CircuitParams = BaseCircuitParams;
4862
type BreakPoints = MultiPhaseThreadBreakPoints;
4963

50-
fn from_path<P: AsRef<Path>>(path: P) -> Self {
51-
let pinning: Self = serde_json::from_reader(
52-
File::open(&path)
53-
.unwrap_or_else(|e| panic!("{:?} does not exist: {e:?}", path.as_ref())),
54-
)
55-
.unwrap();
56-
pinning.set_var();
57-
pinning
58-
}
59-
60-
fn set_var(&self) {
61-
set_var(
62-
"GATE_CONFIG_PARAMS",
63-
serde_json::to_string(&self.params).unwrap(),
64-
);
65-
set_var("LOOKUP_BITS", (self.params.k - 1).to_string());
66-
}
67-
68-
fn break_points(self) -> MultiPhaseThreadBreakPoints {
69-
self.break_points
70-
}
71-
72-
fn from_var(break_points: MultiPhaseThreadBreakPoints) -> Self {
73-
let params: BaseCircuitParams =
74-
serde_json::from_str(&var("GATE_CONFIG_PARAMS").unwrap()).unwrap();
64+
fn new(params: Self::CircuitParams, break_points: Self::BreakPoints) -> Self {
7565
Self {
7666
params,
7767
break_points,
7868
}
7969
}
8070

71+
fn break_points(self) -> MultiPhaseThreadBreakPoints {
72+
self.break_points
73+
}
74+
8175
fn degree(&self) -> u32 {
8276
self.params.k as u32
8377
}
@@ -86,13 +80,7 @@ impl Halo2ConfigPinning for Eth2ConfigPinning {
8680
pub trait PinnableCircuit<F: Field>: CircuitExt<F> {
8781
type Pinning: Halo2ConfigPinning;
8882

89-
fn break_points(&self) -> <Self::Pinning as Halo2ConfigPinning>::BreakPoints;
90-
91-
fn write_pinning(&self, path: impl AsRef<Path>) {
92-
let break_points = self.break_points();
93-
let pinning: Self::Pinning = Halo2ConfigPinning::from_var(break_points);
94-
serde_json::to_writer_pretty(File::create(path).unwrap(), &pinning).unwrap();
95-
}
83+
fn pinning(&self) -> Self::Pinning;
9684
}
9785

9886
pub trait AppCircuit {
@@ -143,7 +131,7 @@ pub trait AppCircuit {
143131
let pk = gen_pk(params, &circuit, Some(pk_path.as_ref()));
144132
if !pk_exists {
145133
// should only write pinning data if we created a new pkey
146-
circuit.write_pinning(pinning_path);
134+
circuit.pinning().write(pinning_path);
147135
}
148136
pk
149137
}

0 commit comments

Comments
 (0)