Skip to content

Commit

Permalink
add tests for SetBytesUncompressed border cases
Browse files Browse the repository at this point in the history
Signed-off-by: Ignacio Hagopian <jsign.uy@gmail.com>
  • Loading branch information
jsign committed Dec 4, 2023
1 parent 2e47660 commit 4d343c5
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions banderwagon/element_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,48 @@ func TestBatchNormalize(t *testing.T) {
})
}

func TestSetUncompressedFail(t *testing.T) {
t.Parallel()
one := fp.One()

t.Run("X not in curve", func(t *testing.T) {
startX := one
// Find in startX a point that isn't in the curve
for {
point := bandersnatch.GetPointFromX(&startX, true)
if point == nil {
break
}
startX.Add(&startX, &one)
continue
}
var serializedPoint [UncompressedSize]byte
xBytes := startX.Bytes()
yBytes := Generator.inner.Y.Bytes() // Use some valid-ish Y, but this shouldn't matter much.
copy(serializedPoint[:], xBytes[:])
copy(serializedPoint[CompressedSize:], yBytes[:])

var point2 Element
if err := point2.SetBytesUncompressed(serializedPoint[:], false); err == nil {
t.Fatalf("the point must be rejected")
}
})

t.Run("wrong Y", func(t *testing.T) {
gen := Generator
// Despite X would lead to a point in the curve,
// we modify Y+1 to check the provided (serialized) Y
// coordinate isn't trusted blindly.
gen.inner.Y.Add(&gen.inner.Y, &one)

pointBytes := gen.BytesUncompressed()
var point2 Element
if err := point2.SetBytesUncompressed(pointBytes[:], false); err == nil {
t.Fatalf("the point must be rejected")
}
})
}

func FuzzDeserializationCompressed(f *testing.F) {
f.Fuzz(func(t *testing.T, serializedpoint []byte) {
var point Element
Expand Down

0 comments on commit 4d343c5

Please sign in to comment.