-
Notifications
You must be signed in to change notification settings - Fork 12.1k
Add Base58 library #5762
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Amxx
wants to merge
29
commits into
OpenZeppelin:master
Choose a base branch
from
Amxx:feature/base58
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add Base58 library #5762
Changes from 9 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
5a400eb
Add Bytes.splice, an inplace variant of Buffer.slice
Amxx 65292d5
Add Base58 library
Amxx 99a1835
docs
Amxx bddf4f6
Merge branch 'feature/Bytes-splice' into feature/base58
Amxx 88c03e7
Add Bytes.countConsecutive and Bytes.countLeading
Amxx a3c4667
fix
Amxx 41b586b
efficient decoding
Amxx c6d6bdd
coverage
Amxx 48bf13b
Update thirty-pugs-pick.md
Amxx eebd51e
docs
Amxx 296a87e
pragma
Amxx 8c94acc
pragma
Amxx d09ebfa
coverage
Amxx a25bd11
rewrite _encode in assembly
Amxx a4ce8c8
more inline documentation
Amxx 7474f2a
test vectors
Amxx bef2e4f
document
Amxx ce1c5ad
remove auxiliary utils
Amxx c33e933
mload is actually cheaper than jump
Amxx 855a1c6
up
Amxx ec641c7
Update contracts/utils/Base58.sol
Amxx 7429bcc
up
Amxx 45edb76
do base58 arithmetics in chunks of 248 bits
Amxx 20f3611
update
Amxx 8e60a99
codespell
Amxx dd8e895
decode assembly
Amxx 45f04b4
char valdity filter
Amxx da84743
slither
Amxx c80f693
slither
Amxx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'openzeppelin-solidity': minor | ||
--- | ||
|
||
`Bytes`: Add `countLeading` and `countConsecutive` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.24; | ||
|
||
import {SafeCast} from "./math/SafeCast.sol"; | ||
import {Bytes} from "./Bytes.sol"; | ||
|
||
/** | ||
* @dev Provides a set of functions to operate with Base58 strings. | ||
* | ||
* Based on the original https://github.com/storyicon/base58-solidity/commit/807428e5174e61867e4c606bdb26cba58a8c5cb1[implementation of storyicon] (MIT). | ||
*/ | ||
library Base58 { | ||
using SafeCast for bool; | ||
using Bytes for bytes; | ||
|
||
error InvalidBase56Digit(uint8); | ||
|
||
bytes internal constant _TABLE = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; | ||
bytes internal constant _LOOKUP_TABLE = | ||
hex"000102030405060708ffffffffffffff090a0b0c0d0e0f10ff1112131415ff161718191a1b1c1d1e1f20ffffffffffff2122232425262728292a2bff2c2d2e2f30313233343536373839"; | ||
|
||
function encode(bytes memory data) internal pure returns (string memory) { | ||
return string(_encode(data)); | ||
} | ||
|
||
function decode(string memory data) internal pure returns (bytes memory) { | ||
return _decode(bytes(data)); | ||
} | ||
|
||
function _encode(bytes memory data) private pure returns (bytes memory) { | ||
unchecked { | ||
uint256 dataCLZ = data.countLeading(0x00); | ||
uint256 length = dataCLZ + ((data.length - dataCLZ) * 8351) / 6115 + 1; | ||
bytes memory slot = new bytes(length); | ||
|
||
uint256 end = length; | ||
for (uint256 i = 0; i < data.length; i++) { | ||
uint256 ptr = length; | ||
for (uint256 carry = _mload8i(data, i); ptr > end || carry != 0; --ptr) { | ||
carry += 256 * _mload8i(slot, ptr - 1); | ||
_mstore8i(slot, ptr - 1, uint8(carry % 58)); | ||
carry /= 58; | ||
} | ||
end = ptr; | ||
} | ||
|
||
uint256 slotCLZ = slot.countLeading(0x00); | ||
length -= slotCLZ - dataCLZ; | ||
slot.splice(slotCLZ - dataCLZ); | ||
|
||
bytes memory cache = _TABLE; | ||
for (uint256 i = 0; i < length; ++i) { | ||
// equivalent to `slot[i] = TABLE[slot[i]];` | ||
_mstore8(slot, i, _mload8(cache, _mload8i(slot, i))); | ||
} | ||
|
||
return slot; | ||
} | ||
} | ||
|
||
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 = _mload8i(data, i); | ||
require(chr > 48 && chr < 123, InvalidBase56Digit(chr)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = _mload8i(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; | ||
_mstore8(binu, ptr, bytes1(uint8(outi[j] >> (8 * mask)))); | ||
ptr++; | ||
} | ||
mask = 4; | ||
} | ||
|
||
uint256 dataCLZ = data.countLeading(0x31); | ||
uint256 msb = binu.countConsecutive(dataCLZ, 0x00); | ||
return binu.splice(msb * (dataCLZ + msb < binu.length).toUint(), ptr); | ||
} | ||
} | ||
|
||
function _mload8(bytes memory buffer, uint256 offset) private pure returns (bytes1 value) { | ||
// This is not memory safe in the general case, but all calls to this private function are within bounds. | ||
assembly ("memory-safe") { | ||
value := mload(add(add(buffer, 0x20), offset)) | ||
} | ||
} | ||
|
||
function _mload8i(bytes memory buffer, uint256 offset) private pure returns (uint8 value) { | ||
// This is not memory safe in the general case, but all calls to this private function are within bounds. | ||
assembly ("memory-safe") { | ||
value := shr(248, mload(add(add(buffer, 0x20), offset))) | ||
} | ||
} | ||
|
||
function _mstore8(bytes memory buffer, uint256 offset, bytes1 value) private pure { | ||
// This is not memory safe in the general case, but all calls to this private function are within bounds. | ||
assembly ("memory-safe") { | ||
mstore8(add(add(buffer, 0x20), offset), shr(248, value)) | ||
} | ||
} | ||
|
||
function _mstore8i(bytes memory buffer, uint256 offset, uint8 value) private pure { | ||
// This is not memory safe in the general case, but all calls to this private function are within bounds. | ||
assembly ("memory-safe") { | ||
mstore8(add(add(buffer, 0x20), offset), value) | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.20; | ||
|
||
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); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
const { ethers } = require('hardhat'); | ||
const { expect } = require('chai'); | ||
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers'); | ||
|
||
async function fixture() { | ||
const mock = await ethers.deployContract('$Base58'); | ||
return { mock }; | ||
} | ||
|
||
describe('Base58', function () { | ||
beforeEach(async function () { | ||
Object.assign(this, await loadFixture(fixture)); | ||
}); | ||
|
||
describe('base58', function () { | ||
describe('encode/decode', function () { | ||
for (const length of [0, 1, 2, 3, 4, 32, 42, 128, 384]) // 512 runs out of gas | ||
it(`buffer of length ${length}`, async function () { | ||
const buffer = ethers.randomBytes(length); | ||
const hex = ethers.hexlify(buffer); | ||
const b58 = ethers.encodeBase58(buffer); | ||
|
||
await expect(this.mock.$encode(hex)).to.eventually.equal(b58); | ||
await expect(this.mock.$decode(b58)).to.eventually.equal(hex); | ||
}); | ||
}); | ||
|
||
describe('decode invalid format', function () { | ||
for (const chr of ['I', '-', '~']) | ||
it(`Invalid base58 char ${chr}`, async function () { | ||
await expect(this.mock.$decode(`VYRWKp${chr}pnN7`)) | ||
.to.be.revertedWithCustomError(this.mock, 'InvalidBase56Digit') | ||
.withArgs(chr.codePointAt(0)); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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: