Skip to content
Open
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
5a400eb
Add Bytes.splice, an inplace variant of Buffer.slice
Amxx Jun 10, 2025
65292d5
Add Base58 library
Amxx Jun 20, 2025
99a1835
docs
Amxx Jun 20, 2025
bddf4f6
Merge branch 'feature/Bytes-splice' into feature/base58
Amxx Jun 21, 2025
88c03e7
Add Bytes.countConsecutive and Bytes.countLeading
Amxx Jun 21, 2025
a3c4667
fix
Amxx Jun 21, 2025
41b586b
efficient decoding
Amxx Jun 21, 2025
c6d6bdd
coverage
Amxx Jun 21, 2025
48bf13b
Update thirty-pugs-pick.md
Amxx Jun 21, 2025
eebd51e
docs
Amxx Jun 21, 2025
296a87e
pragma
Amxx Jun 21, 2025
8c94acc
pragma
Amxx Jun 21, 2025
d09ebfa
coverage
Amxx Jun 21, 2025
a25bd11
rewrite _encode in assembly
Amxx Jun 22, 2025
a4ce8c8
more inline documentation
Amxx Jun 22, 2025
7474f2a
test vectors
Amxx Jun 22, 2025
bef2e4f
document
Amxx Jun 22, 2025
ce1c5ad
remove auxiliary utils
Amxx Jun 22, 2025
c33e933
mload is actually cheaper than jump
Amxx Jun 23, 2025
855a1c6
up
Amxx Jun 23, 2025
ec641c7
Update contracts/utils/Base58.sol
Amxx Jun 25, 2025
7429bcc
up
Amxx Jun 26, 2025
45edb76
do base58 arithmetics in chunks of 248 bits
Amxx Jun 26, 2025
20f3611
update
Amxx Jun 26, 2025
8e60a99
codespell
Amxx Jun 26, 2025
dd8e895
decode assembly
Amxx Jun 26, 2025
45f04b4
char valdity filter
Amxx Jun 26, 2025
da84743
slither
Amxx Jun 26, 2025
c80f693
slither
Amxx Jun 27, 2025
f7ac27d
fix custom error name + testing
Amxx Aug 22, 2025
2696cd8
Apply suggestions from code review
Amxx Aug 27, 2025
1736f38
optimize zero limbs accounting
Amxx Aug 27, 2025
8652d20
Update contracts/utils/Base58.sol
Amxx Aug 27, 2025
8098fb2
Update test/utils/Base58.t.sol
Amxx Aug 27, 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/afraid-chicken-attack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'openzeppelin-solidity': minor
---

`Bytes`: Add `splice(bytes,uint256)` and `splice(bytes,uint256,uint256)`, two "in place" variants of the existing slice functions
5 changes: 5 additions & 0 deletions .changeset/loose-lamps-bake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'openzeppelin-solidity': minor
---

`Base58`: Add a library for encoding and decoding bytes buffers into base58 strings.
5 changes: 5 additions & 0 deletions .changeset/thirty-pugs-pick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'openzeppelin-solidity': minor
---

`Bytes`: Add `countLeading` and `countConsecutive`
1 change: 1 addition & 0 deletions contracts/mocks/Stateless.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pragma solidity ^0.8.26;
import {Address} from "../utils/Address.sol";
import {Arrays} from "../utils/Arrays.sol";
import {AuthorityUtils} from "../access/manager/AuthorityUtils.sol";
import {Base58} from "../utils/Base58.sol";
import {Base64} from "../utils/Base64.sol";
import {BitMaps} from "../utils/structs/BitMaps.sol";
import {Blockhash} from "../utils/Blockhash.sol";
Expand Down
184 changes: 184 additions & 0 deletions contracts/utils/Base58.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.26;

import {SafeCast} from "./math/SafeCast.sol";
import {Bytes} from "./Bytes.sol";

