-
Notifications
You must be signed in to change notification settings - Fork 40
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
blobToSharesV0 #327
Closed
Closed
blobToSharesV0 #327
Changes from 9 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
c1fbdf9
forge install: forge-std
S1nus e412e18
Merge remote-tracking branch 'origin'
S1nus 1e2fcd0
blob to shares
S1nus 3c49274
typo
S1nus b7b79e8
this is annoying, but accelerate
S1nus 443cd52
somewhat functioning test setup. Now to make it work.
S1nus e3bfc12
test setup works. now to fix the implementation
S1nus 650595b
so close
S1nus c13cba6
blob to shares
S1nus 396eca4
cleanups
S1nus 95d16ce
comment
S1nus 9fde028
start createCommitment
S1nus bdf6d18
update libs
S1nus 9ecf545
cooking on createCommitment
S1nus 902471c
subtree roots
S1nus c1bb1a4
trying to debug a seg fault
S1nus fab0080
cleanup
S1nus 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 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 |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
solc_version = "0.8.22" | ||
via_ir = true | ||
gas_reports = ["*"] | ||
fs_permissions = [{ access = "read", path = "./"}] |
This file contains 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,117 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.22; | ||
|
||
import {Namespace, isReservedNamespace} from "../tree/Types.sol"; | ||
import "openzeppelin-contracts/contracts/utils/math/Math.sol"; | ||
import "forge-std/console.sol"; | ||
|
||
// Turn little-endian into big-endian | ||
// Source: https://ethereum.stackexchange.com/a/83627 | ||
function reverse(uint32 input) pure returns (uint32 v) { | ||
v = input; | ||
|
||
// swap bytes | ||
v = ((v & 0xFF00FF00) >> 8) | | ||
((v & 0x00FF00FF) << 8); | ||
|
||
// swap 2-byte long pairs | ||
v = (v >> 16) | (v << 16); | ||
} | ||
|
||
function div_ceil(uint256 x, uint256 y) pure returns (uint256 z) { | ||
z = x / y + (x % y == 0 ? 0 : 1); | ||
} | ||
|
||
function num_shares(uint256 blobSize) pure returns (uint256) { | ||
return div_ceil((Math.max(blobSize, 478) - 478), 482) + 1; | ||
} | ||
|
||
function copyNamespace(bytes memory share, bytes29 namespaceBytes) pure { | ||
for (uint256 i = 0; i < namespaceBytes.length; i++) { | ||
share[i] = namespaceBytes[i]; | ||
} | ||
} | ||
|
||
function writeInfoByteV0(bytes memory share, bool startingSequence) pure { | ||
share[29] = bytes1(startingSequence ? 1 : 0); | ||
} | ||
|
||
function writeSequenceLength(bytes memory share, uint32 sequenceLength) pure { | ||
// Removed the "reverse", because it didn't work- apparently it's already big-endian? | ||
//bytes4 sequenceLengthBigEndianBytes = bytes4(abi.encodePacked(reverse(sequenceLength))); | ||
bytes4 sequenceLengthBigEndianBytes = bytes4(abi.encodePacked(sequenceLength)); | ||
share[30] = sequenceLengthBigEndianBytes[0]; | ||
share[31] = sequenceLengthBigEndianBytes[1]; | ||
share[32] = sequenceLengthBigEndianBytes[2]; | ||
share[33] = sequenceLengthBigEndianBytes[3]; | ||
} | ||
|
||
function copyBytes(bytes memory buffer, uint32 cursor, bytes memory data, uint32 length) pure returns (uint32) { | ||
|
||
uint256 start = buffer.length - length; | ||
for (uint256 i = start; i < buffer.length; i++) { | ||
if (cursor < data.length) { | ||
buffer[i] = data[cursor]; | ||
cursor++; | ||
} | ||
else { | ||
buffer[i] = 0; | ||
} | ||
} | ||
return cursor; | ||
} | ||
|
||
function bytesToHexString(bytes memory buffer) pure returns (string memory) { | ||
|
||
// Fixed buffer size for hexadecimal convertion | ||
bytes memory converted = new bytes(buffer.length * 2); | ||
|
||
bytes memory _base = "0123456789abcdef"; | ||
|
||
for (uint256 i = 0; i < buffer.length; i++) { | ||
converted[i * 2] = _base[uint8(buffer[i]) / _base.length]; | ||
converted[i * 2 + 1] = _base[uint8(buffer[i]) % _base.length]; | ||
} | ||
|
||
return string(abi.encodePacked(converted)); | ||
} | ||
|
||
// Share Version 0 | ||
function bytesToSharesV0(bytes memory blobData, Namespace memory namespace) pure returns (bytes[] memory shares, bool err) { | ||
if (namespace.version != 0) { | ||
return (new bytes[](0), true); | ||
} | ||
if (isReservedNamespace(namespace)) { | ||
return (new bytes[](0), true); | ||
} | ||
// Allocate memory for the shares | ||
uint256 numShares = num_shares(blobData.length); | ||
bytes[] memory _shares = new bytes[](numShares); | ||
for (uint256 i = 0; i < _shares.length; i++) { | ||
_shares[i] = new bytes(512); | ||
} | ||
// Get the namespace bytes | ||
bytes29 namespaceBytes = namespace.toBytes(); | ||
|
||
// The first share is special, because it has startingSequence set to true and the 4-byte sequence length | ||
copyNamespace(_shares[0], namespaceBytes); | ||
writeInfoByteV0(_shares[0], true); | ||
writeSequenceLength(_shares[0], uint32(blobData.length)); | ||
uint32 cursor = 0; | ||
cursor = copyBytes(_shares[0], cursor, blobData, uint32(478)); //only 478 bytes, because 4 bytes are used for the sequence length | ||
|
||
if (shares.length != 1) { | ||
// The remaining shares are all the same | ||
for (uint256 i = 1; i < _shares.length; i++) { | ||
// Copy the namespace | ||
copyNamespace(_shares[i], namespaceBytes); | ||
// Write the info byte | ||
writeInfoByteV0(_shares[i], false); | ||
// Copy the data | ||
cursor = copyBytes(_shares[i], cursor, blobData, uint32(482)); // copy the full 482 bytes | ||
} | ||
} | ||
|
||
shares = _shares; | ||
err = false; | ||
} | ||
This file contains 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,67 @@ | ||
pragma solidity ^0.8.22; | ||
|
||
import "ds-test/test.sol"; | ||
import "forge-std/Vm.sol"; | ||
import "forge-std/console.sol"; | ||
import {bytesToSharesV0, bytesToHexString} from "../Commitment.sol"; | ||
import {toNamespace, Namespace} from "../../tree/Types.sol"; | ||
|
||
contract CommitmentTest is DSTest { | ||
Vm private constant vm = Vm(address(uint160(uint256(keccak256("hevm cheat code"))))); | ||
|
||
struct TestVector { | ||
string data; | ||
string namespace; | ||
string shares; | ||
} | ||
|
||
function fromHexChar(uint8 c) public pure returns (uint8) { | ||
if (bytes1(c) >= bytes1('0') && bytes1(c) <= bytes1('9')) { | ||
return c - uint8(bytes1('0')); | ||
} | ||
if (bytes1(c) >= bytes1('a') && bytes1(c) <= bytes1('f')) { | ||
return 10 + c - uint8(bytes1('a')); | ||
} | ||
if (bytes1(c) >= bytes1('A') && bytes1(c) <= bytes1('F')) { | ||
return 10 + c - uint8(bytes1('A')); | ||
} | ||
revert("fail"); | ||
} | ||
|
||
function fromHex(string memory s) public pure returns (bytes memory) { | ||
bytes memory ss = bytes(s); | ||
require(ss.length%2 == 0); // length must be even | ||
bytes memory r = new bytes(ss.length/2); | ||
for (uint i=0; i<ss.length/2; ++i) { | ||
r[i] = bytes1(fromHexChar(uint8(ss[2*i])) * 16 + fromHexChar(uint8(ss[2*i+1]))); | ||
} | ||
return r; | ||
} | ||
|
||
function compareStrings(string memory a, string memory b) public pure returns (bool) { | ||
return (keccak256(abi.encodePacked((a))) == keccak256(abi.encodePacked((b)))); | ||
} | ||
|
||
function testBytesToSharesV0() view external { | ||
|
||
// test vectors were generated here: https://github.com/S1nus/share-test-vec-gen | ||
string memory path = "./src/lib/commitment/test/testVectors.json"; | ||
string memory jsonData = vm.readFile(path); | ||
bytes memory vecsData = vm.parseJson(jsonData); | ||
TestVector[] memory vecs = abi.decode(vecsData, (TestVector[])); | ||
|
||
for (uint i = 0; i < vecs.length; i++) { | ||
bytes29 nsString = bytes29(fromHex(vecs[i].namespace)); | ||
Namespace memory ns = toNamespace(nsString); | ||
bytes memory data = fromHex(vecs[i].data); | ||
(bytes[] memory shares, bool err) = bytesToSharesV0(data, ns); | ||
string memory out = ""; | ||
for (uint j = 0; j < shares.length; j++) { | ||
out = string.concat(out, bytesToHexString(shares[j])); | ||
} | ||
// none of the test vectors should cause an error | ||
assert(!err); | ||
assert(compareStrings(out, vecs[i].shares)); | ||
} | ||
} | ||
} |
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.
Handle the case where
blobData
is empty.The
bytesToSharesV0
function does not handle the case whereblobData
is empty. This could lead to unexpected behavior or errors.Committable suggestion