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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
use groupy::{CurveAffine, EncodedPoint};
use paired::Engine;
use crate::SynthesisError;
use memmap::{Mmap, MmapOptions};
use std::fs::File;
use std::io;
use std::marker::PhantomData;
use std::ops::Range;
use std::path::PathBuf;
use std::sync::Arc;
use super::{ParameterSource, VerifyingKey};
#[derive(Clone)]
pub struct MappedParameters<E: Engine> {
pub param_file: PathBuf,
pub vk: VerifyingKey<E>,
pub h: Vec<Range<usize>>,
pub l: Vec<Range<usize>>,
pub a: Vec<Range<usize>>,
pub b_g1: Vec<Range<usize>>,
pub b_g2: Vec<Range<usize>>,
pub checked: bool,
pub _e: PhantomData<E>,
}
unsafe impl<E: Engine> Sync for MappedParameters<E> {}
impl<'a, E: Engine> ParameterSource<E> for &'a MappedParameters<E> {
type G1Builder = (Arc<Vec<E::G1Affine>>, usize);
type G2Builder = (Arc<Vec<E::G2Affine>>, usize);
fn get_vk(&mut self, _: usize) -> Result<VerifyingKey<E>, SynthesisError> {
Ok(self.vk.clone())
}
fn get_h(&mut self, _num_h: usize) -> Result<Self::G1Builder, SynthesisError> {
let params = File::open(self.param_file.clone())?;
let mmap = unsafe { MmapOptions::new().map(¶ms)? };
let mut h = vec![];
for i in 0..self.h.len() {
h.push(read_g1::<E>(
&mmap,
self.h[i].start,
self.h[i].end,
self.checked,
)?);
}
Ok((Arc::new(h), 0))
}
fn get_l(&mut self, _num_l: usize) -> Result<Self::G1Builder, SynthesisError> {
let params = File::open(self.param_file.clone())?;
let mmap = unsafe { MmapOptions::new().map(¶ms)? };
let mut l = vec![];
for i in 0..self.l.len() {
l.push(read_g1::<E>(
&mmap,
self.l[i].start,
self.l[i].end,
self.checked,
)?);
}
Ok((Arc::new(l), 0))
}
fn get_a(
&mut self,
num_inputs: usize,
_num_a: usize,
) -> Result<(Self::G1Builder, Self::G1Builder), SynthesisError> {
let params = File::open(self.param_file.clone())?;
let mmap = unsafe { MmapOptions::new().map(¶ms)? };
let mut a = vec![];
for i in 0..self.a.len() {
a.push(read_g1::<E>(
&mmap,
self.a[i].start,
self.a[i].end,
self.checked,
)?);
}
Ok(((Arc::new(a.clone()), 0), (Arc::new(a), num_inputs)))
}
fn get_b_g1(
&mut self,
num_inputs: usize,
_num_b_g1: usize,
) -> Result<(Self::G1Builder, Self::G1Builder), SynthesisError> {
let params = File::open(self.param_file.clone())?;
let mmap = unsafe { MmapOptions::new().map(¶ms)? };
let mut b_g1 = vec![];
for i in 0..self.b_g1.len() {
b_g1.push(read_g1::<E>(
&mmap,
self.b_g1[i].start,
self.b_g1[i].end,
self.checked,
)?);
}
Ok(((Arc::new(b_g1.clone()), 0), (Arc::new(b_g1), num_inputs)))
}
fn get_b_g2(
&mut self,
num_inputs: usize,
_num_b_g2: usize,
) -> Result<(Self::G2Builder, Self::G2Builder), SynthesisError> {
let params = File::open(self.param_file.clone())?;
let mmap = unsafe { MmapOptions::new().map(¶ms)? };
let mut b_g2 = vec![];
for i in 0..self.b_g2.len() {
b_g2.push(read_g2::<E>(
&mmap,
self.b_g2[i].start,
self.b_g2[i].end,
self.checked,
)?);
}
Ok(((Arc::new(b_g2.clone()), 0), (Arc::new(b_g2), num_inputs)))
}
}
pub fn read_g1<E: Engine>(
mmap: &Mmap,
start: usize,
end: usize,
checked: bool,
) -> Result<E::G1Affine, std::io::Error> {
let ptr = &mmap[start..end];
let repr =
unsafe { *(ptr as *const [u8] as *const <E::G1Affine as CurveAffine>::Uncompressed) };
if checked {
repr.into_affine()
} else {
repr.into_affine_unchecked()
}
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
.and_then(|e| {
if e.is_zero() {
Err(io::Error::new(
io::ErrorKind::InvalidData,
"point at infinity",
))
} else {
Ok(e)
}
})
}
pub fn read_g2<E: Engine>(
mmap: &Mmap,
start: usize,
end: usize,
checked: bool,
) -> Result<E::G2Affine, std::io::Error> {
let ptr = &mmap[start..end];
let repr =
unsafe { *(ptr as *const [u8] as *const <E::G2Affine as CurveAffine>::Uncompressed) };
if checked {
repr.into_affine()
} else {
repr.into_affine_unchecked()
}
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
.and_then(|e| {
if e.is_zero() {
Err(io::Error::new(
io::ErrorKind::InvalidData,
"point at infinity",
))
} else {
Ok(e)
}
})
}