~moody/libdp9ik

libdp9ik/elligator2.go -rw-r--r-- 2.9 KiB
4efa26b9 — Jacob Moody Progress made towards authpak_new and authpak_finish 9 months ago
`                                                                                `
```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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137```
```package libdp9ik

import (
"math/big"
)

func modmul(b1 *big.Int, b2 *big.Int, m *big.Int, r *big.Int) {
r.Mul(b1, b2)
r.Mod(r, m)
}

func modadd(b1 *big.Int, b2 *big.Int, m *big.Int, r *big.Int) {
r.Mod(r, m)
}

func modsub(b1 *big.Int, b2 *big.Int, m *big.Int, r *big.Int) {
r.Sub(b1, b2)
r.Mod(r, m)
}

func misqrt(a *big.Int, p *big.Int, r *big.Int) {
e := big.NewInt(0)
tmp1 := big.NewInt(4)
tmp1.Mod(p, tmp1)
tmp2 := big.NewInt(3)
if tmp1.Cmp(tmp2) == 0 {
e.SetUint64(3)
e.Sub(p, e)
e.Rsh(e, 2)
r.Exp(a, e, p)
} else {
r.ModSqrt(a, p)
if r.Cmp(big.NewInt(0)) != 0 {
r.ModInverse(r, p)
}
}
}

//Probably not safe from timing attacks
func elligator2(p *big.Int, a *big.Int, d *big.Int, n *big.Int, r0 *big.Int, X *big.Int, Y *big.Int, Z *big.Int, T *big.Int) {
t := big.NewInt(0)
s := big.NewInt(0)
e := big.NewInt(0)
c := big.NewInt(0)
ND := big.NewInt(0)
N := big.NewInt(0)
D := big.NewInt(0)
r := big.NewInt(0)
tmp1 := big.NewInt(0)

modmul(n, r0, p, tmp1)
modmul(tmp1, r0, p, r)
tmp1.SetInt64(0)
modmul(d, r, p, tmp1)
modsub(tmp1, d, p, tmp1)
tmp2 := big.NewInt(0)
modmul(d, r, p, tmp2)
tmp3 := big.NewInt(0)
modmul(a, r, p, tmp3)
modsub(tmp2, tmp3, p, tmp2)
modsub(tmp2, d, p, tmp2)
modmul(tmp1, tmp2, p, D)
tmp2.SetInt64(0)
tmp1.SetInt64(0)
modsub(a, tmp1, p, tmp1)
modmul(tmp2, tmp1, p, N)
modmul(N, D, p, ND)
if ND.Cmp(big.NewInt(0)) == 0 {
c.SetInt64(1)
e.SetInt64(0)
} else {
e.ModSqrt(ND, p)
if e.Cmp(big.NewInt(0)) != 0 {
c.SetInt64(1)
e.ModInverse(e, p)
} else {
modsub(big.NewInt(0), big.NewInt(1), p, c)
tmp4 := big.NewInt(0)
modmul(n, r0, p, tmp4)
tmp5 := big.NewInt(0)
tmp6 := big.NewInt(0)
modmul(n, ND, p, tmp6)
misqrt(tmp6, p, tmp5)
modmul(tmp4, tmp5, p, e)
}
}
tmp1.SetInt64(0)
modmul(c, N, p, tmp1)
modmul(tmp1, e, p, s)
tmp1.SetInt64(0)
tmp2.SetInt64(0)
modmul(c, N, p, tmp2)
tmp3.SetInt64(0)
modsub(r, big.NewInt(1), p, tmp3)
modmul(tmp2, tmp3, p, tmp1)
tmp3.SetInt64(0)
tmp2.SetInt64(0)
modsub(a, tmp2, p, tmp2)
modmul(tmp2, e, p, tmp3)
modmul(tmp3, tmp3, p, tmp3)
modmul(tmp1, tmp3, p, t)
modsub(big.NewInt(0), t, p, t)
modsub(t, big.NewInt(1), p, t)
tmp3.SetInt64(0)
modmul(tmp3, t, p, X)
tmp3.SetInt64(0)
tmp1.SetInt64(0)
modmul(a, s, p, tmp1)
modmul(tmp1, s, p, tmp3)
modsub(big.NewInt(1), tmp3, p, tmp3)
tmp1.SetInt64(0)
tmp2.SetInt64(0)
modmul(a, s, p, tmp2)
modmul(tmp2, s, p, tmp1)
modmul(tmp3, tmp1, p, Y)
tmp1.SetInt64(0)
tmp3.SetInt64(0)
modmul(a, s, p, tmp3)
modmul(tmp3, s, p, tmp1)