-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfield.h
69 lines (57 loc) · 1.29 KB
/
field.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
#ifndef H_FIELD
#define H_FIELD
#include "common.h"
#include "repr.h"
#include "ctbignum/slicing.hpp"
template <usize N>
class PrimeField
{
Repr<N> modulus;
u64 mont_power_;
Repr<N> mont_r_;
Repr<N> mont_r2_;
u64 mont_inv_;
public:
PrimeField(Repr<N> modulus) : modulus(modulus), mont_power_(N * LIMB_BITS)
{
// Compute -m^-1 mod 2**64 by exponentiating by totient(2**64) - 1
u64 inv = 1;
for (auto i = 0; i < 63; i++)
{
inv = inv * inv;
inv = inv * modulus[0];
}
inv = (std::numeric_limits<u64>::max() - inv) + 2 + std::numeric_limits<u64>::max();
mont_inv_ = inv;
Repr<N + 1> pow_N_LIMB_BITS = {0};
pow_N_LIMB_BITS[N] = 1;
mont_r_ = pow_N_LIMB_BITS % modulus;
mont_r2_ = (mont_r_ * mont_r_) % modulus;
}
Repr<N> mod() const
{
return modulus;
}
Repr<N> mont_r() const
{
return mont_r_;
}
Repr<N> mont_r2() const
{
return mont_r2_;
}
u64 mont_power() const
{
return mont_power_;
}
// Montgomery parametare for multiplication
u64 mont_inv() const
{
return mont_inv_;
}
bool is_valid(Repr<N> const &repr) const
{
return repr < modulus;
}
};
#endif