/**
* @dev Provides a set of functions to operate with Base58 strings.
*
* Based on https://github.com/storyicon/base58-solidity/commit/807428e5174e61867e4c606bdb26cba58a8c5cb1[storyicon's implementation] (MIT).
*/
library Base58 {
using SafeCast for bool;
using Bytes for bytes;

error InvalidBase56Digit(uint8);

/**
* @dev Base58 encoding & decoding tables
* See sections 2 of https://datatracker.ietf.org/doc/html/draft-msporny-base58-03
*/
bytes internal constant _TABLE = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
bytes internal constant _LOOKUP_TABLE =
hex"000102030405060708ffffffffffffff090a0b0c0d0e0f10ff1112131415ff161718191a1b1c1d1e1f20ffffffffffff2122232425262728292a2bff2c2d2e2f30313233343536373839";
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For anyone curious, here is how you build these lookup tables:

const { ethers } = require("ethers");

const max = (...values) => values.slice(1).reduce((x, y) => (x > y ? x : y), values.at(0));
const min = (...values) => values.slice(1).reduce((x, y) => (x < y ? x : y), values.at(0));

const buildLookup = (...tables) => {
    const bTables = tables.map(table => Array.from(ethers.toUtf8Bytes(table)));
    const MINIMUM = min(...bTables.flatMap(x => x));
    const MAXIMUM = max(...bTables.flatMap(x => x));
    const lookup = Uint8Array.from(Array.from({ length: MAXIMUM - MINIMUM + 1 }).map((_, i) => bTables.map(table => table.indexOf(i + MINIMUM)).find(x => x != -1) ?? 0xff));
    const valid = tables.every(table => Object.entries(table).every(([ i, c]) => i == lookup.at(c.charCodeAt(0) - MINIMUM)));
    return valid ? { tables, lookup: ethers.hexlify(lookup), MINIMUM, MAXIMUM } : undefined;
}

console.log(buildLookup(
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", // base64
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_", // base64url
));

console.log(buildLookup(
    "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz", // base58
));


/**
* @dev Encode a `bytes` buffer as a Base58 `string`.
*/
function encode(bytes memory data) internal pure returns (string memory) {
return string(_encode(data));
}

/**
* @dev Decode a Base58 `string` into a `bytes` buffer.
*/
function decode(string memory data) internal pure returns (bytes memory) {
return _decode(bytes(data));
}

function _encode(bytes memory data) private pure returns (bytes memory encoded) {
// For reference, solidity implementation
// unchecked {
// uint256 dataLeadingZeros = data.countLeading(0x00);
// uint256 length = dataLeadingZeros + ((data.length - dataLeadingZeros) * 8351) / 6115 + 1;
// encoded = new bytes(length);
// uint256 end = length;
// for (uint256 i = 0; i < data.length; ++i) {
// uint256 ptr = length;
// for (uint256 carry = uint8(data[i]); ptr > end || carry != 0; --ptr) {
// carry += 256 * uint8(encoded[ptr - 1]);
// encoded[ptr - 1] = bytes1(uint8(carry % 58));
// carry /= 58;
// }
// end = ptr;
// }
// uint256 encodedCLZ = encoded.countLeading(0x00);
// length -= encodedCLZ - dataLeadingZeros;
// encoded.splice(encodedCLZ - dataLeadingZeros);
// for (uint256 i = 0; i < length; ++i) {
// encoded[i] = _TABLE[uint8(encoded[i])];
// }
// }

// Assembly is ~50% cheaper for buffers of size 32.
assembly ("memory-safe") {
function clzBytes(ptr, length) -> i {
for {
i := 0
} and(iszero(byte(0, mload(add(ptr, i)))), lt(i, length)) {
i := add(i, 1)
} {}
}

encoded := mload(0x40)
let dataLength := mload(data)

// Count number of zero bytes at the beginning of `data`. These are encoded using the same number of '1's
// at then beginning of the encoded string.
let dataLeadingZeros := clzBytes(add(data, 0x20), dataLength)

// Initial encoding length: 100% of zero bytes (zero prefix) + 138% of non zero bytes + 1
let slotLength := add(add(div(mul(sub(dataLength, dataLeadingZeros), 138), 100), dataLeadingZeros), 1)

// Zero the encoded buffer
for {
let i := 0
} lt(i, slotLength) {
i := add(i, 0x20)
} {
mstore(add(add(encoded, 0x20), i), 0)
}

// Build the "slots"
for {
let i := 0
let end := slotLength
} lt(i, dataLength) {
i := add(i, 1)
} {
let ptr := slotLength
for {
let carry := byte(0, mload(add(add(data, 0x20), i)))
} or(carry, lt(end, ptr)) {
ptr := sub(ptr, 1)
carry := div(carry, 58)
} {
carry := add(carry, mul(256, byte(0, mload(add(add(encoded, 0x1f), ptr)))))
mstore8(add(add(encoded, 0x1f), ptr), mod(carry, 58))
}
end := ptr
}

// Count number of zero bytes at the beginning of slots. This is a pointer to the first non zero slot that
// contains the base58 data. This base58 data span over `slotLength-slotLeadingZeros` bytes.
let slotLeadingZeros := clzBytes(add(encoded, 0x20), slotLength)

// Update length: `slotLength-slotLeadingZeros` of non-zero data plus `dataLeadingZeros` of zero prefix.
let offset := sub(slotLeadingZeros, dataLeadingZeros)
let encodedLength := sub(slotLength, offset)

// Store the encoding table. This overlaps with the FMP that we are going to reset later anyway.
mstore(0x1f, "123456789ABCDEFGHJKLMNPQRSTUVWXY")
mstore(0x3f, "Zabcdefghijkmnopqrstuvwxyz")

// For each slot, use the table to obtain the corresponding base58 "digit".
for {
let i := 0
} lt(i, encodedLength) {
i := add(i, 1)
} {
mstore8(add(add(encoded, 0x20), i), mload(byte(0, mload(add(add(encoded, 0x20), add(offset, i))))))
}

// Store length and allocate (reserve) memory
mstore(encoded, encodedLength)
mstore(0x40, add(add(encoded, 0x20), encodedLength))
}
}

function _decode(bytes memory data) private pure returns (bytes memory) {
unchecked {
uint256 b58Length = data.length;

uint256 size = 2 * ((b58Length * 8351) / 6115 + 1);
bytes memory binu = new bytes(size);

bytes memory cache = _LOOKUP_TABLE;
uint256 outiLength = (b58Length + 3) / 4;
// Note: allocating uint32[] would be enough, but solidity doesn't pack memory.
uint256[] memory outi = new uint256[](outiLength);
for (uint256 i = 0; i < data.length; ++i) {
// get b58 char
uint8 chr = uint8(data[i]);
require(chr > 48 && chr < 123, InvalidBase56Digit(chr));
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

48 and 123 are derived from the minimum and maximum values taken by b58 chars, see https://github.com/OpenZeppelin/openzeppelin-contracts/pull/5762/files#r2160061084


// decode b58 char
uint256 carry = uint8(cache[chr - 49]);
require(carry < 58, InvalidBase56Digit(chr));

for (uint256 j = outiLength; j > 0; --j) {
uint256 value = carry + 58 * outi[j - 1];
carry = value >> 32;
outi[j - 1] = value & 0xffffffff;
}
}

uint256 ptr = 0;
uint256 mask = ((b58Length - 1) % 4) + 1;
for (uint256 j = 0; j < outiLength; ++j) {
while (mask > 0) {
--mask;
binu[ptr] = bytes1(uint8(outi[j] >> (8 * mask)));
++ptr;
}
mask = 4;
}

uint256 dataLeadingZeros = data.countLeading(0x31);
uint256 msb = binu.countConsecutive(dataLeadingZeros, 0x00);
return binu.splice(msb * (dataLeadingZeros + msb < binu.length).toUint(), ptr);
}
}
}
6 changes: 3 additions & 3 deletions contracts/utils/Base64.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ pragma solidity ^0.8.20;
*/
library Base64 {
/**
* @dev Base64 Encoding/Decoding Table
* @dev Base64 encoding table
* See sections 4 and 5 of https://datatracker.ietf.org/doc/html/rfc4648
*/
string internal constant _TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
string internal constant _TABLE_URL = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";

/**
* @dev Converts a `bytes` to its Bytes64 `string` representation.
* @dev Converts a `bytes` to its Base64 `string` representation.
*/
function encode(bytes memory data) internal pure returns (string memory) {
return _encode(data, _TABLE, true);
}

/**
* @dev Converts a `bytes` to its Bytes64Url `string` representation.
* @dev Converts a `bytes` to its Base64Url `string` representation.
* Output is not padded with `=` as specified in https://www.rfc-editor.org/rfc/rfc4648[rfc4648].
*/
function encodeURL(bytes memory data) internal pure returns (string memory) {
Expand Down
51 changes: 51 additions & 0 deletions contracts/utils/Bytes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,28 @@ library Bytes {
}
}

/**
* @dev Count number of occurrences of `search` at the beginning of `buffer`.
*/
function countLeading(bytes memory buffer, bytes1 search) internal pure returns (uint256) {
return countConsecutive(buffer, 0, search);
}

/**
* @dev Count number of occurrences of `search` in `buffer`, starting from position `offset`.
*/
function countConsecutive(bytes memory buffer, uint256 offset, bytes1 search) internal pure returns (uint256 i) {
uint256 length = Math.saturatingSub(buffer.length, offset);
assembly ("memory-safe") {
for {
let ptr := add(add(buffer, 0x20), offset)
i := 0
} and(iszero(shr(248, xor(mload(add(ptr, i)), search))), lt(i, length)) {
i := add(i, 1)
} {}
}
}

Comment on lines +71 to +92
Copy link

@coderabbitai coderabbitai bot Aug 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Bug: incorrect byte comparison and potential OOB read in countConsecutive loop condition.

  • xor(mload(add(ptr, i)), search) xors the least-significant byte of the 32-byte word, but you later shr(248, ...) to examine the most-significant byte; this never compares against search and effectively counts leading zero bytes instead.
  • Using and(...) in the for condition evaluates both operands; at i == length this performs an out-of-bounds mload.

Fix by reading only when i < length and comparing the correct byte.

-        assembly ("memory-safe") {
-            for {
-                let ptr := add(add(buffer, 0x20), offset)
-                i := 0
-            } and(iszero(shr(248, xor(mload(add(ptr, i)), search))), lt(i, length)) {
-                i := add(i, 1)
-            } {}
-        }
+        assembly ("memory-safe") {
+            let ptr := add(add(buffer, 0x20), offset)
+            // Iterate while in-bounds; break on first non-match.
+            for { i := 0 } lt(i, length) { i := add(i, 1) } {
+                if iszero(eq(byte(0, mload(add(ptr, i))), search))) { break }
+            }
+        }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
/**
* @dev Count number of occurrences of `search` at the beginning of `buffer`.
*/
function countLeading(bytes memory buffer, bytes1 search) internal pure returns (uint256) {
return countConsecutive(buffer, 0, search);
}
/**
* @dev Count number of occurrences of `search` in `buffer`, starting from position `offset`.
*/
function countConsecutive(bytes memory buffer, uint256 offset, bytes1 search) internal pure returns (uint256 i) {
uint256 length = Math.saturatingSub(buffer.length, offset);
assembly ("memory-safe") {
for {
let ptr := add(add(buffer, 0x20), offset)
i := 0
} and(iszero(shr(248, xor(mload(add(ptr, i)), search))), lt(i, length)) {
i := add(i, 1)
} {}
}
}
/**
* @dev Count number of occurrences of `search` in `buffer`, starting from position `offset`.
*/
function countConsecutive(bytes memory buffer, uint256 offset, bytes1 search) internal pure returns (uint256 i) {
uint256 length = Math.saturatingSub(buffer.length, offset);
assembly ("memory-safe") {
let ptr := add(add(buffer, 0x20), offset)
// Iterate while in-bounds; break on first non-match.
for { i := 0 } lt(i, length) { i := add(i, 1) } {
if iszero(eq(byte(0, mload(add(ptr, i))), search)) { break }
}
}
}
🤖 Prompt for AI Agents
In contracts/utils/Bytes.sol around lines 71 to 92, the inline assembly loop
both compares the wrong byte and can mload out-of-bounds when i == length;
update the for-condition to check i < length before doing any mload, and change
the byte comparison to extract and compare the correct byte (mask the loaded
word to 0xff or shift so the byte under test is compared to search) instead of
xor+shr that looks at the opposite byte; ensure you only perform mload after the
bounds check so no OOB loads occur.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: replacing iszero(shr(248, xor(mload(add(ptr, i)), search))) with iszero(eq(byte(0, mload(add(ptr, i))), search))) implies that the data in search is in the leftmost 8 bits as opposed to the rightmost 8 bits. Need to check (this is a bytes1, so maybe its the correct approach)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.

