Skip to content

Commit 2d30145

Browse files
committed
adding codex multicodecs and hashes
1 parent e25fb14 commit 2d30145

File tree

6 files changed

+59
-22
lines changed

6 files changed

+59
-22
lines changed

libp2p/cid.nim

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ const
6161
multiCodec("ed25519-pub"),
6262
multiCodec("codex-manifest"),
6363
multiCodec("codex-block"),
64-
multiCodec("codex-root")
64+
multiCodec("codex-slot-root"),
65+
multiCodec("codex-proving-root")
6566
]
6667

6768
proc initCidCodeTable(): Table[int, MultiCodec] {.compileTime.} =

libp2p/multibase.nim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
{.push raises: [].}
1717

1818
import tables
19+
import sequtils
1920
import stew/[base32, base58, base64, results]
2021

2122
type
@@ -338,6 +339,7 @@ proc initMultiBaseNameTable(): Table[string, MBCodec] {.compileTime.} =
338339
const
339340
CodeMultiBases = initMultiBaseCodeTable()
340341
NameMultiBases = initMultiBaseNameTable()
342+
MultibaseList* = MultiBaseCodecs.mapIt( it.name )
341343

342344
proc encodedLength*(mbtype: typedesc[MultiBase], encoding: string,
343345
length: int): int =

libp2p/multicodec.nim

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,9 @@ const MultiCodecList = [
177177
("skein1024-1008", 0xB3DE), ("skein1024-1016", 0xB3DF),
178178
("skein1024-1024", 0xB3E0),
179179
# poseidon2
180-
("poseidon2-alt_bn_128-sponge-r2", 0xCD10), # bn128 rate 2 sponge
181-
("poseidon2-alt_bn_128-mekle-2kb", 0xCD11), # bn128 2kb compress & merkleize
180+
("poseidon2-alt_bn_128-sponge-r2", 0xCD10), # bn128 rate 2 sponge
181+
("poseidon2-alt_bn_128-mekle-2kb", 0xCD11), # bn128 2kb compress & merkleize
182+
("poseidon2-alt_bn_128-keyed-compress", 0xCD12), # bn128 keyed compress
182183
# multiaddrs
183184
("ip4", 0x04),
184185
("ip6", 0x29),
@@ -240,6 +241,8 @@ const MultiCodecList = [
240241
("codex-manifest", 0xCD01),
241242
("codex-block", 0xCD02),
242243
("codex-root", 0xCD03),
244+
("codex-slot-root", 0xCD04),
245+
("codex-proving-root", 0xCD05),
243246
]
244247

245248
type

libp2p/multihash.nim

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
{.push raises: [].}
2525

2626
import tables
27+
import sequtils
2728
import nimcrypto/[sha, sha2, keccak, blake2, hash, utils]
2829
import poseidon2
2930
import varint, vbuffer, multicodec, multibase
@@ -180,9 +181,14 @@ proc shake_256hash(data: openArray[byte], output: var openArray[byte]) =
180181
discard sctx.output(addr output[0], uint(len(output)))
181182
sctx.clear()
182183

183-
proc poseidon2hash(data: openArray[byte], output: var openArray[byte]) =
184+
proc poseidon2_sponge_rate2(data: openArray[byte], output: var openArray[byte]) =
184185
if len(output) > 0:
185-
var digest = poseidon2.merkleRoot(data).toBytes()
186+
var digest = poseidon2.Sponge.digest(data).toBytes()
187+
copyMem(addr output[0], addr digest[0], uint(len(output)))
188+
189+
proc poseidon2_merkle_2kb_sponge(data: openArray[byte], output: var openArray[byte]) =
190+
if len(output) > 0:
191+
var digest = poseidon2.SpongeMerkle.digest(data, 2048).toBytes()
186192
copyMem(addr output[0], addr digest[0], uint(len(output)))
187193

188194
const
@@ -322,7 +328,8 @@ const
322328
MHash(mcodec: multiCodec("blake2s-240"), size: 30, coder: blake2Shash),
323329
MHash(mcodec: multiCodec("blake2s-248"), size: 31, coder: blake2Shash),
324330
MHash(mcodec: multiCodec("blake2s-256"), size: 32, coder: blake2Shash),
325-
MHash(mcodec: multiCodec("poseidon2-alt_bn_128-a2-cdx1"), size: 32, coder: poseidon2hash)
331+
MHash(mcodec: multiCodec("poseidon2-alt_bn_128-sponge-r2"), size: 32, coder: poseidon2_sponge_rate2),
332+
MHash(mcodec: multiCodec("poseidon2-alt_bn_128-mekle-2kb"), size: 32, coder: poseidon2_merkle_2kb_sponge)
326333
]
327334

