From 1ae3ecd8236ffb1f2904a643822002da5cad14ba Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Tue, 16 Jan 2024 19:29:24 -0600 Subject: [PATCH] fix tests that work with correct cell indices --- tests/codex/slots/provingtestenv.nim | 32 +++++++++++++--------- tests/codex/slots/testsampler.nim | 13 +++++---- tests/codex/slots/testsampler_expected.nim | 18 ++++++------ tests/codex/slots/testutils.nim | 6 ++-- 4 files changed, 38 insertions(+), 31 deletions(-) diff --git a/tests/codex/slots/provingtestenv.nim b/tests/codex/slots/provingtestenv.nim index 95faab67..0e1ae85d 100644 --- a/tests/codex/slots/provingtestenv.nim +++ b/tests/codex/slots/provingtestenv.nim @@ -22,6 +22,18 @@ import pkg/codex/utils/asynciter import ../helpers import ../merkletree/helpers +const + # The number of slot blocks and number of slots, combined with + # the bytes per block, make it so that there are exactly 256 cells + # in the dataset. + bytesPerBlock* = 64 * 1024 + cellsPerBlock* = bytesPerBlock div DefaultCellSize.int + numberOfSlotBlocks* = 4 + totalNumberOfSlots* = 2 + datasetSlotIndex* = 1 + cellsPerSlot* = (bytesPerBlock * numberOfSlotBlocks) div DefaultCellSize.int + totalNumCells = ((numberOfSlotBlocks * totalNumberOfSlots * bytesPerBlock) div DefaultCellSize.int) + type ProvingTestEnvironment* = ref object # Invariant: @@ -31,6 +43,7 @@ type manifest*: Manifest manifestBlock*: bt.Block slot*: Slot + slotIndicies*: seq[seq[int]] datasetBlocks*: seq[bt.Block] slotTree*: Poseidon2Tree slotRootCid*: Cid @@ -38,23 +51,12 @@ type datasetToSlotTree*: Poseidon2Tree datasetRootHash*: Poseidon2Hash -const - # The number of slot blocks and number of slots, combined with - # the bytes per block, make it so that there are exactly 256 cells - # in the dataset. - bytesPerBlock* = 64 * 1024 - numberOfSlotBlocks* = 4 - totalNumberOfSlots* = 2 - datasetSlotIndex* = 1 - cellsPerSlot* = (bytesPerBlock * numberOfSlotBlocks) div DefaultCellSize.int - proc createDatasetBlocks(self: ProvingTestEnvironment): Future[void] {.async.} = - let numberOfCellsNeeded = (numberOfSlotBlocks * totalNumberOfSlots * bytesPerBlock).uint64 div DefaultCellSize.uint64 var data: seq[byte] = @[] # This generates a number of blocks that have different data, such that # Each cell in each block is unique, but nothing is random. - for i in 0 ..< numberOfCellsNeeded: + for i in 0 ..< totalNumCells: data = data & (i.byte).repeat(DefaultCellSize.uint64) let chunker = MockChunker.new( @@ -76,6 +78,8 @@ proc createSlotTree(self: ProvingTestEnvironment, dSlotIndex: uint64): Future[Po datasetBlockIndexingStrategy = SteppedIndexingStrategy.new(0, self.datasetBlocks.len - 1, totalNumberOfSlots) datasetBlockIndices = toSeq(datasetBlockIndexingStrategy.getIndicies(dSlotIndex.int)) + self.slotIndicies[dSlotIndex] = datasetBlockIndices + let slotBlocks = datasetBlockIndices.mapIt(self.datasetBlocks[it]) numBlockCells = bytesPerBlock.int div DefaultCellSize.int @@ -97,6 +101,7 @@ proc createDatasetRootHashAndSlotTree(self: ProvingTestEnvironment): Future[void var slotTrees = newSeq[Poseidon2Tree]() for i in 0 ..< totalNumberOfSlots: slotTrees.add(await self.createSlotTree(i.uint64)) + self.slotTree = slotTrees[datasetSlotIndex] self.slotRootCid = slotTrees[datasetSlotIndex].root().tryGet().toSlotCid().tryGet() self.slotRoots = slotTrees.mapIt(it.root().tryGet()) @@ -159,7 +164,8 @@ proc createSlot(self: ProvingTestEnvironment): void = proc createProvingTestEnvironment*(): Future[ProvingTestEnvironment] {.async.} = var testEnv = ProvingTestEnvironment( - challenge: toF(12345) + challenge: toF(12345), + slotIndicies: newSeq[seq[int]](totalNumberOfSlots) ) testEnv.localStore = CacheStore.new() diff --git a/tests/codex/slots/testsampler.nim b/tests/codex/slots/testsampler.nim index afb79e5e..a13cde40 100644 --- a/tests/codex/slots/testsampler.nim +++ b/tests/codex/slots/testsampler.nim @@ -44,6 +44,7 @@ asyncchecksuite "Test DataSampler": SlotsBuilder.new(env.localStore, env.manifest).tryGet()).tryGet() setup: + randomize() env = await createProvingTestEnvironment() let bytes = newSeqWith(bytesPerBlock, rand(uint8)) blk = bt.Block.new(bytes).tryGet() @@ -96,22 +97,22 @@ asyncchecksuite "Test DataSampler": input.verifyProof == expectedProof # block-slot proofs - input.samples[0].slotBlockIdx == 2 - input.samples[1].slotBlockIdx == 2 - input.samples[2].slotBlockIdx == 0 + input.samples[0].slotBlockIdx == 3 + input.samples[1].slotBlockIdx == 3 + input.samples[2].slotBlockIdx == 3 toStr(input.samples[0].slotProof) == expectedBlockSlotProofs[0] toStr(input.samples[1].slotProof) == expectedBlockSlotProofs[1] toStr(input.samples[2].slotProof) == expectedBlockSlotProofs[2] # cell-block proofs - input.samples[0].blockCellIdx == 26 + input.samples[0].blockCellIdx == 1 input.samples[1].blockCellIdx == 29 - input.samples[2].blockCellIdx == 29 + input.samples[2].blockCellIdx == 5 toStr(input.samples[0].cellProof) == expectedCellBlockProofs[0] toStr(input.samples[1].cellProof) == expectedCellBlockProofs[1] toStr(input.samples[2].cellProof) == expectedCellBlockProofs[2] - # # cell data + # cell data toHex(input.samples[0].data) == expectedCellData[0] toHex(input.samples[1].data) == expectedCellData[1] toHex(input.samples[2].data) == expectedCellData[2] diff --git a/tests/codex/slots/testsampler_expected.nim b/tests/codex/slots/testsampler_expected.nim index 7d041f86..946652eb 100644 --- a/tests/codex/slots/testsampler_expected.nim +++ b/tests/codex/slots/testsampler_expected.nim @@ -5,21 +5,21 @@ import pkg/codex/codextypes proc getExpectedCellBlockProofs*(): seq[string] = @[ - "0x189890bedf2a40f2757554c5f089811e07601543a576e2d40d68a1bd295adbee0x059f227fe687a7abd9c3d9878c0b812aa7829c85d30c23154b8824a907909b060x2c685fc951f1684fd3979f7e3a558fa6aeed3f960ef0a9e2ba51cc62cff7de0e0x10ebae3fb12d502044216e40319726ed36308b9ae4ab3fb0a36c77e5614c6fbd0x1decd3fb7ff458261149731115657cecd7eb2fe4a6cf84f3c6761aa8b0dd6b9a", - "0x2567cd93b3fe058b31908656c05d3a09fd33cc0d7df3c7c005d991a8cda60ba80x16e4788105082295706c49c604216518f16ca9dd106012c7c98e6ee138893f6e0x1c258af996aecf9bba249130182ccfe44a9090bc58fe59063a06db67efb7b5240x10ebae3fb12d502044216e40319726ed36308b9ae4ab3fb0a36c77e5614c6fbd0x1decd3fb7ff458261149731115657cecd7eb2fe4a6cf84f3c6761aa8b0dd6b9a", - "0x0568735989a51526104eddbcf386b8aaef186a2d31afce0c2671c8ce8dd8cd1a0x20d06082668338924981a9e0e4f18e7ec6e2b7912e7fb74c1b6dc921b824def60x2fd45662152ae87192971a0e9b7d50de48d7bc8ab22e5711680173a302120bf00x0f528a58c839889e4bb9195e2bcbc2addb7022e47c8fb11bbdeba0a0e9c6f4cb0x0edf43ec0f277500371537a4d566f3f352d0c49bfa9d4659e07d776ffe119437" + "0x25a5eb25b99ac58d637c44e776d235c14d4dd242692af5ee5f408321b09efe080x0a1917880f9a012ff69ecb78f2998686a8f52fb5ae5f7ba06bc1bf71fbb07a4f0x20cb422cbf0525bdc22c5d984fadf66f8a8e05e8e4a019424b30d3ee2a3ce4c00x0e85faef68f8933db587cb8807e31eb03e0ec8bfb9043bfddadcad1dde50f9790x0363952a6d08539ad707dbfb3bbcb249b1658fd1409c14ba4ee49e8a61c8a7ee", + "0x1f0f029e9bc8044e3f06752d09cbc0d2c3c32579fcd38f074bae321d55465d2a0x15847d909cabdac1a9612cc3b62eaf3375aa7f0a6929dd1fbaed1bfeb39066ac0x11b677732d8a0d196a9fba82f4b2fa4eb549524504d6d3aacedf6bbb6d9356d90x27a362c0f92c887d74ee77df2df5e51f0f66b0564a2af8b1c11b02dab0a34f780x095eb3c0d166c19f9cac8ea0e6ba274dfeef802cf7d01c90378585fd4d788e56", + "0x2aa17646c1b567df6775dedfdec1f22c775c7457d7d8452a6800cb3820fb77e70x2a48f8260d5757a8683ede9909dcff2f82ffa6343d39bd2d758fbec6ad0645710x199b5348b9d0ac6f9839b31fb38defcf811eefd29b8f7c95e615ac572e5a80c90x0e85faef68f8933db587cb8807e31eb03e0ec8bfb9043bfddadcad1dde50f9790x0363952a6d08539ad707dbfb3bbcb249b1658fd1409c14ba4ee49e8a61c8a7ee" ] proc getExpectedBlockSlotProofs*(): seq[string] = @[ - "0x0684458ea77eca59be05e368bb26b7ca318b8e836100c415e60136876c01ba170x2a66917fa49371e835376fcece0d854c77008ac1195740963b1ac4491ee1aaf1", - "0x0684458ea77eca59be05e368bb26b7ca318b8e836100c415e60136876c01ba170x2a66917fa49371e835376fcece0d854c77008ac1195740963b1ac4491ee1aaf1", - "0x03883ad2637a4c68f29bc0910400259291d9c3d730de7e3925adbf26c80b7f440x2d6a888f50b14b0c686f64c4bd0d8389cd555cdf0e3d6f387682c4722ac2a674" + "0x2120044583a9c578407f44da2acd0d0d15c656a28476c689103795855e40766e0x2a66917fa49371e835376fcece0d854c77008ac1195740963b1ac4491ee1aaf1", + "0x2120044583a9c578407f44da2acd0d0d15c656a28476c689103795855e40766e0x2a66917fa49371e835376fcece0d854c77008ac1195740963b1ac4491ee1aaf1", + "0x2120044583a9c578407f44da2acd0d0d15c656a28476c689103795855e40766e0x2a66917fa49371e835376fcece0d854c77008ac1195740963b1ac4491ee1aaf1" ] proc getExpectedCellData*(): seq[string] = @[ - "BA".repeat(DefaultCellSize.int), - "BD".repeat(DefaultCellSize.int), - "3D".repeat(DefaultCellSize.int) + "61".repeat(DefaultCellSize.int), + "7D".repeat(DefaultCellSize.int), + "65".repeat(DefaultCellSize.int) ] diff --git a/tests/codex/slots/testutils.nim b/tests/codex/slots/testutils.nim index 04e38b40..caced2e0 100644 --- a/tests/codex/slots/testutils.nim +++ b/tests/codex/slots/testutils.nim @@ -79,10 +79,10 @@ asyncchecksuite "Test proof sampler utils": slotCellIndex(3) == knownIndices[2] test "Can find sequence of slot-cell indices": - proc slotCellIndices(n: int): seq[Natural] = - cellIndices(env.challenge, slotRoot, numCells, n) + proc slotCellIndices(n: int): seq[Natural] = + cellIndices(env.challenge, slotRoot, env.slotIndicies.concat, cellsPerBlock, numCells, n) - proc getExpectedIndices(n: int): seq[Natural] = + proc getExpectedIndices(n: int): seq[Natural] = return collect(newSeq, (for i in 1..n: cellIndex(env.challenge, slotRoot, numCells, i))) check: