Skip to content

Commit

Permalink
forge fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
S1nus committed Aug 23, 2024
1 parent 2f40a19 commit 6305985
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 37 deletions.
31 changes: 16 additions & 15 deletions src/lib/commitment/Commitment.sol
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,19 @@ function _writeSequenceLength(bytes memory share, uint32 sequenceLength) pure {
}

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 {
} 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);

Expand All @@ -73,15 +70,18 @@ function _bytesToHexString(bytes memory buffer) pure returns (string memory) {
}

// Share Version 0
function _bytesToSharesV0(bytes memory blobData, Namespace memory namespace) pure returns (bytes[] memory shares, bool err) {
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 = _numShares(blobData.length);
uint256 numShares = _numShares(blobData.length);
bytes[] memory _shares = new bytes[](numShares);
for (uint256 i = 0; i < _shares.length; i++) {
_shares[i] = new bytes(512);
Expand Down Expand Up @@ -149,18 +149,16 @@ function _merkleMountainRangeSizes(uint256 totalSize, uint256 maxTreeSize) pure
uint256 numTrees;
if (leftovers == 0) {
numTrees = bigTrees;
}
else {
numTrees = bigTrees + MathUpgradeable.log2(leftovers) + (leftovers%2);
} else {
numTrees = bigTrees + MathUpgradeable.log2(leftovers) + (leftovers % 2);
}
uint256[] memory treeSizes = new uint256[](numTrees);
uint256 count = 0;
while (totalSize != 0) {
if (totalSize >= maxTreeSize) {
treeSizes[count] = maxTreeSize;
totalSize -= maxTreeSize;
}
else {
} else {
uint256 treeSize = _roundDownPowerOfTwo(totalSize);
treeSizes[count] = treeSize;
totalSize -= treeSize;
Expand Down Expand Up @@ -203,12 +201,15 @@ function _createCommitment(bytes[] memory shares, Namespace memory namespace) re
leafNamespaceNodes[j] = leafDigest(namespace, leafSets[i][j]);
}
//NamespaceMerkleMultiproof memory emptyProof = NamespaceMerkleMultiproof(0, leafSets[i].length, new NamespaceNode[](0));
NamespaceMerkleMultiproof memory emptyProof = NamespaceMerkleMultiproof(0, leafSets[i].length, leafNamespaceNodes);
(NamespaceNode memory root,,,) = NamespaceMerkleTree._computeRoot(emptyProof, leafNamespaceNodes, 0, leafNamespaceNodes.length, 0, 0);
NamespaceMerkleMultiproof memory emptyProof =
NamespaceMerkleMultiproof(0, leafSets[i].length, leafNamespaceNodes);
(NamespaceNode memory root,,,) =
NamespaceMerkleTree._computeRoot(emptyProof, leafNamespaceNodes, 0, leafNamespaceNodes.length, 0, 0);
subtreeRoots[i] = bLeafDigest(bytes(abi.encodePacked(root.min.toBytes(), root.max.toBytes(), root.digest)));
}
//BinaryMerkleMultiproof memory nullBinaryProof = BinaryMerkleMultiproof(new bytes32[](0), 0, 0);
BinaryMerkleMultiproof memory emptyBinaryProof = BinaryMerkleMultiproof(new bytes32[](0), 0, subtreeRoots.length);
(bytes32 binaryTreeRoot,,,) = BinaryMerkleTree._computeRootMulti(emptyBinaryProof, subtreeRoots, 0, subtreeRoots.length, 0, 0);
(bytes32 binaryTreeRoot,,,) =
BinaryMerkleTree._computeRootMulti(emptyBinaryProof, subtreeRoots, 0, subtreeRoots.length, 0, 0);
commitment = binaryTreeRoot;
}
}
35 changes: 15 additions & 20 deletions src/lib/commitment/test/Commitment.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,24 @@ contract CommitmentTest is DSTest {
}

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("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"));
}
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])));
require(ss.length % 2 == 0); // length must be even
bytes memory r = new bytes(ss.length / 2);
for (uint256 i = 0; i < ss.length / 2; ++i) {
r[i] = bytes1(fromHexChar(uint8(ss[2 * i])) * 16 + fromHexChar(uint8(ss[2 * i + 1])));
}
return r;
}
Expand All @@ -45,26 +45,23 @@ contract CommitmentTest is DSTest {
return (keccak256(abi.encodePacked((a))) == keccak256(abi.encodePacked((b))));
}

function testBytesToSharesV0() view external {

function testBytesToSharesV0() external view {
// 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++) {
for (uint256 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++) {
for (uint256 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));
if (!compareStrings(out, vecs[i].shares)) {
console.log("expected: ", vecs[i].shares);
console.log("got: ", out);
Expand All @@ -79,8 +76,7 @@ contract CommitmentTest is DSTest {
bytes memory vecsData = vm.parseJson(jsonData);
TestVector[] memory vecs = abi.decode(vecsData, (TestVector[]));


for (uint i = 0; i < vecs.length; i++) {
for (uint256 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);
Expand All @@ -93,6 +89,5 @@ contract CommitmentTest is DSTest {
revert();
}
}

}
}
}
2 changes: 1 addition & 1 deletion src/lib/tree/binary/TreeHasher.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ function nodeDigest(bytes32 left, bytes32 right) pure returns (bytes32 digest) {
// solhint-disable-next-line func-visibility
function leafDigest(bytes memory data) pure returns (bytes32 digest) {
digest = sha256(abi.encodePacked(Constants.LEAF_PREFIX, data));
}
}
1 change: 0 additions & 1 deletion src/lib/tree/namespace/NamespaceMerkleTree.sol
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ library NamespaceMerkleTree {
return namespaceNodeEquals(rootHash, root);
}


/// @notice Computes the NMT root recursively.
/// @param proof Namespace Merkle multiproof for the leaves.
/// @param leafNodes Leaf nodes for which inclusion is proven.
Expand Down

0 comments on commit 6305985

Please sign in to comment.