Skip to content

Commit

Permalink
Bump nim-kzg4844 and nimbus-eth2 for gcc-14 compatibility (#2357)
Browse files Browse the repository at this point in the history
  • Loading branch information
jangko committed Jun 14, 2024
1 parent 68f462e commit 4c45819
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 41 deletions.
14 changes: 7 additions & 7 deletions hive_integration/nodocker/engine/cancun/blobs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func verifyBlob*(blobId: BlobID, blob: kzg.KzgBlob): bool =
# This chunk is greater than the modulus, but we can't reduce it in this byte position, so we will try in the next byte position
expectedFieldElem[blobByteIdx] = BLS_MODULUS[i]

if not equalMem(blob[chunkIdx*32].unsafeaddr, expectedFieldElem[0].addr, 32):
if not equalMem(blob.bytes[chunkIdx*32].unsafeaddr, expectedFieldElem[0].addr, 32):
return false

# Hash the current hash
Expand All @@ -109,24 +109,24 @@ proc fillBlob(blobId: BlobID): KzgBlob =
var currentHashed = sha256.digest(blobIdBytes)

for chunkIdx in 0..<FIELD_ELEMENTS_PER_BLOB:
copyMem(result[chunkIdx*32].addr, currentHashed.data[0].addr, 32)
copyMem(result.bytes[chunkIdx*32].addr, currentHashed.data[0].addr, 32)

# Check that no 32 bytes chunks are greater than the BLS modulus
for i in 0..<32:
#blobByteIdx = ((chunkIdx + 1) * 32) - i - 1
let blobByteIdx = (chunkIdx * 32) + i
if result[blobByteIdx] < BLS_MODULUS[i]:
if result.bytes[blobByteIdx] < BLS_MODULUS[i]:
# go to next chunk
break
elif result[blobByteIdx] >= BLS_MODULUS[i]:
elif result.bytes[blobByteIdx] >= BLS_MODULUS[i]:
if BLS_MODULUS[i] > 0:
# This chunk is greater than the modulus, and we can reduce it in this byte position
result[blobByteIdx] = BLS_MODULUS[i] - 1
result.bytes[blobByteIdx] = BLS_MODULUS[i] - 1
# go to next chunk
break
else:
# This chunk is greater than the modulus, but we can't reduce it in this byte position, so we will try in the next byte position
result[blobByteIdx] = BLS_MODULUS[i]
result.bytes[blobByteIdx] = BLS_MODULUS[i]

# Hash the current hash
currentHashed = sha256.digest(currentHashed.data)
Expand All @@ -140,7 +140,7 @@ proc generateBlob(blobid: BlobID): BlobCommitment =

proc getVersionedHash*(blobid: BlobID, commitmentVersion: byte): Hash256 =
let res = blobid.generateBlob()
result = sha256.digest(res.commitment)
result = sha256.digest(res.commitment.bytes)
result.data[0] = commitmentVersion

proc blobDataGenerator*(startBlobId: BlobID, blobCount: int): BlobTxWrapData =
Expand Down
6 changes: 3 additions & 3 deletions hive_integration/nodocker/engine/cancun/helpers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ proc getBlobDataInPayload*(pool: TestBlobTxPool, payload: ExecutionPayload): Res
for i in 0..<blobTx.tx.versionedHashes.len:
blobData.data.add BlobWrapData(
versionedHash: blobTx.tx.versionedHashes[i],
commitment : np.commitments[i],
blob : np.blobs[i],
proof : np.proofs[i],
commitment : kzg.KzgCommitment(bytes: np.commitments[i]),
blob : kzg.KzgBlob(bytes: np.blobs[i]),
proof : kzg.KzgProof(bytes: np.proofs[i]),
)
blobData.txs.add blobTx.tx

Expand Down
6 changes: 3 additions & 3 deletions hive_integration/nodocker/engine/cancun/step_newpayloads.nim
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,15 @@ proc verifyBlobBundle(step: NewPayloads,
let bundleBlob = blobBundle.blobs[i].bytes
let bundleProof = blobBundle.proofs[i].bytes

if bundleCommitment != blobData.commitment:
if bundleCommitment != blobData.commitment.bytes:
error "KZG mismatch at index of the bundle", index=i
return false

if bundleBlob != blobData.blob:
if bundleBlob != blobData.blob.bytes:
error "blob mismatch at index of the bundle", index=i
return false

if bundleProof != blobData.proof:
if bundleProof != blobData.proof.bytes:
error "proof mismatch at index of the bundle", index=i
return false

Expand Down
14 changes: 8 additions & 6 deletions hive_integration/nodocker/engine/tx_sender.nim
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import
../../../nimbus/common,
../../../nimbus/utils/utils

from std/sequtils import mapIt

type
BaseTx* = object of RootObj
recipient* : Opt[EthAddress]
Expand Down Expand Up @@ -193,9 +195,9 @@ proc makeTxOfType(params: MakeTxParams, tc: BaseTx): PooledTransaction =
versionedHashes: system.move(blobData.hashes),
),
networkPayload: NetworkPayload(
blobs: system.move(blobData.blobs),
commitments: system.move(blobData.commitments),
proofs: system.move(blobData.proofs),
blobs: blobData.blobs.mapIt(it.bytes),
commitments: blobData.commitments.mapIt(it.bytes),
proofs: blobData.proofs.mapIt(it.bytes),
)
)
else:
Expand Down Expand Up @@ -337,9 +339,9 @@ proc makeTx*(params: MakeTxParams, tc: BlobTx): PooledTransaction =
PooledTransaction(
tx: signTransaction(unsignedTx, params.key, params.chainId, eip155 = true),
networkPayload: NetworkPayload(
blobs : data.blobs,
commitments: data.commitments,
proofs : data.proofs,
blobs : data.blobs.mapIt(it.bytes),
commitments: data.commitments.mapIt(it.bytes),
proofs : data.proofs.mapIt(it.bytes),
),
)

