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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/// Compute a + b + carry, returning the result and the new carry over.
#[inline(always)]
pub const fn adc(a: u64, b: u64, carry: u64) -> (u64, u64) {
let ret = (a as u128) + (b as u128) + (carry as u128);
(ret as u64, (ret >> 64) as u64)
}
/// Compute a - (b + borrow), returning the result and the new borrow.
#[inline(always)]
pub const fn sbb(a: u64, b: u64, borrow: u64) -> (u64, u64) {
let ret = (a as u128).wrapping_sub((b as u128) + ((borrow >> 63) as u128));
(ret as u64, (ret >> 64) as u64)
}
/// Compute a + (b * c) + carry, returning the result and the new carry over.
#[inline(always)]
pub const fn mac(a: u64, b: u64, c: u64, carry: u64) -> (u64, u64) {
let ret = (a as u128) + ((b as u128) * (c as u128)) + (carry as u128);
(ret as u64, (ret >> 64) as u64)
}
macro_rules! impl_add_binop_specify_output {
($lhs:ident, $rhs:ident, $output:ident) => {
impl<'b> Add<&'b $rhs> for $lhs {
type Output = $output;
#[inline]
fn add(self, rhs: &'b $rhs) -> $output {
&self + rhs
}
}
impl<'a> Add<$rhs> for &'a $lhs {
type Output = $output;
#[inline]
fn add(self, rhs: $rhs) -> $output {
self + &rhs
}
}
impl Add<$rhs> for $lhs {
type Output = $output;
#[inline]
fn add(self, rhs: $rhs) -> $output {
&self + &rhs
}
}
};
}
macro_rules! impl_sub_binop_specify_output {
($lhs:ident, $rhs:ident, $output:ident) => {
impl<'b> Sub<&'b $rhs> for $lhs {
type Output = $output;
#[inline]
fn sub(self, rhs: &'b $rhs) -> $output {
&self - rhs
}
}
impl<'a> Sub<$rhs> for &'a $lhs {
type Output = $output;
#[inline]
fn sub(self, rhs: $rhs) -> $output {
self - &rhs
}
}
impl Sub<$rhs> for $lhs {
type Output = $output;
#[inline]
fn sub(self, rhs: $rhs) -> $output {
&self - &rhs
}
}
};
}
macro_rules! impl_binops_additive_specify_output {
($lhs:ident, $rhs:ident, $output:ident) => {
impl_add_binop_specify_output!($lhs, $rhs, $output);
impl_sub_binop_specify_output!($lhs, $rhs, $output);
};
}
macro_rules! impl_binops_multiplicative_mixed {
($lhs:ident, $rhs:ident, $output:ident) => {
impl<'b> Mul<&'b $rhs> for $lhs {
type Output = $output;
#[inline]
fn mul(self, rhs: &'b $rhs) -> $output {
&self * rhs
}
}
impl<'a> Mul<$rhs> for &'a $lhs {
type Output = $output;
#[inline]
fn mul(self, rhs: $rhs) -> $output {
self * &rhs
}
}
impl Mul<$rhs> for $lhs {
type Output = $output;
#[inline]
fn mul(self, rhs: $rhs) -> $output {
&self * &rhs
}
}
};
}
macro_rules! impl_binops_additive {
($lhs:ident, $rhs:ident) => {
impl_binops_additive_specify_output!($lhs, $rhs, $lhs);
impl SubAssign<$rhs> for $lhs {
#[inline]
fn sub_assign(&mut self, rhs: $rhs) {
*self = &*self - &rhs;
}
}
impl AddAssign<$rhs> for $lhs {
#[inline]
fn add_assign(&mut self, rhs: $rhs) {
*self = &*self + &rhs;
}
}
impl<'b> SubAssign<&'b $rhs> for $lhs {
#[inline]
fn sub_assign(&mut self, rhs: &'b $rhs) {
*self = &*self - rhs;
}
}
impl<'b> AddAssign<&'b $rhs> for $lhs {
#[inline]
fn add_assign(&mut self, rhs: &'b $rhs) {
*self = &*self + rhs;
}
}
};
}
macro_rules! impl_binops_multiplicative {
($lhs:ident, $rhs:ident) => {
impl_binops_multiplicative_mixed!($lhs, $rhs, $lhs);
impl MulAssign<$rhs> for $lhs {
#[inline]
fn mul_assign(&mut self, rhs: $rhs) {
*self = &*self * &rhs;
}
}
impl<'b> MulAssign<&'b $rhs> for $lhs {
#[inline]
fn mul_assign(&mut self, rhs: &'b $rhs) {
*self = &*self * rhs;
}
}
};
}
macro_rules! impl_pippenger_sum_of_products {
() => {
/// Use pippenger multi-exponentiation method to compute
/// the sum of multiple points raise to scalars.
/// This uses a fixed window of 4 to be constant time
#[cfg(feature = "alloc")]
pub fn sum_of_products(points: &[Self], scalars: &[Scalar]) -> Self {
use alloc::vec::Vec;
let ss: Vec<Scalar> = scalars
.iter()
.map(|s| Scalar::montgomery_reduce(s.0[0], s.0[1], s.0[2], s.0[3], 0, 0, 0, 0))
.collect();
Self::sum_of_products_pippenger(points, ss.as_slice())
}
/// Use pippenger multi-exponentiation method to compute
/// the sum of multiple points raise to scalars.
/// This uses a fixed window of 4 to be constant time
/// The scalars are used as place holders for temporary computations
pub fn sum_of_products_in_place(points: &[Self], scalars: &mut [Scalar]) -> Self {
// Scalars are in montgomery form, hack them in place to be temporarily
// in canonical form, do the computation, then switch them back
for i in 0..scalars.len() {
// Turn into canonical form by computing (a.R) / R = a
scalars[i] = Scalar::montgomery_reduce(
scalars[i].0[0],
scalars[i].0[1],
scalars[i].0[2],
scalars[i].0[3],
0,
0,
0,
0,
);
}
let res = Self::sum_of_products_pippenger(points, scalars);
for i in 0..scalars.len() {
scalars[i] = Scalar::from_raw(scalars[i].0);
}
res
}
/// Compute pippenger multi-exponentiation.
/// Pippenger relies on scalars in canonical form
/// This uses a fixed window of 4 to be constant time
fn sum_of_products_pippenger(points: &[Self], scalars: &[Scalar]) -> Self {
const WINDOW: usize = 4;
const NUM_BUCKETS: usize = 1 << WINDOW;
const EDGE: usize = WINDOW - 1;
const MASK: u64 = (NUM_BUCKETS - 1) as u64;
let num_components = core::cmp::min(points.len(), scalars.len());
let mut buckets = [Self::identity(); NUM_BUCKETS];
let mut res = Self::identity();
let mut num_doubles = 0;
let mut bit_sequence_index = 255usize; // point to top bit we need to process
loop {
for _ in 0..num_doubles {
res = res.double();
}
let mut max_bucket = 0;
let word_index = bit_sequence_index >> 6; // divide by 64 to find word_index
let bit_index = bit_sequence_index & 63; // mod by 64 to find bit_index
if bit_index < EDGE {
// we are on the edge of a word; have to look at the previous word, if it exists
if word_index == 0 {
// there is no word before
let smaller_mask = ((1 << (bit_index + 1)) - 1) as u64;
for i in 0..num_components {
let bucket_index: usize =
(scalars[i].0[word_index] & smaller_mask) as usize;
if bucket_index > 0 {
buckets[bucket_index] += points[i];
if bucket_index > max_bucket {
max_bucket = bucket_index;
}
}
}
} else {
// there is a word before
let high_order_mask = ((1 << (bit_index + 1)) - 1) as u64;
let high_order_shift = EDGE - bit_index;
let low_order_mask = ((1 << high_order_shift) - 1) as u64;
let low_order_shift = 64 - high_order_shift;
let prev_word_index = word_index - 1;
for i in 0..num_components {
let mut bucket_index = ((scalars[i].0[word_index] & high_order_mask)
<< high_order_shift)
as usize;
bucket_index |= ((scalars[i].0[prev_word_index] >> low_order_shift)
& low_order_mask) as usize;
if bucket_index > 0 {
buckets[bucket_index] += points[i];
if bucket_index > max_bucket {
max_bucket = bucket_index;
}
}
}
}
} else {
let shift = bit_index - EDGE;
for i in 0..num_components {
let bucket_index: usize =
((scalars[i].0[word_index] >> shift) & MASK) as usize;
assert!(bit_sequence_index != 255 || scalars[i].0[3] >> 63 == 0);
if bucket_index > 0 {
buckets[bucket_index] += points[i];
if bucket_index > max_bucket {
max_bucket = bucket_index;
}
}
}
}
res += &buckets[max_bucket];
for i in (1..max_bucket).rev() {
buckets[i] += buckets[i + 1];
res += buckets[i];
buckets[i + 1] = Self::identity();
}
buckets[1] = Self::identity();
if bit_sequence_index < WINDOW {
break;
}
bit_sequence_index -= WINDOW;
num_doubles = {
if bit_sequence_index < EDGE {
bit_sequence_index + 1
} else {
WINDOW
}
};
}
res
}
};
}
macro_rules! impl_serde {
($name:ident, $serfunc:expr, $deserfunc:expr, $len:expr) => {
impl serde::Serialize for $name {
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
use serde::ser::SerializeTuple;
let bytes = $serfunc(self);
let mut seq = s.serialize_tuple(bytes.len())?;
for b in &bytes[..] {
seq.serialize_element(b)?;
}
seq.end()
}
}
impl<'de> serde::Deserialize<'de> for $name {
fn deserialize<D>(deserializer: D) -> Result<$name, D::Error>
where
D: serde::Deserializer<'de>,
{
struct ByteArrayVisitor;
impl<'de> serde::de::Visitor<'de> for ByteArrayVisitor {
type Value = $name;
fn expecting(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "an array of {} bytes", $len)
}
fn visit_seq<A>(self, mut seq: A) -> Result<$name, A::Error>
where
A: serde::de::SeqAccess<'de>,
{
let mut arr = [0u8; $len];
for i in 0..arr.len() {
arr[i] = seq
.next_element()?
.ok_or_else(|| serde::de::Error::invalid_length(i, &self))?;
}
let p = $deserfunc(&arr);
if p.is_some().unwrap_u8() == 1 {
Ok(p.unwrap())
} else {
Err(serde::de::Error::invalid_value(
serde::de::Unexpected::Bytes(&arr),
&self,
))
}
}
}
deserializer.deserialize_tuple($len, ByteArrayVisitor)
}
}
};
}