-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFieldGF127.icl
62 lines (44 loc) · 1.59 KB
/
FieldGF127.icl
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
implementation module FieldGF127
import StdDebug
import StdArray
import StdBool
import StdInt
import StdList
import StdMisc
import StdOverloaded
import StdGeneric
import StdEnv
import Gast
:: FieldGF127 = FieldGF127 Int
normalize :: FieldGF127 -> FieldGF127
normalize (FieldGF127 a) = FieldGF127 (((a rem 127) + 127) rem 127)
instance == FieldGF127 where
(==) (FieldGF127 a) (FieldGF127 b) = ((a + 127) rem 127) == ((b + 127) rem 127)
instance + FieldGF127 where
(+) (FieldGF127 a) (FieldGF127 b) = FieldGF127 ((a + b + 127) rem 127)
instance - FieldGF127 where
(-) (FieldGF127 a) (FieldGF127 b) = FieldGF127 ((a - b + 127) rem 127)
instance * FieldGF127 where
(*) (FieldGF127 a) (FieldGF127 b) = FieldGF127 ((a * b) rem 127)
inverses :: {#Int}
inverses =: { find_inverse 0 x \\ x <- [0..126] }
where find_inverse inv x
| x == 0 = 0 // dummy value
| (((inv * x) rem 127) + 127) rem 127 == 1 = inv
| otherwise = find_inverse (inv + 1) x
inverse :: FieldGF127 -> FieldGF127
inverse a = FieldGF127 inverses.[a`]
where (FieldGF127 a`) = normalize a
instance / FieldGF127 where
(/) _ (FieldGF127 0) = abort "division by 0"
(/) a b = a * inverse b
instance ~ FieldGF127 where
~ (FieldGF127 a) = FieldGF127 ((127 - a) rem 127)
instance fromInt FieldGF127 where
fromInt i = FieldGF127 (((i rem 127) + 127) rem 127)
instance toReal FieldGF127 where
toReal (FieldGF127 a) = toReal a
instance toString FieldGF127 where
toString a = toString a`
where (FieldGF127 a`) = normalize a
ggen{|FieldGF127|} state = map fromInt [0 .. 126]