2024-01-08 22:52:46 +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.
|
|
|
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
|
|
import std/math
|
|
|
|
import std/sequtils
|
|
|
|
import std/sugar
|
|
|
|
|
|
|
|
import pkg/libp2p
|
|
|
|
import pkg/chronos
|
|
|
|
import pkg/chronicles
|
|
|
|
import pkg/questionable/results
|
|
|
|
import pkg/poseidon2
|
|
|
|
import pkg/poseidon2/io
|
2024-01-11 16:45:23 +00:00
|
|
|
import pkg/constantine/math/arithmetic/finite_fields
|
2024-01-08 22:52:46 +00:00
|
|
|
|
|
|
|
import ../indexingstrategy
|
|
|
|
import ../merkletree
|
|
|
|
import ../stores
|
|
|
|
import ../manifest
|
|
|
|
import ../utils
|
2024-01-15 16:45:04 +00:00
|
|
|
import ../utils/asynciter
|
2024-01-08 22:52:46 +00:00
|
|
|
import ../utils/digest
|
2024-01-11 16:45:23 +00:00
|
|
|
import ../utils/poseidon2digest
|
2024-01-08 22:52:46 +00:00
|
|
|
|
2024-01-15 16:45:04 +00:00
|
|
|
import ./converters
|
|
|
|
|
|
|
|
export converters
|
|
|
|
|
2024-01-08 22:52:46 +00:00
|
|
|
const
|
|
|
|
# TODO: Unified with the CellSize specified in branch "data-sampler"
|
|
|
|
# in the proving circuit.
|
|
|
|
CellSize* = 2048
|
|
|
|
|
2024-01-11 16:45:23 +00:00
|
|
|
DefaultEmptyBlock* = newSeq[byte](DefaultBlockSize.int)
|
|
|
|
DefaultEmptyCell* = newSeq[byte](DefaultCellSize.int)
|
|
|
|
|
2024-01-08 22:52:46 +00:00
|
|
|
type
|
2024-01-11 16:45:23 +00:00
|
|
|
# TODO: should be a generic type that
|
|
|
|
# supports all merkle trees
|
|
|
|
SlotsBuilder* = ref object of RootObj
|
2024-01-08 22:52:46 +00:00
|
|
|
store: BlockStore
|
|
|
|
manifest: Manifest
|
|
|
|
strategy: IndexingStrategy
|
|
|
|
cellSize: int
|
2024-01-11 16:45:23 +00:00
|
|
|
blockEmptyDigest: Poseidon2Hash
|
2024-01-08 22:52:46 +00:00
|
|
|
blockPadBytes: seq[byte]
|
|
|
|
slotsPadLeafs: seq[Poseidon2Hash]
|
|
|
|
rootsPadLeafs: seq[Poseidon2Hash]
|
2024-01-11 16:45:23 +00:00
|
|
|
slotRoots: seq[Poseidon2Hash]
|
|
|
|
verifyRoot: ?Poseidon2Hash
|
|
|
|
|
|
|
|
func slotRoots*(self: SlotsBuilder): seq[Poseidon2Hash] {.inline.} =
|
|
|
|
## Returns the slot roots.
|
|
|
|
##
|
|
|
|
|
|
|
|
self.slotRoots
|
|
|
|
|
|
|
|
func verifyRoot*(self: SlotsBuilder): ?Poseidon2Hash {.inline.} =
|
|
|
|
## Returns the slots root (verification root).
|
|
|
|
##
|
|
|
|
|
|
|
|
self.verifyRoot
|
2024-01-08 22:52:46 +00:00
|
|
|
|
2024-01-11 16:45:23 +00:00
|
|
|
func nextPowerOfTwoPad*(a: int): int =
|
|
|
|
## Returns the difference between the original
|
|
|
|
## value and the next power of two.
|
|
|
|
##
|
|
|
|
|
|
|
|
nextPowerOfTwo(a) - a
|
|
|
|
|
|
|
|
func numBlockPadBytes*(self: SlotsBuilder): Natural =
|
2024-01-08 22:52:46 +00:00
|
|
|
## Number of padding bytes required for a pow2
|
|
|
|
## merkle tree for each block.
|
|
|
|
##
|
|
|
|
|
|
|
|
self.blockPadBytes.len
|
|
|
|
|
2024-01-11 16:45:23 +00:00
|
|
|
func numSlotsPadLeafs*(self: SlotsBuilder): Natural =
|
2024-01-08 22:52:46 +00:00
|
|
|
## Number of padding field elements required for a pow2
|
|
|
|
## merkle tree for each slot.
|
|
|
|
##
|
|
|
|
|
|
|
|
self.slotsPadLeafs.len
|
|
|
|
|
2024-01-11 16:45:23 +00:00
|
|
|
func numRootsPadLeafs*(self: SlotsBuilder): Natural =
|
2024-01-08 22:52:46 +00:00
|
|
|
## Number of padding field elements required for a pow2
|
|
|
|
## merkle tree for the slot roots.
|
|
|
|
##
|
|
|
|
|
|
|
|
self.rootsPadLeafs.len
|
|
|
|
|
2024-01-11 16:45:23 +00:00
|
|
|
func numSlots*(self: SlotsBuilder): Natural =
|
|
|
|
## Number of slots.
|
|
|
|
##
|
|
|
|
|
|
|
|
self.manifest.numSlots
|
|
|
|
|
|
|
|
func numSlotBlocks*(self: SlotsBuilder): Natural =
|
2024-01-08 22:52:46 +00:00
|
|
|
## Number of blocks per slot.
|
|
|
|
##
|
|
|
|
|
|
|
|
self.manifest.blocksCount div self.manifest.numSlots
|
|
|
|
|
2024-01-11 16:45:23 +00:00
|
|
|
func slotBytes*(self: SlotsBuilder): NBytes =
|
|
|
|
## Number of bytes per slot.
|
|
|
|
##
|
|
|
|
|
|
|
|
(self.manifest.blockSize.int * self.numSlotBlocks).NBytes
|
|
|
|
|
|
|
|
func numBlockCells*(self: SlotsBuilder): Natural =
|
2024-01-08 22:52:46 +00:00
|
|
|
## Number of cells per block.
|
|
|
|
##
|
|
|
|
|
|
|
|
self.manifest.blockSize.int div self.cellSize
|
|
|
|
|
2024-01-15 16:45:04 +00:00
|
|
|
func slotIndicies*(self: SlotsBuilder, slot: Natural): ?!Iter[int] =
|
|
|
|
## Returns the slot indices.
|
|
|
|
## TODO: should return an iterator
|
|
|
|
##
|
2024-01-08 22:52:46 +00:00
|
|
|
|
2024-01-15 16:45:04 +00:00
|
|
|
self.strategy.getIndicies(slot).catch
|
2024-01-08 22:52:46 +00:00
|
|
|
|
|
|
|
proc getCellHashes*(
|
2024-01-11 16:45:23 +00:00
|
|
|
self: SlotsBuilder,
|
2024-01-08 22:52:46 +00:00
|
|
|
slotIndex: int): Future[?!seq[Poseidon2Hash]] {.async.} =
|
|
|
|
|
|
|
|
let
|
|
|
|
treeCid = self.manifest.treeCid
|
|
|
|
blockCount = self.manifest.blocksCount
|
|
|
|
numberOfSlots = self.manifest.numSlots
|
|
|
|
|
|
|
|
logScope:
|
|
|
|
treeCid = treeCid
|
|
|
|
blockCount = blockCount
|
|
|
|
numberOfSlots = numberOfSlots
|
|
|
|
index = blockIndex
|
|
|
|
slotIndex = slotIndex
|
|
|
|
|
|
|
|
let
|
|
|
|
hashes: seq[Poseidon2Hash] = collect(newSeq):
|
|
|
|
for blockIndex in self.strategy.getIndicies(slotIndex):
|
|
|
|
trace "Getting block CID for tree at index"
|
|
|
|
|
|
|
|
without blk =? (await self.store.getBlock(treeCid, blockIndex)), err:
|
|
|
|
error "Failed to get block CID for tree at index"
|
|
|
|
return failure(err)
|
|
|
|
|
2024-01-11 16:45:23 +00:00
|
|
|
if blk.isEmpty:
|
|
|
|
self.blockEmptyDigest
|
|
|
|
else:
|
|
|
|
without digest =? Poseidon2Tree.digest(blk.data & self.blockPadBytes, self.cellSize), err:
|
|
|
|
error "Failed to create digest for block"
|
|
|
|
return failure(err)
|
2024-01-08 22:52:46 +00:00
|
|
|
|
2024-01-11 16:45:23 +00:00
|
|
|
digest
|
2024-01-08 22:52:46 +00:00
|
|
|
|
|
|
|
success hashes
|
|
|
|
|
|
|
|
proc buildSlotTree*(
|
2024-01-11 16:45:23 +00:00
|
|
|
self: SlotsBuilder,
|
2024-01-08 22:52:46 +00:00
|
|
|
slotIndex: int): Future[?!Poseidon2Tree] {.async.} =
|
|
|
|
without cellHashes =? (await self.getCellHashes(slotIndex)), err:
|
|
|
|
error "Failed to select slot blocks", err = err.msg
|
|
|
|
return failure(err)
|
|
|
|
|
|
|
|
Poseidon2Tree.init(cellHashes & self.slotsPadLeafs)
|
|
|
|
|
|
|
|
proc buildSlot*(
|
2024-01-11 16:45:23 +00:00
|
|
|
self: SlotsBuilder,
|
2024-01-15 16:45:04 +00:00
|
|
|
slotIndex: Natural): Future[?!Poseidon2Hash] {.async.} =
|
2024-01-08 22:52:46 +00:00
|
|
|
## Build a slot tree and store it in the block store.
|
|
|
|
##
|
|
|
|
|
2024-01-11 16:45:23 +00:00
|
|
|
logScope:
|
|
|
|
cid = self.manifest.treeCid
|
|
|
|
slotIndex = slotIndex
|
|
|
|
|
|
|
|
trace "Building slot tree"
|
|
|
|
|
2024-01-08 22:52:46 +00:00
|
|
|
without tree =? (await self.buildSlotTree(slotIndex)) and
|
|
|
|
treeCid =? tree.root.?toSlotCid, err:
|
|
|
|
error "Failed to build slot tree", err = err.msg
|
|
|
|
return failure(err)
|
|
|
|
|
|
|
|
trace "Storing slot tree", treeCid, slotIndex, leaves = tree.leavesCount
|
|
|
|
for i, leaf in tree.leaves:
|
|
|
|
without cellCid =? leaf.toCellCid, err:
|
|
|
|
error "Failed to get CID for slot cell", err = err.msg
|
|
|
|
return failure(err)
|
|
|
|
|
|
|
|
without proof =? tree.getProof(i) and
|
|
|
|
encodableProof =? proof.toEncodableProof, err:
|
|
|
|
error "Failed to get proof for slot tree", err = err.msg
|
|
|
|
return failure(err)
|
|
|
|
|
|
|
|
if err =? (await self.store.putCidAndProof(
|
|
|
|
treeCid, i, cellCid, encodableProof)).errorOption:
|
|
|
|
error "Failed to store slot tree", err = err.msg
|
|
|
|
return failure(err)
|
|
|
|
|
|
|
|
tree.root()
|
|
|
|
|
2024-01-11 16:45:23 +00:00
|
|
|
func buildRootsTree*(
|
|
|
|
self: SlotsBuilder,
|
|
|
|
slotRoots: openArray[Poseidon2Hash]): ?!Poseidon2Tree =
|
|
|
|
Poseidon2Tree.init(@slotRoots & self.rootsPadLeafs)
|
|
|
|
|
|
|
|
proc buildSlots*(self: SlotsBuilder): Future[?!void] {.async.} =
|
|
|
|
## Build all slot trees and store them in the block store.
|
|
|
|
##
|
|
|
|
|
|
|
|
logScope:
|
|
|
|
cid = self.manifest.treeCid
|
|
|
|
blockCount = self.manifest.blocksCount
|
|
|
|
|
|
|
|
trace "Building slots"
|
|
|
|
|
|
|
|
if self.slotRoots.len == 0:
|
|
|
|
self.slotRoots = collect(newSeq):
|
2024-01-08 22:52:46 +00:00
|
|
|
for i in 0..<self.manifest.numSlots:
|
2024-01-11 16:45:23 +00:00
|
|
|
without slotRoot =? (await self.buildSlot(i)), err:
|
2024-01-08 22:52:46 +00:00
|
|
|
error "Failed to build slot", err = err.msg, index = i
|
|
|
|
return failure(err)
|
2024-01-11 16:45:23 +00:00
|
|
|
slotRoot
|
2024-01-08 22:52:46 +00:00
|
|
|
|
2024-01-11 16:45:23 +00:00
|
|
|
without root =? self.buildRootsTree(self.slotRoots).?root(), err:
|
|
|
|
error "Failed to build slot roots tree", err = err.msg
|
|
|
|
return failure(err)
|
2024-01-08 22:52:46 +00:00
|
|
|
|
2024-01-11 16:45:23 +00:00
|
|
|
if self.verifyRoot.isSome and self.verifyRoot.get != root: # TODO: `!=` doesn't work for SecretBool
|
|
|
|
return failure "Existing slots root doesn't match reconstructed root."
|
|
|
|
else:
|
|
|
|
self.verifyRoot = some root
|
2024-01-08 22:52:46 +00:00
|
|
|
|
2024-01-11 16:45:23 +00:00
|
|
|
success()
|
2024-01-08 22:52:46 +00:00
|
|
|
|
2024-01-11 16:45:23 +00:00
|
|
|
proc buildManifest*(self: SlotsBuilder): Future[?!Manifest] {.async.} =
|
|
|
|
if err =? (await self.buildSlots()).errorOption:
|
2024-01-08 22:52:46 +00:00
|
|
|
error "Failed to build slot roots", err = err.msg
|
|
|
|
return failure(err)
|
|
|
|
|
2024-01-11 16:45:23 +00:00
|
|
|
without rootCids =? self.slotRoots.toSlotCids(), err:
|
|
|
|
error "Failed to map slot roots to CIDs", err = err.msg
|
2024-01-08 22:52:46 +00:00
|
|
|
return failure(err)
|
|
|
|
|
2024-01-11 16:45:23 +00:00
|
|
|
without rootProvingCidRes =? self.verifyRoot.?toSlotsRootsCid() and
|
|
|
|
rootProvingCid =? rootProvingCidRes, err: # TODO: why doesn't `.?` unpack the result?
|
2024-01-08 22:52:46 +00:00
|
|
|
error "Failed to map slot roots to CIDs", err = err.msg
|
|
|
|
return failure(err)
|
|
|
|
|
2024-01-11 16:45:23 +00:00
|
|
|
Manifest.new(self.manifest, rootProvingCid, rootCids)
|
2024-01-08 22:52:46 +00:00
|
|
|
|
|
|
|
proc new*(
|
2024-01-11 16:45:23 +00:00
|
|
|
T: type SlotsBuilder,
|
2024-01-08 22:52:46 +00:00
|
|
|
store: BlockStore,
|
|
|
|
manifest: Manifest,
|
2024-01-11 16:45:23 +00:00
|
|
|
strategy: ?IndexingStrategy = none IndexingStrategy,
|
|
|
|
cellSize = CellSize): ?!SlotsBuilder =
|
2024-01-08 22:52:46 +00:00
|
|
|
|
|
|
|
if not manifest.protected:
|
2024-01-11 16:45:23 +00:00
|
|
|
return failure("Can only create SlotsBuilder using protected manifests.")
|
2024-01-08 22:52:46 +00:00
|
|
|
|
|
|
|
if (manifest.blocksCount mod manifest.numSlots) != 0:
|
|
|
|
return failure("Number of blocks must be divisable by number of slots.")
|
|
|
|
|
|
|
|
if (manifest.blockSize.int mod cellSize) != 0:
|
|
|
|
return failure("Block size must be divisable by cell size.")
|
|
|
|
|
|
|
|
let
|
2024-01-11 16:45:23 +00:00
|
|
|
strategy = if strategy.isNone:
|
2024-01-15 16:45:04 +00:00
|
|
|
? SteppedIndexingStrategy.new(
|
|
|
|
0, manifest.blocksCount - 1, manifest.numSlots).catch
|
2024-01-08 22:52:46 +00:00
|
|
|
else:
|
2024-01-11 16:45:23 +00:00
|
|
|
strategy.get
|
2024-01-08 22:52:46 +00:00
|
|
|
|
|
|
|
# all trees have to be padded to power of two
|
|
|
|
numBlockCells = manifest.blockSize.int div cellSize # number of cells per block
|
|
|
|
blockPadBytes = newSeq[byte](numBlockCells.nextPowerOfTwoPad * cellSize) # power of two padding for blocks
|
|
|
|
numSlotLeafs = (manifest.blocksCount div manifest.numSlots)
|
|
|
|
slotsPadLeafs = newSeqWith(numSlotLeafs.nextPowerOfTwoPad, Poseidon2Zero) # power of two padding for block roots
|
|
|
|
rootsPadLeafs = newSeqWith(manifest.numSlots.nextPowerOfTwoPad, Poseidon2Zero)
|
2024-01-11 16:45:23 +00:00
|
|
|
blockEmptyDigest = ? Poseidon2Tree.digest(DefaultEmptyBlock & blockPadBytes, CellSize)
|
2024-01-08 22:52:46 +00:00
|
|
|
|
2024-01-11 16:45:23 +00:00
|
|
|
var self = SlotsBuilder(
|
2024-01-08 22:52:46 +00:00
|
|
|
store: store,
|
|
|
|
manifest: manifest,
|
|
|
|
strategy: strategy,
|
|
|
|
cellSize: cellSize,
|
|
|
|
blockPadBytes: blockPadBytes,
|
|
|
|
slotsPadLeafs: slotsPadLeafs,
|
2024-01-11 16:45:23 +00:00
|
|
|
rootsPadLeafs: rootsPadLeafs,
|
|
|
|
blockEmptyDigest: blockEmptyDigest)
|
|
|
|
|
|
|
|
if manifest.verifiable:
|
|
|
|
if manifest.slotRoots.len == 0 or manifest.slotRoots.len != manifest.numSlots:
|
2024-01-15 16:45:04 +00:00
|
|
|
return failure "Manifest is verifiable but slot roots are missing or invalid."
|
2024-01-11 16:45:23 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
slotRoot = ? Poseidon2Hash.fromBytes(
|
|
|
|
( ? manifest.verifyRoot.mhash.mapFailure ).digestBytes.toArray32
|
|
|
|
).toFailure
|
|
|
|
|
|
|
|
slotRoots = manifest.slotRoots.mapIt(
|
|
|
|
? Poseidon2Hash.fromBytes(
|
|
|
|
( ? it.mhash.mapFailure ).digestBytes.toArray32
|
|
|
|
).toFailure
|
|
|
|
)
|
|
|
|
|
|
|
|
without root =? self.buildRootsTree(slotRoots).?root(), err:
|
|
|
|
error "Failed to build slot roots tree", err = err.msg
|
|
|
|
return failure(err)
|
|
|
|
|
|
|
|
if slotRoot != root:
|
|
|
|
return failure "Existing slots root doesn't match reconstructed root."
|
|
|
|
|
|
|
|
self.slotRoots = slotRoots
|
|
|
|
self.verifyRoot = some slotRoot
|
|
|
|
|
|
|
|
success self
|