Skip to content
Merged
Show file tree
Hide file tree
Changes from 74 commits
Commits
Show all changes
146 commits
Select commit Hold shift + click to select a range
9eb5f1c
Add memory utils
ernestognw Sep 4, 2024
2d397f4
Fix tests upgradeable
ernestognw Sep 4, 2024
2a0fb7e
Add docs
ernestognw Sep 5, 2024
a7e61c3
Make use of the library
ernestognw Sep 5, 2024
1aae8bb
Update docs/modules/ROOT/pages/utilities.adoc
ernestognw Oct 9, 2024
1b2679a
Merge branch 'master' into utils/memory
Amxx Mar 6, 2025
d514606
fix tests
Amxx Mar 6, 2025
14fa04e
Update contracts/utils/Memory.sol
ernestognw May 7, 2025
d0d55fc
Update contracts/utils/Memory.sol
arr00 May 7, 2025
7b3cb66
Add RLP library
ernestognw May 10, 2025
95149f8
Add TrieProof library
ernestognw May 11, 2025
ad5d4ac
up
ernestognw May 11, 2025
18540ef
Add docs
ernestognw May 11, 2025
163f27c
Workaround stack too deep
ernestognw May 24, 2025
c484289
Add Changesets
ernestognw May 29, 2025
e0d4790
Add more changesets
ernestognw Jun 7, 2025
a6f9053
Add FV and fuzz tests
ernestognw Jun 7, 2025
e2b5e4c
Merge branch 'master' into feature/rlp
ernestognw Jun 7, 2025
203d1a2
up
ernestognw Jun 7, 2025
48eabc1
docs
ernestognw Jun 7, 2025
63ced95
up pragma
ernestognw Jun 7, 2025
f342756
Add missing Bytes test
ernestognw Jun 7, 2025
23dba37
Add unit tests
ernestognw Jun 7, 2025
0cacca2
up pragma
ernestognw Jun 7, 2025
831e8ab
Move TrieProof
ernestognw Jun 7, 2025
5da111f
Fix countLeadingZeroes
ernestognw Jun 8, 2025
ba2293e
nits
ernestognw Jun 8, 2025
9409bc6
Improve
ernestognw Jun 8, 2025
e740dac
Fix
ernestognw Jun 8, 2025
0332ffe
Add Memory.sol library
ernestognw Jun 8, 2025
608e3cd
Merge branch 'master' into utils/memory
ernestognw Jun 8, 2025
ac92bb4
up
ernestognw Jun 8, 2025
6094bb7
Merge branch 'master' into utils/memory
ernestognw Jun 8, 2025
6bb96d5
WIP: Add more Memory functions
ernestognw Jun 8, 2025
860e5a8
up
ernestognw Jun 8, 2025
ecdb768
revert
ernestognw Jun 8, 2025
95907aa
Update docs
ernestognw Jun 8, 2025
124ccee
Nit
ernestognw Jun 8, 2025
c3237df
Finish fuzz tests and FV
ernestognw Jun 9, 2025
27f0a9b
up
ernestognw Jun 9, 2025
282ce39
up
ernestognw Jun 9, 2025
bdd2cf1
Add operations to Math.sol
ernestognw Jun 9, 2025
42c79f1
Add new equal, nibbles and countLeadingZeroes functions
ernestognw Jun 9, 2025
5754ab8
Rename countLeadingZeroes to clz
ernestognw Jun 9, 2025
44f0e14
up
ernestognw Jun 9, 2025
05c73bd
Pragma changes
ernestognw Jun 9, 2025
3a6fbf6
up
ernestognw Jun 9, 2025
e67e8b4
up
ernestognw Jul 4, 2025
3385718
Rename to in Math library and update corresponding tests for consis…
ernestognw Jul 9, 2025
40d7922
Update return types of reverseBits functions to match their respectiv…
ernestognw Jul 9, 2025
89860bc
Refactor reverseBits functions in to use fixed-size byte types
ernestognw Jul 9, 2025
9b58730
Test nits
ernestognw Jul 9, 2025
77ffa8c
Simplify
ernestognw Jul 9, 2025
ce91c80
up
ernestognw Jul 9, 2025
b3e3add
Move reverse functions to Bytes.sol
ernestognw Jul 9, 2025
2f3107c
Move Bytes.t.sol
ernestognw Jul 9, 2025
4383e01
Merge branch 'master' into feat/bytes-rlp
ernestognw Jul 9, 2025
5a44b11
up
ernestognw Jul 9, 2025
d6db2d7
Document
ernestognw Jul 9, 2025
3847050
Remove extra functions
ernestognw Jul 9, 2025
4fd1947
Update docs
ernestognw Jul 9, 2025
c4e0375
up
ernestognw Jul 9, 2025
acb14cb
Merge branch 'utils/memory' into feature/rlp
ernestognw Jul 9, 2025
2208006
Merge branch 'feat/math-reverse-bits' into feature/rlp
ernestognw Jul 9, 2025
13f4d8f
Merge branch 'feat/bytes-rlp' into feature/rlp
ernestognw Jul 9, 2025
502a520
Merge branch 'master' into feature/rlp
ernestognw Jul 12, 2025
aab9274
Merge branch 'master' into feature/rlp
Amxx Jul 15, 2025
aa26e48
up
Amxx Jul 15, 2025
948f0a1
Merge branch 'master' into feature/rlp
ernestognw Jul 31, 2025
d4bfb8b
Fix compilation
ernestognw Jul 31, 2025
138de7f
Remove dangling clz
ernestognw Jul 31, 2025
5efeb37
Make nibbles function private
ernestognw Jul 31, 2025
00ff228
Remove nibbles test
ernestognw Jul 31, 2025
fd7d2b5
up
ernestognw Jul 31, 2025
940ede8
Merge remote-tracking branch 'upstream/master' into feat/trie-proof
james-toussaint Nov 14, 2025
5b93acb
Verify proof
james-toussaint Nov 18, 2025
48c7dcb
Mirror storage on both networks
james-toussaint Nov 19, 2025
eaf3527
Gracefully kill anvil
james-toussaint Nov 20, 2025
f587b88
Verify proof with hashed key
james-toussaint Nov 20, 2025
03f42b9
Traverse after extension node
james-toussaint Nov 21, 2025
67a74b0
Delete .changeset/lovely-cooks-add.md
Amxx Nov 27, 2025
85d638e
tests update
Amxx Nov 27, 2025
62f255d
remove unecessary string casting
Amxx Nov 27, 2025
4d4edb8
refactor TrieProof library
Amxx Nov 27, 2025
1286c96
add to stateless
Amxx Nov 27, 2025
9853d3d
fuzz tests for Memory.getHash and RLP.readBytesHash
Amxx Nov 27, 2025
2ef93d2
remove radix reference
Amxx Nov 27, 2025
6211aed
update
Amxx Nov 27, 2025
466ecdf
Return error if leaf value is too large
james-toussaint Nov 28, 2025
0b6d207
fix issue identified though optimism-bedrock unit tests
Amxx Nov 28, 2025
d47d52e
update
Amxx Nov 28, 2025
fc69547
Test invalid internal short node
james-toussaint Nov 28, 2025
702bf9c
update
Amxx Nov 28, 2025
7020ab4
Merge branch 'feat/trie-proof' of https://github.com/ernestognw/openz…
Amxx Nov 28, 2025
a37e305
fix issue with small nodes and add unit test from optimism
Amxx Nov 28, 2025
4366235
more unit tests
Amxx Nov 28, 2025
2a9a3ec
Make unit tests validProof 8, 9 and 10 pass
Amxx Nov 28, 2025
60f5d7d
more optimism unit testing
Amxx Nov 28, 2025
c3cacbd
testing
Amxx Nov 28, 2025
1ed7f87
typo
Amxx Nov 28, 2025
b3c7f16
return bytes memory
Amxx Nov 29, 2025
658c9ca
check node length
Amxx Nov 29, 2025
8f60bec
refactor tests
Amxx Nov 29, 2025
278ce24
expand testing to proving accounts against the block state root
Amxx Dec 1, 2025
ea53d08
typo
Amxx Dec 1, 2025
8fbcb34
Apply suggestions from code review
Amxx Dec 1, 2025
ff31d24
revert unused new features in RLP
Amxx Dec 1, 2025
0706595
Merge branch 'feat/trie-proof' of https://github.com/ernestognw/openz…
Amxx Dec 1, 2025
0a3e229
Merge branch 'master' into feat/trie-proof
Amxx Dec 1, 2025
6e38f3e
Remove _commonPrefixLength function
Amxx Dec 1, 2025
b5bc785
Update test/utils/cryptography/TrieProof.test.js
Amxx Dec 1, 2025
1725d92
use some assembly to optimize memory operations
Amxx Dec 1, 2025
05d05c1
fix test flackiness
Amxx Dec 2, 2025
926d704
move toNibbles to Bytes.sol and update doc
Amxx Dec 2, 2025
47a7d2f
cache pathRemainder.length()
Amxx Dec 2, 2025
3e6b05f
Add toNibbles tests
james-toussaint Dec 2, 2025
77192fe
Update Bytes.t.sol
Amxx Dec 2, 2025
18e1c8e
reorder arguments
Amxx Dec 5, 2025
70d76d9
update
Amxx Dec 5, 2025
13619d5
Update contracts/utils/cryptography/TrieProof.sol
Amxx Dec 5, 2025
8d80329
Handle empty leaf/ext path and empty ext path remainder
james-toussaint Dec 9, 2025
66ddaa1
Rename and reorder errors
james-toussaint Dec 9, 2025
217fdf1
Return extra proof error without reading value
james-toussaint Dec 9, 2025
7a7bb97
Merge branch 'master' into feat/trie-proof
james-toussaint Dec 9, 2025
536dc09
Update TrieProof.sol
Amxx Dec 9, 2025
9603aee
lint
Amxx Dec 9, 2025
39e56bb
add travese / tryTraverse options
Amxx Dec 9, 2025
8f7ed6a
remove superflous check that can be proven as a loop invariant
Amxx Dec 9, 2025
4004388
remove the Node structure
Amxx Dec 9, 2025
ad5476b
Update contracts/utils/cryptography/TrieProof.sol
Amxx Dec 9, 2025
19336bf
Remove skipped tests related to removed scenario
james-toussaint Dec 9, 2025
14fe8e4
Update doc with traverse naming
james-toussaint Dec 9, 2025
8707438
fix test flakyness
Amxx Dec 10, 2025
486a562
sanity check the proof value and key
Amxx Dec 10, 2025
bdff2ad
spelling
Amxx Dec 10, 2025
80149e4
Merge branch 'master' into feat/trie-proof
ernestognw Dec 10, 2025
0600dd4
Update contracts/utils/cryptography/TrieProof.sol
Amxx Dec 11, 2025
8a04d05
Add comment on path expansion
ernestognw Dec 12, 2025
b683357
Add note about 32 length in _getNodeId
ernestognw Dec 12, 2025
86ed474
Remove `Memory.getHash` in favor of `Memory.equal`
ernestognw Dec 12, 2025
6a6a4b2
Test verification of transaction inclusion
Amxx Dec 12, 2025
30cefc6
up
Amxx Dec 12, 2025
784ff8a
Update contracts/utils/cryptography/TrieProof.sol
Amxx Dec 12, 2025
d19f5f9
Update contracts/utils/cryptography/TrieProof.sol
ernestognw Dec 12, 2025
2ca2a3d
Lint
ernestognw Dec 12, 2025
bb02a8e
verify event inclusion
Amxx Dec 12, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lovely-cooks-add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'openzeppelin-solidity': minor
---