Expand Down
45 changes: 25 additions & 20 deletions nimbus/core/eip4844.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ import
../constants,
../common/common

from std/sequtils import mapIt

{.push raises: [].}

type
Bytes32 = array[32, byte]
Bytes64 = array[64, byte]
Bytes48 = array[48, byte]

const
BLS_MODULUS_STR = "52435875175126190479447740508185965837690552500527637822603658699938581184513"
Expand All @@ -40,7 +40,7 @@ const

# kzgToVersionedHash implements kzg_to_versioned_hash from EIP-4844
proc kzgToVersionedHash*(kzg: kzg.KZGCommitment): VersionedHash =
result = sha256.digest(kzg)
result = sha256.digest(kzg.bytes)
result.data[0] = VERSIONED_HASH_VERSION_KZG

# pointEvaluation implements point_evaluation_precompile from EIP-4844
Expand All @@ -55,19 +55,19 @@ proc pointEvaluation*(input: openArray[byte]): Result[void, string] =
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]

if kzgToVersionedHash(commitment).data != versionedHash:
versionedHash: KzgBytes32
z: KzgBytes32
y: KzgBytes32
commitment: KzgBytes48
kzgProof: KzgBytes48

versionedHash.bytes[0..<32] = input[0..<32]
z.bytes[0..<32] = input[32..<64]
y.bytes[0..<32] = input[64..<96]
commitment.bytes[0..<48] = input[96..<144]
kzgProof.bytes[0..<48] = input[144..<192]

if kzgToVersionedHash(commitment).data != versionedHash.bytes:
return err("versionedHash should equal to kzgToVersionedHash(commitment)")

# Verify KZG proof
Expand Down Expand Up @@ -183,9 +183,15 @@ proc validateBlobTransactionWrapper*(tx: PooledTransaction):
if not goodFormatted:
return err("tx wrapper is ill formatted")

let commitments = tx.networkPayload.commitments.mapIt(
kzg.KzgCommitment(bytes: it))

# Verify that commitments match the blobs by checking the KZG proof
let res = kzg.verifyBlobKzgProofBatch(tx.networkPayload.blobs,
tx.networkPayload.commitments, tx.networkPayload.proofs)
let res = kzg.verifyBlobKzgProofBatch(
tx.networkPayload.blobs.mapIt(kzg.KzgBlob(bytes: it)),
commitments,
tx.networkPayload.proofs.mapIt(kzg.KzgProof(bytes: it)))

if res.isErr:
return err(res.error)

Expand All @@ -199,8 +205,7 @@ proc validateBlobTransactionWrapper*(tx: PooledTransaction):
if tx.tx.versionedHashes[i].data[0] != VERSIONED_HASH_VERSION_KZG:
return err("wrong kzg version in versioned hash at index " & $i)

if tx.tx.versionedHashes[i] !=
kzgToVersionedHash(tx.networkPayload.commitments[i]):
if tx.tx.versionedHashes[i] != kzgToVersionedHash(commitments[i]):
return err("tx versioned hash not match commitments at index " & $i)

ok()
Expand Down
2 changes: 1 addition & 1 deletion vendor/nim-kzg4844
Submodule nim-kzg4844 updated 1 files
+1 −1 kzg4844/csources

0 comments on commit 4c45819

Please sign in to comment.