diff --git a/codex/proof/misc.nim b/codex/proof/misc.nim deleted file mode 100644 index 58fa25b1..00000000 --- a/codex/proof/misc.nim +++ /dev/null @@ -1,13 +0,0 @@ -func floorLog2* (x : int) : int = - var k = -1 - var y = x - while (y > 0): - k += 1 - y = y shr 1 - return k - -func ceilingLog2* (x : int) : int = - if (x==0): - return -1 - else: - return (floorLog2(x-1) + 1) diff --git a/codex/proof/slotblocks.nim b/codex/proof/slotblocks.nim deleted file mode 100644 index 8a71bd50..00000000 --- a/codex/proof/slotblocks.nim +++ /dev/null @@ -1,70 +0,0 @@ -import std/bitops -import std/sugar - -import pkg/chronos -import pkg/chronicles -import pkg/libp2p -import pkg/questionable -import pkg/questionable/results - -import ../contracts/requests -import ../stores/blockstore -import ../manifest - -type - SlotBlocks* = ref object of RootObj - slot: Slot - blockStore: BlockStore - manifest: Manifest - -proc getManifestForSlot(slot: Slot, blockStore: BlockStore): Future[?!Manifest] {.async.} = - without manifestBlockCid =? Cid.init(slot.request.content.cid).mapFailure, err: - error "Unable to init CID from slot.content.cid" - return failure err - - without manifestBlock =? await blockStore.getBlock(manifestBlockCid), err: - error "Failed to fetch manifest block", cid = manifestBlockCid - return failure err - - without manifest =? Manifest.decode(manifestBlock): - error "Unable to decode manifest" - return failure("Unable to decode manifest") - - return success(manifest) - -proc new*( - T: type SlotBlocks, - slot: Slot, - blockStore: BlockStore -): Future[?!SlotBlocks] {.async.} = - # Create a SlotBlocks object for a slot. - # SlotBlocks lets you get the manifest of a slot and blocks by slotBlockIndex for a slot. - without manifest =? await getManifestForSlot(slot, blockStore): - error "Failed to get manifest for slot" - return failure("Failed to get manifest for slot") - - success(SlotBlocks( - slot: slot, - blockStore: blockStore, - manifest: manifest - )) - -proc manifest*(self: SlotBlocks): Manifest = - self.manifest - -proc getDatasetBlockIndexForSlotBlockIndex*(self: SlotBlocks, slotBlockIndex: uint64): uint64 = - let - slotSize = self.slot.request.ask.slotSize.truncate(uint64) - blocksInSlot = slotSize div self.manifest.blockSize.uint64 - datasetSlotIndex = self.slot.slotIndex.truncate(uint64) - return (datasetSlotIndex * blocksInSlot) + slotBlockIndex - -proc getSlotBlock*(self: SlotBlocks, slotBlockIndex: uint64): Future[?!Block] {.async.} = - let - blocksInManifest = (self.manifest.datasetSize div self.manifest.blockSize).uint64 - datasetBlockIndex = self.getDatasetBlockIndexForSlotBlockIndex(slotBlockIndex) - - if datasetBlockIndex >= blocksInManifest: - return failure("Found datasetBlockIndex that is out-of-range: " & $datasetBlockIndex) - - return await self.blockStore.getBlock(self.manifest.treeCid, datasetBlockIndex) diff --git a/codex/slots/sampler.nim b/codex/slots/sampler.nim new file mode 100644 index 00000000..12e25dfb --- /dev/null +++ b/codex/slots/sampler.nim @@ -0,0 +1,5 @@ +import ./sampler/sampler +import ./sampler/types +import ./sampler/utils + +export sampler, types, utils diff --git a/codex/proof/datasampler.nim b/codex/slots/sampler/sampler.nim similarity index 85% rename from codex/proof/datasampler.nim rename to codex/slots/sampler/sampler.nim index 1f5573fd..15feb8e2 100644 --- a/codex/proof/datasampler.nim +++ b/codex/slots/sampler/sampler.nim @@ -32,34 +32,16 @@ type DataSampler* = ref object of RootObj slot: Slot blockStore: BlockStore - slotBlocks: SlotBlocks # The following data is invariant over time for a given slot: - datasetRoot: FieldElement - slotRootHash: FieldElement - slotPoseidonTree: MerkleTree - datasetToSlotProof: MerkleProof - blockSize: uint64 - numberOfCellsInSlot: uint64 - datasetSlotIndex: uint64 - numberOfCellsPerBlock: uint64 - -proc getNumberOfCellsInSlot*(slot: Slot): uint64 = - (slot.request.ask.slotSize.truncate(uint64) div CellSize) + builder: SlotsBuilder proc new*( T: type DataSampler, slot: Slot, blockStore: BlockStore, - datasetRoot: FieldElement, - slotPoseidonTree: MerkleTree, - datasetToSlotProof: MerkleProof -): Future[?!DataSampler] {.async.} = + builder: SlotsBuilder): Future[?!DataSampler] {.async.} = # Create a DataSampler for a slot. # A DataSampler can create the input required for the proving circuit. - without slotBlocks =? await SlotBlocks.new(slot, blockStore), err: - error "Failed to create SlotBlocks object for slot" - return failure(err) - let numberOfCellsInSlot = getNumberOfCellsInSlot(slot) blockSize = slotBlocks.manifest.blockSize.uint64 @@ -78,18 +60,22 @@ proc new*( numberOfCellsPerBlock: blockSize div CellSize )) -func extractLowBits*[n: static int](A: BigInt[n], k: int): uint64 = - assert(k > 0 and k <= 64) - var r: uint64 = 0 - for i in 0..= blocksInManifest: + return failure("Found datasetBlockIndex that is out-of-range: " & $datasetBlockIndex) + + return await self.blockStore.getBlock(self.manifest.treeCid, datasetBlockIndex) proc convertToSlotCellIndex(self: DataSampler, fe: FieldElement): uint64 = let diff --git a/codex/proof/types.nim b/codex/slots/sampler/types.nim similarity index 100% rename from codex/proof/types.nim rename to codex/slots/sampler/types.nim diff --git a/codex/slots/sampler/utils.nim b/codex/slots/sampler/utils.nim new file mode 100644 index 00000000..a7d2cd99 --- /dev/null +++ b/codex/slots/sampler/utils.nim @@ -0,0 +1,26 @@ +func extractLowBits*[n: static int](A: BigInt[n], k: int): uint64 = + assert(k > 0 and k <= 64) + var r: uint64 = 0 + for i in 0.. 0): + k += 1 + y = y shr 1 + return k + +func ceilingLog2* (x : int) : int = + if (x==0): + return -1 + else: + return (floorLog2(x-1) + 1)