From 54b0fe6f423d5040e67fdae4d1caf44ab935d197 Mon Sep 17 00:00:00 2001 From: benbierens Date: Mon, 27 Nov 2023 13:23:19 +0100 Subject: [PATCH] Cleanup --- codex/proof/datasampler.nim | 23 ++- codex/proof/types.nim | 5 +- tests/codex/proof/testdatasampler.nim | 209 +++++++++++++------------- 3 files changed, 118 insertions(+), 119 deletions(-) diff --git a/codex/proof/datasampler.nim b/codex/proof/datasampler.nim index 469b1c68..95562f72 100644 --- a/codex/proof/datasampler.nim +++ b/codex/proof/datasampler.nim @@ -34,7 +34,7 @@ type blockStore: BlockStore slotBlocks: SlotBlocks # The following data is invariant over time for a given slot: - slotRootHash: DSFieldElement + slotRootHash: FieldElement slotPoseidonTree: MerkleTree datasetToSlotProof: MerkleProof blockSize: uint64 @@ -48,7 +48,7 @@ proc new*( T: type DataSampler, slot: Slot, blockStore: BlockStore, - slotRootHash: DSFieldElement, + slotRootHash: FieldElement, slotPoseidonTree: MerkleTree, datasetToSlotProof: MerkleProof ): Future[?!DataSampler] {.async.} = @@ -87,7 +87,7 @@ func extractLowBits*[n: static int](A: BigInt[n], k: int): uint64 = r = bitor(r, 1'u64 shl i) return r -proc convertToSlotCellIndex(self: DataSampler, fe: DSFieldElement): uint64 = +proc convertToSlotCellIndex(self: DataSampler, fe: FieldElement): uint64 = let n = self.numberOfCellsInSlot.int log2 = ceilingLog2(n) @@ -95,33 +95,32 @@ proc convertToSlotCellIndex(self: DataSampler, fe: DSFieldElement): uint64 = return extractLowBits(fe.toBig(), log2) -proc getSlotBlockIndexForSlotCellIndex*(self: DataSampler, slotCellIndex: DSSlotCellIndex): uint64 = +func getSlotBlockIndexForSlotCellIndex*(self: DataSampler, slotCellIndex: uint64): uint64 = return slotCellIndex div self.numberOfCellsPerBlock -proc getBlockCellIndexForSlotCellIndex*(self: DataSampler, slotCellIndex: DSSlotCellIndex): uint64 = +func getBlockCellIndexForSlotCellIndex*(self: DataSampler, slotCellIndex: uint64): uint64 = return slotCellIndex mod self.numberOfCellsPerBlock -proc findSlotCellIndex*(self: DataSampler, challenge: DSFieldElement, counter: DSFieldElement): DSSlotCellIndex = +proc findSlotCellIndex*(self: DataSampler, challenge: FieldElement, counter: FieldElement): uint64 = # Computes the slot-cell index for a single sample. let input = @[self.slotRootHash, challenge, counter] hash = Sponge.digest(input, rate = 2) return convertToSlotCellIndex(self, hash) -func findSlotCellIndices*(self: DataSampler, challenge: DSFieldElement, nSamples: int): seq[DSSlotCellIndex] = +func findSlotCellIndices*(self: DataSampler, challenge: FieldElement, nSamples: int): seq[uint64] = # Computes nSamples slot-cell indices. return collect(newSeq, (for i in 1..nSamples: self.findSlotCellIndex(challenge, toF(i)))) -proc getCellFromBlock*(self: DataSampler, blk: bt.Block, slotCellIndex: DSSlotCellIndex): DSCell = +proc getCellFromBlock*(self: DataSampler, blk: bt.Block, slotCellIndex: uint64): Cell = let blockCellIndex = self.getBlockCellIndexForSlotCellIndex(slotCellIndex) dataStart = (CellSize * blockCellIndex) dataEnd = dataStart + CellSize - return blk.data[dataStart ..< dataEnd] -proc getBlockCells*(self: DataSampler, blk: bt.Block): seq[DSCell] = - var cells: seq[DSCell] +proc getBlockCells*(self: DataSampler, blk: bt.Block): seq[Cell] = + var cells: seq[Cell] for i in 0 ..< self.numberOfCellsPerBlock: cells.add(self.getCellFromBlock(blk, i)) return cells @@ -139,7 +138,7 @@ proc getBlockCellMiniTree*(self: DataSampler, blk: bt.Block): ?!MerkleTree = return builder.build() -proc getProofInput*(self: DataSampler, challenge: DSFieldElement, nSamples: int): Future[?!ProofInput] {.async.} = +proc getProofInput*(self: DataSampler, challenge: FieldElement, nSamples: int): Future[?!ProofInput] {.async.} = var slotToBlockProofs: seq[MerkleProof] blockToCellProofs: seq[MerkleProof] diff --git a/codex/proof/types.nim b/codex/proof/types.nim index f91c7b38..076bac59 100644 --- a/codex/proof/types.nim +++ b/codex/proof/types.nim @@ -7,9 +7,8 @@ const CellSize* = 2048.uint64 type - DSFieldElement* = F - DSSlotCellIndex* = uint64 - DSCell* = seq[byte] + FieldElement* = F + Cell* = seq[byte] ProofInput* = ref object datasetToSlotProof*: MerkleProof slotToBlockProofs*: seq[MerkleProof] diff --git a/tests/codex/proof/testdatasampler.nim b/tests/codex/proof/testdatasampler.nim index c7a34d43..b4ea71ed 100644 --- a/tests/codex/proof/testdatasampler.nim +++ b/tests/codex/proof/testdatasampler.nim @@ -28,8 +28,8 @@ import testdatasampler_expected let bytesPerBlock = 64 * 1024 - challenge: DSFieldElement = toF(12345) - slotRootHash: DSFieldElement = toF(6789) + challenge: FieldElement = toF(12345) + slotRootHash: FieldElement = toF(6789) asyncchecksuite "Test proof datasampler - components": let @@ -181,6 +181,109 @@ asyncchecksuite "Test proof datasampler - main": createSlotPoseidonTree() await createDataSampler() + test "Number of cells is a power of two": + # This is to check that the data used for testing is sane. + proc isPow2(value: int): bool = + let log2 = ceilingLog2(value) + return (1 shl log2) == value + + let numberOfCells = getNumberOfCellsInSlot(slot).int + + check: + isPow2(numberOfCells) + + let knownIndices = @[50.uint64, 21.uint64, 110.uint64] + + test "Can find single slot-cell index": + proc slotCellIndex(i: int): uint64 = + let counter: FieldElement = toF(i) + return dataSampler.findSlotCellIndex(challenge, counter) + + proc getExpectedIndex(i: int): uint64 = + let + numberOfCellsInSlot = (bytesPerBlock * numberOfSlotBlocks) div CellSize.int + hash = Sponge.digest(@[slotRootHash, challenge, toF(i)], rate = 2) + return extractLowBits(hash.toBig(), ceilingLog2(numberOfCellsInSlot)) + + check: + slotCellIndex(1) == getExpectedIndex(1) + slotCellIndex(1) == knownIndices[0] + slotCellIndex(2) == getExpectedIndex(2) + slotCellIndex(2) == knownIndices[1] + slotCellIndex(3) == getExpectedIndex(3) + slotCellIndex(3) == knownIndices[2] + + test "Can find sequence of slot-cell indices": + proc slotCellIndices(n: int): seq[uint64] = + dataSampler.findSlotCellIndices(challenge, n) + + proc getExpectedIndices(n: int): seq[uint64] = + return collect(newSeq, (for i in 1..n: dataSampler.findSlotCellIndex(challenge, toF(i)))) + + check: + slotCellIndices(3) == getExpectedIndices(3) + slotCellIndices(3) == knownIndices + + let + bytes = newSeqWith(bytesPerBlock, rand(uint8)) + blk = bt.Block.new(bytes).tryGet() + cell0Bytes = bytes[0.. " & $expected & ")": let @@ -198,105 +301,3 @@ asyncchecksuite "Test proof datasampler - main": check: blockCellIndex == expected.uint64 - - let knownIndices = @[178.uint64, 277.uint64, 366.uint64] - - test "Can find single slot-cell index": - proc slotCellIndex(i: int): DSSlotCellIndex = - let counter: DSFieldElement = toF(i) - return dataSampler.findSlotCellIndex(challenge, counter) - - proc getExpectedIndex(i: int): DSSlotCellIndex = - let - numberOfCellsInSlot = (bytesPerBlock * numberOfSlotBlocks) div CellSize.int - hash = Sponge.digest(@[slotRootHash, challenge, toF(i)], rate = 2) - return extractLowBits(hash.toBig(), ceilingLog2(numberOfCellsInSlot)) - - check: - slotCellIndex(1) == getExpectedIndex(1) - slotCellIndex(1) == knownIndices[0] - slotCellIndex(2) == getExpectedIndex(2) - slotCellIndex(2) == knownIndices[1] - slotCellIndex(3) == getExpectedIndex(3) - slotCellIndex(3) == knownIndices[2] - - # test "Can find sequence of slot-cell indices": - # proc slotCellIndices(n: int): seq[DSSlotCellIndex] = - # dataSampler.findSlotCellIndices(challenge, n) - - # proc getExpectedIndices(n: int): seq[DSSlotCellIndex] = - # return collect(newSeq, (for i in 1..n: dataSampler.findSlotCellIndex(challenge, toF(i)))) - - # check: - # slotCellIndices(3) == getExpectedIndices(3) - # slotCellIndices(3) == knownIndices - - # test "Can get cell from block": - # let - # bytes = newSeqWith(bytesPerBlock, rand(uint8)) - # blk = bt.Block.new(bytes).tryGet() - - # sample0 = dataSampler.getCellFromBlock(blk, 0) - # sample1 = dataSampler.getCellFromBlock(blk, 1) - # sample2 = dataSampler.getCellFromBlock(blk, 2) - - # check: - # sample0 == bytes[0..