forked from jancarlsson/snarklib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutoTest_MultiExp.hpp
157 lines (129 loc) · 4.73 KB
/
AutoTest_MultiExp.hpp
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
#ifndef _SNARKLIB_AUTOTEST_MULTIEXP_HPP_
#define _SNARKLIB_AUTOTEST_MULTIEXP_HPP_
#include <gmp.h>
#include <string>
#include <vector>
#include "algebra/fields/bigint.hpp"
#include "AutoTest.hpp"
#include "BigInt.hpp"
#include "common/wnaf.hpp"
#include "encoding/multiexp.hpp"
#include "MultiExp.hpp"
namespace snarklib {
////////////////////////////////////////////////////////////////////////////////
// wNAF exponentiation matches original
//
template <mp_size_t N, typename T, typename U>
class AutoTest_MultiExp_wnafExp : public AutoTest
{
public:
AutoTest_MultiExp_wnafExp(const std::string& scalar,
const std::string& base)
: AutoTest(scalar, base),
m_scalarA(scalar.c_str()),
m_scalarB(scalar),
m_baseA(to_bigint<N>(base) * U::one()),
m_baseB(BigInt<N>(base) * T::one())
{}
void runTest() {
const auto a = opt_window_wnaf_exp(U::zero(),
m_baseA,
m_scalarA,
m_scalarA.num_bits());
const auto b = wnafExp(m_scalarB, m_baseB);
checkPass(sameData(a, b));
}
private:
const libsnark::bigint<N> m_scalarA;
const BigInt<N> m_scalarB;
const U m_baseA;
const T m_baseB;
};
////////////////////////////////////////////////////////////////////////////////
// sum of multiple exponentiation matches original
//
template <mp_size_t N, typename T, typename F, typename U, typename G>
class AutoTest_MultiExp_multiExp : public AutoTest
{
public:
AutoTest_MultiExp_multiExp(const std::size_t numTerms)
: AutoTest(numTerms),
m_numTerms(numTerms)
{
m_baseB.reserve(numTerms);
m_scalarB.reserve(numTerms);
m_baseA.reserve(numTerms);
m_scalarA.reserve(numTerms);
for (std::size_t i = 0; i < numTerms; ++i) {
const auto
randomBase = uniformBase10(0, 1000000),
randomScalar = uniformBase10(0, 1000000);
m_baseB.emplace_back(T(BigInt<N>(randomBase) * T::one()));
m_scalarB.emplace_back(F(randomScalar));
m_baseA.emplace_back(U(to_bigint<N>(randomBase) * U::one()));
m_scalarA.emplace_back(G(randomScalar.c_str()));
}
}
void runTest() {
const auto a = libsnark::multi_exp<U, G>(U::zero(),
m_baseA.begin(),
m_baseA.end(),
m_scalarA.begin(),
m_scalarA.end(),
1,
true);
const auto b = multiExp(m_baseB, m_scalarB);
checkPass(sameData(a, b));
}
private:
const std::size_t m_numTerms;
std::vector<T> m_baseB;
std::vector<F> m_scalarB;
std::vector<U> m_baseA;
std::vector<G> m_scalarA;
};
////////////////////////////////////////////////////////////////////////////////
// sum of multiple exponentiation with zeros and ones matches original
//
template <mp_size_t N, typename T, typename F, typename U, typename G>
class AutoTest_MultiExp_multiExp01 : public AutoTest
{
public:
AutoTest_MultiExp_multiExp01(const std::size_t numTerms)
: AutoTest(numTerms),
m_numTerms(numTerms)
{
m_baseB.reserve(numTerms);
m_scalarB.reserve(numTerms);
m_baseA.reserve(numTerms);
m_scalarA.reserve(numTerms);
for (std::size_t i = 0; i < numTerms; ++i) {
const auto
randomBase = uniformBase10(0, 1000000),
randomScalar = sparseUniformBase10(0, 1000000);
m_baseB.emplace_back(T(BigInt<N>(randomBase) * T::one()));
m_scalarB.emplace_back(F(randomScalar));
m_baseA.emplace_back(U(to_bigint<N>(randomBase) * U::one()));
m_scalarA.emplace_back(G(randomScalar.c_str()));
}
}
void runTest() {
const auto a = libsnark::multi_exp<U, G>(U::zero(),
m_baseA.begin(),
m_baseA.end(),
m_scalarA.begin(),
m_scalarA.end(),
1,
true);
const auto b = multiExp(m_baseB, m_scalarB);
checkPass(sameData(a, b));
}
private:
const std::size_t m_numTerms;
std::vector<T> m_baseB;
std::vector<F> m_scalarB;
std::vector<U> m_baseA;
std::vector<G> m_scalarA;
};
} // namespace snarklib
#endif