2024-01-17 19:24:34 +00:00
|
|
|
## Nim-Codex
|
|
|
|
## Copyright (c) 2023 Status Research & Development GmbH
|
|
|
|
## Licensed under either of
|
|
|
|
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
## at your option.
|
|
|
|
## This file may not be copied, modified, or distributed except according to
|
|
|
|
## those terms.
|
|
|
|
|
|
|
|
import std/sugar
|
|
|
|
|
2024-02-08 02:27:11 +00:00
|
|
|
import pkg/chronicles
|
2024-01-17 19:24:34 +00:00
|
|
|
import pkg/chronos
|
|
|
|
import pkg/questionable
|
|
|
|
import pkg/questionable/results
|
|
|
|
import pkg/stew/arrayops
|
|
|
|
|
|
|
|
import ../../market
|
|
|
|
import ../../blocktype as bt
|
|
|
|
import ../../merkletree
|
|
|
|
import ../../manifest
|
|
|
|
import ../../stores
|
|
|
|
|
2024-02-08 02:27:11 +00:00
|
|
|
import ../converters
|
2024-01-17 19:24:34 +00:00
|
|
|
import ../builder
|
2024-02-08 02:27:11 +00:00
|
|
|
import ../types
|
2024-01-17 19:24:34 +00:00
|
|
|
import ./utils
|
|
|
|
|
|
|
|
logScope:
|
|
|
|
topics = "codex datasampler"
|
|
|
|
|
|
|
|
type
|
2024-02-08 02:27:11 +00:00
|
|
|
DataSampler*[T, H] = ref object of RootObj
|
2024-01-17 19:24:34 +00:00
|
|
|
index: Natural
|
|
|
|
blockStore: BlockStore
|
2024-02-08 02:27:11 +00:00
|
|
|
builder: SlotsBuilder[T, H]
|
2024-01-17 19:24:34 +00:00
|
|
|
|
2024-02-08 02:27:11 +00:00
|
|
|
func getCell*[T, H](
|
|
|
|
self: DataSampler[T, H],
|
|
|
|
blkBytes: seq[byte],
|
|
|
|
blkCellIdx: Natural): seq[byte] =
|
2024-01-17 19:24:34 +00:00
|
|
|
let
|
|
|
|
cellSize = self.builder.cellSize.uint64
|
|
|
|
dataStart = cellSize * blkCellIdx.uint64
|
|
|
|
dataEnd = dataStart + cellSize
|
|
|
|
|
2024-02-08 02:27:11 +00:00
|
|
|
doAssert (dataEnd - dataStart) == cellSize, "Invalid cell size"
|
|
|
|
|
|
|
|
toInputData[H](blkBytes[dataStart ..< dataEnd])
|
|
|
|
|
|
|
|
proc getSample*[T, H](
|
|
|
|
self: DataSampler[T, H],
|
|
|
|
cellIdx: int,
|
|
|
|
slotTreeCid: Cid,
|
|
|
|
slotRoot: H): Future[?!Sample[H]] {.async.} =
|
|
|
|
|
2024-01-25 23:34:38 +00:00
|
|
|
let
|
|
|
|
cellsPerBlock = self.builder.numBlockCells
|
2024-02-08 02:27:11 +00:00
|
|
|
blkCellIdx = cellIdx.toCellInBlk(cellsPerBlock) # block cell index
|
|
|
|
blkSlotIdx = cellIdx.toBlkInSlot(cellsPerBlock) # slot tree index
|
|
|
|
origBlockIdx = self.builder.slotIndicies(self.index)[blkSlotIdx] # convert to original dataset block index
|
2024-01-25 23:34:38 +00:00
|
|
|
|
|
|
|
logScope:
|
2024-02-08 02:27:11 +00:00
|
|
|
cellIdx = cellIdx
|
|
|
|
blkSlotIdx = blkSlotIdx
|
|
|
|
blkCellIdx = blkCellIdx
|
|
|
|
origBlockIdx = origBlockIdx
|
|
|
|
|
|
|
|
trace "Retrieving sample from block tree"
|
|
|
|
let
|
|
|
|
(_, proof) = (await self.blockStore.getCidAndProof(
|
|
|
|
slotTreeCid, blkSlotIdx.Natural)).valueOr:
|
|
|
|
return failure("Failed to get slot tree CID and proof")
|
|
|
|
|
|
|
|
slotProof = proof.toVerifiableProof().valueOr:
|
|
|
|
return failure("Failed to get verifiable proof")
|
|
|
|
|
|
|
|
(bytes, blkTree) = (await self.builder.buildBlockTree(
|
|
|
|
origBlockIdx, blkSlotIdx)).valueOr:
|
|
|
|
return failure("Failed to build block tree")
|
|
|
|
|
|
|
|
cellData = self.getCell(bytes, blkCellIdx)
|
|
|
|
cellProof = blkTree.getProof(blkCellIdx).valueOr:
|
|
|
|
return failure("Failed to get proof from block tree")
|
|
|
|
|
|
|
|
success Sample[H](
|
|
|
|
cellData: cellData,
|
|
|
|
merklePaths: (cellProof.path & slotProof.path))
|
|
|
|
|
|
|
|
proc getProofInput*[T, H](
|
|
|
|
self: DataSampler[T, H],
|
2024-01-17 19:24:34 +00:00
|
|
|
entropy: ProofChallenge,
|
2024-02-08 02:27:11 +00:00
|
|
|
nSamples: Natural): Future[?!ProofInput[H]] {.async.} =
|
2024-01-17 19:24:34 +00:00
|
|
|
## Generate proofs as input to the proving circuit.
|
|
|
|
##
|
|
|
|
|
|
|
|
let
|
2024-02-08 02:27:11 +00:00
|
|
|
entropy = H.fromBytes(
|
2024-01-17 19:24:34 +00:00
|
|
|
array[31, byte].initCopyFrom(entropy[0..30])) # truncate to 31 bytes, otherwise it _might_ be greater than mod
|
|
|
|
|
2024-02-08 02:27:11 +00:00
|
|
|
verifyTree = self.builder.verifyTree.toFailure.valueOr:
|
|
|
|
return failure("Failed to get verify tree")
|
2024-01-17 19:24:34 +00:00
|
|
|
|
2024-02-08 02:27:11 +00:00
|
|
|
slotProof = verifyTree.getProof(self.index).valueOr:
|
|
|
|
return failure("Failed to get slot proof")
|
2024-01-17 19:24:34 +00:00
|
|
|
|
2024-02-08 02:27:11 +00:00
|
|
|
datasetRoot = verifyTree.root().valueOr:
|
|
|
|
return failure("Failed to get dataset root")
|
2024-01-17 19:24:34 +00:00
|
|
|
|
2024-02-08 02:27:11 +00:00
|
|
|
slotTreeCid = self.builder.manifest.slotRoots[self.index]
|
|
|
|
slotRoot = self.builder.slotRoots[self.index]
|
|
|
|
cellIdxs = entropy.cellIndices(
|
|
|
|
slotRoot,
|
|
|
|
self.builder.numSlotCells,
|
2024-01-25 23:34:38 +00:00
|
|
|
nSamples)
|
2024-01-17 19:24:34 +00:00
|
|
|
|
2024-02-08 02:27:11 +00:00
|
|
|
logScope:
|
|
|
|
cells = cellIdxs
|
|
|
|
|
|
|
|
trace "Collecting input for proof"
|
2024-01-25 23:34:38 +00:00
|
|
|
let samples = collect(newSeq):
|
|
|
|
for cellIdx in cellIdxs:
|
2024-02-08 02:27:11 +00:00
|
|
|
(await self.getSample(cellIdx, slotTreeCid, slotRoot)).valueOr:
|
|
|
|
return failure("Failed to get sample")
|
2024-01-17 19:24:34 +00:00
|
|
|
|
2024-02-08 02:27:11 +00:00
|
|
|
success ProofInput[H](
|
2024-01-17 19:24:34 +00:00
|
|
|
entropy: entropy,
|
2024-02-08 02:27:11 +00:00
|
|
|
datasetRoot: datasetRoot,
|
|
|
|
slotProof: slotProof.path,
|
|
|
|
nSlotsPerDataSet: self.builder.numSlots,
|
|
|
|
nCellsPerSlot: self.builder.numSlotCells,
|
|
|
|
slotRoot: slotRoot,
|
2024-01-17 19:24:34 +00:00
|
|
|
slotIndex: self.index,
|
|
|
|
samples: samples)
|
2024-02-08 02:27:11 +00:00
|
|
|
|
|
|
|
proc new*[T, H](
|
|
|
|
_: type DataSampler[T, H],
|
|
|
|
index: Natural,
|
|
|
|
blockStore: BlockStore,
|
|
|
|
builder: SlotsBuilder[T, H]): ?!DataSampler[T, H] =
|
|
|
|
|
|
|
|
if index > builder.slotRoots.high:
|
|
|
|
error "Slot index is out of range"
|
|
|
|
return failure("Slot index is out of range")
|
|
|
|
|
|
|
|
if not builder.verifiable:
|
|
|
|
return failure("Cannot instantiate DataSampler for non-verifiable builder")
|
|
|
|
|
|
|
|
success DataSampler[T, H](
|
|
|
|
index: index,
|
|
|
|
blockStore: blockStore,
|
|
|
|
builder: builder)
|