328335
proc initMultiHashCodeTable(): Table[MultiCodec, MHash] {.compileTime.} =
@@ -331,6 +338,7 @@ proc initMultiHashCodeTable(): Table[MultiCodec, MHash] {.compileTime.} =
331338

332339
const
333340
CodeHashes = initMultiHashCodeTable()
341+
MultiHashCodecsList* = HashesList.mapIt( it.mcodec )
334342

335343
proc digestImplWithHash(hash: MHash, data: openArray[byte]): MultiHash =
336344
var buffer: array[MaxHashSize, byte]

tests/testcid.nim

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,29 @@ suite "Content identifier CID test suite":
6262
cid1 != cid5
6363
cid2 != cid4
6464
cid3 != cid6
65+
66+
test "Check all cids and hashes":
67+
var msg = cast[seq[byte]]("Hello World!")
68+
for cidCodec in ContentIdsList:
69+
for mhashCodec in MultiHashCodecsList:
70+
let
71+
cid = Cid.init(
72+
CidVersion.CIDv1,
73+
cidCodec,
74+
MultiHash.digest($mhashCodec, msg).get()).get()
75+
check:
76+
cid.mcodec == cidCodec
77+
cid.mhash().get().mcodec == mhashCodec
78+
79+
test "Check all cids and hashes base encode":
80+
var msg = cast[seq[byte]]("Hello World!")
81+
for cidCodec in ContentIdsList:
82+
for mhashCodec in MultiHashCodecsList:
83+
let
84+
cid = Cid.init(
85+
CidVersion.CIDv1,
86+
cidCodec,
87+
MultiHash.digest($mhashCodec, msg).get()).get()
88+
check:
89+
cid.mcodec == cidCodec
90+
cid.mhash().get().mcodec == mhashCodec

tests/testmultihash.nim

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import nimcrypto/utils
1414
import ../libp2p/multihash
1515

1616
const
17-
RustTestVectors = [
17+
TestVectors = [
1818
[
1919
"sha1",
2020
"beep boop",
@@ -82,18 +82,19 @@ const
8282
"""1D403EE2B40047B8060F68C67242175660F4174D0AF5C01D47168EC20ED619B0
8383
B7C42181F40AA1046F39E2EF9EFC6910782A998E0013D172458957957FAC9405
8484
B67D"""
85+
],
86+
[
87+
"poseidon2-alt_bn_128-sponge-r2",
88+
"hello world",
89+
"""909A0320823F7FB71C0998153E73AC734AE4870518F5FE324BD2484B68B565C288CF1E1E"""
90+
],
91+
[
92+
"poseidon2-alt_bn_128-mekle-2kb",
93+
"hello world",
94+
"""919A0320D9A6AE0CBF28C5E9CBE28D7231D3A4DEDF8B3826B0F8C3C002CA95C21253E614"""
8595
]
8696
]
8797

88-
const CodexTestVectors = [
89-
[
90-
"poseidon2-alt_bn_128-a2-cdx1",
91-
"hello world",
92-
# TODO: https://github.com/codex-storage/nim-poseidon2/issues/5#issuecomment-1787447258
93-
"8180B406 20 68656C6C6F20776F726C64000000000000000000000000000000000000000000"
94-
]
95-
]
96-
9798
suite "MultiHash test suite":
9899

99100
template checkTestVector(vector) =
@@ -106,10 +107,6 @@ suite "MultiHash test suite":
106107
hex(mh1) == hex(mh2)
107108
mh1 == mh2
108109

109-
test "rust-multihash test vectors":
110-
for item in RustTestVectors:
111-
checkTestVector(item)
112-
113-
test "codex test vectors":
114-
for item in CodexTestVectors:
110+
test "multihash test vectors":
111+
for item in TestVectors:
115112
checkTestVector(item)

0 commit comments

Comments
 (0)