add: compute matrix (#6550)

* add: compute matrix

* rm unsused imports

* rm unused imports 2

* rm bin

* rm unused line

* improve doc

* rid space on proc sig

* review 1

* added seeded PRNG

* randomize blob count as well, with smaller seed

* review 2
This commit is contained in:
Agnish Ghosh 2024-09-16 16:49:34 +05:30 committed by GitHub
parent 72b5c2bd6e
commit b3c8c71776
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 85 additions and 3 deletions

View File

@ -536,6 +536,11 @@ OK: 5/5 Fail: 0/5 Skip: 0/5
+ EIP7594: Extended Sample Count OK
```
OK: 1/1 Fail: 0/1 Skip: 0/1
## EIP-7594 Unit Tests
```diff
+ EIP-7594: Compute Matrix OK
```
OK: 1/1 Fail: 0/1 Skip: 0/1
## EL Configuration
```diff
+ Empty config file OK
@ -1119,4 +1124,4 @@ OK: 2/2 Fail: 0/2 Skip: 0/2
OK: 9/9 Fail: 0/9 Skip: 0/9
---TOTAL---
OK: 760/765 Fail: 0/765 Skip: 5/765
OK: 761/766 Fail: 0/766 Skip: 5/766

View File

@ -99,6 +99,27 @@ func get_custody_column_list*(node_id: NodeId,
sortedColumnIndexList(ColumnIndex(columns_per_subnet), subnet_ids)
# https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.5/specs/_features/eip7594/das-core.md#compute_matrix
proc compute_matrix*(blobs: seq[KzgBlob]): Result[seq[MatrixEntry], cstring] =
## `compute_matrix` helper demonstrates the relationship
## between blobs and the `MatrixEntries`
var extended_matrix: seq[MatrixEntry]
for blbIdx, blob in blobs.pairs:
let cellsAndProofs = computeCellsAndKzgProofs(blob)
if not cellsAndProofs.isOk:
return err("Computing Extended Matrix: Issue computing cells and proofs")
for i in 0..<eip7594.CELLS_PER_EXT_BLOB:
extended_matrix.add(MatrixEntry(
cell: cellsAndProofs.get.cells[i],
kzg_proof: cellsAndProofs.get.proofs[i],
row_index: blbIdx.uint64,
column_index: i.uint64
))
ok(extended_matrix)
# https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.5/specs/_features/eip7594/peer-sampling.md#get_extended_sample_count
func get_extended_sample_count*(samples_per_slot: int,
allowed_failures: int):

View File

@ -9,8 +9,62 @@
{.used.}
import
std/random,
unittest2,
../beacon_chain/spec/[helpers, eip7594_helpers]
results,
kzg4844/[kzg_abi, kzg],
./consensus_spec/[os_ops, fixtures_utils],
../beacon_chain/spec/[helpers, eip7594_helpers],
../beacon_chain/spec/datatypes/[eip7594, deneb]
from std/strutils import rsplit
block:
template sourceDir: string = currentSourcePath.rsplit(DirSep, 1)[0]
doAssert loadTrustedSetup(
sourceDir &
"/../vendor/nim-kzg4844/kzg4844/csources/src/trusted_setup.txt", 0).isOk
# 114 is the MSB (most/max significant byte)
# such that BLS modulus does not overflow
const MAX_TOP_BYTE = 114
proc createSampleKzgBlobs(n: int, seed: int): seq[KzgBlob] =
var
blobs: seq[KzgBlob] = @[]
# Initialize the PRNG with the given seed
rng = initRand(seed)
for blobIndex in 0..<n:
var blob: array[int(BYTES_PER_BLOB), byte]
# Fill the blob with random bytes using the seeded PRNG
for byteIndex in 0..<int(BYTES_PER_BLOB):
blob[byteIndex] = rng.rand(byte)
# Adjust bytes according to the given condition
for byteIndex in 0..<int(BYTES_PER_BLOB):
if blob[byteIndex] > MAX_TOP_BYTE and
byteIndex mod kzg_abi.BYTES_PER_FIELD_ELEMENT == 0:
blob[byteIndex] = MAX_TOP_BYTE
blobs.add(KzgBlob(bytes: blob))
blobs
iterator chunks[T](lst: seq[T], n: int): seq[T] =
## Iterator that yields N-sized chunks from the list.
for i in countup(0, len(lst) - 1, n):
yield lst[i..min(i + n - 1, len(lst) - 1)]
suite "EIP-7594 Unit Tests":
test "EIP-7594: Compute Matrix":
proc testComputeExtendedMatrix() =
var
rng = initRand(126)
blob_count = rng.rand(1..(deneb.MAX_BLOB_COMMITMENTS_PER_BLOCK.int))
let
input_blobs = createSampleKzgBlobs(blob_count, rng.rand(int))
extended_matrix = compute_matrix(input_blobs)
doAssert extended_matrix.get.len == kzg_abi.CELLS_PER_EXT_BLOB * blob_count
for row in chunks(extended_matrix.get, kzg_abi.CELLS_PER_EXT_BLOB):
doAssert len(row) == kzg_abi.CELLS_PER_EXT_BLOB
testComputeExtendedMatrix()
suite "EIP-7594 Sampling Tests":
test "EIP7594: Extended Sample Count":
@ -43,4 +97,6 @@ suite "EIP-7594 Sampling Tests":
for (allowed_failures, extendedSampleCount) in tests:
check: get_extended_sample_count(
samplesPerSlot, allowed_failures) == extendedSampleCount
testExtendedSampleCount()
testExtendedSampleCount()
doAssert freeTrustedSetup().isOk