~moody/libdp9ik

libdp9ik/decaf.go -rw-r--r-- 2.5 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
package libdp9ik

import "math/big"
import "unsafe"

func decaf_neg(p *big.Int, n *big.Int, r *big.Int) {
	m := big.NewInt(0)
	modsub(big.NewInt(0), r, p, m)
	tmp1 := big.NewInt(0)
	tmp1.Sub(p, big.NewInt(1))
	tmp1.Rsh(tmp1, 1)
	c := -tmp1.Cmp(n)
	if (c >> (unsafe.Sizeof(c)*8 - 1)) != 0 {
		r.Set(m)
	}
}

func decaf_encode(p *big.Int, a *big.Int, d *big.Int, X *big.Int, Y *big.Int, Z *big.Int, T *big.Int, s *big.Int) {
	u := big.NewInt(0)
	r := big.NewInt(0)
	tmp1 := big.NewInt(0)
	tmp2 := big.NewInt(0)
	tmp3 := big.NewInt(0)
	modsub(a, d, p, tmp3)
	tmp4 := big.NewInt(0)
	modadd(Z, Y, p, tmp4)
	modmul(tmp3, tmp4, p, tmp2)
	tmp4.SetInt64(0)
	modsub(Z, Y, p, tmp4)
	modmul(tmp2, tmp4, p, tmp1)
	misqrt(tmp1, p, r)
	tmp1.SetInt64(0)
	modsub(a, d, p, tmp1)
	modmul(tmp1, r, p, u)
	tmp1.SetInt64(0)
	tmp4.SetInt64(0)
	modadd(u, u, p, tmp4)
	modmul(tmp4, Z, p, tmp1)
	modsub(big.NewInt(0), tmp1, p, tmp1)
	decaf_neg(p, tmp1, r)
	tmp1.SetInt64(0)
	tmp4.SetInt64(0)
	tmp2.SetInt64(0)
	tmp3.SetInt64(0)
	modmul(a, Z, p, tmp3)
	modmul(tmp3, X, p, tmp2)
	tmp3.SetInt64(0)
	tmp5 := big.NewInt(0)
	modmul(d, Y, p, tmp5)
	modmul(tmp5, T, p, tmp3)
	modsub(tmp2, tmp3, p, tmp2)
	modmul(r, tmp2, p, tmp4)
	modadd(tmp4, Y, p, tmp4)
	modmul(u, tmp4, p, tmp1)
	tmp4.SetInt64(0)
	tmp4.ModInverse(a, p)
	modmul(tmp1, tmp4, p, s)
	decaf_neg(p, s, s)
}

func decaf_decode(p *big.Int, a *big.Int, d *big.Int, s *big.Int, ok *big.Int, X *big.Int, Y *big.Int, Z *big.Int, T *big.Int) {
	w := big.NewInt(0)
	v := big.NewInt(0)
	u := big.NewInt(0)
	ss := big.NewInt(0)
	tmp1 := big.NewInt(0)
	tmp1.Sub(p, big.NewInt(1))
	tmp1.Rsh(tmp1, 1)
	if tmp1.Cmp(s) > 0 {
		ok.SetInt64(0)
	} else {
		modmul(s, s, p, ss)
		modmul(a, ss, p, Z)
		modadd(big.NewInt(1), Z, p, Z)
		modmul(Z, Z, p, u)
		tmp2 := big.NewInt(0)
		tmp3 := big.NewInt(0)
		tmp4 := big.NewInt(0)
		tmp4.SetUint64(4)
		modmul(tmp4, d, p, tmp3)
		modmul(tmp3, ss, p, tmp2)
		modsub(u, tmp2, p, u)
		modmul(u, ss, p, v)
		if big.NewInt(0).Cmp(v) == 0 {
			ok.SetInt64(1)
		} else {
			ok.ModSqrt(v, p)
			if big.NewInt(0).Cmp(ok) != 0 {
				v.ModInverse(ok, p)
				ok.SetInt64(1)
			}
		}
		if big.NewInt(0).Cmp(ok) != 0 {
			tmp5 := big.NewInt(0)
			modmul(u, v, p, tmp5)
			decaf_neg(p, tmp5, v)
			tmp5.SetInt64(0)
			modmul(v, s, p, tmp5)
			tmp6 := big.NewInt(0)
			modsub(big.NewInt(2), Z, p, tmp6)
			modmul(tmp5, tmp6, p, w)
			if big.NewInt(0).Cmp(s) == 0 {
				modadd(w, big.NewInt(1), p, w)
			}
			modadd(s, s, p, X)
			modmul(w, Z, p, Y)
			modmul(w, X, p, T)
		}
	}
}