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

feat(backend/solidity): Implement BLS12-381 pairing precompile (0x0a). #1368 #1417

Closed
wants to merge 4 commits into from
Closed
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
65 changes: 65 additions & 0 deletions backend/solidity/bls12_381_pairing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package solidity

import (
"errors"
"math/big"

"github.com/consensys/gnark-crypto/ecc/bls12-381"
"github.com/consensys/gnark-crypto/ecc/bls12-381/fp"
"github.com/consensys/gnark-crypto/ecc/bls12-381/fr"
)

// BLS12381PairingPrecompile implements the BLS12-381 pairing check precompile (0x0a)
// as specified in EIP-2537.
type BLS12381PairingPrecompile struct{}

// Input length for a single pairing check: 2 * 3 * 64 = 384 bytes
const pairLength = 384

// Run implements the BLS12-381 pairing check precompile
func (b *BLS12381PairingPrecompile) Run(input []byte) ([]byte, error) {
// Input length must be a multiple of 384 bytes (pairs of G1, G2 points)
if len(input)%pairLength != 0 {
return nil, errors.New("invalid input length")
}

// If input is empty, return 1 (vacuously true)
if len(input) == 0 {
return []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, nil
}

// Process each pair
var product bls12381.GT
product.SetOne()

for i := 0; i < len(input); i += pairLength {
pair := input[i : i+pairLength]

// Parse G1 point (first 128 bytes)
var g1 bls12381.G1Affine
if _, err := g1.SetBytes(pair[:128]); err != nil {
return nil, errors.New("invalid G1 point")
}

// Parse G2 point (next 256 bytes)
var g2 bls12381.G2Affine
if _, err := g2.SetBytes(pair[128:]); err != nil {
return nil, errors.New("invalid G2 point")
}

// Check if points are in correct subgroup
if !g1.IsInSubGroup() || !g2.IsInSubGroup() {
return nil, errors.New("point not in correct subgroup")
}

// Calculate pairing and multiply with product
res, _ := bls12381.Pair([]bls12381.G1Affine{g1}, []bls12381.G2Affine{g2})
product.Mul(&product, &res)
}

// Check if final product equals 1
if product.IsOne() {
return []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, nil
}
return []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, nil
}
44 changes: 44 additions & 0 deletions backend/solidity/bls12_381_pairing_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package solidity

import (
"testing"

"github.com/consensys/gnark-crypto/ecc/bls12-381"
"github.com/stretchr/testify/require"
)

func TestBLS12381PairingPrecompile(t *testing.T) {
precompile := &BLS12381PairingPrecompile{}

t.Run("empty input", func(t *testing.T) {
result, err := precompile.Run([]byte{})
require.NoError(t, err)
require.Equal(t, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, result)
})

t.Run("invalid input length", func(t *testing.T) {
_, err := precompile.Run(make([]byte, 100))
require.Error(t, err)
})

t.Run("valid pairing check", func(t *testing.T) {
// Generate valid test points
var g1 bls12381.G1Affine
var g2 bls12381.G2Affine
g1.Generator()
g2.Generator()

// Serialize points
g1Bytes := g1.Bytes()
g2Bytes := g2.Bytes()

// Combine into input
input := make([]byte, pairLength)
copy(input[:128], g1Bytes[:])
copy(input[128:], g2Bytes[:])

result, err := precompile.Run(input)
require.NoError(t, err)
require.Equal(t, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, result)
})
}