1 package goldilocks
2
3 import (
4 "fmt"
5
6 fp "github.com/cloudflare/circl/math/fp448"
7 )
8
9 type twistPoint struct{ x, y, z, ta, tb fp.Elt }
10
11 type preTwistPointAffine struct{ addYX, subYX, dt2 fp.Elt }
12
13 type preTwistPointProy struct {
14 preTwistPointAffine
15 z2 fp.Elt
16 }
17
18 func (P *twistPoint) String() string {
19 return fmt.Sprintf("x: %v\ny: %v\nz: %v\nta: %v\ntb: %v", P.x, P.y, P.z, P.ta, P.tb)
20 }
21
22
23 func (P *twistPoint) cneg(b uint) {
24 t := &fp.Elt{}
25 fp.Neg(t, &P.x)
26 fp.Cmov(&P.x, t, b)
27 fp.Neg(t, &P.ta)
28 fp.Cmov(&P.ta, t, b)
29 }
30
31
32 func (P *twistPoint) Double() {
33
34
35
36 Px, Py, Pz, Pta, Ptb := &P.x, &P.y, &P.z, &P.ta, &P.tb
37 a, b, c, e, f, g, h := Px, Py, Pz, Pta, Px, Py, Ptb
38 fp.Add(e, Px, Py)
39 fp.Sqr(a, Px)
40 fp.Sqr(b, Py)
41 fp.Sqr(c, Pz)
42 fp.Add(c, c, c)
43 fp.Add(h, a, b)
44 fp.Sqr(e, e)
45 fp.Sub(e, e, h)
46 fp.Sub(g, b, a)
47 fp.Sub(f, c, g)
48 fp.Mul(Pz, f, g)
49 fp.Mul(Px, e, f)
50 fp.Mul(Py, g, h)
51 }
52
53
54 func (P *twistPoint) mixAddZ1(Q *preTwistPointAffine) {
55 fp.Add(&P.z, &P.z, &P.z)
56 P.coreAddition(Q)
57 }
58
59
60 func (P *twistPoint) coreAddition(Q *preTwistPointAffine) {
61
62
63
64 Px, Py, Pz, Pta, Ptb := &P.x, &P.y, &P.z, &P.ta, &P.tb
65 addYX2, subYX2, dt2 := &Q.addYX, &Q.subYX, &Q.dt2
66 a, b, c, d, e, f, g, h := Px, Py, &fp.Elt{}, Pz, Pta, Px, Py, Ptb
67 fp.Mul(c, Pta, Ptb)
68 fp.Sub(h, Py, Px)
69 fp.Add(b, Py, Px)
70 fp.Mul(a, h, subYX2)
71 fp.Mul(b, b, addYX2)
72 fp.Mul(c, c, dt2)
73 fp.Sub(e, b, a)
74 fp.Add(h, b, a)
75 fp.Sub(f, d, c)
76 fp.Add(g, d, c)
77 fp.Mul(Pz, f, g)
78 fp.Mul(Px, e, f)
79 fp.Mul(Py, g, h)
80 }
81
82 func (P *preTwistPointAffine) neg() {
83 P.addYX, P.subYX = P.subYX, P.addYX
84 fp.Neg(&P.dt2, &P.dt2)
85 }
86
87 func (P *preTwistPointAffine) cneg(b int) {
88 t := &fp.Elt{}
89 fp.Cswap(&P.addYX, &P.subYX, uint(b))
90 fp.Neg(t, &P.dt2)
91 fp.Cmov(&P.dt2, t, uint(b))
92 }
93
94 func (P *preTwistPointAffine) cmov(Q *preTwistPointAffine, b uint) {
95 fp.Cmov(&P.addYX, &Q.addYX, b)
96 fp.Cmov(&P.subYX, &Q.subYX, b)
97 fp.Cmov(&P.dt2, &Q.dt2, b)
98 }
99
100
101 func (P *twistPoint) mixAdd(Q *preTwistPointProy) {
102 fp.Mul(&P.z, &P.z, &Q.z2)
103 P.coreAddition(&Q.preTwistPointAffine)
104 }
105
106
107 func (P *twistPoint) oddMultiples(T []preTwistPointProy) {
108 if n := len(T); n > 0 {
109 T[0].FromTwistPoint(P)
110 _2P := *P
111 _2P.Double()
112 R := &preTwistPointProy{}
113 R.FromTwistPoint(&_2P)
114 for i := 1; i < n; i++ {
115 P.mixAdd(R)
116 T[i].FromTwistPoint(P)
117 }
118 }
119 }
120
121
122 func (P *preTwistPointProy) cmov(Q *preTwistPointProy, b uint) {
123 P.preTwistPointAffine.cmov(&Q.preTwistPointAffine, b)
124 fp.Cmov(&P.z2, &Q.z2, b)
125 }
126
127
128 func (P *preTwistPointProy) FromTwistPoint(Q *twistPoint) {
129 fp.Add(&P.addYX, &Q.y, &Q.x)
130 fp.Sub(&P.subYX, &Q.y, &Q.x)
131 fp.Mul(&P.dt2, &Q.ta, &Q.tb)
132 fp.Mul(&P.dt2, &P.dt2, ¶mDTwist)
133 fp.Add(&P.dt2, &P.dt2, &P.dt2)
134 fp.Add(&P.z2, &Q.z, &Q.z)
135 }
136
View as plain text