import eth/common/eth_types, stint, kzg4844/kzg_ex as kzg, stew/endians2, nimcrypto/sha2, stew/results, ../../../nimbus/core/eip4844 type BlobID* = uint64 BlobIDs* = seq[BlobID] BlobCommitment* = object blob*: kzg.KzgBlob commitment*: kzg.KZGCommitment BlobTxWrapData* = object hashes*: seq[Hash256] blobs*: seq[kzg.KzgBlob] commitments*: seq[kzg.KZGCommitment] proofs*: seq[kzg.KzgProof] func getBlobList*(startId: BlobID, count: int): BlobIDs = result = newSeq[BlobID](count) for i in 0.. startIndex: count = uint64(endIndex - startIndex + 1) else: count = uint64(startIndex - endIndex + 1) result = newSeq[BlobID](count) if endIndex > startIndex: for i in 0..= BLS_MODULUS[i]: if BLS_MODULUS[i] > 0: # This chunk is greater than the modulus, and we can reduce it in this byte position expectedFieldElem[blobByteIdx] = BLS_MODULUS[i] - 1 # done with this field element 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 expectedFieldElem[blobByteIdx] = BLS_MODULUS[i] if not equalMem(blob[chunkIdx*32].unsafeaddr, expectedFieldElem[0].addr, 32): return false # Hash the current hash currentHashed = sha256.digest(currentHashed.data) return true proc fillBlob(blobId: BlobID): KzgBlob = if blobId == 0: # Blob zero is empty blob, so leave as is return # Fill the blob with deterministic data let blobIdBytes = toBytesBE blobId # First 32 bytes are the hash of the blob ID var currentHashed = sha256.digest(blobIdBytes) for chunkIdx in 0..= 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 # 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] # Hash the current hash currentHashed = sha256.digest(currentHashed.data) proc generateBlob(blobid: BlobID): BlobCommitment = result.blob = blobId.fillBlob() let res = blobToKzgCommitment(result.blob) if res.isErr: doAssert(false, res.error) result.commitment = res.get proc getVersionedHash*(blobid: BlobID, commitmentVersion: byte): Hash256 = let res = blobId.generateBlob() result = sha256.digest(res.commitment) result.data[0] = commitmentVersion proc blobDataGenerator*(startBlobId: BlobID, blobCount: int): BlobTxWrapData = result.blobs = newSeq[kzg.KzgBlob](blobCount) result.commitments = newSeq[kzg.KZGCommitment](blobCount) result.hashes = newSeq[Hash256](blobCount) result.proofs = newSeq[kzg.KzgProof](blobCount) for i in 0..