crypto/bn256: refactor to use bitutil.TestBytes

This commit is contained in:
cuiweixie 2025-08-14 20:16:38 +08:00
parent 25cce4dfe4
commit b76adce4d6
2 changed files with 4 additions and 11 deletions

View file

@ -5,6 +5,7 @@ import (
"math/big" "math/big"
"github.com/consensys/gnark-crypto/ecc/bn254" "github.com/consensys/gnark-crypto/ecc/bn254"
"github.com/ethereum/go-ethereum/common/bitutil"
) )
// G1 is the affine representation of a G1 group element. // G1 is the affine representation of a G1 group element.
@ -43,7 +44,7 @@ func (g *G1) Unmarshal(buf []byte) (int, error) {
return 0, errors.New("invalid G1 point size") return 0, errors.New("invalid G1 point size")
} }
if allZeroes(buf[:64]) { if !bitutil.TestBytes(buf[:64]) {
// point at infinity // point at infinity
g.inner.X.SetZero() g.inner.X.SetZero()
g.inner.Y.SetZero() g.inner.Y.SetZero()
@ -82,12 +83,3 @@ func (p *G1) Marshal() []byte {
return output return output
} }
func allZeroes(buf []byte) bool {
for i := range buf {
if buf[i] != 0 {
return false
}
}
return true
}

View file

@ -4,6 +4,7 @@ import (
"errors" "errors"
"github.com/consensys/gnark-crypto/ecc/bn254" "github.com/consensys/gnark-crypto/ecc/bn254"
"github.com/ethereum/go-ethereum/common/bitutil"
) )
// G2 is the affine representation of a G2 group element. // G2 is the affine representation of a G2 group element.
@ -31,7 +32,7 @@ func (g *G2) Unmarshal(buf []byte) (int, error) {
return 0, errors.New("invalid G2 point size") return 0, errors.New("invalid G2 point size")
} }
if allZeroes(buf[:128]) { if !bitutil.TestBytes(buf[:128]) {
// point at infinity // point at infinity
g.inner.X.A0.SetZero() g.inner.X.A0.SetZero()
g.inner.X.A1.SetZero() g.inner.X.A1.SetZero()