Skip to content

Commit eef0878

Browse files
committed
fourq: Test showing isEqual and IsOnCurve fail.
1 parent 2298474 commit eef0878

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

ecc/fourq/point_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,61 @@ func (P *pointR1) random() {
1515
P.ScalarBaseMult(&k)
1616
}
1717

18+
func TestPoint(t *testing.T) {
19+
const testTimes = 1 << 10
20+
t.Run("IsOnCurve(ok)", func(t *testing.T) {
21+
var gen Point
22+
var goodGen pointR1
23+
gen.SetGenerator()
24+
gen.toR1(&goodGen)
25+
test.CheckOk(goodGen.IsOnCurve(), "valid point should pass", t)
26+
})
27+
28+
t.Run("IsOnCurve(zero)", func(t *testing.T) {
29+
var allZeros pointR1
30+
test.CheckOk(!allZeros.IsOnCurve(), "invalid point should be detected", t)
31+
})
32+
33+
t.Run("IsOnCurve(bad)", func(t *testing.T) {
34+
var badGen pointR1
35+
badGen.X = genX
36+
badGen.Y = genY
37+
test.CheckOk(!badGen.IsOnCurve(), "invalid point should be detected", t)
38+
})
39+
40+
t.Run("IsEqual", func(t *testing.T) {
41+
var badGen pointR1
42+
badGen.X = genX
43+
badGen.Y = genY
44+
var gen Point
45+
var goodGen pointR1
46+
gen.SetGenerator()
47+
gen.toR1(&goodGen)
48+
test.CheckOk(!badGen.isEqual(&goodGen), "invalid point shouldn't match generator", t)
49+
test.CheckOk(!goodGen.isEqual(&badGen), "invalid point shouldn't match generator", t)
50+
test.CheckOk(goodGen.isEqual(&goodGen), "valid point should match generator", t)
51+
test.CheckOk(!badGen.isEqual(&badGen), "invalid point shouldn't match anything", t)
52+
})
53+
54+
t.Run("isEqual(fail-w/random)", func(t *testing.T) {
55+
var badG pointR1
56+
badG.X = genX
57+
badG.Y = genY
58+
test.CheckOk(!badG.IsOnCurve(), "invalid point should be detected", t)
59+
60+
var k [Size]byte
61+
var got, want pointR1
62+
for i := 0; i < testTimes; i++ {
63+
_, _ = rand.Read(k[:])
64+
got.ScalarMult(&k, &badG)
65+
want.random()
66+
if got.isEqual(&want) {
67+
test.ReportError(t, got, want, k)
68+
}
69+
}
70+
})
71+
}
72+
1873
func TestPointAddition(t *testing.T) {
1974
const testTimes = 1 << 10
2075
var P, Q pointR1
@@ -121,6 +176,16 @@ func TestScalarMult(t *testing.T) {
121176
}
122177
}
123178
})
179+
t.Run("mult-non_curve_point_issue", func(t *testing.T) {
180+
for i := 0; i < testTimes; i++ {
181+
_, _ = rand.Read(k[:])
182+
Q.random()
183+
P.ScalarMult(&k, &Q)
184+
if !P.IsOnCurve() {
185+
t.Fatalf("Point is not on curve: %X\n", P)
186+
}
187+
}
188+
})
124189
}
125190

126191
func TestScalar(t *testing.T) {

0 commit comments

Comments
 (0)