`RLP`: Add library for Ethereum's Recursive Length Prefix encoding/decoding.
5 changes: 5 additions & 0 deletions .changeset/shaky-phones-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'openzeppelin-solidity': minor
---

`TrieProof`: Add library for verifying Ethereum Merkle-Patricia trie inclusion proofs.
3 changes: 3 additions & 0 deletions contracts/utils/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Miscellaneous contracts and libraries containing utility functions you can use t
* {InteroperableAddress}: Library for formatting and parsing ERC-7930 interoperable addresses.
* {Blockhash}: A library for accessing historical block hashes beyond the standard 256 block limit utilizing EIP-2935's historical blockhash functionality.
* {Time}: A library that provides helpers for manipulating time-related objects, including a `Delay` type.
* {RLP}: Library for encoding and decoding data in Ethereum's Recursive Length Prefix format.

[NOTE]
====
Expand Down Expand Up @@ -143,3 +144,5 @@ Ethereum contracts have no native concept of an interface, so applications must
{{Blockhash}}

{{Time}}

{{RLP}}
327 changes: 327 additions & 0 deletions contracts/utils/RLP.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,327 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.27;

import {Math} from "./math/Math.sol";
import {Bytes} from "./Bytes.sol";
import {Memory} from "./Memory.sol";

/**
* @dev Library for encoding and decoding data in RLP format.
* Recursive Length Prefix (RLP) is the main encoding method used to serialize objects in Ethereum.
* It's used for encoding everything from transactions to blocks to Patricia-Merkle tries.
*/
library RLP {
using Math for uint256;
using Bytes for *;
using Memory for *;

/// @dev Items with length 0 are not RLP items.
error RLPEmptyItem();

/// @dev The `item` is not of the `expected` type.
error RLPUnexpectedType(ItemType expected, ItemType actual);

/// @dev The item is not long enough to contain the data.
error RLPInvalidDataRemainder(uint256 minLength, uint256 actualLength);

/// @dev The content length does not match the expected length.
error RLPContentLengthMismatch(uint256 expectedLength, uint256 actualLength);

struct Item {
uint256 length; // Total length of the item in bytes
Memory.Pointer ptr; // Memory pointer to the start of the item
}

enum ItemType {
DATA_ITEM, // Single data value
LIST_ITEM // List of RLP encoded items
}

/**
* @dev Maximum length for data that will be encoded using the short format.
* If `data.length <= 55 bytes`, it will be encoded as: `[0x80 + length]` + data.
*/
uint8 internal constant SHORT_THRESHOLD = 55;

/// @dev Single byte prefix for short strings (0-55 bytes)
uint8 internal constant SHORT_OFFSET = 128;
/// @dev Prefix for long string length (0xB8)
uint8 internal constant LONG_LENGTH_OFFSET = SHORT_OFFSET + SHORT_THRESHOLD + 1; // 184
/// @dev Prefix for list items (0xC0)
uint8 internal constant LONG_OFFSET = LONG_LENGTH_OFFSET + 8; // 192
/// @dev Prefix for long list length (0xF8)
uint8 internal constant SHORT_LIST_OFFSET = LONG_OFFSET + SHORT_THRESHOLD + 1; // 248

/**
* @dev Encodes a bytes array using RLP rules.
* Single bytes below 128 are encoded as themselves, otherwise as length prefix + data.
*/
function encode(bytes memory buffer) internal pure returns (bytes memory) {
return _isSingleByte(buffer) ? buffer : bytes.concat(_encodeLength(buffer.length, SHORT_OFFSET), buffer);
}

/**
* @dev Encodes an array of bytes using RLP (as a list).
* First it {_flatten}s the list of byte arrays, then encodes it with the list prefix.
*/
function encode(bytes[] memory list) internal pure returns (bytes memory) {
bytes memory flattened = _flatten(list);
return bytes.concat(_encodeLength(flattened.length, LONG_OFFSET), flattened);
}

/// @dev Convenience method to encode a string as RLP.
function encode(string memory str) internal pure returns (bytes memory) {
return encode(bytes(str));
}

/// @dev Convenience method to encode an address as RLP bytes (i.e. encoded as packed 20 bytes).
function encode(address addr) internal pure returns (bytes memory) {
return encode(abi.encodePacked(addr));
}

/// @dev Convenience method to encode a uint256 as RLP. See {_binaryBuffer}.
function encode(uint256 value) internal pure returns (bytes memory) {
return encode(_binaryBuffer(value));
}

/// @dev Same as {encode-uint256-}, but for bytes32.
function encode(bytes32 value) internal pure returns (bytes memory) {
return encode(uint256(value));
}

/**
* @dev Convenience method to encode a boolean as RLP.
*
* Boolean `true` is encoded as 0x01, `false` as 0x80 (equivalent to encoding integers 1 and 0).
* This follows the de facto ecosystem standard where booleans are treated as 0/1 integers.
*
* NOTE: Both this and {encodeStrict} produce identical encoded bytes at the output level.
* Use this for ecosystem compatibility; use {encodeStrict} for strict RLP spec compliance.
*/
function encode(bool value) internal pure returns (bytes memory) {
return encode(value ? uint256(1) : uint256(0));
}

/**
* @dev Strict RLP encoding of a boolean following literal spec interpretation.
* Boolean `true` is encoded as 0x01, `false` as empty bytes (0x80).
*
* NOTE: This is the strict RLP spec interpretation where false represents "empty".
* Use this for strict RLP spec compliance; use {encode} for ecosystem compatibility.
*/
function encodeStrict(bool value) internal pure returns (bytes memory) {
return value ? abi.encodePacked(bytes1(0x01)) : encode(new bytes(0));
}

/// @dev Creates an RLP Item from a bytes array.
function toItem(bytes memory value) internal pure returns (Item memory) {
require(value.length != 0, RLPEmptyItem()); // Empty arrays are not RLP items.
return Item(value.length, _addOffset(_asPointer(value), 32));
}

/// @dev Decodes an RLP encoded list into an array of RLP Items. See {_decodeLength}
function readList(Item memory item) internal pure returns (Item[] memory) {
(uint256 listOffset, uint256 listLength, ItemType itemType) = _decodeLength(item);
require(itemType == ItemType.LIST_ITEM, RLPUnexpectedType(ItemType.LIST_ITEM, itemType));
uint256 expectedLength = listOffset + listLength;
require(expectedLength == item.length, RLPContentLengthMismatch(expectedLength, item.length));
Item[] memory items = new Item[](32);

uint256 itemCount;

for (uint256 currentOffset = listOffset; currentOffset < item.length; ++itemCount) {
(uint256 itemOffset, uint256 itemLength, ) = _decodeLength(
Item(item.length - currentOffset, _addOffset(item.ptr, currentOffset))
);
items[itemCount] = Item(itemLength + itemOffset, _addOffset(item.ptr, currentOffset));
currentOffset += itemOffset + itemLength;
}

// Decrease the array size to match the actual item count.
assembly ("memory-safe") {
mstore(items, itemCount)
}
return items;
}

/// @dev Same as {readList} but for `bytes`. See {toItem}.
function readList(bytes memory value) internal pure returns (Item[] memory) {
return readList(toItem(value));
}

/// @dev Decodes an RLP encoded item.
function readBytes(Item memory item) internal pure returns (bytes memory) {
(uint256 itemOffset, uint256 itemLength, ItemType itemType) = _decodeLength(item);
require(itemType == ItemType.DATA_ITEM, RLPUnexpectedType(ItemType.DATA_ITEM, itemType));
uint256 expectedLength = itemOffset + itemLength;
require(expectedLength == item.length, RLPContentLengthMismatch(expectedLength, item.length));

bytes memory result = new bytes(itemLength);
_copy(_addOffset(_asPointer(result), 32), _addOffset(item.ptr, itemOffset), itemLength);

return result;
}

/// @dev Same as {readBytes} but for `bytes`. See {toItem}.
function readBytes(bytes memory item) internal pure returns (bytes memory) {
return readBytes(toItem(item));
}

/// @dev Reads the raw bytes of an RLP item without decoding the content. Includes prefix bytes.
function readRawBytes(Item memory item) internal pure returns (bytes memory) {
uint256 itemLength = item.length;
bytes memory result = new bytes(itemLength);
_copy(_addOffset(_asPointer(result), 32), item.ptr, itemLength);

return result;
}

/// @dev Checks if a buffer is a single byte below 128 (0x80). Encoded as-is in RLP.
function _isSingleByte(bytes memory buffer) private pure returns (bool) {
return buffer.length == 1 && uint8(buffer[0]) < SHORT_OFFSET;
}

/**
* @dev Encodes a length with appropriate RLP prefix.
*
* Uses short encoding for lengths <= 55 bytes (i.e. `abi.encodePacked(bytes1(uint8(length) + uint8(offset)))`).
* Uses long encoding for lengths > 55 bytes See {_encodeLongLength}.
*/
function _encodeLength(uint256 length, uint256 offset) private pure returns (bytes memory) {
return
length <= SHORT_THRESHOLD
? abi.encodePacked(bytes1(uint8(length) + uint8(offset)))
: _encodeLongLength(length, offset);
}

/**
* @dev Encodes a long length value (>55 bytes) with a length-of-length prefix.
* Format: [prefix + length of the length] + [length in big-endian]
*/
function _encodeLongLength(uint256 length, uint256 offset) private pure returns (bytes memory) {
uint256 bytesLength = length.log256() + 1; // Result is floored
return
abi.encodePacked(
bytes1(uint8(bytesLength) + uint8(offset) + SHORT_THRESHOLD),
bytes32(length).reverseBytes32() // to big-endian
);
}

/// @dev Converts a uint256 to minimal binary representation, removing leading zeros.
function _binaryBuffer(uint256 value) private pure returns (bytes memory) {
return abi.encodePacked(value).slice(value.clz());
}

/// @dev Concatenates all byte arrays in the `list` sequentially. Returns a flattened buffer.
function _flatten(bytes[] memory list) private pure returns (bytes memory) {
// TODO: Move to Arrays.sol
bytes memory flattened = new bytes(_totalLength(list));
Memory.Pointer dataPtr = _addOffset(_asPointer(flattened), 32);
for (uint256 i = 0; i < list.length; i++) {
bytes memory item = list[i];
uint256 length = item.length;
_copy(dataPtr, _asPointer(item), length);
dataPtr = _addOffset(dataPtr, length);
}
return flattened;
}

/// @dev Sums up the length of each array in the list.
function _totalLength(bytes[] memory list) private pure returns (uint256) {
// TODO: Move to Arrays.sol
uint256 totalLength;
for (uint256 i = 0; i < list.length; i++) {
totalLength += list[i].length;
}
return totalLength;
}

/**
* @dev Decodes an RLP `item`'s `length and type from its prefix.
* Returns the offset, length, and type of the RLP item based on the encoding rules.
*/
function _decodeLength(Item memory item) private pure returns (uint256 offset, uint256 length, ItemType) {
require(item.length != 0, RLPEmptyItem());
uint256 prefix = uint8(_loadByte(item.ptr, 0));

// Single byte below 128
if (prefix < SHORT_OFFSET) return (0, 1, ItemType.DATA_ITEM);

// Short string (0-55 bytes)
if (prefix < LONG_LENGTH_OFFSET) return _decodeShortString(prefix - SHORT_OFFSET, item);

// Long string (>55 bytes)
if (prefix < LONG_OFFSET) {
(offset, length) = _decodeLong(prefix - LONG_LENGTH_OFFSET, item);
return (offset, length, ItemType.DATA_ITEM);
}

// Short list
if (prefix < SHORT_LIST_OFFSET) return _decodeShortList(prefix - LONG_OFFSET, item);

// Long list
(offset, length) = _decodeLong(prefix - SHORT_LIST_OFFSET, item);
return (offset, length, ItemType.LIST_ITEM);
}

/// @dev Decodes a short string (0-55 bytes). The first byte contains the length, and the rest is the payload.
function _decodeShortString(
uint256 strLength,
Item memory item
) private pure returns (uint256 offset, uint256 length, ItemType) {
require(item.length > strLength, RLPInvalidDataRemainder(strLength, item.length));
require(strLength != 1 || _loadByte(_addOffset(item.ptr, 1), 0) >= bytes1(SHORT_OFFSET));
return (1, strLength, ItemType.DATA_ITEM);
}

/// @dev Decodes a short list (0-55 bytes). The first byte contains the length of the entire list.
function _decodeShortList(
uint256 listLength,
Item memory item
) private pure returns (uint256 offset, uint256 length, ItemType) {
require(item.length > listLength, RLPInvalidDataRemainder(listLength, item.length));
return (1, listLength, ItemType.LIST_ITEM);
}

/// @dev Decodes a long string or list (>55 bytes). The first byte indicates the length of the length, followed by the length itself.
function _decodeLong(uint256 lengthLength, Item memory item) private pure returns (uint256 offset, uint256 length) {
lengthLength += 1; // 1 byte for the length itself
require(item.length > lengthLength, RLPInvalidDataRemainder(lengthLength, item.length));
require(_loadByte(item.ptr, 0) != 0x00);

// Extract the length value from the next bytes
uint256 len = uint256(_load(_addOffset(item.ptr, 1)) >> (256 - 8 * lengthLength));
require(len > SHORT_THRESHOLD, RLPInvalidDataRemainder(SHORT_THRESHOLD, len));
uint256 expectedLength = lengthLength + len;
require(item.length <= expectedLength, RLPContentLengthMismatch(expectedLength, item.length));
return (lengthLength + 1, len);
}

function _addOffset(Memory.Pointer ptr, uint256 offset) private pure returns (Memory.Pointer) {
return bytes32(uint256(ptr.asBytes32()) + offset).asPointer();
}

function _copy(Memory.Pointer destPtr, Memory.Pointer srcPtr, uint256 length) private pure {
assembly ("memory-safe") {
mcopy(destPtr, srcPtr, length)
}
}

function _loadByte(Memory.Pointer ptr, uint256 offset) private pure returns (bytes1 v) {
assembly ("memory-safe") {
v := byte(offset, mload(ptr))
}
}

function _load(Memory.Pointer ptr) private pure returns (bytes32 v) {
assembly ("memory-safe") {
v := mload(ptr)
}
}

function _asPointer(bytes memory value) private pure returns (Memory.Pointer ptr) {
assembly ("memory-safe") {
ptr := value
}
}
}
3 changes: 3 additions & 0 deletions contracts/utils/cryptography/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ A collection of contracts and libraries that implement various signature validat
* {SignatureChecker}: A library helper to support regular ECDSA from EOAs as well as ERC-1271 signatures for smart contracts.
* {Hashes}: Commonly used hash functions.
* {MerkleProof}: Functions for verifying https://en.wikipedia.org/wiki/Merkle_tree[Merkle Tree] proofs.
* {TrieProof}: Library for verifying Ethereum Merkle-Patricia trie inclusion proofs.
* {EIP712}: Contract with functions to allow processing signed typed structure data according to https://eips.ethereum.org/EIPS/eip-712[EIP-712].
* {ERC7739Utils}: Utilities library that implements a defensive rehashing mechanism to prevent replayability of smart contract signatures based on ERC-7739.
* {WebAuthn}: Library for verifying WebAuthn Authentication Assertions.
Expand Down Expand Up @@ -38,6 +39,8 @@ A collection of contracts and libraries that implement various signature validat

{{MerkleProof}}

{{TrieProof}}

{{EIP712}}

{{ERC7739Utils}}
Expand Down
Loading