github.com/theQRL/go-zond@v0.1.1/core/vm/contracts.go (about) 1 // Copyright 2014 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package vm 18 19 import ( 20 "bytes" 21 "crypto/sha256" 22 "encoding/binary" 23 "errors" 24 "fmt" 25 "math/big" 26 27 pkgerrors "github.com/pkg/errors" 28 ssz "github.com/prysmaticlabs/fastssz" 29 "github.com/theQRL/go-zond/common" 30 "github.com/theQRL/go-zond/common/math" 31 "github.com/theQRL/go-zond/crypto/blake2b" 32 "github.com/theQRL/go-zond/crypto/bls12381" 33 "github.com/theQRL/go-zond/crypto/bn256" 34 "github.com/theQRL/go-zond/crypto/kzg4844" 35 "github.com/theQRL/go-zond/params" 36 "golang.org/x/crypto/ripemd160" 37 ) 38 39 // PrecompiledContract is the basic interface for native Go contracts. The implementation 40 // requires a deterministic gas count based on the input size of the Run method of the 41 // contract. 42 type PrecompiledContract interface { 43 RequiredGas(input []byte) uint64 // RequiredPrice calculates the contract gas use 44 Run(input []byte) ([]byte, error) // Run runs the precompiled contract 45 } 46 47 // PrecompiledContractsHomestead contains the default set of pre-compiled Ethereum 48 // contracts used in the Frontier and Homestead releases. 49 var PrecompiledContractsHomestead = map[common.Address]PrecompiledContract{ 50 // common.BytesToAddress([]byte{1}): &ecrecover{}, 51 common.BytesToAddress([]byte{1}): &depositroot{}, 52 common.BytesToAddress([]byte{2}): &sha256hash{}, 53 common.BytesToAddress([]byte{3}): &ripemd160hash{}, 54 common.BytesToAddress([]byte{4}): &dataCopy{}, 55 } 56 57 // PrecompiledContractsByzantium contains the default set of pre-compiled Ethereum 58 // contracts used in the Byzantium release. 59 var PrecompiledContractsByzantium = map[common.Address]PrecompiledContract{ 60 // common.BytesToAddress([]byte{1}): &ecrecover{}, 61 common.BytesToAddress([]byte{1}): &depositroot{}, 62 common.BytesToAddress([]byte{2}): &sha256hash{}, 63 common.BytesToAddress([]byte{3}): &ripemd160hash{}, 64 common.BytesToAddress([]byte{4}): &dataCopy{}, 65 common.BytesToAddress([]byte{5}): &bigModExp{eip2565: false}, 66 common.BytesToAddress([]byte{6}): &bn256AddByzantium{}, 67 common.BytesToAddress([]byte{7}): &bn256ScalarMulByzantium{}, 68 common.BytesToAddress([]byte{8}): &bn256PairingByzantium{}, 69 } 70 71 // PrecompiledContractsIstanbul contains the default set of pre-compiled Ethereum 72 // contracts used in the Istanbul release. 73 var PrecompiledContractsIstanbul = map[common.Address]PrecompiledContract{ 74 // common.BytesToAddress([]byte{1}): &ecrecover{}, 75 common.BytesToAddress([]byte{1}): &depositroot{}, 76 common.BytesToAddress([]byte{2}): &sha256hash{}, 77 common.BytesToAddress([]byte{3}): &ripemd160hash{}, 78 common.BytesToAddress([]byte{4}): &dataCopy{}, 79 common.BytesToAddress([]byte{5}): &bigModExp{eip2565: false}, 80 common.BytesToAddress([]byte{6}): &bn256AddIstanbul{}, 81 common.BytesToAddress([]byte{7}): &bn256ScalarMulIstanbul{}, 82 common.BytesToAddress([]byte{8}): &bn256PairingIstanbul{}, 83 common.BytesToAddress([]byte{9}): &blake2F{}, 84 } 85 86 // PrecompiledContractsBerlin contains the default set of pre-compiled Ethereum 87 // contracts used in the Berlin release. 88 var PrecompiledContractsBerlin = map[common.Address]PrecompiledContract{ 89 // common.BytesToAddress([]byte{1}): &ecrecover{}, 90 common.BytesToAddress([]byte{1}): &depositroot{}, 91 common.BytesToAddress([]byte{2}): &sha256hash{}, 92 common.BytesToAddress([]byte{3}): &ripemd160hash{}, 93 common.BytesToAddress([]byte{4}): &dataCopy{}, 94 common.BytesToAddress([]byte{5}): &bigModExp{eip2565: true}, 95 common.BytesToAddress([]byte{6}): &bn256AddIstanbul{}, 96 common.BytesToAddress([]byte{7}): &bn256ScalarMulIstanbul{}, 97 common.BytesToAddress([]byte{8}): &bn256PairingIstanbul{}, 98 common.BytesToAddress([]byte{9}): &blake2F{}, 99 } 100 101 // PrecompiledContractsCancun contains the default set of pre-compiled Ethereum 102 // contracts used in the Cancun release. 103 var PrecompiledContractsCancun = map[common.Address]PrecompiledContract{ 104 common.BytesToAddress([]byte{1}): &depositroot{}, 105 common.BytesToAddress([]byte{2}): &sha256hash{}, 106 common.BytesToAddress([]byte{3}): &ripemd160hash{}, 107 common.BytesToAddress([]byte{4}): &dataCopy{}, 108 common.BytesToAddress([]byte{5}): &bigModExp{eip2565: true}, 109 common.BytesToAddress([]byte{6}): &bn256AddIstanbul{}, 110 common.BytesToAddress([]byte{7}): &bn256ScalarMulIstanbul{}, 111 common.BytesToAddress([]byte{8}): &bn256PairingIstanbul{}, 112 common.BytesToAddress([]byte{9}): &blake2F{}, 113 common.BytesToAddress([]byte{0x0a}): &kzgPointEvaluation{}, 114 } 115 116 // PrecompiledContractsBLS contains the set of pre-compiled Ethereum 117 // contracts specified in EIP-2537. These are exported for testing purposes. 118 var PrecompiledContractsBLS = map[common.Address]PrecompiledContract{ 119 common.BytesToAddress([]byte{10}): &bls12381G1Add{}, 120 common.BytesToAddress([]byte{11}): &bls12381G1Mul{}, 121 common.BytesToAddress([]byte{12}): &bls12381G1MultiExp{}, 122 common.BytesToAddress([]byte{13}): &bls12381G2Add{}, 123 common.BytesToAddress([]byte{14}): &bls12381G2Mul{}, 124 common.BytesToAddress([]byte{15}): &bls12381G2MultiExp{}, 125 common.BytesToAddress([]byte{16}): &bls12381Pairing{}, 126 common.BytesToAddress([]byte{17}): &bls12381MapG1{}, 127 common.BytesToAddress([]byte{18}): &bls12381MapG2{}, 128 } 129 130 var ( 131 PrecompiledAddressesCancun []common.Address 132 PrecompiledAddressesBerlin []common.Address 133 PrecompiledAddressesIstanbul []common.Address 134 PrecompiledAddressesByzantium []common.Address 135 PrecompiledAddressesHomestead []common.Address 136 ) 137 138 func init() { 139 for k := range PrecompiledContractsHomestead { 140 PrecompiledAddressesHomestead = append(PrecompiledAddressesHomestead, k) 141 } 142 for k := range PrecompiledContractsByzantium { 143 PrecompiledAddressesByzantium = append(PrecompiledAddressesByzantium, k) 144 } 145 for k := range PrecompiledContractsIstanbul { 146 PrecompiledAddressesIstanbul = append(PrecompiledAddressesIstanbul, k) 147 } 148 for k := range PrecompiledContractsBerlin { 149 PrecompiledAddressesBerlin = append(PrecompiledAddressesBerlin, k) 150 } 151 for k := range PrecompiledContractsCancun { 152 PrecompiledAddressesCancun = append(PrecompiledAddressesCancun, k) 153 } 154 } 155 156 // ActivePrecompiles returns the precompiles enabled with the current configuration. 157 func ActivePrecompiles(rules params.Rules) []common.Address { 158 switch { 159 case rules.IsCancun: 160 return PrecompiledAddressesCancun 161 case rules.IsBerlin: 162 return PrecompiledAddressesBerlin 163 case rules.IsIstanbul: 164 return PrecompiledAddressesIstanbul 165 case rules.IsByzantium: 166 return PrecompiledAddressesByzantium 167 default: 168 return PrecompiledAddressesHomestead 169 } 170 } 171 172 // RunPrecompiledContract runs and evaluates the output of a precompiled contract. 173 // It returns 174 // - the returned bytes, 175 // - the _remaining_ gas, 176 // - any error that occurred 177 func RunPrecompiledContract(p PrecompiledContract, input []byte, suppliedGas uint64) (ret []byte, remainingGas uint64, err error) { 178 gasCost := p.RequiredGas(input) 179 if suppliedGas < gasCost { 180 return nil, 0, ErrOutOfGas 181 } 182 suppliedGas -= gasCost 183 output, err := p.Run(input) 184 return output, suppliedGas, err 185 } 186 187 /* 188 // ECRECOVER implemented as a native contract. 189 type ecrecover struct{} 190 191 func (c *ecrecover) RequiredGas(input []byte) uint64 { 192 return params.EcrecoverGas 193 } 194 195 func (c *ecrecover) Run(input []byte) ([]byte, error) { 196 const ecRecoverInputLength = 128 197 198 input = common.RightPadBytes(input, ecRecoverInputLength) 199 // "input" is (hash, v, r, s), each 32 bytes 200 // but for ecrecover we want (r, s, v) 201 202 r := new(big.Int).SetBytes(input[64:96]) 203 s := new(big.Int).SetBytes(input[96:128]) 204 v := input[63] - 27 205 206 // tighter sig s values input homestead only apply to tx sigs 207 if !allZero(input[32:63]) || !crypto.ValidateSignatureValues(v, r, s, false) { 208 return nil, nil 209 } 210 // We must make sure not to modify the 'input', so placing the 'v' along with 211 // the signature needs to be done on a new allocation 212 sig := make([]byte, 65) 213 copy(sig, input[64:128]) 214 sig[64] = v 215 // v needs to be at the end for libsecp256k1 216 pubKey, err := crypto.Ecrecover(input[:32], sig) 217 // make sure the public key is a valid one 218 if err != nil { 219 return nil, nil 220 } 221 222 // the first byte of pubkey is bitcoin heritage 223 return common.LeftPadBytes(crypto.Keccak256(pubKey[1:])[12:], 32), nil 224 } 225 */ 226 227 type depositroot struct{} 228 229 // TODO(rgeraldes24): review required gas 230 func (c *depositroot) RequiredGas(input []byte) uint64 { 231 return params.DepositrootGas 232 } 233 234 // TODO(rgeraldes24): assess why extra bytes are being added 235 func (c *depositroot) Run(input []byte) ([]byte, error) { 236 const depositRootInputLength = 7508 // 7251 in the correct version 237 input = common.RightPadBytes(input, depositRootInputLength) 238 // "input" is (pubkey, withdrawal_credentials, amount, signature) 239 // pubkey is 2592 bytes 240 // withdrawal_credentials is 32 bytes 241 // signature is 4595 bytes 242 243 var amount uint64 244 245 buf := bytes.NewReader(input[2848:2880]) 246 err := binary.Read(buf, binary.LittleEndian, &amount) 247 if err != nil { 248 return nil, err 249 } 250 251 data := &depositdata{ 252 PublicKey: input[160:2752], // 2592 bytes 253 WithdrawalCredentials: input[2784:2816], // 32 bytes 254 Amount: amount, // 32 bytes 255 Signature: input[2912:7507], // 4595 bytes 256 } 257 h, err := data.HashTreeRoot() 258 if err != nil { 259 return nil, pkgerrors.Wrap(err, "could not hash tree root deposit data item") 260 } 261 262 return h[:], nil 263 } 264 265 type depositdata struct { 266 PublicKey []byte 267 WithdrawalCredentials []byte 268 Amount uint64 269 Signature []byte 270 } 271 272 // HashTreeRoot ssz hashes the Deposit_Data object 273 func (d *depositdata) HashTreeRoot() ([32]byte, error) { 274 return ssz.HashWithDefaultHasher(d) 275 } 276 277 // HashTreeRootWith ssz hashes the Deposit_Data object with a hasher 278 func (d *depositdata) HashTreeRootWith(hh *ssz.Hasher) (err error) { 279 indx := hh.Index() 280 281 // Field (0) 'Pubkey' 282 if size := len(d.PublicKey); size != 2592 { 283 err = ssz.ErrBytesLengthFn("--.Pubkey", size, 2592) 284 return 285 } 286 hh.PutBytes(d.PublicKey) 287 288 // Field (1) 'WithdrawalCredentials' 289 if size := len(d.WithdrawalCredentials); size != 32 { 290 err = ssz.ErrBytesLengthFn("--.WithdrawalCredentials", size, 32) 291 return 292 } 293 hh.PutBytes(d.WithdrawalCredentials) 294 295 // Field (2) 'Amount' 296 hh.PutUint64(d.Amount) 297 298 // Field (3) 'Signature' 299 if size := len(d.Signature); size != 4595 { 300 err = ssz.ErrBytesLengthFn("--.Signature", size, 4595) 301 return 302 } 303 hh.PutBytes(d.Signature) 304 305 if ssz.EnableVectorizedHTR { 306 hh.MerkleizeVectorizedHTR(indx) 307 } else { 308 hh.Merkleize(indx) 309 } 310 return 311 } 312 313 // SHA256 implemented as a native contract. 314 type sha256hash struct{} 315 316 // RequiredGas returns the gas required to execute the pre-compiled contract. 317 // 318 // This method does not require any overflow checking as the input size gas costs 319 // required for anything significant is so high it's impossible to pay for. 320 func (c *sha256hash) RequiredGas(input []byte) uint64 { 321 return uint64(len(input)+31)/32*params.Sha256PerWordGas + params.Sha256BaseGas 322 } 323 func (c *sha256hash) Run(input []byte) ([]byte, error) { 324 h := sha256.Sum256(input) 325 return h[:], nil 326 } 327 328 // RIPEMD160 implemented as a native contract. 329 type ripemd160hash struct{} 330 331 // RequiredGas returns the gas required to execute the pre-compiled contract. 332 // 333 // This method does not require any overflow checking as the input size gas costs 334 // required for anything significant is so high it's impossible to pay for. 335 func (c *ripemd160hash) RequiredGas(input []byte) uint64 { 336 return uint64(len(input)+31)/32*params.Ripemd160PerWordGas + params.Ripemd160BaseGas 337 } 338 func (c *ripemd160hash) Run(input []byte) ([]byte, error) { 339 ripemd := ripemd160.New() 340 ripemd.Write(input) 341 return common.LeftPadBytes(ripemd.Sum(nil), 32), nil 342 } 343 344 // data copy implemented as a native contract. 345 type dataCopy struct{} 346 347 // RequiredGas returns the gas required to execute the pre-compiled contract. 348 // 349 // This method does not require any overflow checking as the input size gas costs 350 // required for anything significant is so high it's impossible to pay for. 351 func (c *dataCopy) RequiredGas(input []byte) uint64 { 352 return uint64(len(input)+31)/32*params.IdentityPerWordGas + params.IdentityBaseGas 353 } 354 func (c *dataCopy) Run(in []byte) ([]byte, error) { 355 return common.CopyBytes(in), nil 356 } 357 358 // bigModExp implements a native big integer exponential modular operation. 359 type bigModExp struct { 360 eip2565 bool 361 } 362 363 var ( 364 big0 = big.NewInt(0) 365 big1 = big.NewInt(1) 366 big3 = big.NewInt(3) 367 big4 = big.NewInt(4) 368 big7 = big.NewInt(7) 369 big8 = big.NewInt(8) 370 big16 = big.NewInt(16) 371 big20 = big.NewInt(20) 372 big32 = big.NewInt(32) 373 big64 = big.NewInt(64) 374 big96 = big.NewInt(96) 375 big480 = big.NewInt(480) 376 big1024 = big.NewInt(1024) 377 big3072 = big.NewInt(3072) 378 big199680 = big.NewInt(199680) 379 ) 380 381 // modexpMultComplexity implements bigModexp multComplexity formula, as defined in EIP-198 382 // 383 // def mult_complexity(x): 384 // if x <= 64: return x ** 2 385 // elif x <= 1024: return x ** 2 // 4 + 96 * x - 3072 386 // else: return x ** 2 // 16 + 480 * x - 199680 387 // 388 // where is x is max(length_of_MODULUS, length_of_BASE) 389 func modexpMultComplexity(x *big.Int) *big.Int { 390 switch { 391 case x.Cmp(big64) <= 0: 392 x.Mul(x, x) // x ** 2 393 case x.Cmp(big1024) <= 0: 394 // (x ** 2 // 4 ) + ( 96 * x - 3072) 395 x = new(big.Int).Add( 396 new(big.Int).Div(new(big.Int).Mul(x, x), big4), 397 new(big.Int).Sub(new(big.Int).Mul(big96, x), big3072), 398 ) 399 default: 400 // (x ** 2 // 16) + (480 * x - 199680) 401 x = new(big.Int).Add( 402 new(big.Int).Div(new(big.Int).Mul(x, x), big16), 403 new(big.Int).Sub(new(big.Int).Mul(big480, x), big199680), 404 ) 405 } 406 return x 407 } 408 409 // RequiredGas returns the gas required to execute the pre-compiled contract. 410 func (c *bigModExp) RequiredGas(input []byte) uint64 { 411 var ( 412 baseLen = new(big.Int).SetBytes(getData(input, 0, 32)) 413 expLen = new(big.Int).SetBytes(getData(input, 32, 32)) 414 modLen = new(big.Int).SetBytes(getData(input, 64, 32)) 415 ) 416 if len(input) > 96 { 417 input = input[96:] 418 } else { 419 input = input[:0] 420 } 421 // Retrieve the head 32 bytes of exp for the adjusted exponent length 422 var expHead *big.Int 423 if big.NewInt(int64(len(input))).Cmp(baseLen) <= 0 { 424 expHead = new(big.Int) 425 } else { 426 if expLen.Cmp(big32) > 0 { 427 expHead = new(big.Int).SetBytes(getData(input, baseLen.Uint64(), 32)) 428 } else { 429 expHead = new(big.Int).SetBytes(getData(input, baseLen.Uint64(), expLen.Uint64())) 430 } 431 } 432 // Calculate the adjusted exponent length 433 var msb int 434 if bitlen := expHead.BitLen(); bitlen > 0 { 435 msb = bitlen - 1 436 } 437 adjExpLen := new(big.Int) 438 if expLen.Cmp(big32) > 0 { 439 adjExpLen.Sub(expLen, big32) 440 adjExpLen.Mul(big8, adjExpLen) 441 } 442 adjExpLen.Add(adjExpLen, big.NewInt(int64(msb))) 443 // Calculate the gas cost of the operation 444 gas := new(big.Int).Set(math.BigMax(modLen, baseLen)) 445 if c.eip2565 { 446 // EIP-2565 has three changes 447 // 1. Different multComplexity (inlined here) 448 // in EIP-2565 (https://eips.ethereum.org/EIPS/eip-2565): 449 // 450 // def mult_complexity(x): 451 // ceiling(x/8)^2 452 // 453 //where is x is max(length_of_MODULUS, length_of_BASE) 454 gas = gas.Add(gas, big7) 455 gas = gas.Div(gas, big8) 456 gas.Mul(gas, gas) 457 458 gas.Mul(gas, math.BigMax(adjExpLen, big1)) 459 // 2. Different divisor (`GQUADDIVISOR`) (3) 460 gas.Div(gas, big3) 461 if gas.BitLen() > 64 { 462 return math.MaxUint64 463 } 464 // 3. Minimum price of 200 gas 465 if gas.Uint64() < 200 { 466 return 200 467 } 468 return gas.Uint64() 469 } 470 gas = modexpMultComplexity(gas) 471 gas.Mul(gas, math.BigMax(adjExpLen, big1)) 472 gas.Div(gas, big20) 473 474 if gas.BitLen() > 64 { 475 return math.MaxUint64 476 } 477 return gas.Uint64() 478 } 479 480 func (c *bigModExp) Run(input []byte) ([]byte, error) { 481 var ( 482 baseLen = new(big.Int).SetBytes(getData(input, 0, 32)).Uint64() 483 expLen = new(big.Int).SetBytes(getData(input, 32, 32)).Uint64() 484 modLen = new(big.Int).SetBytes(getData(input, 64, 32)).Uint64() 485 ) 486 if len(input) > 96 { 487 input = input[96:] 488 } else { 489 input = input[:0] 490 } 491 // Handle a special case when both the base and mod length is zero 492 if baseLen == 0 && modLen == 0 { 493 return []byte{}, nil 494 } 495 // Retrieve the operands and execute the exponentiation 496 var ( 497 base = new(big.Int).SetBytes(getData(input, 0, baseLen)) 498 exp = new(big.Int).SetBytes(getData(input, baseLen, expLen)) 499 mod = new(big.Int).SetBytes(getData(input, baseLen+expLen, modLen)) 500 v []byte 501 ) 502 switch { 503 case mod.BitLen() == 0: 504 // Modulo 0 is undefined, return zero 505 return common.LeftPadBytes([]byte{}, int(modLen)), nil 506 case base.BitLen() == 1: // a bit length of 1 means it's 1 (or -1). 507 //If base == 1, then we can just return base % mod (if mod >= 1, which it is) 508 v = base.Mod(base, mod).Bytes() 509 default: 510 v = base.Exp(base, exp, mod).Bytes() 511 } 512 return common.LeftPadBytes(v, int(modLen)), nil 513 } 514 515 // newCurvePoint unmarshals a binary blob into a bn256 elliptic curve point, 516 // returning it, or an error if the point is invalid. 517 func newCurvePoint(blob []byte) (*bn256.G1, error) { 518 p := new(bn256.G1) 519 if _, err := p.Unmarshal(blob); err != nil { 520 return nil, err 521 } 522 return p, nil 523 } 524 525 // newTwistPoint unmarshals a binary blob into a bn256 elliptic curve point, 526 // returning it, or an error if the point is invalid. 527 func newTwistPoint(blob []byte) (*bn256.G2, error) { 528 p := new(bn256.G2) 529 if _, err := p.Unmarshal(blob); err != nil { 530 return nil, err 531 } 532 return p, nil 533 } 534 535 // runBn256Add implements the Bn256Add precompile, referenced by both 536 // Byzantium and Istanbul operations. 537 func runBn256Add(input []byte) ([]byte, error) { 538 x, err := newCurvePoint(getData(input, 0, 64)) 539 if err != nil { 540 return nil, err 541 } 542 y, err := newCurvePoint(getData(input, 64, 64)) 543 if err != nil { 544 return nil, err 545 } 546 res := new(bn256.G1) 547 res.Add(x, y) 548 return res.Marshal(), nil 549 } 550 551 // bn256Add implements a native elliptic curve point addition conforming to 552 // Istanbul consensus rules. 553 type bn256AddIstanbul struct{} 554 555 // RequiredGas returns the gas required to execute the pre-compiled contract. 556 func (c *bn256AddIstanbul) RequiredGas(input []byte) uint64 { 557 return params.Bn256AddGasIstanbul 558 } 559 560 func (c *bn256AddIstanbul) Run(input []byte) ([]byte, error) { 561 return runBn256Add(input) 562 } 563 564 // bn256AddByzantium implements a native elliptic curve point addition 565 // conforming to Byzantium consensus rules. 566 type bn256AddByzantium struct{} 567 568 // RequiredGas returns the gas required to execute the pre-compiled contract. 569 func (c *bn256AddByzantium) RequiredGas(input []byte) uint64 { 570 return params.Bn256AddGasByzantium 571 } 572 573 func (c *bn256AddByzantium) Run(input []byte) ([]byte, error) { 574 return runBn256Add(input) 575 } 576 577 // runBn256ScalarMul implements the Bn256ScalarMul precompile, referenced by 578 // both Byzantium and Istanbul operations. 579 func runBn256ScalarMul(input []byte) ([]byte, error) { 580 p, err := newCurvePoint(getData(input, 0, 64)) 581 if err != nil { 582 return nil, err 583 } 584 res := new(bn256.G1) 585 res.ScalarMult(p, new(big.Int).SetBytes(getData(input, 64, 32))) 586 return res.Marshal(), nil 587 } 588 589 // bn256ScalarMulIstanbul implements a native elliptic curve scalar 590 // multiplication conforming to Istanbul consensus rules. 591 type bn256ScalarMulIstanbul struct{} 592 593 // RequiredGas returns the gas required to execute the pre-compiled contract. 594 func (c *bn256ScalarMulIstanbul) RequiredGas(input []byte) uint64 { 595 return params.Bn256ScalarMulGasIstanbul 596 } 597 598 func (c *bn256ScalarMulIstanbul) Run(input []byte) ([]byte, error) { 599 return runBn256ScalarMul(input) 600 } 601 602 // bn256ScalarMulByzantium implements a native elliptic curve scalar 603 // multiplication conforming to Byzantium consensus rules. 604 type bn256ScalarMulByzantium struct{} 605 606 // RequiredGas returns the gas required to execute the pre-compiled contract. 607 func (c *bn256ScalarMulByzantium) RequiredGas(input []byte) uint64 { 608 return params.Bn256ScalarMulGasByzantium 609 } 610 611 func (c *bn256ScalarMulByzantium) Run(input []byte) ([]byte, error) { 612 return runBn256ScalarMul(input) 613 } 614 615 var ( 616 // true32Byte is returned if the bn256 pairing check succeeds. 617 true32Byte = []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} 618 619 // false32Byte is returned if the bn256 pairing check fails. 620 false32Byte = make([]byte, 32) 621 622 // errBadPairingInput is returned if the bn256 pairing input is invalid. 623 errBadPairingInput = errors.New("bad elliptic curve pairing size") 624 ) 625 626 // runBn256Pairing implements the Bn256Pairing precompile, referenced by both 627 // Byzantium and Istanbul operations. 628 func runBn256Pairing(input []byte) ([]byte, error) { 629 // Handle some corner cases cheaply 630 if len(input)%192 > 0 { 631 return nil, errBadPairingInput 632 } 633 // Convert the input into a set of coordinates 634 var ( 635 cs []*bn256.G1 636 ts []*bn256.G2 637 ) 638 for i := 0; i < len(input); i += 192 { 639 c, err := newCurvePoint(input[i : i+64]) 640 if err != nil { 641 return nil, err 642 } 643 t, err := newTwistPoint(input[i+64 : i+192]) 644 if err != nil { 645 return nil, err 646 } 647 cs = append(cs, c) 648 ts = append(ts, t) 649 } 650 // Execute the pairing checks and return the results 651 if bn256.PairingCheck(cs, ts) { 652 return true32Byte, nil 653 } 654 return false32Byte, nil 655 } 656 657 // bn256PairingIstanbul implements a pairing pre-compile for the bn256 curve 658 // conforming to Istanbul consensus rules. 659 type bn256PairingIstanbul struct{} 660 661 // RequiredGas returns the gas required to execute the pre-compiled contract. 662 func (c *bn256PairingIstanbul) RequiredGas(input []byte) uint64 { 663 return params.Bn256PairingBaseGasIstanbul + uint64(len(input)/192)*params.Bn256PairingPerPointGasIstanbul 664 } 665 666 func (c *bn256PairingIstanbul) Run(input []byte) ([]byte, error) { 667 return runBn256Pairing(input) 668 } 669 670 // bn256PairingByzantium implements a pairing pre-compile for the bn256 curve 671 // conforming to Byzantium consensus rules. 672 type bn256PairingByzantium struct{} 673 674 // RequiredGas returns the gas required to execute the pre-compiled contract. 675 func (c *bn256PairingByzantium) RequiredGas(input []byte) uint64 { 676 return params.Bn256PairingBaseGasByzantium + uint64(len(input)/192)*params.Bn256PairingPerPointGasByzantium 677 } 678 679 func (c *bn256PairingByzantium) Run(input []byte) ([]byte, error) { 680 return runBn256Pairing(input) 681 } 682 683 type blake2F struct{} 684 685 func (c *blake2F) RequiredGas(input []byte) uint64 { 686 // If the input is malformed, we can't calculate the gas, return 0 and let the 687 // actual call choke and fault. 688 if len(input) != blake2FInputLength { 689 return 0 690 } 691 return uint64(binary.BigEndian.Uint32(input[0:4])) 692 } 693 694 const ( 695 blake2FInputLength = 213 696 blake2FFinalBlockBytes = byte(1) 697 blake2FNonFinalBlockBytes = byte(0) 698 ) 699 700 var ( 701 errBlake2FInvalidInputLength = errors.New("invalid input length") 702 errBlake2FInvalidFinalFlag = errors.New("invalid final flag") 703 ) 704 705 func (c *blake2F) Run(input []byte) ([]byte, error) { 706 // Make sure the input is valid (correct length and final flag) 707 if len(input) != blake2FInputLength { 708 return nil, errBlake2FInvalidInputLength 709 } 710 if input[212] != blake2FNonFinalBlockBytes && input[212] != blake2FFinalBlockBytes { 711 return nil, errBlake2FInvalidFinalFlag 712 } 713 // Parse the input into the Blake2b call parameters 714 var ( 715 rounds = binary.BigEndian.Uint32(input[0:4]) 716 final = input[212] == blake2FFinalBlockBytes 717 718 h [8]uint64 719 m [16]uint64 720 t [2]uint64 721 ) 722 for i := 0; i < 8; i++ { 723 offset := 4 + i*8 724 h[i] = binary.LittleEndian.Uint64(input[offset : offset+8]) 725 } 726 for i := 0; i < 16; i++ { 727 offset := 68 + i*8 728 m[i] = binary.LittleEndian.Uint64(input[offset : offset+8]) 729 } 730 t[0] = binary.LittleEndian.Uint64(input[196:204]) 731 t[1] = binary.LittleEndian.Uint64(input[204:212]) 732 733 // Execute the compression function, extract and return the result 734 blake2b.F(&h, m, t, final, rounds) 735 736 output := make([]byte, 64) 737 for i := 0; i < 8; i++ { 738 offset := i * 8 739 binary.LittleEndian.PutUint64(output[offset:offset+8], h[i]) 740 } 741 return output, nil 742 } 743 744 var ( 745 errBLS12381InvalidInputLength = errors.New("invalid input length") 746 errBLS12381InvalidFieldElementTopBytes = errors.New("invalid field element top bytes") 747 errBLS12381G1PointSubgroup = errors.New("g1 point is not on correct subgroup") 748 errBLS12381G2PointSubgroup = errors.New("g2 point is not on correct subgroup") 749 ) 750 751 // bls12381G1Add implements EIP-2537 G1Add precompile. 752 type bls12381G1Add struct{} 753 754 // RequiredGas returns the gas required to execute the pre-compiled contract. 755 func (c *bls12381G1Add) RequiredGas(input []byte) uint64 { 756 return params.Bls12381G1AddGas 757 } 758 759 func (c *bls12381G1Add) Run(input []byte) ([]byte, error) { 760 // Implements EIP-2537 G1Add precompile. 761 // > G1 addition call expects `256` bytes as an input that is interpreted as byte concatenation of two G1 points (`128` bytes each). 762 // > Output is an encoding of addition operation result - single G1 point (`128` bytes). 763 if len(input) != 256 { 764 return nil, errBLS12381InvalidInputLength 765 } 766 var err error 767 var p0, p1 *bls12381.PointG1 768 769 // Initialize G1 770 g := bls12381.NewG1() 771 772 // Decode G1 point p_0 773 if p0, err = g.DecodePoint(input[:128]); err != nil { 774 return nil, err 775 } 776 // Decode G1 point p_1 777 if p1, err = g.DecodePoint(input[128:]); err != nil { 778 return nil, err 779 } 780 781 // Compute r = p_0 + p_1 782 r := g.New() 783 g.Add(r, p0, p1) 784 785 // Encode the G1 point result into 128 bytes 786 return g.EncodePoint(r), nil 787 } 788 789 // bls12381G1Mul implements EIP-2537 G1Mul precompile. 790 type bls12381G1Mul struct{} 791 792 // RequiredGas returns the gas required to execute the pre-compiled contract. 793 func (c *bls12381G1Mul) RequiredGas(input []byte) uint64 { 794 return params.Bls12381G1MulGas 795 } 796 797 func (c *bls12381G1Mul) Run(input []byte) ([]byte, error) { 798 // Implements EIP-2537 G1Mul precompile. 799 // > G1 multiplication call expects `160` bytes as an input that is interpreted as byte concatenation of encoding of G1 point (`128` bytes) and encoding of a scalar value (`32` bytes). 800 // > Output is an encoding of multiplication operation result - single G1 point (`128` bytes). 801 if len(input) != 160 { 802 return nil, errBLS12381InvalidInputLength 803 } 804 var err error 805 var p0 *bls12381.PointG1 806 807 // Initialize G1 808 g := bls12381.NewG1() 809 810 // Decode G1 point 811 if p0, err = g.DecodePoint(input[:128]); err != nil { 812 return nil, err 813 } 814 // Decode scalar value 815 e := new(big.Int).SetBytes(input[128:]) 816 817 // Compute r = e * p_0 818 r := g.New() 819 g.MulScalar(r, p0, e) 820 821 // Encode the G1 point into 128 bytes 822 return g.EncodePoint(r), nil 823 } 824 825 // bls12381G1MultiExp implements EIP-2537 G1MultiExp precompile. 826 type bls12381G1MultiExp struct{} 827 828 // RequiredGas returns the gas required to execute the pre-compiled contract. 829 func (c *bls12381G1MultiExp) RequiredGas(input []byte) uint64 { 830 // Calculate G1 point, scalar value pair length 831 k := len(input) / 160 832 if k == 0 { 833 // Return 0 gas for small input length 834 return 0 835 } 836 // Lookup discount value for G1 point, scalar value pair length 837 var discount uint64 838 if dLen := len(params.Bls12381MultiExpDiscountTable); k < dLen { 839 discount = params.Bls12381MultiExpDiscountTable[k-1] 840 } else { 841 discount = params.Bls12381MultiExpDiscountTable[dLen-1] 842 } 843 // Calculate gas and return the result 844 return (uint64(k) * params.Bls12381G1MulGas * discount) / 1000 845 } 846 847 func (c *bls12381G1MultiExp) Run(input []byte) ([]byte, error) { 848 // Implements EIP-2537 G1MultiExp precompile. 849 // G1 multiplication call expects `160*k` bytes as an input that is interpreted as byte concatenation of `k` slices each of them being a byte concatenation of encoding of G1 point (`128` bytes) and encoding of a scalar value (`32` bytes). 850 // Output is an encoding of multiexponentiation operation result - single G1 point (`128` bytes). 851 k := len(input) / 160 852 if len(input) == 0 || len(input)%160 != 0 { 853 return nil, errBLS12381InvalidInputLength 854 } 855 var err error 856 points := make([]*bls12381.PointG1, k) 857 scalars := make([]*big.Int, k) 858 859 // Initialize G1 860 g := bls12381.NewG1() 861 862 // Decode point scalar pairs 863 for i := 0; i < k; i++ { 864 off := 160 * i 865 t0, t1, t2 := off, off+128, off+160 866 // Decode G1 point 867 if points[i], err = g.DecodePoint(input[t0:t1]); err != nil { 868 return nil, err 869 } 870 // Decode scalar value 871 scalars[i] = new(big.Int).SetBytes(input[t1:t2]) 872 } 873 874 // Compute r = e_0 * p_0 + e_1 * p_1 + ... + e_(k-1) * p_(k-1) 875 r := g.New() 876 g.MultiExp(r, points, scalars) 877 878 // Encode the G1 point to 128 bytes 879 return g.EncodePoint(r), nil 880 } 881 882 // bls12381G2Add implements EIP-2537 G2Add precompile. 883 type bls12381G2Add struct{} 884 885 // RequiredGas returns the gas required to execute the pre-compiled contract. 886 func (c *bls12381G2Add) RequiredGas(input []byte) uint64 { 887 return params.Bls12381G2AddGas 888 } 889 890 func (c *bls12381G2Add) Run(input []byte) ([]byte, error) { 891 // Implements EIP-2537 G2Add precompile. 892 // > G2 addition call expects `512` bytes as an input that is interpreted as byte concatenation of two G2 points (`256` bytes each). 893 // > Output is an encoding of addition operation result - single G2 point (`256` bytes). 894 if len(input) != 512 { 895 return nil, errBLS12381InvalidInputLength 896 } 897 var err error 898 var p0, p1 *bls12381.PointG2 899 900 // Initialize G2 901 g := bls12381.NewG2() 902 r := g.New() 903 904 // Decode G2 point p_0 905 if p0, err = g.DecodePoint(input[:256]); err != nil { 906 return nil, err 907 } 908 // Decode G2 point p_1 909 if p1, err = g.DecodePoint(input[256:]); err != nil { 910 return nil, err 911 } 912 913 // Compute r = p_0 + p_1 914 g.Add(r, p0, p1) 915 916 // Encode the G2 point into 256 bytes 917 return g.EncodePoint(r), nil 918 } 919 920 // bls12381G2Mul implements EIP-2537 G2Mul precompile. 921 type bls12381G2Mul struct{} 922 923 // RequiredGas returns the gas required to execute the pre-compiled contract. 924 func (c *bls12381G2Mul) RequiredGas(input []byte) uint64 { 925 return params.Bls12381G2MulGas 926 } 927 928 func (c *bls12381G2Mul) Run(input []byte) ([]byte, error) { 929 // Implements EIP-2537 G2MUL precompile logic. 930 // > G2 multiplication call expects `288` bytes as an input that is interpreted as byte concatenation of encoding of G2 point (`256` bytes) and encoding of a scalar value (`32` bytes). 931 // > Output is an encoding of multiplication operation result - single G2 point (`256` bytes). 932 if len(input) != 288 { 933 return nil, errBLS12381InvalidInputLength 934 } 935 var err error 936 var p0 *bls12381.PointG2 937 938 // Initialize G2 939 g := bls12381.NewG2() 940 941 // Decode G2 point 942 if p0, err = g.DecodePoint(input[:256]); err != nil { 943 return nil, err 944 } 945 // Decode scalar value 946 e := new(big.Int).SetBytes(input[256:]) 947 948 // Compute r = e * p_0 949 r := g.New() 950 g.MulScalar(r, p0, e) 951 952 // Encode the G2 point into 256 bytes 953 return g.EncodePoint(r), nil 954 } 955 956 // bls12381G2MultiExp implements EIP-2537 G2MultiExp precompile. 957 type bls12381G2MultiExp struct{} 958 959 // RequiredGas returns the gas required to execute the pre-compiled contract. 960 func (c *bls12381G2MultiExp) RequiredGas(input []byte) uint64 { 961 // Calculate G2 point, scalar value pair length 962 k := len(input) / 288 963 if k == 0 { 964 // Return 0 gas for small input length 965 return 0 966 } 967 // Lookup discount value for G2 point, scalar value pair length 968 var discount uint64 969 if dLen := len(params.Bls12381MultiExpDiscountTable); k < dLen { 970 discount = params.Bls12381MultiExpDiscountTable[k-1] 971 } else { 972 discount = params.Bls12381MultiExpDiscountTable[dLen-1] 973 } 974 // Calculate gas and return the result 975 return (uint64(k) * params.Bls12381G2MulGas * discount) / 1000 976 } 977 978 func (c *bls12381G2MultiExp) Run(input []byte) ([]byte, error) { 979 // Implements EIP-2537 G2MultiExp precompile logic 980 // > G2 multiplication call expects `288*k` bytes as an input that is interpreted as byte concatenation of `k` slices each of them being a byte concatenation of encoding of G2 point (`256` bytes) and encoding of a scalar value (`32` bytes). 981 // > Output is an encoding of multiexponentiation operation result - single G2 point (`256` bytes). 982 k := len(input) / 288 983 if len(input) == 0 || len(input)%288 != 0 { 984 return nil, errBLS12381InvalidInputLength 985 } 986 var err error 987 points := make([]*bls12381.PointG2, k) 988 scalars := make([]*big.Int, k) 989 990 // Initialize G2 991 g := bls12381.NewG2() 992 993 // Decode point scalar pairs 994 for i := 0; i < k; i++ { 995 off := 288 * i 996 t0, t1, t2 := off, off+256, off+288 997 // Decode G1 point 998 if points[i], err = g.DecodePoint(input[t0:t1]); err != nil { 999 return nil, err 1000 } 1001 // Decode scalar value 1002 scalars[i] = new(big.Int).SetBytes(input[t1:t2]) 1003 } 1004 1005 // Compute r = e_0 * p_0 + e_1 * p_1 + ... + e_(k-1) * p_(k-1) 1006 r := g.New() 1007 g.MultiExp(r, points, scalars) 1008 1009 // Encode the G2 point to 256 bytes. 1010 return g.EncodePoint(r), nil 1011 } 1012 1013 // bls12381Pairing implements EIP-2537 Pairing precompile. 1014 type bls12381Pairing struct{} 1015 1016 // RequiredGas returns the gas required to execute the pre-compiled contract. 1017 func (c *bls12381Pairing) RequiredGas(input []byte) uint64 { 1018 return params.Bls12381PairingBaseGas + uint64(len(input)/384)*params.Bls12381PairingPerPairGas 1019 } 1020 1021 func (c *bls12381Pairing) Run(input []byte) ([]byte, error) { 1022 // Implements EIP-2537 Pairing precompile logic. 1023 // > Pairing call expects `384*k` bytes as an inputs that is interpreted as byte concatenation of `k` slices. Each slice has the following structure: 1024 // > - `128` bytes of G1 point encoding 1025 // > - `256` bytes of G2 point encoding 1026 // > Output is a `32` bytes where last single byte is `0x01` if pairing result is equal to multiplicative identity in a pairing target field and `0x00` otherwise 1027 // > (which is equivalent of Big Endian encoding of Solidity values `uint256(1)` and `uin256(0)` respectively). 1028 k := len(input) / 384 1029 if len(input) == 0 || len(input)%384 != 0 { 1030 return nil, errBLS12381InvalidInputLength 1031 } 1032 1033 // Initialize BLS12-381 pairing engine 1034 e := bls12381.NewPairingEngine() 1035 g1, g2 := e.G1, e.G2 1036 1037 // Decode pairs 1038 for i := 0; i < k; i++ { 1039 off := 384 * i 1040 t0, t1, t2 := off, off+128, off+384 1041 1042 // Decode G1 point 1043 p1, err := g1.DecodePoint(input[t0:t1]) 1044 if err != nil { 1045 return nil, err 1046 } 1047 // Decode G2 point 1048 p2, err := g2.DecodePoint(input[t1:t2]) 1049 if err != nil { 1050 return nil, err 1051 } 1052 1053 // 'point is on curve' check already done, 1054 // Here we need to apply subgroup checks. 1055 if !g1.InCorrectSubgroup(p1) { 1056 return nil, errBLS12381G1PointSubgroup 1057 } 1058 if !g2.InCorrectSubgroup(p2) { 1059 return nil, errBLS12381G2PointSubgroup 1060 } 1061 1062 // Update pairing engine with G1 and G2 points 1063 e.AddPair(p1, p2) 1064 } 1065 // Prepare 32 byte output 1066 out := make([]byte, 32) 1067 1068 // Compute pairing and set the result 1069 if e.Check() { 1070 out[31] = 1 1071 } 1072 return out, nil 1073 } 1074 1075 // decodeBLS12381FieldElement decodes BLS12-381 elliptic curve field element. 1076 // Removes top 16 bytes of 64 byte input. 1077 func decodeBLS12381FieldElement(in []byte) ([]byte, error) { 1078 if len(in) != 64 { 1079 return nil, errors.New("invalid field element length") 1080 } 1081 // check top bytes 1082 for i := 0; i < 16; i++ { 1083 if in[i] != byte(0x00) { 1084 return nil, errBLS12381InvalidFieldElementTopBytes 1085 } 1086 } 1087 out := make([]byte, 48) 1088 copy(out[:], in[16:]) 1089 return out, nil 1090 } 1091 1092 // bls12381MapG1 implements EIP-2537 MapG1 precompile. 1093 type bls12381MapG1 struct{} 1094 1095 // RequiredGas returns the gas required to execute the pre-compiled contract. 1096 func (c *bls12381MapG1) RequiredGas(input []byte) uint64 { 1097 return params.Bls12381MapG1Gas 1098 } 1099 1100 func (c *bls12381MapG1) Run(input []byte) ([]byte, error) { 1101 // Implements EIP-2537 Map_To_G1 precompile. 1102 // > Field-to-curve call expects `64` bytes an an input that is interpreted as a an element of the base field. 1103 // > Output of this call is `128` bytes and is G1 point following respective encoding rules. 1104 if len(input) != 64 { 1105 return nil, errBLS12381InvalidInputLength 1106 } 1107 1108 // Decode input field element 1109 fe, err := decodeBLS12381FieldElement(input) 1110 if err != nil { 1111 return nil, err 1112 } 1113 1114 // Initialize G1 1115 g := bls12381.NewG1() 1116 1117 // Compute mapping 1118 r, err := g.MapToCurve(fe) 1119 if err != nil { 1120 return nil, err 1121 } 1122 1123 // Encode the G1 point to 128 bytes 1124 return g.EncodePoint(r), nil 1125 } 1126 1127 // bls12381MapG2 implements EIP-2537 MapG2 precompile. 1128 type bls12381MapG2 struct{} 1129 1130 // RequiredGas returns the gas required to execute the pre-compiled contract. 1131 func (c *bls12381MapG2) RequiredGas(input []byte) uint64 { 1132 return params.Bls12381MapG2Gas 1133 } 1134 1135 func (c *bls12381MapG2) Run(input []byte) ([]byte, error) { 1136 // Implements EIP-2537 Map_FP2_TO_G2 precompile logic. 1137 // > Field-to-curve call expects `128` bytes an an input that is interpreted as a an element of the quadratic extension field. 1138 // > Output of this call is `256` bytes and is G2 point following respective encoding rules. 1139 if len(input) != 128 { 1140 return nil, errBLS12381InvalidInputLength 1141 } 1142 1143 // Decode input field element 1144 fe := make([]byte, 96) 1145 c0, err := decodeBLS12381FieldElement(input[:64]) 1146 if err != nil { 1147 return nil, err 1148 } 1149 copy(fe[48:], c0) 1150 c1, err := decodeBLS12381FieldElement(input[64:]) 1151 if err != nil { 1152 return nil, err 1153 } 1154 copy(fe[:48], c1) 1155 1156 // Initialize G2 1157 g := bls12381.NewG2() 1158 1159 // Compute mapping 1160 r, err := g.MapToCurve(fe) 1161 if err != nil { 1162 return nil, err 1163 } 1164 1165 // Encode the G2 point to 256 bytes 1166 return g.EncodePoint(r), nil 1167 } 1168 1169 // kzgPointEvaluation implements the EIP-4844 point evaluation precompile. 1170 type kzgPointEvaluation struct{} 1171 1172 // RequiredGas estimates the gas required for running the point evaluation precompile. 1173 func (b *kzgPointEvaluation) RequiredGas(input []byte) uint64 { 1174 return params.BlobTxPointEvaluationPrecompileGas 1175 } 1176 1177 const ( 1178 blobVerifyInputLength = 192 // Max input length for the point evaluation precompile. 1179 blobCommitmentVersionKZG uint8 = 0x01 // Version byte for the point evaluation precompile. 1180 blobPrecompileReturnValue = "000000000000000000000000000000000000000000000000000000000000100073eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001" 1181 ) 1182 1183 var ( 1184 errBlobVerifyInvalidInputLength = errors.New("invalid input length") 1185 errBlobVerifyMismatchedVersion = errors.New("mismatched versioned hash") 1186 errBlobVerifyKZGProof = errors.New("error verifying kzg proof") 1187 ) 1188 1189 // Run executes the point evaluation precompile. 1190 func (b *kzgPointEvaluation) Run(input []byte) ([]byte, error) { 1191 if len(input) != blobVerifyInputLength { 1192 return nil, errBlobVerifyInvalidInputLength 1193 } 1194 // versioned hash: first 32 bytes 1195 var versionedHash common.Hash 1196 copy(versionedHash[:], input[:]) 1197 1198 var ( 1199 point kzg4844.Point 1200 claim kzg4844.Claim 1201 ) 1202 // Evaluation point: next 32 bytes 1203 copy(point[:], input[32:]) 1204 // Expected output: next 32 bytes 1205 copy(claim[:], input[64:]) 1206 1207 // input kzg point: next 48 bytes 1208 var commitment kzg4844.Commitment 1209 copy(commitment[:], input[96:]) 1210 if kZGToVersionedHash(commitment) != versionedHash { 1211 return nil, errBlobVerifyMismatchedVersion 1212 } 1213 1214 // Proof: next 48 bytes 1215 var proof kzg4844.Proof 1216 copy(proof[:], input[144:]) 1217 1218 if err := kzg4844.VerifyProof(commitment, point, claim, proof); err != nil { 1219 return nil, fmt.Errorf("%w: %v", errBlobVerifyKZGProof, err) 1220 } 1221 1222 return common.Hex2Bytes(blobPrecompileReturnValue), nil 1223 } 1224 1225 // kZGToVersionedHash implements kzg_to_versioned_hash from EIP-4844 1226 func kZGToVersionedHash(kzg kzg4844.Commitment) common.Hash { 1227 h := sha256.Sum256(kzg[:]) 1228 h[0] = blobCommitmentVersionKZG 1229 1230 return h 1231 }