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],
|
2024-06-19 01:57:45 +00:00
|
|
|
stew/arrayops,
|
2023-09-24 15:25:41 +00:00
|
|
|
nimcrypto/sha2,
|
2024-09-29 12:37:09 +00:00
|
|
|
kzg4844/kzg,
|
2024-05-30 12:54:03 +00:00
|
|
|
results,
|
2023-06-24 13:56:44 +00:00
|
|
|
stint,
|
2024-12-20 09:12:16 +00:00
|
|
|
./eip7691,
|
2023-06-24 13:56:44 +00:00
|
|
|
../constants,
|
2022-12-13 20:06:26 +00:00
|
|
|
../common/common
|
|
|
|
|
2024-07-04 05:41:32 +00:00
|
|
|
from std/sequtils import mapIt
|
|
|
|
|
2023-01-30 22:10:23 +00:00
|
|
|
{.push raises: [].}
|
|
|
|
|
2023-06-24 13:56:44 +00:00
|
|
|
type
|
|
|
|
Bytes64 = array[64, 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
|
2024-06-19 01:57:45 +00:00
|
|
|
proc kzgToVersionedHash*(kzg: kzg.KzgCommitment): VersionedHash =
|
2024-10-01 17:40:37 +00:00
|
|
|
result = sha256.digest(kzg.bytes).to(Hash32)
|
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")
|
|
|
|
|
2024-07-04 05:41:32 +00:00
|
|
|
template copyFrom(T: type, input, a, b): auto =
|
|
|
|
type X = (type T().bytes)
|
|
|
|
T(bytes: X.initCopyFrom(input.toOpenArray(a, b)))
|
|
|
|
|
2024-06-19 01:57:45 +00:00
|
|
|
let
|
2024-07-04 05:41:32 +00:00
|
|
|
versionedHash = KzgBytes32.copyFrom(input, 0, 31)
|
|
|
|
z = KzgBytes32.copyFrom(input, 32, 63)
|
|
|
|
y = KzgBytes32.copyFrom(input, 64, 95)
|
|
|
|
commitment = KzgBytes48.copyFrom(input, 96, 143)
|
|
|
|
kzgProof = KzgBytes48.copyFrom(input, 144, 191)
|
2024-06-19 01:57:45 +00:00
|
|
|
|
2024-07-04 05:41:32 +00:00
|
|
|
if kzgToVersionedHash(commitment).data != versionedHash.bytes:
|
2023-08-24 05:11:19 +00:00
|
|
|
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
|
2024-12-20 09:12:16 +00:00
|
|
|
proc calcExcessBlobGas*(parent: Header, electra: bool): 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)
|
2024-12-20 09:12:16 +00:00
|
|
|
targetBlobGasPerBlock = getTargetBlobGasPerBlock(electra)
|
2023-06-24 13:56:44 +00:00
|
|
|
|
2024-12-20 09:12:16 +00:00
|
|
|
if excessBlobGas + blobGasUsed < targetBlobGasPerBlock:
|
2023-06-24 13:56:44 +00:00
|
|
|
0'u64
|
|
|
|
else:
|
2024-12-20 09:12:16 +00:00
|
|
|
excessBlobGas + blobGasUsed - targetBlobGasPerBlock
|
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
|
2024-12-20 09:12:16 +00:00
|
|
|
func getBlobBaseFee*(excessBlobGas: uint64, electra: bool): UInt256 =
|
|
|
|
let blobBaseFeeUpdateFraction = getBlobBaseFeeUpdateFraction(electra).u256
|
2023-06-24 13:56:44 +00:00
|
|
|
fakeExponential(
|
2023-09-24 15:25:41 +00:00
|
|
|
MIN_BLOB_GASPRICE.u256,
|
|
|
|
excessBlobGas.u256,
|
2024-12-20 09:12:16 +00:00
|
|
|
blobBaseFeeUpdateFraction
|
2023-06-24 13:56:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
proc calcDataFee*(versionedHashesLen: int,
|
2024-12-20 09:12:16 +00:00
|
|
|
excessBlobGas: uint64,
|
|
|
|
electra: bool): UInt256 =
|
2023-09-24 15:25:41 +00:00
|
|
|
getTotalBlobGas(versionedHashesLen).u256 *
|
2024-12-20 09:12:16 +00:00
|
|
|
getBlobBaseFee(excessBlobGas, electra)
|
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*(
|
2024-10-16 01:34:12 +00:00
|
|
|
com: CommonRef, header, parentHeader: Header,
|
2023-06-24 13:56:44 +00:00
|
|
|
txs: openArray[Transaction]): Result[void, string] {.raises: [].} =
|
|
|
|
|
2024-07-17 10:05:53 +00:00
|
|
|
if not com.isCancunOrLater(header.timestamp):
|
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
|
2024-12-20 09:12:16 +00:00
|
|
|
electra = com.isPragueOrLater(header.timestamp)
|
2023-08-04 12:43:30 +00:00
|
|
|
headerBlobGasUsed = header.blobGasUsed.get()
|
|
|
|
blobGasUsed = blobGasUsed(txs)
|
|
|
|
headerExcessBlobGas = header.excessBlobGas.get
|
2024-12-20 09:12:16 +00:00
|
|
|
excessBlobGas = calcExcessBlobGas(parentHeader, electra)
|
|
|
|
maxBlobGasPerBlock = getMaxBlobGasPerBlock(electra)
|
2023-06-24 13:56:44 +00:00
|
|
|
|
2024-12-20 09:12:16 +00:00
|
|
|
if blobGasUsed > maxBlobGasPerBlock:
|
|
|
|
return err("blobGasUsed " & $blobGasUsed & " exceeds maximum allowance " & $maxBlobGasPerBlock)
|
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")
|
|
|
|
|
2024-07-04 05:41:32 +00:00
|
|
|
let commitments = tx.networkPayload.commitments.mapIt(
|
2024-09-29 12:37:09 +00:00
|
|
|
kzg.KzgCommitment(bytes: it.data))
|
2024-06-14 14:41:59 +00:00
|
|
|
|
2023-06-24 13:56:44 +00:00
|
|
|
# Verify that commitments match the blobs by checking the KZG proof
|
2024-06-14 14:41:59 +00:00
|
|
|
let res = kzg.verifyBlobKzgProofBatch(
|
2024-07-04 05:41:32 +00:00
|
|
|
tx.networkPayload.blobs.mapIt(kzg.KzgBlob(bytes: it)),
|
2024-06-14 14:41:59 +00:00
|
|
|
commitments,
|
2024-09-29 12:37:09 +00:00
|
|
|
tx.networkPayload.proofs.mapIt(kzg.KzgProof(bytes: it.data)))
|
2024-06-14 14:41:59 +00:00
|
|
|
|
2023-06-24 13:56:44 +00:00
|
|
|
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-06-14 14:41:59 +00:00
|
|
|
if tx.tx.versionedHashes[i] != kzgToVersionedHash(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
|
|
|
|
2024-09-29 12:37:09 +00:00
|
|
|
loadTrustedSetupFromString(trustedSetup, 0)
|