-
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 2 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 | ||
--- | ||
|
||
`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
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,135 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.24; | ||
|
||
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 Bytes for bytes; | ||
|
||
string internal constant _TABLE = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; | ||
|
||
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 = _countLeading(data, 0x00); | ||
uint256 slotLength = dataCLZ + ((data.length - dataCLZ) * 8351) / 6115 + 1; | ||
|
||
bytes memory slot = new bytes(slotLength); | ||
uint256 end = slotLength; | ||
for (uint256 i = 0; i < data.length; i++) { | ||
uint256 ptr = slotLength; | ||
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 = _countLeading(slot, 0x00); | ||
uint256 resultLength = slotLength + dataCLZ - slotCLZ; | ||
|
||
bytes memory cache = bytes(_TABLE); | ||
for (uint256 i = 0; i < resultLength; ++i) { | ||
uint256 idx = _mload8i(slot, i + slotCLZ - dataCLZ); | ||
bytes1 c = _mload8(cache, idx); | ||
_mstore8(slot, i, c); | ||
} | ||
|
||
assembly ("memory-safe") { | ||
mstore(slot, resultLength) | ||
} | ||
|
||
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 = bytes(_TABLE); | ||
uint32[] memory outi = new uint32[]((b58Length + 3) / 4); | ||
for (uint256 i = 0; i < data.length; i++) { | ||
bytes1 r = _mload8(data, i); | ||
uint256 c = cache.indexOf(r); // can we avoid the loop here ? | ||
require(c != type(uint256).max, "invalid base58 digit"); | ||
for (uint256 k = outi.length; k > 0; --k) { | ||
uint256 t = uint64(outi[k - 1]) * 58 + c; | ||
c = t >> 32; | ||
outi[k - 1] = uint32(t & 0xffffffff); | ||
} | ||
} | ||
|
||
uint256 ptr = 0; | ||
uint256 mask = ((b58Length - 1) % 4) + 1; | ||
for (uint256 j = 0; j < outi.length; ++j) { | ||
while (mask > 0) { | ||
--mask; | ||
_mstore8(binu, ptr, bytes1(uint8(outi[j] >> (8 * mask)))); | ||
ptr++; | ||
} | ||
mask = 4; | ||
} | ||
|
||
uint256 dataCLZ = _countLeading(data, 0x31); | ||
for (uint256 msb = dataCLZ; msb < binu.length; ++msb) { | ||
if (_mload8(binu, msb) != 0x00) { | ||
return binu.slice(msb - dataCLZ, ptr); | ||
Amxx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
return binu.slice(0, ptr); | ||
Amxx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
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) | ||
} | ||
} | ||
|
||
function _countLeading(bytes memory buffer, bytes1 el) private pure returns (uint256) { | ||
uint256 length = buffer.length; | ||
uint256 i = 0; | ||
while (i < length && _mload8(buffer, i) == el) ++i; | ||
return i; | ||
} | ||
} |
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,16 @@ | ||
// 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 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,26 @@ | ||
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 () { | ||
for (const length of [0, 1, 2, 3, 4, 32, 42, 128, 384]) // 512 runs out of gas | ||
it(`Encode/Decode buffer of length ${length}`, async function () { | ||
const buffer = ethers.randomBytes(length); | ||
const hex = ethers.hexlify(buffer); | ||
const b58 = ethers.encodeBase58(buffer); | ||
|
||
expect(await this.mock.$encode(hex)).to.equal(b58); | ||
expect(await this.mock.$decode(b58)).to.equal(hex); | ||
}); | ||
}); | ||
}); |
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.
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.
Uh oh!
There was an error while loading. Please reload this page.