Skip to content

Commit c988ceb

Browse files
committed
fourq: Correctly unmarshalling point.
1 parent ef2611d commit c988ceb

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

ecc/fourq/curve.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ const Size = 32
1212
// Point represents an affine point of the curve. The identity is (0,1).
1313
type Point struct{ X, Y Fq }
1414

15+
func (P Point) String() string { return "(x: " + P.X.String() + ", y: " + P.Y.String() + ")" }
16+
1517
// CurveParams contains the parameters of the elliptic curve.
1618
type CurveParams struct {
1719
Name string // The canonical name of the curve.

ecc/fourq/point.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -249,24 +249,31 @@ func (P *Point) Marshal(out *[Size]byte) {
249249

250250
// Unmarshal retrieves a point P from the input buffer. On success, returns true.
251251
func (P *Point) Unmarshal(in *[Size]byte) bool {
252+
var Q Point
252253
s := in[Size-1] >> 7
253254
in[Size-1] &= 0x7F
254-
if ok := P.Y.fromBytes(in[:]); !ok {
255-
return ok
256-
}
255+
ok := Q.Y.fromBytes(in[:])
257256
in[Size-1] |= s << 7
257+
if !ok {
258+
return false
259+
}
258260

259261
t0, t1, one := &Fq{}, &Fq{}, &Fq{}
260262
one.setOne()
261-
fqSqr(t0, &P.Y) // t0 = y^2
263+
fqSqr(t0, &Q.Y) // t0 = y^2
262264
fqMul(t1, t0, &paramD) // t1 = d*y^2
263265
fqSub(t0, t0, one) // t0 = y^2 - 1
264266
fqAdd(t1, t1, one) // t1 = d*y^2 + 1
265-
fqSqrt(&P.X, t0, t1, 1-2*int(s)) // x = sqrt(t0/t1)
267+
fqSqrt(&Q.X, t0, t1, 1-2*int(s)) // x = sqrt(t0/t1)
266268

267-
if !P.IsOnCurve() {
268-
fpNeg(&P.X[1], &P.X[1])
269+
if !Q.IsOnCurve() {
270+
fpNeg(&Q.X[1], &Q.X[1])
269271
}
272+
if !Q.IsOnCurve() {
273+
return false
274+
}
275+
276+
*P = Q
270277
return true
271278
}
272279

0 commit comments

Comments
 (0)