@@ -2,6 +2,7 @@ package goldilocks
2
2
3
3
import (
4
4
"fmt"
5
+ "unsafe"
5
6
6
7
fp "github.com/cloudflare/circl/math/fp448"
7
8
)
@@ -13,15 +14,6 @@ func (P Point) String() string {
13
14
return fmt .Sprintf ("x: %v\n y: %v\n z: %v\n ta: %v\n tb: %v" , P .x , P .y , P .z , P .ta , P .tb )
14
15
}
15
16
16
- // // FromAffine creates a point from affine coordinates.
17
- // func FromAffine(x, y *fp.Elt) (*Point, error) {
18
- // P := &Point{x: *x, y: *y, z: fp.One(), ta: *x, tb: *y}
19
- // if !(Curve{}).IsOnCurve(P) {
20
- // return nil, errors.New("point not on curve")
21
- // }
22
- // return P, nil
23
- // }
24
-
25
17
// isLessThan returns true if 0 <= x < y, and assumes that slices are of the
26
18
// same length and are interpreted in little-endian order.
27
19
func isLessThan (x , y []byte ) bool {
@@ -110,7 +102,7 @@ func (P *Point) Add(Q *Point) {
110
102
// CurveEncodingSize bytes of data.
111
103
func (P * Point ) UnmarshalBinary (data []byte ) error {
112
104
if len (data ) < CurveEncodingSize {
113
- return errInvalidDecoding
105
+ return ErrInvalidDecoding
114
106
}
115
107
116
108
x , y := & fp.Elt {}, & fp.Elt {}
@@ -123,17 +115,26 @@ func (P *Point) UnmarshalBinary(data []byte) error {
123
115
one := fp .One ()
124
116
fp .Sqr (u , y ) // u = y^2
125
117
fp .Mul (v , u , & paramD ) // v = dy^2
126
- fp .Sub (u , u , & one ) // u = y^2-a
118
+ fp .Sub (u , u , & one ) // u = y^2-1
127
119
fp .Sub (v , v , & one ) // v = dy^2-a
128
120
isQR := fp .InvSqrt (x , u , v ) // x = sqrt(u/v)
129
121
isValidXSign := ! (fp .IsZero (x ) && signX == 1 )
130
122
fp .Neg (u , x ) // u = -x
131
123
fp .Cmov (x , u , uint (signX ^ (x [0 ]& 1 ))) // if signX != x mod 2
132
- if ! (isLessThanP && isQR && isValidXSign ) {
133
- return errInvalidDecoding
124
+
125
+ isValid := isLessThanP && isQR && isValidXSign
126
+ b := * ((* uint )(unsafe .Pointer (& isValid )))
127
+ fp .Cmov (& P .x , x , b )
128
+ fp .Cmov (& P .y , y , b )
129
+ fp .Cmov (& P .ta , x , b )
130
+ fp .Cmov (& P .tb , y , b )
131
+ fp .Cmov (& P .z , & one , b )
132
+
133
+ var err error
134
+ if ! isValid {
135
+ err = ErrInvalidDecoding
134
136
}
135
- P .x , P .y , P .ta , P .tb , P .z = * x , * y , * x , * y , one
136
- return nil
137
+ return err
137
138
}
138
139
139
140
// MarshalBinary returns a unique encoding of the point P.
0 commit comments