Files
crypto/bn256/gnark/native_format_test.go
zeekay b096c17714 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.
2025-12-14 21:24:02 -08:00

47 lines
1.1 KiB
Go

package bn256
import (
"testing"
"github.com/consensys/gnark-crypto/ecc/bn254"
)
func TestNativeGnarkFormatIncompatibility(t *testing.T) {
// Use official gnark serialization
_, _, g1Gen, _ := bn254.Generators()
wrongSer := g1Gen.Bytes()
var evmG1 G1
_, err := evmG1.Unmarshal(wrongSer[:])
if err == nil {
t.Fatalf("points serialized using the official bn254 serialization algorithm, should not work with the evm format")
}
}
func TestSerRoundTrip(t *testing.T) {
_, _, g1Gen, g2Gen := bn254.Generators()
expectedG1 := G1{inner: g1Gen}
bytesG1 := expectedG1.Marshal()
expectedG2 := G2{inner: g2Gen}
bytesG2 := expectedG2.Marshal()
var gotG1 G1
if _, err := gotG1.Unmarshal(bytesG1); err != nil {
t.Fatalf("G1 unmarshal failed: %v", err)
}
var gotG2 G2
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")
}
if !expectedG2.inner.Equal(&gotG2.inner) {
t.Errorf("serialization roundtrip failed for G2")
}
}