mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-06-17 12:21:38 +00:00
core/vm: reduce heap allocations in precompile contracts
This commit is contained in:
parent
fc43170cdd
commit
59663aff1b
1 changed files with 58 additions and 50 deletions
|
|
@ -355,7 +355,9 @@ func (c *ripemd160hash) RequiredGas(input []byte) uint64 {
|
||||||
func (c *ripemd160hash) Run(input []byte) ([]byte, error) {
|
func (c *ripemd160hash) Run(input []byte) ([]byte, error) {
|
||||||
ripemd := ripemd160.New()
|
ripemd := ripemd160.New()
|
||||||
ripemd.Write(input)
|
ripemd.Write(input)
|
||||||
return common.LeftPadBytes(ripemd.Sum(nil), 32), nil
|
// Pre-allocate 32 bytes with 12-byte zero prefix for left-padding.
|
||||||
|
// Sum appends the 20-byte hash after the prefix.
|
||||||
|
return ripemd.Sum(make([]byte, 12, 32)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *ripemd160hash) Name() string {
|
func (c *ripemd160hash) Name() string {
|
||||||
|
|
@ -640,19 +642,21 @@ func (c *bigModExp) Run(input []byte) ([]byte, error) {
|
||||||
base = new(patched_big.Int).SetBytes(getData(input, 0, baseLen))
|
base = new(patched_big.Int).SetBytes(getData(input, 0, baseLen))
|
||||||
exp = new(patched_big.Int).SetBytes(getData(input, baseLen, expLen))
|
exp = new(patched_big.Int).SetBytes(getData(input, baseLen, expLen))
|
||||||
mod = new(patched_big.Int).SetBytes(getData(input, baseLen+expLen, modLen))
|
mod = new(patched_big.Int).SetBytes(getData(input, baseLen+expLen, modLen))
|
||||||
v []byte
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Allocate the result buffer once, zero-filled.
|
||||||
|
result := make([]byte, modLen)
|
||||||
switch {
|
switch {
|
||||||
case mod.BitLen() == 0:
|
case mod.BitLen() == 0:
|
||||||
// Modulo 0 is undefined, return zero
|
// Modulo 0 is undefined, return zero
|
||||||
return common.LeftPadBytes([]byte{}, int(modLen)), nil
|
return common.LeftPadBytes([]byte{}, int(modLen)), nil
|
||||||
case base.BitLen() == 1: // a bit length of 1 means it's 1 (or -1).
|
case base.BitLen() == 1: // a bit length of 1 means it's 1 (or -1).
|
||||||
//If base == 1, then we can just return base % mod (if mod >= 1, which it is)
|
//If base == 1, then we can just return base % mod (if mod >= 1, which it is)
|
||||||
v = base.Mod(base, mod).Bytes()
|
result[modLen-1] = 1
|
||||||
default:
|
default:
|
||||||
v = base.Exp(base, exp, mod).Bytes()
|
base.Exp(base, exp, mod).FillBytes(result)
|
||||||
}
|
}
|
||||||
return common.LeftPadBytes(v, int(modLen)), nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *bigModExp) Name() string {
|
func (c *bigModExp) Name() string {
|
||||||
|
|
@ -938,25 +942,25 @@ func (c *bls12381G1Add) Run(input []byte) ([]byte, error) {
|
||||||
if len(input) != 256 {
|
if len(input) != 256 {
|
||||||
return nil, errBLS12381InvalidInputLength
|
return nil, errBLS12381InvalidInputLength
|
||||||
}
|
}
|
||||||
var err error
|
|
||||||
var p0, p1 *bls12381.G1Affine
|
|
||||||
|
|
||||||
// Decode G1 point p_0
|
// Decode G1 point p_0
|
||||||
if p0, err = decodePointG1(input[:128]); err != nil {
|
p0, err := decodePointG1(input[:128])
|
||||||
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
// Decode G1 point p_1
|
// Decode G1 point p_1
|
||||||
if p1, err = decodePointG1(input[128:]); err != nil {
|
p1, err := decodePointG1(input[128:])
|
||||||
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// No need to check the subgroup here, as specified by EIP-2537
|
// No need to check the subgroup here, as specified by EIP-2537
|
||||||
|
|
||||||
// Compute r = p_0 + p_1
|
// Compute r = p_0 + p_1
|
||||||
p0.Add(p0, p1)
|
p0.Add(&p0, &p1)
|
||||||
|
|
||||||
// Encode the G1 point result into 128 bytes
|
// Encode the G1 point result into 128 bytes
|
||||||
return encodePointG1(p0), nil
|
return encodePointG1(&p0), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *bls12381G1Add) Name() string {
|
func (c *bls12381G1Add) Name() string {
|
||||||
|
|
@ -1010,17 +1014,17 @@ func (c *bls12381G1MultiExp) Run(input []byte) ([]byte, error) {
|
||||||
if !p.IsInSubGroup() {
|
if !p.IsInSubGroup() {
|
||||||
return nil, errBLS12381G1PointSubgroup
|
return nil, errBLS12381G1PointSubgroup
|
||||||
}
|
}
|
||||||
points[i] = *p
|
points[i] = p
|
||||||
// Decode scalar value
|
// Decode scalar value
|
||||||
scalars[i] = *new(fr.Element).SetBytes(input[t1:t2])
|
scalars[i].SetBytes(input[t1:t2])
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compute r = e_0 * p_0 + e_1 * p_1 + ... + e_(k-1) * p_(k-1)
|
// Compute r = e_0 * p_0 + e_1 * p_1 + ... + e_(k-1) * p_(k-1)
|
||||||
r := new(bls12381.G1Affine)
|
var r bls12381.G1Affine
|
||||||
r.MultiExp(points, scalars, ecc.MultiExpConfig{})
|
r.MultiExp(points, scalars, ecc.MultiExpConfig{})
|
||||||
|
|
||||||
// Encode the G1 point to 128 bytes
|
// Encode the G1 point to 128 bytes
|
||||||
return encodePointG1(r), nil
|
return encodePointG1(&r), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *bls12381G1MultiExp) Name() string {
|
func (c *bls12381G1MultiExp) Name() string {
|
||||||
|
|
@ -1042,26 +1046,26 @@ func (c *bls12381G2Add) Run(input []byte) ([]byte, error) {
|
||||||
if len(input) != 512 {
|
if len(input) != 512 {
|
||||||
return nil, errBLS12381InvalidInputLength
|
return nil, errBLS12381InvalidInputLength
|
||||||
}
|
}
|
||||||
var err error
|
|
||||||
var p0, p1 *bls12381.G2Affine
|
|
||||||
|
|
||||||
// Decode G2 point p_0
|
// Decode G2 point p_0
|
||||||
if p0, err = decodePointG2(input[:256]); err != nil {
|
p0, err := decodePointG2(input[:256])
|
||||||
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
// Decode G2 point p_1
|
// Decode G2 point p_1
|
||||||
if p1, err = decodePointG2(input[256:]); err != nil {
|
p1, err := decodePointG2(input[256:])
|
||||||
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// No need to check the subgroup here, as specified by EIP-2537
|
// No need to check the subgroup here, as specified by EIP-2537
|
||||||
|
|
||||||
// Compute r = p_0 + p_1
|
// Compute r = p_0 + p_1
|
||||||
r := new(bls12381.G2Affine)
|
var r bls12381.G2Affine
|
||||||
r.Add(p0, p1)
|
r.Add(&p0, &p1)
|
||||||
|
|
||||||
// Encode the G2 point into 256 bytes
|
// Encode the G2 point into 256 bytes
|
||||||
return encodePointG2(r), nil
|
return encodePointG2(&r), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *bls12381G2Add) Name() string {
|
func (c *bls12381G2Add) Name() string {
|
||||||
|
|
@ -1115,17 +1119,20 @@ func (c *bls12381G2MultiExp) Run(input []byte) ([]byte, error) {
|
||||||
if !p.IsInSubGroup() {
|
if !p.IsInSubGroup() {
|
||||||
return nil, errBLS12381G2PointSubgroup
|
return nil, errBLS12381G2PointSubgroup
|
||||||
}
|
}
|
||||||
points[i] = *p
|
points[i] = p
|
||||||
// Decode scalar value
|
// Decode scalar value
|
||||||
scalars[i] = *new(fr.Element).SetBytes(input[t1:t2])
|
scalars[i].SetBytes(input[t1:t2])
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compute r = e_0 * p_0 + e_1 * p_1 + ... + e_(k-1) * p_(k-1)
|
// Compute r = e_0 * p_0 + e_1 * p_1 + ... + e_(k-1) * p_(k-1)
|
||||||
r := new(bls12381.G2Affine)
|
var r bls12381.G2Affine
|
||||||
r.MultiExp(points, scalars, ecc.MultiExpConfig{})
|
_, err := r.MultiExp(points, scalars, ecc.MultiExpConfig{})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
// Encode the G2 point to 256 bytes.
|
// Encode the G2 point to 256 bytes.
|
||||||
return encodePointG2(r), nil
|
return encodePointG2(&r), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *bls12381G2MultiExp) Name() string {
|
func (c *bls12381G2MultiExp) Name() string {
|
||||||
|
|
@ -1152,10 +1159,8 @@ func (c *bls12381Pairing) Run(input []byte) ([]byte, error) {
|
||||||
return nil, errBLS12381InvalidInputLength
|
return nil, errBLS12381InvalidInputLength
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
p := make([]bls12381.G1Affine, k)
|
||||||
p []bls12381.G1Affine
|
q := make([]bls12381.G2Affine, k)
|
||||||
q []bls12381.G2Affine
|
|
||||||
)
|
|
||||||
|
|
||||||
// Decode pairs
|
// Decode pairs
|
||||||
for i := 0; i < k; i++ {
|
for i := 0; i < k; i++ {
|
||||||
|
|
@ -1181,8 +1186,8 @@ func (c *bls12381Pairing) Run(input []byte) ([]byte, error) {
|
||||||
if !p2.IsInSubGroup() {
|
if !p2.IsInSubGroup() {
|
||||||
return nil, errBLS12381G2PointSubgroup
|
return nil, errBLS12381G2PointSubgroup
|
||||||
}
|
}
|
||||||
p = append(p, *p1)
|
p[i] = p1
|
||||||
q = append(q, *p2)
|
q[i] = p2
|
||||||
}
|
}
|
||||||
// Prepare 32 byte output
|
// Prepare 32 byte output
|
||||||
out := make([]byte, 32)
|
out := make([]byte, 32)
|
||||||
|
|
@ -1199,55 +1204,55 @@ func (c *bls12381Pairing) Name() string {
|
||||||
return "BLS12_PAIRING_CHECK"
|
return "BLS12_PAIRING_CHECK"
|
||||||
}
|
}
|
||||||
|
|
||||||
func decodePointG1(in []byte) (*bls12381.G1Affine, error) {
|
func decodePointG1(in []byte) (bls12381.G1Affine, error) {
|
||||||
if len(in) != 128 {
|
if len(in) != 128 {
|
||||||
return nil, errors.New("invalid g1 point length")
|
return bls12381.G1Affine{}, errors.New("invalid g1 point length")
|
||||||
}
|
}
|
||||||
// decode x
|
// decode x
|
||||||
x, err := decodeBLS12381FieldElement(in[:64])
|
x, err := decodeBLS12381FieldElement(in[:64])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return bls12381.G1Affine{}, err
|
||||||
}
|
}
|
||||||
// decode y
|
// decode y
|
||||||
y, err := decodeBLS12381FieldElement(in[64:])
|
y, err := decodeBLS12381FieldElement(in[64:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return bls12381.G1Affine{}, err
|
||||||
}
|
}
|
||||||
elem := bls12381.G1Affine{X: x, Y: y}
|
elem := bls12381.G1Affine{X: x, Y: y}
|
||||||
if !elem.IsOnCurve() {
|
if !elem.IsOnCurve() {
|
||||||
return nil, errors.New("invalid point: not on curve")
|
return bls12381.G1Affine{}, errors.New("invalid point: not on curve")
|
||||||
}
|
}
|
||||||
|
|
||||||
return &elem, nil
|
return elem, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// decodePointG2 given encoded (x, y) coordinates in 256 bytes returns a valid G2 Point.
|
// decodePointG2 given encoded (x, y) coordinates in 256 bytes returns a valid G2 Point.
|
||||||
func decodePointG2(in []byte) (*bls12381.G2Affine, error) {
|
func decodePointG2(in []byte) (bls12381.G2Affine, error) {
|
||||||
if len(in) != 256 {
|
if len(in) != 256 {
|
||||||
return nil, errors.New("invalid g2 point length")
|
return bls12381.G2Affine{}, errors.New("invalid g2 point length")
|
||||||
}
|
}
|
||||||
x0, err := decodeBLS12381FieldElement(in[:64])
|
x0, err := decodeBLS12381FieldElement(in[:64])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return bls12381.G2Affine{}, err
|
||||||
}
|
}
|
||||||
x1, err := decodeBLS12381FieldElement(in[64:128])
|
x1, err := decodeBLS12381FieldElement(in[64:128])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return bls12381.G2Affine{}, err
|
||||||
}
|
}
|
||||||
y0, err := decodeBLS12381FieldElement(in[128:192])
|
y0, err := decodeBLS12381FieldElement(in[128:192])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return bls12381.G2Affine{}, err
|
||||||
}
|
}
|
||||||
y1, err := decodeBLS12381FieldElement(in[192:])
|
y1, err := decodeBLS12381FieldElement(in[192:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return bls12381.G2Affine{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
p := bls12381.G2Affine{X: bls12381.E2{A0: x0, A1: x1}, Y: bls12381.E2{A0: y0, A1: y1}}
|
p := bls12381.G2Affine{X: bls12381.E2{A0: x0, A1: x1}, Y: bls12381.E2{A0: y0, A1: y1}}
|
||||||
if !p.IsOnCurve() {
|
if !p.IsOnCurve() {
|
||||||
return nil, errors.New("invalid point: not on curve")
|
return bls12381.G2Affine{}, errors.New("invalid point: not on curve")
|
||||||
}
|
}
|
||||||
return &p, err
|
return p, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// decodeBLS12381FieldElement decodes BLS12-381 elliptic curve field element.
|
// decodeBLS12381FieldElement decodes BLS12-381 elliptic curve field element.
|
||||||
|
|
@ -1444,11 +1449,14 @@ func (c *p256Verify) Run(input []byte) ([]byte, error) {
|
||||||
|
|
||||||
// Extract hash, r, s, x, y from the input.
|
// Extract hash, r, s, x, y from the input.
|
||||||
hash := input[0:32]
|
hash := input[0:32]
|
||||||
r, s := new(big.Int).SetBytes(input[32:64]), new(big.Int).SetBytes(input[64:96])
|
var r, s, x, y big.Int
|
||||||
x, y := new(big.Int).SetBytes(input[96:128]), new(big.Int).SetBytes(input[128:160])
|
r.SetBytes(input[32:64])
|
||||||
|
s.SetBytes(input[64:96])
|
||||||
|
x.SetBytes(input[96:128])
|
||||||
|
y.SetBytes(input[128:160])
|
||||||
|
|
||||||
// Verify the signature.
|
// Verify the signature.
|
||||||
if secp256r1.Verify(hash, r, s, x, y) {
|
if secp256r1.Verify(hash, &r, &s, &x, &y) {
|
||||||
return true32Byte, nil
|
return true32Byte, nil
|
||||||
}
|
}
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue