|
|
@ -28,6 +28,7 @@ import ( |
|
|
|
"github.com/ethereum/go-ethereum/crypto/blake2b" |
|
|
|
"github.com/ethereum/go-ethereum/crypto/blake2b" |
|
|
|
"github.com/ethereum/go-ethereum/crypto/bls12381" |
|
|
|
"github.com/ethereum/go-ethereum/crypto/bls12381" |
|
|
|
"github.com/ethereum/go-ethereum/crypto/bn256" |
|
|
|
"github.com/ethereum/go-ethereum/crypto/bn256" |
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/crypto/secp256r1" |
|
|
|
"github.com/ethereum/go-ethereum/params" |
|
|
|
"github.com/ethereum/go-ethereum/params" |
|
|
|
"golang.org/x/crypto/ripemd160" |
|
|
|
"golang.org/x/crypto/ripemd160" |
|
|
|
) |
|
|
|
) |
|
|
@ -104,6 +105,12 @@ var PrecompiledContractsBLS = map[common.Address]PrecompiledContract{ |
|
|
|
common.BytesToAddress([]byte{18}): &bls12381MapG2{}, |
|
|
|
common.BytesToAddress([]byte{18}): &bls12381MapG2{}, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// PrecompiledContractsEcverify contains the precompiled Ethereum
|
|
|
|
|
|
|
|
// contract specified in EIP-N. This is exported for testing purposes.
|
|
|
|
|
|
|
|
var PrecompiledContractsEcverify = map[common.Address]PrecompiledContract{ |
|
|
|
|
|
|
|
common.BytesToAddress([]byte{19}): &ecverify{}, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var ( |
|
|
|
var ( |
|
|
|
PrecompiledAddressesBerlin []common.Address |
|
|
|
PrecompiledAddressesBerlin []common.Address |
|
|
|
PrecompiledAddressesIstanbul []common.Address |
|
|
|
PrecompiledAddressesIstanbul []common.Address |
|
|
@ -1048,3 +1055,33 @@ func (c *bls12381MapG2) Run(input []byte) ([]byte, error) { |
|
|
|
// Encode the G2 point to 256 bytes
|
|
|
|
// Encode the G2 point to 256 bytes
|
|
|
|
return g.EncodePoint(r), nil |
|
|
|
return g.EncodePoint(r), nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ECVERIFY (secp256r1 signature verification)
|
|
|
|
|
|
|
|
// implemented as a native contract
|
|
|
|
|
|
|
|
type ecverify struct{} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// RequiredGas returns the gas required to execute the precompiled contract
|
|
|
|
|
|
|
|
func (c *ecverify) RequiredGas(input []byte) uint64 { |
|
|
|
|
|
|
|
return params.EcverifyGas |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Run executes the precompiled contract, returning the output and the used gas
|
|
|
|
|
|
|
|
func (c *ecverify) Run(input []byte) ([]byte, error) { |
|
|
|
|
|
|
|
// Required input length is 160 bytes
|
|
|
|
|
|
|
|
const ecverifyInputLength = 160 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// "input" is (hash, r, s, x, y), each 32 bytes
|
|
|
|
|
|
|
|
input = common.RightPadBytes(input, ecverifyInputLength) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Extract the hash, r, s, x, y from the input
|
|
|
|
|
|
|
|
hash := input[0:32] |
|
|
|
|
|
|
|
r, s := new(big.Int).SetBytes(input[32:64]), new(big.Int).SetBytes(input[64:96]) |
|
|
|
|
|
|
|
x, y := new(big.Int).SetBytes(input[96:128]), new(big.Int).SetBytes(input[128:160]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Verify the secp256r1 signature
|
|
|
|
|
|
|
|
if result, err := secp256r1.Verify(hash, r, s, x, y); err != nil { |
|
|
|
|
|
|
|
return nil, err |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
return result, nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|