forked from jancarlsson/snarkfront
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBigIntOps.hpp
More file actions
131 lines (110 loc) · 2.87 KB
/
BigIntOps.hpp
File metadata and controls
131 lines (110 loc) · 2.87 KB
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
#ifndef _SNARKFRONT_BIG_INT_OPS_HPP_
#define _SNARKFRONT_BIG_INT_OPS_HPP_
#include <array>
#include <cassert>
#include <climits>
#include <cstdint>
#include <gmp.h>
#include <BigInt.hpp> // snarklib
namespace snarkfront {
////////////////////////////////////////////////////////////////////////////////
// BigInt arithmetic operations independent of a prime field
//
//
// a + b --> c
//
template <mp_size_t N>
bool addBigInt(const snarklib::BigInt<N>& a,
const snarklib::BigInt<N>& b,
snarklib::BigInt<N>& c)
{
const mp_limb_t carry = mpn_add_n(c.data(), a.data(), b.data(), N);
return ! carry; // failure if overflow
}
template <mp_size_t N>
snarklib::BigInt<N> operator+ (const snarklib::BigInt<N>& a,
const snarklib::BigInt<N>& b)
{
snarklib::BigInt<N> c;
#ifdef USE_ASSERT
assert(addBigInt(a, b, c));
#endif
return c;
}
//
// a - b --> c
//
template <mp_size_t N>
bool subBigInt(const snarklib::BigInt<N>& a,
const snarklib::BigInt<N>& b,
snarklib::BigInt<N>& c)
{
if (mpn_cmp(a.data(), b.data(), N) < 0) return false; // failure
// if b > a
const mp_limb_t borrow = mpn_sub(c.data(), a.data(), N, b.data(), N);
#ifdef USE_ASSERT
assert(0 == borrow);
#endif
return true;
}
template <mp_size_t N>
snarklib::BigInt<N> operator- (const snarklib::BigInt<N>& a,
const snarklib::BigInt<N>& b)
{
snarklib::BigInt<N> c;
#ifdef USE_ASSERT
assert(subBigInt(a, b, c));
#endif
return c;
}
//
// a * b --> c
//
template <mp_size_t N>
bool mulBigInt(const snarklib::BigInt<N>& a,
const snarklib::BigInt<N>& b,
snarklib::BigInt<N>& c)
{
std::array<mp_limb_t, 2*N> scratch;
mpn_mul_n(scratch.data(), a.data(), b.data(), N);
for (std::size_t i = N; i < 2*N; ++i) {
if (scratch[i]) return false; // failure if overflow
}
mpn_copyi(c.data(), scratch.data(), N);
return true;
}
template <mp_size_t N>
snarklib::BigInt<N> operator* (const snarklib::BigInt<N>& a,
const snarklib::BigInt<N>& b)
{
snarklib::BigInt<N> c;
#ifdef USE_ASSERT
assert(mulBigInt(a, b, c));
#endif
return c;
}
//
// Russian peasant algorithm
//
template <mp_size_t N>
snarklib::BigInt<N> powerBigInt(const std::size_t exponent)
{
// set lower bits
auto mask = exponent;
for (std::size_t i = 1; i <= (sizeof(std::size_t) * CHAR_BIT) / 2; i *= 2) {
mask |= (mask >> i);
}
mask &= ~(mask >> 1); // most significant bit
const snarklib::BigInt<N> ONE(1);
auto accum = snarklib::BigInt<N>::zero();
while (mask) {
accum = accum + accum;
if (exponent & mask) {
accum = accum + ONE;
}
mask >>= 1;
}
return accum;
}
} // namespace snarkfront
#endif