This repository has been archived by the owner on Jul 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathfs.rs
211 lines (178 loc) · 5.91 KB
/
fs.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use halo2_proofs::{
arithmetic::{BaseExt, CurveAffine, MultiMillerLoop},
plonk::{keygen_vk, VerifyingKey},
poly::commitment::Params,
};
use pairing_bn256::bn256::{Bn256, Fr, G1Affine};
use crate::{sample_circuit::TargetCircuit, verify_circuit::Halo2VerifierCircuit};
use std::{
io::{Cursor, Read, Write},
path::PathBuf,
};
pub fn read_file(folder: &mut PathBuf, filename: &str) -> Vec<u8> {
let mut buf = vec![];
folder.push(filename);
let mut fd = std::fs::File::open(folder.as_path()).unwrap();
folder.pop();
fd.read_to_end(&mut buf).unwrap();
buf
}
pub fn write_file(folder: &mut PathBuf, filename: &str, buf: &Vec<u8>) {
folder.push(filename);
let mut fd = std::fs::File::create(folder.as_path()).unwrap();
folder.pop();
fd.write(buf).unwrap();
}
pub fn read_target_circuit_params<
C: CurveAffine,
E: MultiMillerLoop<G1Affine = C, Scalar = C::ScalarExt>,
Circuit: TargetCircuit<C, E>,
>(
folder: &mut PathBuf,
) -> Vec<u8> {
read_file(
folder,
&format!("sample_circuit_{}.params", Circuit::PARAMS_NAME),
)
}
pub fn load_target_circuit_params<
C: CurveAffine,
E: MultiMillerLoop<G1Affine = C, Scalar = C::ScalarExt>,
Circuit: TargetCircuit<C, E>,
>(
folder: &mut PathBuf,
) -> Params<C> {
Params::<C>::read(Cursor::new(&read_target_circuit_params::<C, E, Circuit>(
&mut folder.clone(),
)))
.unwrap()
}
pub fn read_target_circuit_vk<
C: CurveAffine,
E: MultiMillerLoop<G1Affine = C, Scalar = C::ScalarExt>,
Circuit: TargetCircuit<C, E>,
>(
folder: &mut PathBuf,
) -> Vec<u8> {
read_file(
folder,
&format!("sample_circuit_{}.vkey", Circuit::PARAMS_NAME),
)
}
pub fn load_target_circuit_vk<
C: CurveAffine,
E: MultiMillerLoop<G1Affine = C, Scalar = C::ScalarExt>,
Circuit: TargetCircuit<C, E>,
>(
folder: &mut PathBuf,
params: &Params<C>,
) -> VerifyingKey<C> {
if Circuit::READABLE_VKEY {
VerifyingKey::<C>::read::<_, Circuit::Circuit>(
&mut Cursor::new(&read_target_circuit_vk::<C, E, Circuit>(
&mut folder.clone(),
)),
&load_target_circuit_params::<C, E, Circuit>(&mut folder.clone()),
)
.unwrap()
} else {
let circuit = Circuit::Circuit::default();
let vk =
keygen_vk::<C, Circuit::Circuit>(params, &circuit).expect("keygen_vk should not fail");
vk
}
}
pub fn load_target_circuit_instance<Circuit: TargetCircuit<G1Affine, Bn256>>(
folder: &mut PathBuf,
index: usize,
) -> Vec<u8> {
read_file(
folder,
&format!("sample_circuit_instance_{}{}.data", Circuit::NAME, index),
)
}
pub fn load_target_circuit_proof<Circuit: TargetCircuit<G1Affine, Bn256>>(
folder: &mut PathBuf,
index: usize,
) -> Vec<u8> {
read_file(
folder,
&format!("sample_circuit_proof_{}{}.data", Circuit::NAME, index),
)
}
pub fn read_verify_circuit_params(folder: &mut PathBuf) -> Vec<u8> {
read_file(folder, "verify_circuit.params")
}
pub fn load_verify_circuit_params(folder: &mut PathBuf) -> Params<G1Affine> {
Params::<G1Affine>::read(Cursor::new(&read_verify_circuit_params(
&mut folder.clone(),
)))
.unwrap()
}
pub fn read_verify_circuit_vk(folder: &mut PathBuf) -> Vec<u8> {
read_file(folder, "verify_circuit.vkey")
}
pub fn load_verify_circuit_vk(folder: &mut PathBuf) -> VerifyingKey<G1Affine> {
VerifyingKey::<G1Affine>::read::<_, Halo2VerifierCircuit<'_, Bn256>>(
&mut Cursor::new(&read_verify_circuit_vk(&mut folder.clone())),
&load_verify_circuit_params(&mut folder.clone()),
)
.unwrap()
}
pub fn read_verify_circuit_instance(folder: &mut PathBuf) -> Vec<u8> {
read_file(folder, "verify_circuit_instance.data")
}
fn load_instances<E: MultiMillerLoop>(buf: &[u8]) -> Vec<Vec<Vec<E::Scalar>>> {
let mut ret = vec![];
let cursor = &mut std::io::Cursor::new(buf);
while let Ok(a) = <E::Scalar as BaseExt>::read(cursor) {
ret.push(a);
}
vec![vec![ret]]
}
pub fn load_verify_circuit_instance(folder: &mut PathBuf) -> Vec<Vec<Vec<Fr>>> {
let instances = read_verify_circuit_instance(&mut folder.clone());
load_instances::<Bn256>(&instances)
}
pub fn load_verify_circuit_proof(folder: &mut PathBuf) -> Vec<u8> {
read_file(folder, "verify_circuit_proof.data")
}
pub fn write_verify_circuit_params(folder: &mut PathBuf, verify_circuit_params: &Params<G1Affine>) {
folder.push("verify_circuit.params");
let mut fd = std::fs::File::create(folder.as_path()).unwrap();
folder.pop();
verify_circuit_params.write(&mut fd).unwrap();
}
pub fn write_verify_circuit_vk(folder: &mut PathBuf, verify_circuit_vk: &VerifyingKey<G1Affine>) {
folder.push("verify_circuit.vkey");
let mut fd = std::fs::File::create(folder.as_path()).unwrap();
folder.pop();
verify_circuit_vk.write(&mut fd).unwrap();
}
pub fn write_verify_circuit_instance(
folder: &mut PathBuf,
buf: &Vec<<G1Affine as CurveAffine>::ScalarExt>,
) {
folder.push("verify_circuit_instance.data");
let mut fd = std::fs::File::create(folder.as_path()).unwrap();
folder.pop();
buf.iter().for_each(|x| x.write(&mut fd).unwrap());
}
pub fn write_verify_circuit_final_pair(folder: &mut PathBuf, pair: &(G1Affine, G1Affine, Vec<Fr>)) {
folder.push("verify_circuit_final_pair.data");
let mut fd = std::fs::File::create(folder.as_path()).unwrap();
folder.pop();
pair.0.x.write(&mut fd).unwrap();
pair.0.y.write(&mut fd).unwrap();
pair.1.x.write(&mut fd).unwrap();
pair.1.y.write(&mut fd).unwrap();
pair.2.iter().for_each(|scalar| {
scalar.write(&mut fd).unwrap();
})
}
pub fn write_verify_circuit_proof(folder: &mut PathBuf, buf: &Vec<u8>) {
write_file(folder, "verify_circuit_proof.data", buf)
}
pub fn write_verify_circuit_solidity(folder: &mut PathBuf, buf: &Vec<u8>) {
write_file(folder, "verifier.sol", buf)
}