Skip to content

Commit 6305985

Browse files
committed
forge fmt
1 parent 2f40a19 commit 6305985

File tree

4 files changed

+32
-37
lines changed

4 files changed

+32
-37
lines changed

Diff for: src/lib/commitment/Commitment.sol

+16-15
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,19 @@ function _writeSequenceLength(bytes memory share, uint32 sequenceLength) pure {
4343
}
4444

4545
function _copyBytes(bytes memory buffer, uint32 cursor, bytes memory data, uint32 length) pure returns (uint32) {
46-
4746
uint256 start = buffer.length - length;
4847
for (uint256 i = start; i < buffer.length; i++) {
4948
if (cursor < data.length) {
5049
buffer[i] = data[cursor];
5150
cursor++;
52-
}
53-
else {
51+
} else {
5452
buffer[i] = 0;
5553
}
5654
}
5755
return cursor;
5856
}
5957

6058
function _bytesToHexString(bytes memory buffer) pure returns (string memory) {
61-
6259
// Fixed buffer size for hexadecimal convertion
6360
bytes memory converted = new bytes(buffer.length * 2);
6461

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

7572
// Share Version 0
76-
function _bytesToSharesV0(bytes memory blobData, Namespace memory namespace) pure returns (bytes[] memory shares, bool err) {
73+
function _bytesToSharesV0(bytes memory blobData, Namespace memory namespace)
74+
pure
75+
returns (bytes[] memory shares, bool err)
76+
{
7777
if (namespace.version != 0) {
7878
return (new bytes[](0), true);
7979
}
8080
if (isReservedNamespace(namespace)) {
8181
return (new bytes[](0), true);
8282
}
8383
// Allocate memory for the shares
84-
uint256 numShares = _numShares(blobData.length);
84+
uint256 numShares = _numShares(blobData.length);
8585
bytes[] memory _shares = new bytes[](numShares);
8686
for (uint256 i = 0; i < _shares.length; i++) {
8787
_shares[i] = new bytes(512);
@@ -149,18 +149,16 @@ function _merkleMountainRangeSizes(uint256 totalSize, uint256 maxTreeSize) pure
149149
uint256 numTrees;
150150
if (leftovers == 0) {
151151
numTrees = bigTrees;
152-
}
153-
else {
154-
numTrees = bigTrees + MathUpgradeable.log2(leftovers) + (leftovers%2);
152+
} else {
153+
numTrees = bigTrees + MathUpgradeable.log2(leftovers) + (leftovers % 2);
155154
}
156155
uint256[] memory treeSizes = new uint256[](numTrees);
157156
uint256 count = 0;
158157
while (totalSize != 0) {
159158
if (totalSize >= maxTreeSize) {
160159
treeSizes[count] = maxTreeSize;
161160
totalSize -= maxTreeSize;
162-
}
163-
else {
161+
} else {
164162
uint256 treeSize = _roundDownPowerOfTwo(totalSize);
165163
treeSizes[count] = treeSize;
166164
totalSize -= treeSize;
@@ -203,12 +201,15 @@ function _createCommitment(bytes[] memory shares, Namespace memory namespace) re
203201
leafNamespaceNodes[j] = leafDigest(namespace, leafSets[i][j]);
204202
}
205203
//NamespaceMerkleMultiproof memory emptyProof = NamespaceMerkleMultiproof(0, leafSets[i].length, new NamespaceNode[](0));
206-
NamespaceMerkleMultiproof memory emptyProof = NamespaceMerkleMultiproof(0, leafSets[i].length, leafNamespaceNodes);
207-
(NamespaceNode memory root,,,) = NamespaceMerkleTree._computeRoot(emptyProof, leafNamespaceNodes, 0, leafNamespaceNodes.length, 0, 0);
204+
NamespaceMerkleMultiproof memory emptyProof =
205+
NamespaceMerkleMultiproof(0, leafSets[i].length, leafNamespaceNodes);
206+
(NamespaceNode memory root,,,) =
207+
NamespaceMerkleTree._computeRoot(emptyProof, leafNamespaceNodes, 0, leafNamespaceNodes.length, 0, 0);
208208
subtreeRoots[i] = bLeafDigest(bytes(abi.encodePacked(root.min.toBytes(), root.max.toBytes(), root.digest)));
209209
}
210210
//BinaryMerkleMultiproof memory nullBinaryProof = BinaryMerkleMultiproof(new bytes32[](0), 0, 0);
211211
BinaryMerkleMultiproof memory emptyBinaryProof = BinaryMerkleMultiproof(new bytes32[](0), 0, subtreeRoots.length);
212-
(bytes32 binaryTreeRoot,,,) = BinaryMerkleTree._computeRootMulti(emptyBinaryProof, subtreeRoots, 0, subtreeRoots.length, 0, 0);
212+
(bytes32 binaryTreeRoot,,,) =
213+
BinaryMerkleTree._computeRootMulti(emptyBinaryProof, subtreeRoots, 0, subtreeRoots.length, 0, 0);
213214
commitment = binaryTreeRoot;
214-
}
215+
}

Diff for: src/lib/commitment/test/Commitment.t.sol

+15-20
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,24 @@ contract CommitmentTest is DSTest {
1919
}
2020

2121
function fromHexChar(uint8 c) public pure returns (uint8) {
22-
if (bytes1(c) >= bytes1('0') && bytes1(c) <= bytes1('9')) {
23-
return c - uint8(bytes1('0'));
22+
if (bytes1(c) >= bytes1("0") && bytes1(c) <= bytes1("9")) {
23+
return c - uint8(bytes1("0"));
2424
}
25-
if (bytes1(c) >= bytes1('a') && bytes1(c) <= bytes1('f')) {
26-
return 10 + c - uint8(bytes1('a'));
25+
if (bytes1(c) >= bytes1("a") && bytes1(c) <= bytes1("f")) {
26+
return 10 + c - uint8(bytes1("a"));
2727
}
28-
if (bytes1(c) >= bytes1('A') && bytes1(c) <= bytes1('F')) {
29-
return 10 + c - uint8(bytes1('A'));
28+
if (bytes1(c) >= bytes1("A") && bytes1(c) <= bytes1("F")) {
29+
return 10 + c - uint8(bytes1("A"));
3030
}
3131
revert("fail");
3232
}
3333

3434
function fromHex(string memory s) public pure returns (bytes memory) {
3535
bytes memory ss = bytes(s);
36-
require(ss.length%2 == 0); // length must be even
37-
bytes memory r = new bytes(ss.length/2);
38-
for (uint i=0; i<ss.length/2; ++i) {
39-
r[i] = bytes1(fromHexChar(uint8(ss[2*i])) * 16 + fromHexChar(uint8(ss[2*i+1])));
36+
require(ss.length % 2 == 0); // length must be even
37+
bytes memory r = new bytes(ss.length / 2);
38+
for (uint256 i = 0; i < ss.length / 2; ++i) {
39+
r[i] = bytes1(fromHexChar(uint8(ss[2 * i])) * 16 + fromHexChar(uint8(ss[2 * i + 1])));
4040
}
4141
return r;
4242
}
@@ -45,26 +45,23 @@ contract CommitmentTest is DSTest {
4545
return (keccak256(abi.encodePacked((a))) == keccak256(abi.encodePacked((b))));
4646
}
4747

48-
function testBytesToSharesV0() view external {
49-
48+
function testBytesToSharesV0() external view {
5049
// test vectors were generated here: https://github.com/S1nus/share-test-vec-gen
5150
string memory path = "./src/lib/commitment/test/testVectors.json";
5251
string memory jsonData = vm.readFile(path);
5352
bytes memory vecsData = vm.parseJson(jsonData);
5453
TestVector[] memory vecs = abi.decode(vecsData, (TestVector[]));
5554

56-
for (uint i = 0; i < vecs.length; i++) {
55+
for (uint256 i = 0; i < vecs.length; i++) {
5756
bytes29 nsString = bytes29(fromHex(vecs[i].namespace));
5857
Namespace memory ns = toNamespace(nsString);
5958
bytes memory data = fromHex(vecs[i].data);
6059
(bytes[] memory shares, bool err) = _bytesToSharesV0(data, ns);
6160
string memory out = "";
62-
for (uint j = 0; j < shares.length; j++) {
61+
for (uint256 j = 0; j < shares.length; j++) {
6362
out = string.concat(out, _bytesToHexString(shares[j]));
6463
}
6564
// none of the test vectors should cause an error
66-
//assert(!err);
67-
//assert(compareStrings(out, vecs[i].shares));
6865
if (!compareStrings(out, vecs[i].shares)) {
6966
console.log("expected: ", vecs[i].shares);
7067
console.log("got: ", out);
@@ -79,8 +76,7 @@ contract CommitmentTest is DSTest {
7976
bytes memory vecsData = vm.parseJson(jsonData);
8077
TestVector[] memory vecs = abi.decode(vecsData, (TestVector[]));
8178

82-
83-
for (uint i = 0; i < vecs.length; i++) {
79+
for (uint256 i = 0; i < vecs.length; i++) {
8480
bytes29 nsString = bytes29(fromHex(vecs[i].namespace));
8581
Namespace memory ns = toNamespace(nsString);
8682
bytes memory data = fromHex(vecs[i].data);
@@ -93,6 +89,5 @@ contract CommitmentTest is DSTest {
9389
revert();
9490
}
9591
}
96-
9792
}
98-
}
93+
}

Diff for: src/lib/tree/binary/TreeHasher.sol

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ function nodeDigest(bytes32 left, bytes32 right) pure returns (bytes32 digest) {
2020
// solhint-disable-next-line func-visibility
2121
function leafDigest(bytes memory data) pure returns (bytes32 digest) {
2222
digest = sha256(abi.encodePacked(Constants.LEAF_PREFIX, data));
23-
}
23+
}

Diff for: src/lib/tree/namespace/NamespaceMerkleTree.sol

-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,6 @@ library NamespaceMerkleTree {
193193
return namespaceNodeEquals(rootHash, root);
194194
}
195195

196-
197196
/// @notice Computes the NMT root recursively.
198197
/// @param proof Namespace Merkle multiproof for the leaves.
199198
/// @param leafNodes Leaf nodes for which inclusion is proven.

0 commit comments

Comments
 (0)