2022-12-13 20:06:26 +00:00
|
|
|
# Nimbus
|
2024-02-20 03:07:38 +00:00
|
|
|
# Copyright (c) 2023-2024 Status Research & Development GmbH
|
2022-12-13 20:06:26 +00:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0)
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
|
|
|
|
# http://opensource.org/licenses/MIT)
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except
|
|
|
|
# according to those terms.
|
|
|
|
|
|
|
|
import
|
2023-06-24 13:56:44 +00:00
|
|
|
std/[os, strutils],
|
2023-09-24 15:25:41 +00:00
|
|
|
nimcrypto/sha2,
|
2023-06-24 13:56:44 +00:00
|
|
|
kzg4844/kzg_ex as kzg,
|
2022-12-13 20:06:26 +00:00
|
|
|
stew/results,
|
2023-06-24 13:56:44 +00:00
|
|
|
stint,
|
|
|
|
../constants,
|
2022-12-13 20:06:26 +00:00
|
|
|
../common/common
|
|
|
|
|
2023-01-30 22:10:23 +00:00
|
|
|
{.push raises: [].}
|
|
|
|
|
2023-06-24 13:56:44 +00:00
|
|
|
type
|
|
|
|
Bytes32 = array[32, byte]
|
|
|
|
Bytes64 = array[64, byte]
|
|
|
|
Bytes48 = array[48, byte]
|
|
|
|
|
|
|
|
const
|
|
|
|
BLS_MODULUS_STR = "52435875175126190479447740508185965837690552500527637822603658699938581184513"
|
2023-10-19 00:50:07 +00:00
|
|
|
BLS_MODULUS* = parse(BLS_MODULUS_STR, UInt256, 10).toBytesBE
|
2023-06-24 13:56:44 +00:00
|
|
|
PrecompileInputLength = 192
|
|
|
|
|
|
|
|
proc pointEvaluationResult(): Bytes64 {.compileTime.} =
|
|
|
|
result[0..<32] = FIELD_ELEMENTS_PER_BLOB.u256.toBytesBE[0..^1]
|
2023-10-19 00:50:07 +00:00
|
|
|
result[32..^1] = BLS_MODULUS[0..^1]
|
2023-06-24 13:56:44 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
PointEvaluationResult* = pointEvaluationResult()
|
|
|
|
POINT_EVALUATION_PRECOMPILE_GAS* = 50000.GasInt
|
|
|
|
|
|
|
|
|
|
|
|
# kzgToVersionedHash implements kzg_to_versioned_hash from EIP-4844
|
2023-10-19 00:50:07 +00:00
|
|
|
proc kzgToVersionedHash*(kzg: kzg.KZGCommitment): VersionedHash =
|
2023-09-24 15:25:41 +00:00
|
|
|
result = sha256.digest(kzg)
|
2023-08-24 05:11:19 +00:00
|
|
|
result.data[0] = VERSIONED_HASH_VERSION_KZG
|
2023-06-24 13:56:44 +00:00
|
|
|
|
|
|
|
# pointEvaluation implements point_evaluation_precompile from EIP-4844
|
|
|
|
# return value and gas consumption is handled by pointEvaluation in
|
|
|
|
# precompiles.nim
|
|
|
|
proc pointEvaluation*(input: openArray[byte]): Result[void, string] =
|
|
|
|
# Verify p(z) = y given commitment that corresponds to the polynomial p(x) and a KZG proof.
|
|
|
|
# Also verify that the provided commitment matches the provided versioned_hash.
|
|
|
|
# The data is encoded as follows: versioned_hash | z | y | commitment | proof |
|
|
|
|
|
2023-09-24 15:25:41 +00:00
|
|
|
if input.len != PrecompileInputLength:
|
2023-06-24 13:56:44 +00:00
|
|
|
return err("invalid input length")
|
|
|
|
|
|
|
|
var
|
|
|
|
versionedHash: Bytes32
|
|
|
|
z: Bytes32
|
|
|
|
y: Bytes32
|
|
|
|
commitment: Bytes48
|
|
|
|
kzgProof: Bytes48
|
|
|
|
|
|
|
|
versionedHash[0..<32] = input[0..<32]
|
|
|
|
z[0..<32] = input[32..<64]
|
|
|
|
y[0..<32] = input[64..<96]
|
|
|
|
commitment[0..<48] = input[96..<144]
|
|
|
|
kzgProof[0..<48] = input[144..<192]
|
|
|
|
|
2023-08-24 05:11:19 +00:00
|
|
|
if kzgToVersionedHash(commitment).data != versionedHash:
|
|
|
|
return err("versionedHash should equal to kzgToVersionedHash(commitment)")
|
|
|
|
|
2023-06-24 13:56:44 +00:00
|
|
|
# Verify KZG proof
|
|
|
|
let res = kzg.verifyKzgProof(commitment, z, y, kzgProof)
|
|
|
|
if res.isErr:
|
|
|
|
return err(res.error)
|
|
|
|
|
|
|
|
# The actual verify result
|
|
|
|
if not res.get():
|
|
|
|
return err("Failed to verify KZG proof")
|
|
|
|
|
|
|
|
ok()
|
|
|
|
|
2023-08-04 12:43:30 +00:00
|
|
|
# calcExcessBlobGas implements calc_excess_data_gas from EIP-4844
|
|
|
|
proc calcExcessBlobGas*(parent: BlockHeader): uint64 =
|
2023-06-24 13:56:44 +00:00
|
|
|
let
|
2023-08-04 12:43:30 +00:00
|
|
|
excessBlobGas = parent.excessBlobGas.get(0'u64)
|
|
|
|
blobGasUsed = parent.blobGasUsed.get(0'u64)
|
2023-06-24 13:56:44 +00:00
|
|
|
|
2023-08-04 12:43:30 +00:00
|
|
|
if excessBlobGas + blobGasUsed < TARGET_BLOB_GAS_PER_BLOCK:
|
2023-06-24 13:56:44 +00:00
|
|
|
0'u64
|
|
|
|
else:
|
2023-08-04 12:43:30 +00:00
|
|
|
excessBlobGas + blobGasUsed - TARGET_BLOB_GAS_PER_BLOCK
|
2023-06-24 13:56:44 +00:00
|
|
|
|
|
|
|
# fakeExponential approximates factor * e ** (num / denom) using a taylor expansion
|
|
|
|
# as described in the EIP-4844 spec.
|
2023-09-24 15:25:41 +00:00
|
|
|
func fakeExponential*(factor, numerator, denominator: UInt256): UInt256 =
|
2023-06-24 13:56:44 +00:00
|
|
|
var
|
2023-09-24 15:25:41 +00:00
|
|
|
i = 1.u256
|
|
|
|
output = 0.u256
|
2023-06-24 13:56:44 +00:00
|
|
|
numeratorAccum = factor * denominator
|
|
|
|
|
2023-09-24 15:25:41 +00:00
|
|
|
while numeratorAccum > 0.u256:
|
2023-06-24 13:56:44 +00:00
|
|
|
output += numeratorAccum
|
|
|
|
numeratorAccum = (numeratorAccum * numerator) div (denominator * i)
|
2023-09-24 15:25:41 +00:00
|
|
|
i = i + 1.u256
|
2023-06-24 13:56:44 +00:00
|
|
|
|
|
|
|
output div denominator
|
|
|
|
|
2023-08-04 12:43:30 +00:00
|
|
|
proc getTotalBlobGas*(tx: Transaction): uint64 =
|
|
|
|
GAS_PER_BLOB * tx.versionedHashes.len.uint64
|
2023-06-24 13:56:44 +00:00
|
|
|
|
2023-08-04 12:43:30 +00:00
|
|
|
proc getTotalBlobGas*(versionedHashesLen: int): uint64 =
|
2024-02-20 03:07:38 +00:00
|
|
|
GAS_PER_BLOB * versionedHashesLen.uint64
|
2023-06-24 13:56:44 +00:00
|
|
|
|
2024-02-21 09:14:34 +00:00
|
|
|
# getBlobBaseFee implements get_data_gas_price from EIP-4844
|
|
|
|
func getBlobBaseFee*(excessBlobGas: uint64): UInt256 =
|
2023-06-24 13:56:44 +00:00
|
|
|
fakeExponential(
|
2023-09-24 15:25:41 +00:00
|
|
|
MIN_BLOB_GASPRICE.u256,
|
|
|
|
excessBlobGas.u256,
|
|
|
|
BLOB_GASPRICE_UPDATE_FRACTION.u256
|
2023-06-24 13:56:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
proc calcDataFee*(versionedHashesLen: int,
|
2023-09-24 15:25:41 +00:00
|
|
|
excessBlobGas: uint64): UInt256 =
|
|
|
|
getTotalBlobGas(versionedHashesLen).u256 *
|
2024-02-21 09:14:34 +00:00
|
|
|
getBlobBaseFee(excessBlobGas)
|
2023-06-24 13:56:44 +00:00
|
|
|
|
2023-08-04 12:43:30 +00:00
|
|
|
func blobGasUsed(txs: openArray[Transaction]): uint64 =
|
2023-06-24 13:56:44 +00:00
|
|
|
for tx in txs:
|
2023-08-04 12:43:30 +00:00
|
|
|
result += tx.getTotalBlobGas
|
2023-06-24 13:56:44 +00:00
|
|
|
|
2022-12-13 20:06:26 +00:00
|
|
|
# https://eips.ethereum.org/EIPS/eip-4844
|
|
|
|
func validateEip4844Header*(
|
2023-06-24 13:56:44 +00:00
|
|
|
com: CommonRef, header, parentHeader: BlockHeader,
|
|
|
|
txs: openArray[Transaction]): Result[void, string] {.raises: [].} =
|
|
|
|
|
|
|
|
if not com.forkGTE(Cancun):
|
2023-08-04 12:43:30 +00:00
|
|
|
if header.blobGasUsed.isSome:
|
|
|
|
return err("unexpected EIP-4844 blobGasUsed in block header")
|
2023-06-24 13:56:44 +00:00
|
|
|
|
2023-08-04 12:43:30 +00:00
|
|
|
if header.excessBlobGas.isSome:
|
|
|
|
return err("unexpected EIP-4844 excessBlobGas in block header")
|
2023-06-24 13:56:44 +00:00
|
|
|
|
|
|
|
return ok()
|
|
|
|
|
2023-08-04 12:43:30 +00:00
|
|
|
if header.blobGasUsed.isNone:
|
|
|
|
return err("expect EIP-4844 blobGasUsed in block header")
|
2023-06-24 13:56:44 +00:00
|
|
|
|
2023-08-04 12:43:30 +00:00
|
|
|
if header.excessBlobGas.isNone:
|
|
|
|
return err("expect EIP-4844 excessBlobGas in block header")
|
2023-06-24 13:56:44 +00:00
|
|
|
|
|
|
|
let
|
2023-08-04 12:43:30 +00:00
|
|
|
headerBlobGasUsed = header.blobGasUsed.get()
|
|
|
|
blobGasUsed = blobGasUsed(txs)
|
|
|
|
headerExcessBlobGas = header.excessBlobGas.get
|
|
|
|
excessBlobGas = calcExcessBlobGas(parentHeader)
|
2023-06-24 13:56:44 +00:00
|
|
|
|
2023-08-04 12:43:30 +00:00
|
|
|
if blobGasUsed > MAX_BLOB_GAS_PER_BLOCK:
|
|
|
|
return err("blobGasUsed " & $blobGasUsed & " exceeds maximum allowance " & $MAX_BLOB_GAS_PER_BLOCK)
|
2023-06-24 13:56:44 +00:00
|
|
|
|
2023-08-04 12:43:30 +00:00
|
|
|
if headerBlobGasUsed != blobGasUsed:
|
|
|
|
return err("calculated blobGas not equal header.blobGasUsed")
|
2023-06-24 13:56:44 +00:00
|
|
|
|
2023-08-04 12:43:30 +00:00
|
|
|
if headerExcessBlobGas != excessBlobGas:
|
|
|
|
return err("calculated excessBlobGas not equal header.excessBlobGas")
|
2023-06-24 13:56:44 +00:00
|
|
|
|
2022-12-13 20:06:26 +00:00
|
|
|
return ok()
|
2023-06-24 13:56:44 +00:00
|
|
|
|
2024-05-15 03:07:59 +00:00
|
|
|
proc validateBlobTransactionWrapper*(tx: PooledTransaction):
|
2023-06-24 13:56:44 +00:00
|
|
|
Result[void, string] {.raises: [].} =
|
2023-10-20 08:30:05 +00:00
|
|
|
if tx.networkPayload.isNil:
|
2023-06-24 13:56:44 +00:00
|
|
|
return err("tx wrapper is none")
|
|
|
|
|
|
|
|
# note: assert blobs are not malformatted
|
2024-05-15 03:07:59 +00:00
|
|
|
let goodFormatted = tx.tx.versionedHashes.len ==
|
2023-06-24 13:56:44 +00:00
|
|
|
tx.networkPayload.commitments.len and
|
2024-05-15 03:07:59 +00:00
|
|
|
tx.tx.versionedHashes.len ==
|
2023-06-24 13:56:44 +00:00
|
|
|
tx.networkPayload.blobs.len and
|
2024-05-15 03:07:59 +00:00
|
|
|
tx.tx.versionedHashes.len ==
|
2023-06-24 13:56:44 +00:00
|
|
|
tx.networkPayload.proofs.len
|
|
|
|
|
|
|
|
if not goodFormatted:
|
|
|
|
return err("tx wrapper is ill formatted")
|
|
|
|
|
|
|
|
# Verify that commitments match the blobs by checking the KZG proof
|
|
|
|
let res = kzg.verifyBlobKzgProofBatch(tx.networkPayload.blobs,
|
|
|
|
tx.networkPayload.commitments, tx.networkPayload.proofs)
|
|
|
|
if res.isErr:
|
|
|
|
return err(res.error)
|
|
|
|
|
|
|
|
# Actual verification result
|
|
|
|
if not res.get():
|
|
|
|
return err("Failed to verify network payload of a transaction")
|
|
|
|
|
|
|
|
# Now that all commitments have been verified, check that versionedHashes matches the commitments
|
2024-05-15 03:07:59 +00:00
|
|
|
for i in 0 ..< tx.tx.versionedHashes.len:
|
2023-06-24 13:56:44 +00:00
|
|
|
# this additional check also done in tx validation
|
2024-05-15 03:07:59 +00:00
|
|
|
if tx.tx.versionedHashes[i].data[0] != VERSIONED_HASH_VERSION_KZG:
|
2023-06-24 13:56:44 +00:00
|
|
|
return err("wrong kzg version in versioned hash at index " & $i)
|
|
|
|
|
2024-05-15 03:07:59 +00:00
|
|
|
if tx.tx.versionedHashes[i] !=
|
|
|
|
kzgToVersionedHash(tx.networkPayload.commitments[i]):
|
2023-06-24 13:56:44 +00:00
|
|
|
return err("tx versioned hash not match commitments at index " & $i)
|
|
|
|
|
|
|
|
ok()
|
|
|
|
|
|
|
|
proc loadKzgTrustedSetup*(): Result[void, string] =
|
|
|
|
const
|
|
|
|
vendorDir = currentSourcePath.parentDir.replace('\\', '/') & "/../../vendor"
|
|
|
|
trustedSetupDir = vendorDir & "/nim-kzg4844/kzg4844/csources/src"
|
2023-10-24 05:46:33 +00:00
|
|
|
trustedSetup = staticRead trustedSetupDir & "/trusted_setup.txt"
|
2023-06-24 13:56:44 +00:00
|
|
|
|
2023-10-24 05:46:33 +00:00
|
|
|
Kzg.loadTrustedSetupFromString(trustedSetup)
|