forked from alexstachnik/block-wiedemann-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModular.py
95 lines (66 loc) · 1.65 KB
/
Modular.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
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
import random
import Algorithms
#Represents GF(p) for prime p
class Modular:
def __init__(self,q):
self.q=q
def add(self,lhs,rhs):
return (lhs+rhs)%self.q
def neg(self,x):
return self.q-x
def sub(self,lhs,rhs):
return (self.q+lhs-rhs)%self.q
def mul(self,lhs,rhs):
return (lhs*rhs)%self.q
def make(self,x):
return int(self.q+x)%self.q
def inv(self,x):
if (x==0):
raise Exception("div by zero")
(gcd,a,b)=Algorithms.euclid(x,self.q)
return self.make(a)
def div(self,lhs,rhs):
return self.mul(lhs,self.inv(rhs))
def zero(self):
return 0
def isZero(self,x):
return x == 0
def one(self):
return 1
def isOne(self,x):
return x == 1
def random(self):
return random.randrange(0,self.q)
def toStr(self,x):
return str(x)
def areEqual(self,x,y):
return x==y
class Integer:
def add(self,lhs,rhs):
return lhs+rhs
def neg(self,x):
return -x
def sub(self,lhs,rhs):
return lhs-rhs
def mul(self,lhs,rhs):
return lhs*rhs
def make(self,x):
return int(x)
def inv(self,x):
raise Exception("Not a divison ring")
def div(self,lhs,rhs):
raise Exception("Not a divison ring")
def zero(self):
return 0
def isZero(self,x):
return x == 0
def one(self):
return 1
def isOne(self,x):
return x == 1
def random(self):
return random.randrange(0,9999999)
def toStr(self,x):
return str(x)
def areEqual(self,x,y):
return x==y