You're absolutely correct! Here's the analysis:

  1. bytes1 values are left-aligned in Solidity - they occupy the leftmost (most significant) byte of a 32-byte word
  2. byte(0, x) extracts the leftmost byte from the word
  3. mload(add(ptr, i)) loads 32 bytes where the byte at position i becomes the leftmost byte

So my suggested fix byte(0, mload(add(ptr, i))) == search is correct - both operands have their data in the leftmost position.

The original code's shr(248, ...) was also targeting the leftmost byte (shifting it to the rightmost position), which confirms that the intention was always to compare the leftmost bytes.

However, I notice there's a syntax error in my suggestion - missing closing parenthesis. Here's the corrected version:

assembly ("memory-safe") {
    let ptr := add(add(buffer, 0x20), offset)
    // Iterate while in-bounds; break on first non-match.
    for { i := 0 } lt(i, length) { i := add(i, 1) } {
        if iszero(eq(byte(0, mload(add(ptr, i))), search)) { break }
    }
}

The key insight is that both byte(0, mload(add(ptr, i))) and search (bytes1) have their significant data in the leftmost 8 bits, making the comparison correct.

/**
* @dev Copies the content of `buffer`, from `start` (included) to the end of `buffer` into a new bytes object in
* memory.
Expand Down Expand Up @@ -99,6 +121,35 @@ library Bytes {
return result;
}

/**
* @dev In place slice: moves the content of `buffer`, from `start` (included) to the end of `buffer` to the start of that buffer.
*
* NOTE: This function modifies the provided buffer in place. If you need to preserve the original buffer, use {slice} instead
*/
function splice(bytes memory buffer, uint256 start) internal pure returns (bytes memory) {
return splice(buffer, start, buffer.length);
}

