Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

banderwagon: check point in curve in SetBytesUncompressed #69

Merged
merged 3 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions banderwagon/element.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package banderwagon

import (
"bytes"
"errors"
"fmt"
"math/big"
Expand Down Expand Up @@ -243,17 +244,27 @@ func (p *Element) SetBytesUncompressed(buf []byte, trusted bool) error {
var x fp.Element
x.SetBytes(buf[:coordinateSize])

// subgroup check
var y fp.Element
// point in curve & subgroup check
if !trusted {
point := bandersnatch.GetPointFromX(&x, true)
if point == nil {
return fmt.Errorf("point not in the curve")
}
calculatedYBytes := point.Y.Bytes()
if !bytes.Equal(calculatedYBytes[:], buf[coordinateSize:]) {
return fmt.Errorf("provided Y coordinate doesn't correspond to X")
}
y = point.Y
kevaundray marked this conversation as resolved.
Show resolved Hide resolved

err := subgroupCheck(x)
if err != nil {
return err
}
} else {
y.SetBytes(buf[coordinateSize:])
}

var y fp.Element
y.SetBytes(buf[coordinateSize:])

*p = Element{inner: bandersnatch.PointProj{
X: x,
Y: y,
Expand Down
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