-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmultiexp.h
76 lines (59 loc) · 1.67 KB
/
multiexp.h
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
#ifndef H_MULTIEXP
#define H_MULTIEXP
#include "curve.h"
#include "common.h"
template <class E, class C>
CurvePoint<E> peepinger(std::vector<std::tuple<CurvePoint<E>, std::vector<u64>>> pairs, WeierstrassCurve<E> const &wc, C const &context)
{
u32 c;
if (pairs.size() < 32)
{
c = 3;
}
else
{
c = ceil(log((double)pairs.size()));
};
std::vector<CurvePoint<E>> windows;
std::vector<CurvePoint<E>> buckets;
u64 mask = (u64(1) << c) - u64(1);
u32 cur = 0;
auto const n_bits = num_bits(wc.subgroup_order());
auto const zero_point = CurvePoint<E>::zero(context);
while (cur <= n_bits)
{
auto acc = zero_point;
buckets.resize(0, zero_point);
buckets.resize((1 << c) - 1, zero_point);
for (auto it = pairs.begin(); it != pairs.end(); it++)
{
CurvePoint<E> const &g = std::get<0>(*it);
std::vector<u64> &s = std::get<1>(*it);
usize const index = s[0] & mask;
if (index != 0)
{
buckets[index - 1].add_mixed(g, wc, context);
}
right_shift(s, c);
}
auto running_sum = zero_point;
for (auto it = buckets.crbegin(); it != buckets.crend(); it++)
{
running_sum.add(*it, wc, context);
acc.add(running_sum, wc, context);
}
windows.push_back(acc);
cur += c;
}
auto acc = zero_point;
for (auto it = windows.crbegin(); it != windows.crend(); it++)
{
for (u32 i = 0; i < c; i++)
{
acc.mul2(wc);
}
acc.add(*it, wc, context);
}
return acc;
}
#endif