-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFieldElement.cpp
More file actions
96 lines (73 loc) · 2.68 KB
/
Copy pathFieldElement.cpp
File metadata and controls
96 lines (73 loc) · 2.68 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
//
// Created by ryousuke kaga on 2023/11/09.
//
#include "FieldElement.h"
#include <utility>
boost::multiprecision::int1024_t modpow(boost::multiprecision::int1024_t base, boost::multiprecision::int1024_t exp, const boost::multiprecision::int1024_t& modulus) {
base %= modulus;
boost::multiprecision::int1024_t result = 1;
while (exp > 0) {
if (exp % 2 == 1)
result = (result * base) % modulus;
base = (base * base) % modulus;
exp /= 2;
}
return result;
}
FieldElement::FieldElement(boost::multiprecision::int1024_t num, boost::multiprecision::int1024_t prime): num(std::move(num)), prime(std::move(prime)) {
}
bool FieldElement::operator==(const FieldElement &other) const {
return num == other.num && prime == other.prime;
}
std::string FieldElement::to_string() const {
return "FiniteElement_" + boost::multiprecision::to_string(prime) + "(" + boost::multiprecision::to_string(num) + ")";
}
bool FieldElement::operator!=(const FieldElement &other) const {
return !operator==(other);
}
FieldElement FieldElement::add(const FieldElement &other) const {
return {(num + other.num) % prime, prime};
}
FieldElement FieldElement::sub(const FieldElement &other) const {
auto n = (num - other.num) % prime;
if (n < 0) n += prime;
return {n , prime};
}
FieldElement FieldElement::mul(const FieldElement &other) const {
return {(num * other.num) % prime, prime};
}
FieldElement FieldElement::div(const FieldElement &other) const {
return {num * modpow(other.num, prime - 2, prime) % prime, prime};
}
FieldElement FieldElement::pow(const boost::multiprecision::int1024_t& exp) const {
if(exp == 0) return {1, prime};
if(exp < 0) return pow(((prime - 2) * -1 * exp) % (prime-1));
return {modpow(num, exp % (prime-1), prime), prime};
}
FieldElement FieldElement::check() const {
return {num % prime, prime};
}
FieldElement FieldElement::operator-() const {
return {-1 * num, prime};
}
FieldElement FieldElement::operator*(const boost::multiprecision::int1024_t &sc) const {
return {(num * sc) % prime, prime};
}
std::ostream& operator << ( std::ostream& outs, const FieldElement & finiteElement ) {
return outs << finiteElement.to_string();
}
FieldElement operator+(const FieldElement& f1, const FieldElement& f2) {
return f1.add(f2);
}
FieldElement operator-(const FieldElement& f1, const FieldElement& f2) {
return f1.sub(f2);
}
FieldElement operator*(const FieldElement& f1, const FieldElement& f2) {
return f1.mul(f2);
}
FieldElement operator/(const FieldElement& f1, const FieldElement& f2) {
return f1.div(f2);
}
FieldElement pow(const FieldElement& fe, int exp) {
return fe.pow(exp);
}