forked from mohanson/cryptography-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinite_field.py
60 lines (45 loc) · 1.62 KB
/
finite_field.py
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
class Fp:
# Galois field. In mathematics, a finite field or Galois field is a field that contains a finite number of elements.
# As with any field, a finite field is a set on which the operations of multiplication, addition, subtraction and
# division are defined and satisfy certain basic rules.
#
# https://www.cs.miami.edu/home/burt/learning/Csc609.142/ecdsa-cert.pdf
# Don Johnson, Alfred Menezes and Scott Vanstone, The Elliptic Curve Digital Signature Algorithm (ECDSA)
# 3.1 The Finite Field Fp
p = 0
def __init__(self, x):
self.x = x % self.p
def __repr__(self):
return f'Fp(0x{self.x:064x})'
def __eq__(self, data):
assert self.p == data.p
return self.x == data.x
def __add__(self, data):
assert self.p == data.p
return self.__class__((self.x + data.x) % self.p)
def __sub__(self, data):
assert self.p == data.p
return self.__class__((self.x - data.x) % self.p)
def __mul__(self, data):
assert self.p == data.p
return self.__class__((self.x * data.x) % self.p)
def __truediv__(self, data):
return self * data ** -1
def __pow__(self, data):
return self.__class__(pow(self.x, data, self.p))
def __pos__(self):
return self
def __neg__(self):
return self.__class__(self.p - self.x)
@classmethod
def nil(cls):
return cls(0)
@classmethod
def one(cls):
return cls(1)
if __name__ == '__main__':
Fp.p = 23
assert Fp(12) + Fp(20) == Fp(9)
assert Fp(8) * Fp(9) == Fp(3)
assert Fp(8) ** -1 == Fp(3)
Fp.p = 0