2
2
// Code: https://github.com/ChainSafe/Spectre
3
3
// SPDX-License-Identifier: LGPL-3.0-only
4
4
5
- use std:: env:: { set_var, var} ;
6
5
use std:: fs;
7
6
use std:: { fs:: File , path:: Path } ;
8
7
@@ -24,18 +23,32 @@ use snark_verifier_sdk::{gen_pk, halo2::gen_snark_shplonk, read_pk};
24
23
use snark_verifier_sdk:: { CircuitExt , Snark } ;
25
24
26
25
/// Halo2 circuit configuration parameters.
27
- pub trait Halo2ConfigPinning : Serialize {
26
+ pub trait Halo2ConfigPinning : Serialize + Sized + for < ' de > Deserialize < ' de > {
27
+ type CircuitParams ;
28
+
28
29
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 ;
33
32
/// Returns break points
34
33
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
+
37
35
/// Degree of the circuit, log_2(number of rows)
38
36
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
+ }
39
52
}
40
53
41
54
#[ derive( Clone , Debug , Serialize , Deserialize ) ]
@@ -45,39 +58,20 @@ pub struct Eth2ConfigPinning {
45
58
}
46
59
47
60
impl Halo2ConfigPinning for Eth2ConfigPinning {
61
+ type CircuitParams = BaseCircuitParams ;
48
62
type BreakPoints = MultiPhaseThreadBreakPoints ;
49
63
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 {
75
65
Self {
76
66
params,
77
67
break_points,
78
68
}
79
69
}
80
70
71
+ fn break_points ( self ) -> MultiPhaseThreadBreakPoints {
72
+ self . break_points
73
+ }
74
+
81
75
fn degree ( & self ) -> u32 {
82
76
self . params . k as u32
83
77
}
@@ -86,13 +80,7 @@ impl Halo2ConfigPinning for Eth2ConfigPinning {
86
80
pub trait PinnableCircuit < F : Field > : CircuitExt < F > {
87
81
type Pinning : Halo2ConfigPinning ;
88
82
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 ;
96
84
}
97
85
98
86
pub trait AppCircuit {
@@ -143,7 +131,7 @@ pub trait AppCircuit {
143
131
let pk = gen_pk ( params, & circuit, Some ( pk_path. as_ref ( ) ) ) ;
144
132
if !pk_exists {
145
133
// should only write pinning data if we created a new pkey
146
- circuit. write_pinning ( pinning_path) ;
134
+ circuit. pinning ( ) . write ( pinning_path) ;
147
135
}
148
136
pk
149
137
}
0 commit comments