/**
* @dev In place slice: moves the content of `buffer`, from `start` (included) to end (excluded) to the start of that buffer.
*
* NOTE: This function modifies the provided buffer in place. If you need to preserve the original buffer, use {slice} instead
*/
function splice(bytes memory buffer, uint256 start, uint256 end) internal pure returns (bytes memory) {
// sanitize
uint256 length = buffer.length;
end = Math.min(end, length);
start = Math.min(start, end);

// allocate and copy
assembly ("memory-safe") {
mcopy(add(buffer, 0x20), add(add(buffer, 0x20), start), sub(end, start))
mstore(buffer, sub(end, start))
}

return buffer;
}

/**
* @dev Reads a bytes32 from a bytes array without bounds checking.
*
Expand Down
3 changes: 3 additions & 0 deletions contracts/utils/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Miscellaneous contracts and libraries containing utility functions you can use t
* {Create2}: Wrapper around the https://blog.openzeppelin.com/getting-the-most-out-of-create2/[`CREATE2` EVM opcode] for safe use without having to deal with low-level assembly.
* {Address}: Collection of functions for overloading Solidity's https://docs.soliditylang.org/en/latest/types.html#address[`address`] type.
* {Arrays}: Collection of functions that operate on https://docs.soliditylang.org/en/latest/types.html#arrays[`arrays`].
* {Base58}: On-chain base58 encoding and decoding.
* {Base64}: On-chain base64 and base64URL encoding according to https://datatracker.ietf.org/doc/html/rfc4648[RFC-4648].
* {Bytes}: Common operations on bytes objects.
* {Calldata}: Helpers for manipulating calldata.
Expand Down Expand Up @@ -105,6 +106,8 @@ Ethereum contracts have no native concept of an interface, so applications must

{{Arrays}}

{{Base58}}

{{Base64}}

{{Bytes}}
Expand Down
24 changes: 24 additions & 0 deletions test/utils/Base58.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.26;

import {Test} from "forge-std/Test.sol";
import {Base58} from "@openzeppelin/contracts/utils/Base58.sol";

contract Base58Test is Test {
function testEncodeDecodeEmpty() external pure {
assertEq(Base58.decode(Base58.encode("")), "");
}

function testEncodeDecodeZeros() external pure {
bytes memory zeros = hex"0000000000000000";
assertEq(Base58.decode(Base58.encode(zeros)), zeros);

bytes memory almostZeros = hex"00000000a400000000";
assertEq(Base58.decode(Base58.encode(almostZeros)), almostZeros);
}

function testEncodeDecode(bytes memory input) external pure {
assertEq(Base58.decode(Base58.encode(input)), input);
}
}
Loading