forked from jancarlsson/snarkfront
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPowersOf2.hpp
171 lines (135 loc) · 3.92 KB
/
PowersOf2.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#ifndef _SNARKFRONT_POWERS_OF_2_HPP_
#define _SNARKFRONT_POWERS_OF_2_HPP_
#include <algorithm>
#include <cassert>
#include <climits>
#include <cstdint>
#include <gmp.h>
#include <vector>
#include <BigInt.hpp> // snarklib
namespace snarkfront {
// look up table for powers of 2 for BigInt/field/group
template <typename T>
class PowersOf2
{
public:
PowersOf2()
: m_lut(1, T::one())
{}
const T& lookUp(const std::size_t index)
{
// protect against huge index from accidental pointer argument
#ifdef USE_ASSERT
assert(index < 1024);
#endif
for (std::size_t i = m_lut.size(); i <= index; ++i) {
// m_lut[i] = 2 * m_lut[i - 1];
m_lut.emplace_back(m_lut.back() + m_lut.back());
}
return m_lut[index];
}
T getNumber(const std::size_t number) {
T accum = T::zero();
auto b = number;
std::size_t i = 0;
while (b) {
if (b & 0x1)
accum = accum + lookUp(i);
b >>= 1;
++i;
}
return accum;
}
T getNumber(const std::vector<int>& bits) {
T accum = T::zero();
for (std::size_t i = 0; i < bits.size(); ++i) {
if (bits[i])
accum = accum + lookUp(i);
}
return accum;
}
private:
std::vector<T> m_lut; // index -> T(2^index)
};
// convert Boolean to BigInt/field/group one and zero
template <typename T>
T boolTo(const bool a) {
return a ? T::one() : T::zero();
}
// size of type in bits
std::size_t sizeBits(const bool& dummy);
std::size_t sizeBits(const std::uint32_t& dummy);
std::size_t sizeBits(const std::uint64_t& dummy);
template <mp_size_t N>
std::size_t sizeBits(const snarklib::BigInt<N>& dummy) {
return snarklib::BigInt<N>::maxBits();
}
// returns number of matching bits starting from most significant bit
template <typename BIT>
int matchMSB(const std::vector<BIT>& a,
const std::vector<BIT>& b)
{
if (a.size() != b.size())
return -1; // a and b are different sizes, no matching bits
for (int i = a.size() - 1; i >= 0; --i) {
if (bool(a[i] != b[i]))
return a.size() - 1 - i; // some bits match
}
return a.size(); // all bits match
}
// convert value to bits
std::vector<int> valueBits(const bool& a);
std::vector<int> valueBits(const std::uint32_t& a);
std::vector<int> valueBits(const std::uint64_t& a);
template <mp_size_t N>
std::vector<int> valueBits(const snarklib::BigInt<N>& a) {
std::vector<int> v;
v.reserve(sizeBits(a));
for (std::size_t i = 0; i < sizeBits(a); ++i) {
v.push_back(a.testBit(i));
}
return v;
}
// convert bits to value
template <typename UINT_N>
std::vector<int> bitsValue(UINT_N& a, const std::vector<int>& b)
{
UINT_N result = 0;
const std::size_t N = std::min(sizeBits(a), b.size());
for (std::size_t i = 0; i < N; ++i) {
result |= (UINT_N(b[i]) << i);
}
a = result;
std::vector<int> v;
for (std::size_t i = N; i < b.size(); ++i) {
v.push_back(b[i]);
}
return v;
}
// count number of set bits
std::size_t countBits(const std::vector<int>& v);
// overflow addition (uint32_t and uint64_t)
template <typename UINT_N>
void addover(UINT_N& a1, UINT_N& a0, const UINT_N& b)
{
// a0 = 2 * a0_half + a0_bit
// b = 2 * b_half + b_bit
const UINT_N
a0_half = a0 >> 1, a0_bit = a0 & 0x1,
b_half = b >> 1, b_bit = b & 0x1;
// a0 + b = 2 * (a0_half + b_half) + (a0_bit + b_bit)
// = 2 * halfsum + a0_bit + b_bit
const UINT_N halfsum = a0_half + b_half;
// (high, low) = a0 + b
UINT_N
high = halfsum >> (sizeBits(high) - 1), // carry bit
low = (halfsum << 1) + a0_bit;
const UINT_N lowOriginal = low;
low += b_bit;
if ((0 == low) && (-1 == lowOriginal)) ++high; // handle carry
// accumulate result
a1 += high;
a0 = low;
}
} // namespace snarkfront
#endif