Skip to content

Commit 50fdfaf

Browse files
committed
optimize cid header inspection
1 parent c240214 commit 50fdfaf

File tree

3 files changed

+37
-35
lines changed

3 files changed

+37
-35
lines changed

.gas-snapshot

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ ReadBignum_Test:test_UInt256_middle() (gas: 5686)
8181
ReadBignum_Test:test_UInt256_multi() (gas: 4074)
8282
ReadBignum_Test:test_UInt256_random(uint256) (runs: 256, μ: 4567, ~: 4567)
8383
ReadBignum_Test:test_UInt256_single() (gas: 4367)
84-
ReadCidSha256_Test:test_Cid() (gas: 853)
85-
ReadCidSha256_Test:test_Cid_multicodec_raw() (gas: 799)
86-
ReadCidSha256_Test:test_Cid_random(uint256) (runs: 256, μ: 4260, ~: 4260)
84+
ReadCidSha256_Test:test_Cid() (gas: 590)
85+
ReadCidSha256_Test:test_Cid_multicodec_raw() (gas: 518)
86+
ReadCidSha256_Test:test_Cid_random(uint256) (runs: 256, μ: 4003, ~: 4003)
8787
ReadCidSha256_Test:test_NullableCid_nullCbor() (gas: 3440)
88-
ReadCidSha256_Test:test_NullableCid_random(uint256) (runs: 256, μ: 4121, ~: 4129)
88+
ReadCidSha256_Test:test_NullableCid_random(uint256) (runs: 256, μ: 3868, ~: 3872)
8989
SimpleTest:test_decodeFalse() (gas: 3305)
9090
SimpleTest:test_decodeTrue() (gas: 3311)
9191
SimpleTest:test_skipNull() (gas: 427)

lcov.info

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -472,30 +472,30 @@ BRH:12
472472
end_of_record
473473
TN:
474474
SF:src/tags/ReadCidSha256.sol
475-
DA:67,512
476-
FN:67,ReadCidSha256.Cid
475+
DA:66,512
476+
FN:66,ReadCidSha256.Cid
477477
FNDA:512,ReadCidSha256.Cid
478-
DA:68,512
479-
DA:71,513
480-
FN:71,ReadCidSha256.Cid
478+
DA:67,512
479+
DA:70,513
480+
FN:70,ReadCidSha256.Cid
481481
FNDA:513,ReadCidSha256.Cid
482+
DA:75,513
482483
DA:76,513
483-
DA:77,513
484-
DA:78,513
485-
DA:83,513
486-
DA:84,513
484+
DA:79,513
487485
DA:85,513
488-
DA:88,513
489-
BRDA:88,0,0,1
490-
BRDA:88,0,1,512
491-
DA:89,512
492-
BRDA:89,1,0,2
493-
BRDA:89,1,1,510
494-
DA:91,510
495-
DA:101,258
496-
FN:101,ReadCidSha256.NullableCid
486+
DA:86,513
487+
DA:87,513
488+
DA:90,513
489+
BRDA:90,0,0,1
490+
BRDA:90,0,1,512
491+
DA:91,512
492+
BRDA:91,1,0,2
493+
BRDA:91,1,1,510
494+
DA:93,510
495+
DA:103,258
496+
FN:103,ReadCidSha256.NullableCid
497497
FNDA:258,ReadCidSha256.NullableCid
498-
DA:102,258
498+
DA:104,258
499499
FNF:3
500500
FNH:3
501501
LF:14

src/tags/ReadCidSha256.sol

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: Apache-2.0
22
pragma solidity ^0.8.28;
33

4+
import "forge-std/console.sol";
45
import "../ReadCbor.sol";
56

67
// we will only encounter CID v1 dag-cbor sha256, which perfectly fits uint256.
@@ -45,10 +46,8 @@ library ReadCidSha256 {
4546
// 12 │ multihash type sha-256
4647
// 20 │ multihash size 32 bytes
4748
// ─────────┴─────────
48-
bytes6 private constant cborTag42_cborBytes37_multibaseCidV1 = hex"D82A58250001";
49-
bytes1 private constant multicodecDagCbor = hex"71";
50-
bytes1 private constant multicodecRaw = hex"55";
51-
bytes2 private constant multihashSha256_multihashBytes32 = hex"1220";
49+
bytes9 private constant cborTag42_cborBytes37_multibaseCidV1_multicodecZERO_multihashSha256_multihashBytes32 =
50+
hex"D82A58250001001220";
5251

5352
/**
5453
* @notice Reads a CID from CBOR encoded data at the specified byte index
@@ -65,27 +64,30 @@ library ReadCidSha256 {
6564
* @return CidSha256 The representative hash
6665
*/
6766
function Cid(bytes memory cbor, uint32 i) internal pure returns (uint32, CidSha256) {
68-
return Cid(cbor, i, multicodecDagCbor);
67+
return Cid(cbor, i, 0x71);
6968
}
7069

7170
function Cid(bytes memory cbor, uint32 i, bytes1 multicodec)
7271
internal
7372
pure
7473
returns (uint32 n, CidSha256 cidSha256)
7574
{
76-
assert(multicodec == multicodecDagCbor || multicodec == multicodecRaw);
77-
bytes9 cborMultibase;
78-
bytes9 expect =
79-
bytes9(bytes.concat(cborTag42_cborBytes37_multibaseCidV1, multicodec, multihashSha256_multihashBytes32));
80-
75+
bytes9 expect;
76+
bytes9 cborHeader;
8177
assembly ("memory-safe") {
78+
// expected head
79+
expect :=
80+
or(
81+
cborTag42_cborBytes37_multibaseCidV1_multicodecZERO_multihashSha256_multihashBytes32,
82+
shr(0x30, multicodec)
83+
)
8284
// cbor header at index
83-
cborMultibase := mload(add(cbor, add(0x20, i)))
85+
cborHeader := mload(add(cbor, add(0x20, i)))
8486
cidSha256 := mload(add(cbor, add(0x29, i)))
8587
n := add(i, 41) // 4 bytes cbor header + 5 bytes multibase header + 32 bytes hash
8688
}
8789

88-
require(cborMultibase == expect, "Expected CBOR tag 42 and 37-byte CIDv1");
90+
require(expect == cborHeader, "Expected CBOR tag 42 and 37-byte CIDv1");
8991
require(!cidSha256.isNull(), "Expected non-zero CID value");
9092

9193
cbor.requireRange(n);

0 commit comments

Comments
 (0)