Skip to content

Commit

Permalink
add: recover matrix (#6553)
Browse files Browse the repository at this point in the history
  • Loading branch information
agnxsh committed Sep 17, 2024
1 parent 205dff3 commit 1feeff4
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
5 changes: 3 additions & 2 deletions AllTests-mainnet.md
Original file line number Diff line number Diff line change
Expand Up @@ -539,8 +539,9 @@ OK: 1/1 Fail: 0/1 Skip: 0/1
## EIP-7594 Unit Tests
```diff
+ EIP-7594: Compute Matrix OK
+ EIP:7594: Recover Matrix OK
```
OK: 1/1 Fail: 0/1 Skip: 0/1
OK: 2/2 Fail: 0/2 Skip: 0/2
## EL Configuration
```diff
+ Empty config file OK
Expand Down Expand Up @@ -1124,4 +1125,4 @@ OK: 2/2 Fail: 0/2 Skip: 0/2
OK: 9/9 Fail: 0/9 Skip: 0/9

---TOTAL---
OK: 761/766 Fail: 0/766 Skip: 5/766
OK: 762/767 Fail: 0/767 Skip: 5/767
35 changes: 35 additions & 0 deletions beacon_chain/spec/eip7594_helpers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,41 @@ proc compute_matrix*(blobs: seq[KzgBlob]): Result[seq[MatrixEntry], cstring] =

ok(extended_matrix)

# https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.5/specs/_features/eip7594/das-core.md#recover_matrix
proc recover_matrix*(partial_matrix: seq[MatrixEntry],
blobCount: int):
Result[seq[MatrixEntry], cstring] =
## This helper demonstrates how to apply recover_cells_and_kzg_proofs
## The data structure for storing cells is implementation-dependent
var extended_matrix: seq[MatrixEntry]
for blob_index in 0..<blobCount:
var
cell_indices: seq[CellIndex]
cells: seq[Cell]

for e in partial_matrix:
if e.row_index == uint64(blob_index):
cell_indices.add(e.column_index)
cells.add(e.cell)

let recoveredCellsAndKzgProofs =
recoverCellsAndKzgProofs(cell_indices, cells)
if not recoveredCellsAndKzgProofs.isOk:
return err("Issue in recovering cells and proofs")

for i in 0..<recoveredCellsAndKzgProofs.get.cells.len:
let
cell = recoveredCellsAndKzgProofs.get.cells[i]
proof = recoveredCellsAndKzgProofs.get.proofs[i]
extended_matrix.add(MatrixEntry(
cell: cell,
kzg_proof: proof,
row_index: blob_index.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):
Expand Down
28 changes: 28 additions & 0 deletions tests/test_eip7594_helpers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,34 @@ suite "EIP-7594 Unit Tests":
doAssert len(row) == kzg_abi.CELLS_PER_EXT_BLOB
testComputeExtendedMatrix()

test "EIP:7594: Recover Matrix":
proc testRecoverMatrix() =
var rng = initRand(126)

# Number of samples we shall be recovering
const N_SAMPLES = kzg_abi.CELLS_PER_EXT_BLOB div 2

# Compute an extended matrix with a random
# blob count for this test
let
blob_count = rng.rand(1..(NUMBER_OF_COLUMNS.int))
blobs = createSampleKzgBlobs(blob_count, rng.rand(int))
extended_matrix = compute_matrix(blobs)

# Construct a matrix with some entries missing
var partial_matrix: seq[MatrixEntry]
for blob_entries in chunks(extended_matrix.get, kzg_abi.CELLS_PER_EXT_BLOB):
var blb_entry = blob_entries
rng.shuffle(blb_entry)
partial_matrix.add(blb_entry[0..N_SAMPLES-1])

# Given the partial matrix, recover the missing entries
let recovered_matrix = recover_matrix(partial_matrix, blob_count)

# Ensure that the recovered matrix matches the original matrix
doAssert recovered_matrix.get == extended_matrix.get, "Both matrices don't match!"
testRecoverMatrix()

suite "EIP-7594 Sampling Tests":
test "EIP7594: Extended Sample Count":
proc testExtendedSampleCount() =
Expand Down

0 comments on commit 1feeff4

Please sign in to comment.