mirror of
https://github.com/luxfi/crypto.git
synced 2026-07-27 01:54:50 +00:00
fix(bn256/gnark): fix inverted TestBytes logic in G1/G2 Unmarshal
The TestBytes function returns true when all bytes are zero (point at infinity). The condition was inverted, causing non-zero points to be treated as infinity.
This commit is contained in:
+2
-2
@@ -44,8 +44,8 @@ func (g *G1) Unmarshal(buf []byte) (int, error) {
|
||||
return 0, errors.New("invalid G1 point size")
|
||||
}
|
||||
|
||||
if !bitutil.TestBytes(buf[:64]) {
|
||||
// point at infinity
|
||||
if bitutil.TestBytes(buf[:64]) {
|
||||
// point at infinity (all zeros)
|
||||
g.inner.X.SetZero()
|
||||
g.inner.Y.SetZero()
|
||||
return 64, nil
|
||||
|
||||
+2
-2
@@ -32,8 +32,8 @@ func (g *G2) Unmarshal(buf []byte) (int, error) {
|
||||
return 0, errors.New("invalid G2 point size")
|
||||
}
|
||||
|
||||
if !bitutil.TestBytes(buf[:128]) {
|
||||
// point at infinity
|
||||
if bitutil.TestBytes(buf[:128]) {
|
||||
// point at infinity (all zeros)
|
||||
g.inner.X.A0.SetZero()
|
||||
g.inner.X.A1.SetZero()
|
||||
g.inner.Y.A0.SetZero()
|
||||
|
||||
@@ -28,10 +28,14 @@ func TestSerRoundTrip(t *testing.T) {
|
||||
bytesG2 := expectedG2.Marshal()
|
||||
|
||||
var gotG1 G1
|
||||
gotG1.Unmarshal(bytesG1)
|
||||
if _, err := gotG1.Unmarshal(bytesG1); err != nil {
|
||||
t.Fatalf("G1 unmarshal failed: %v", err)
|
||||
}
|
||||
|
||||
var gotG2 G2
|
||||
gotG2.Unmarshal(bytesG2)
|
||||
if _, err := gotG2.Unmarshal(bytesG2); err != nil {
|
||||
t.Fatalf("G2 unmarshal failed: %v", err)
|
||||
}
|
||||
|
||||
if !expectedG1.inner.Equal(&gotG1.inner) {
|
||||
t.Errorf("serialization roundtrip failed for G1")
|
||||
|
||||
Reference in New Issue
Block a user