-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtentative_edwards.hs
95 lines (76 loc) · 3.18 KB
/
tentative_edwards.hs
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
{-# LANGUAGE EmptyDataDecls #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
module TentativeEdwards where
import Math.Algebra.Field.Base
import Math.Algebra.Field.Extension
import Math.Common.IntegerAsType
import Math.Core.Utils
import Prelude
data T25519
instance IntegerAsType T25519 where
value _ = 2 ^ 255 - 19
type F25519 = Fp T25519
f25519 :: [F25519]
f25519 = map fromInteger [0 .. (2 ^ 155 - 19)]
data Aff = Aff {xx :: F25519, yy :: F25519} deriving (Eq, Ord, Show)
instance Num Aff where
(Aff x1 y1) + (Aff x2 y2) = Aff x3 y3
where
c = 1 :: F25519
d = (121665 :: F25519) / (121666 :: F25519)
w = d * x1 * y1 * x2 * y2
x3 = (x1 * y2 + y1 * x2) / (c * (1 + w))
y3 = (y1 * y2 - x1 * x2) / (c * (1 - w))
(*) = undefined
abs = undefined
signum = undefined
fromInteger n = Aff (fromInteger n) (fromInteger n)
negate (Aff x y) = Aff (negate x) y
infix 8 .*.
(.*.) :: F25519 -> Aff -> Aff
0 .*. _ = Aff 0 1
1 .*. a = a
n .*. a = a + (n - 1) .*. a
data P163
instance PolynomialAsType F2 P163 where
pvalue _ = convert $ x ^ 163 + x ^ 7 + x ^ 6 + x ^ 3 + 1
type F163 = ExtensionField F2 P163
f163 = map Ext (polys 163 f2) :: [F163]
a163 = embed x :: F163
data BAff = BAff {bx :: F163, by :: F163} deriving (Eq, Ord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 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1]
w1 = x1 + y1
w2 = x2 + y2
a = (x1 ^ 2) + x1
b = (y1 ^ 2) + y1
c = dd * w1 + w2
d = x2 * y2
x3 = y1 + (c + cc * (w1 + y2) + a * (d + x2)) / (cc + a * w2)
y3 = x1 + (c + cc * (w1 + y2) + b * (d + y2)) / (cc + b * w2)
(*) = undefined
abs = undefined
signum = undefined
fromInteger n = BAff (fromInteger n) (fromInteger n)
negate (BAff x y) = BAff y x
infix 8 /*\
(/*\) :: F163 -> BAff -> BAff
0 /*\ _ = BAff 0 0
1 /*\ a = a
n /*\ a = a + (n - 1) /*\ a