2024-01-15 16:45:04 +00:00
|
|
|
## Nim-Codex
|
|
|
|
## Copyright (c) 2024 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/sequtils
|
|
|
|
|
|
|
|
import pkg/libp2p
|
2024-01-17 19:24:34 +00:00
|
|
|
import pkg/stew/arrayops
|
2024-01-15 16:45:04 +00:00
|
|
|
import pkg/questionable
|
|
|
|
import pkg/questionable/results
|
|
|
|
import pkg/poseidon2
|
|
|
|
import pkg/poseidon2/io
|
|
|
|
|
|
|
|
import ../codextypes
|
|
|
|
import ../merkletree
|
|
|
|
import ../errors
|
2024-01-17 19:24:34 +00:00
|
|
|
import ../utils/digest
|
2024-01-15 16:45:04 +00:00
|
|
|
|
2024-01-17 19:24:34 +00:00
|
|
|
func toCid(hash: Poseidon2Hash, mcodec: MultiCodec, cidCodec: MultiCodec): ?!Cid =
|
2024-01-15 16:45:04 +00:00
|
|
|
let
|
2024-01-17 19:24:34 +00:00
|
|
|
mhash = ? MultiHash.init(mcodec, hash.toBytes).mapFailure
|
|
|
|
treeCid = ? Cid.init(CIDv1, cidCodec, mhash).mapFailure
|
|
|
|
success treeCid
|
|
|
|
|
|
|
|
proc toPoseidon2Hash(cid: Cid, mcodec: MultiCodec, cidCodec: MultiCodec): ?!Poseidon2Hash =
|
|
|
|
if cid.cidver != CIDv1:
|
|
|
|
return failure("Unexpected CID version")
|
2024-01-15 16:45:04 +00:00
|
|
|
|
2024-01-17 19:24:34 +00:00
|
|
|
if cid.mcodec != cidCodec:
|
|
|
|
return failure("Cid is not of expected codec. Was: " & $cid.mcodec & " but expected: " & $cidCodec)
|
2024-01-15 16:45:04 +00:00
|
|
|
|
|
|
|
let
|
2024-01-17 19:24:34 +00:00
|
|
|
mhash = ? cid.mhash.mapFailure
|
|
|
|
bytes: array[32, byte] = array[32, byte].initCopyFrom(mhash.digestBytes())
|
|
|
|
hash = ? Poseidon2Hash.fromBytes(bytes).toFailure
|
2024-01-15 16:45:04 +00:00
|
|
|
|
2024-01-17 19:24:34 +00:00
|
|
|
success hash
|
|
|
|
|
|
|
|
func toCellCid*(hash: Poseidon2Hash): ?!Cid =
|
|
|
|
toCid(hash, Pos2Bn128MrklCodec, CodexSlotCellCodec)
|
|
|
|
|
|
|
|
func fromCellCid*(cid: Cid): ?!Poseidon2Hash =
|
|
|
|
toPoseidon2Hash(cid, Pos2Bn128MrklCodec, CodexSlotCellCodec)
|
|
|
|
|
|
|
|
func toSlotCid*(hash: Poseidon2Hash): ?!Cid =
|
|
|
|
toCid(hash, multiCodec("identity"), SlotRootCodec)
|
2024-01-15 16:45:04 +00:00
|
|
|
|
|
|
|
func toSlotCids*(slotRoots: openArray[Poseidon2Hash]): ?!seq[Cid] =
|
|
|
|
success slotRoots.mapIt( ? it.toSlotCid )
|
|
|
|
|
2024-01-17 19:24:34 +00:00
|
|
|
func fromSlotCid*(cid: Cid): ?!Poseidon2Hash =
|
|
|
|
toPoseidon2Hash(cid, multiCodec("identity"), SlotRootCodec)
|
2024-01-15 16:45:04 +00:00
|
|
|
|
2024-01-17 19:24:34 +00:00
|
|
|
func toVerifyCid*(hash: Poseidon2Hash): ?!Cid =
|
|
|
|
toCid(hash, multiCodec("identity"), SlotProvingRootCodec)
|
|
|
|
|
|
|
|
func fromVerifyCid*(cid: Cid): ?!Poseidon2Hash =
|
|
|
|
toPoseidon2Hash(cid, multiCodec("identity"), SlotProvingRootCodec)
|
2024-01-15 16:45:04 +00:00
|
|
|
|
|
|
|
func toEncodableProof*(
|
|
|
|
proof: Poseidon2Proof): ?!CodexProof =
|
|
|
|
|
|
|
|
let
|
|
|
|
encodableProof = CodexProof(
|
2024-01-17 19:24:34 +00:00
|
|
|
mcodec: multiCodec("identity"),
|
2024-01-15 16:45:04 +00:00
|
|
|
index: proof.index,
|
|
|
|
nleaves: proof.nleaves,
|
|
|
|
path: proof.path.mapIt( @( it.toBytes ) ))
|
|
|
|
|
|
|
|
success encodableProof
|
|
|
|
|
|
|
|
func toVerifiableProof*(
|
|
|
|
proof: CodexProof): ?!Poseidon2Proof =
|
|
|
|
|
|
|
|
let
|
2024-02-09 21:40:30 +00:00
|
|
|
nodes = proof.path.mapIt(
|
|
|
|
? Poseidon2Hash.fromBytes(it.toArray32).toFailure
|
|
|
|
)
|
|
|
|
|
|
|
|
Poseidon2Proof.init(
|
|
|
|
index = proof.index,
|
|
|
|
nleaves = proof.nleaves,
|
|
|
|
nodes = nodes)
|