Skip to content

Commit

Permalink
refactor: delete unused Bytes library equals and set functions
Browse files Browse the repository at this point in the history
  • Loading branch information
yonadaa committed Jan 8, 2024
1 parent 82bce9c commit ee19186
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 203 deletions.
30 changes: 0 additions & 30 deletions packages/store/gas-report.json
Original file line number Diff line number Diff line change
@@ -1,34 +1,4 @@
[
{
"file": "test/Bytes.t.sol",
"test": "testEquals",
"name": "compare equal bytes",
"gasUsed": 196
},
{
"file": "test/Bytes.t.sol",
"test": "testEqualsFalse",
"name": "compare unequal bytes",
"gasUsed": 196
},
{
"file": "test/Bytes.t.sol",
"test": "testSetBytes1",
"name": "set bytes1 in bytes32",
"gasUsed": 16
},
{
"file": "test/Bytes.t.sol",
"test": "testSetBytes2",
"name": "set bytes2 in bytes32",
"gasUsed": 16
},
{
"file": "test/Bytes.t.sol",
"test": "testSetBytes4",
"name": "set bytes4 in bytes32",
"gasUsed": 16
},
{
"file": "test/Bytes.t.sol",
"test": "testSetBytes4Memory",
Expand Down
98 changes: 0 additions & 98 deletions packages/store/src/Bytes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,6 @@ library Bytes {
*
************************************************************************/

/**
* @dev Compares two bytes blobs for equality.
* @param a First bytes blob.
* @param b Second bytes blob.
* @return True if the two bytes blobs are equal, false otherwise.
*/
function equals(bytes memory a, bytes memory b) internal pure returns (bool) {
if (a.length != b.length) {
return false;
}
return keccak256(a) == keccak256(b);
}

/**
* @dev Sets the length of a bytes blob in memory.
* This function does not resize the memory allocation; it only changes the length
Expand All @@ -46,57 +33,6 @@ library Bytes {
*
************************************************************************/

/**
* @dev Sets a specific byte in a bytes32 value.
* @param input The bytes32 data in which a specific byte is to be altered.
* @param index The position of the byte to be altered. Index starts from the left.
* @param overwrite The new byte value to be set at the specified index.
* @return output The modified bytes32 data with the new byte value at the specified index.
*/
function setBytes1(bytes32 input, uint256 index, bytes1 overwrite) internal pure returns (bytes32 output) {
bytes1 mask = 0xff;
assembly {
mask := shr(mul(8, index), mask) // create a mask by shifting 0xff right by index bytes
output := and(input, not(mask)) // zero out the byte at index
output := or(output, shr(mul(8, index), overwrite)) // set the byte at index
}
return output;
}

/**
* @dev Sets a specific 2-byte sequence in a bytes32 variable.
* @param input The bytes32 data in which a specific 2-byte sequence is to be altered.
* @param index The position of the 2-byte sequence to be altered. Index starts from the left.
* @param overwrite The new 2-byte value to be set at the specified index.
* @return output The modified bytes32 data with the new 2-byte value at the specified index.
*/
function setBytes2(bytes32 input, uint256 index, bytes2 overwrite) internal pure returns (bytes32 output) {
bytes2 mask = 0xffff;
assembly {
mask := shr(mul(8, index), mask) // create a mask by shifting 0xffff right by index bytes
output := and(input, not(mask)) // zero out the byte at index
output := or(output, shr(mul(8, index), overwrite)) // set the byte at index
}
return output;
}

/**
* @dev Sets a specific 4-byte sequence in a bytes32 variable.
* @param input The bytes32 data in which a specific 4-byte sequence is to be altered.
* @param index The position of the 4-byte sequence to be altered. Index starts from the left.
* @param overwrite The new 4-byte value to be set at the specified index.
* @return output The modified bytes32 data with the new 4-byte value at the specified index.
*/
function setBytes4(bytes32 input, uint256 index, bytes4 overwrite) internal pure returns (bytes32 output) {
bytes4 mask = 0xffffffff;
assembly {
mask := shr(mul(8, index), mask) // create a mask by shifting 0xffffffff right by index bytes
output := and(input, not(mask)) // zero out the byte at index
output := or(output, shr(mul(8, index), overwrite)) // set the byte at index
}
return output;
}

/**
* @dev Sets a specific 4-byte sequence in a bytes blob at a given index.
* @param input The bytes blob in which a specific 4-byte sequence is to be altered.
Expand All @@ -115,40 +51,6 @@ library Bytes {
return input;
}

/**
* @dev Sets a specific 5-byte sequence in a bytes32 variable.
* @param input The bytes32 data in which a specific 5-byte sequence is to be altered.
* @param index The position of the 5-byte sequence to be altered. Index starts from the left.
* @param overwrite The new 5-byte value to be set at the specified index.
* @return output The modified bytes32 data with the new 5-byte value at the specified index.
*/
function setBytes5(bytes32 input, uint256 index, bytes5 overwrite) internal pure returns (bytes32 output) {
bytes5 mask = bytes5(type(uint40).max);
assembly {
mask := shr(mul(8, index), mask) // create a mask by shifting 0xff...ff right by index bytes
output := and(input, not(mask)) // zero out the byte at index
output := or(output, shr(mul(8, index), overwrite)) // set the byte at index
}
return output;
}

/**
* @dev Sets a specific 7-byte sequence in a bytes32 variable.
* @param input The bytes32 data in which a specific 7-byte sequence is to be altered.
* @param index The position of the 7-byte sequence to be altered. Index starts from the left.
* @param overwrite The new 7-byte value to be set at the specified index.
* @return output The modified bytes32 data with the new 7-byte value at the specified index.
*/
function setBytes7(bytes32 input, uint256 index, bytes7 overwrite) internal pure returns (bytes32 output) {
bytes7 mask = bytes7(type(uint56).max);
assembly {
mask := shr(mul(8, index), mask) // create a mask by shifting 0xff...ff right by index bytes
output := and(input, not(mask)) // zero out the byte at index
output := or(output, shr(mul(8, index), overwrite)) // set the byte at index
}
return output;
}

/************************************************************************
*
* SLICE
Expand Down
73 changes: 0 additions & 73 deletions packages/store/test/Bytes.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,6 @@ import { GasReporter } from "@latticexyz/gas-report/src/GasReporter.sol";
import { Bytes } from "../src/Bytes.sol";

contract BytesTest is Test, GasReporter {
function testEquals() public {
bytes memory a = bytes("a");
bytes memory b = bytes("a");

startGasReport("compare equal bytes");
bool equals = Bytes.equals(a, b);
endGasReport();

assertTrue(equals);
}

function testEqualsFalse() public {
bytes memory a = bytes("a");
bytes memory b = bytes("b");

startGasReport("compare unequal bytes");
bool equals = Bytes.equals(a, b);
endGasReport();

assertFalse(equals);
}

function testEqualsFalseDiffLength() public {
bytes memory a = bytes("a");
bytes memory b = bytes("aa");
assertFalse(Bytes.equals(a, b));
}

// TODO: add tests for other sliceX functions
function testSlice3() public {
bytes memory a = new bytes(5);
Expand Down Expand Up @@ -64,51 +36,6 @@ contract BytesTest is Test, GasReporter {
assertEq(output, original);
}

function testSetBytes1() public {
bytes32 input = bytes32(0);

startGasReport("set bytes1 in bytes32");
bytes32 output = Bytes.setBytes1(input, 8, 0xff);
endGasReport();

assertEq(output, hex"0000000000000000ff");
assertEq(Bytes.setBytes1(input, 0, 0x01), bytes32(bytes1(0x01)));
assertEq(Bytes.setBytes1(input, 31, 0x01), bytes32(uint256(0x01)));
}

function testSetBytes2() public {
bytes32 input = bytes32(0);

startGasReport("set bytes2 in bytes32");
bytes32 output = Bytes.setBytes2(input, 8, 0xffff);
endGasReport();

assertEq(output, hex"0000000000000000ffff");
assertEq(Bytes.setBytes2(input, 0, 0xffff), bytes32(bytes2(0xffff)));
assertEq(Bytes.setBytes2(input, 30, 0xffff), bytes32(uint256(0xffff)));
}

function testSetBytes4() public {
bytes32 input = bytes32(0);

startGasReport("set bytes4 in bytes32");
bytes32 output = Bytes.setBytes4(input, 8, 0xffffffff);
endGasReport();

assertEq(output, hex"0000000000000000ffffffff");
assertEq(Bytes.setBytes4(input, 0, 0xffffffff), bytes32(bytes4(0xffffffff)));
assertEq(Bytes.setBytes4(input, 30, 0xffffffff), bytes32(uint256(0xffff)));
assertEq(Bytes.setBytes4(input, 28, 0xffffffff), bytes32(uint256(0xffffffff)));

bytes32 input2 = bytes32(0x0000000a000a0000000000000000000000000000000000000000000000000000);
bytes4 overwrite = bytes4(0x0000006d);

assertEq(
Bytes.setBytes4(input2, 0, overwrite),
bytes32(0x0000006d000a0000000000000000000000000000000000000000000000000000)
);
}

function testSetBytes4Memory() public {
bytes4 first = bytes4(0x1000000a);
bytes32 remainder = bytes32(keccak256("some data"));
Expand Down
4 changes: 2 additions & 2 deletions packages/store/test/StoreCore.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ contract StoreCoreTest is Test, StoreMock {
fieldLayout
);

assertTrue(Bytes.equals(staticData, loadedStaticData));
assertEq(staticData, loadedStaticData);
assertEq(_encodedLengths.unwrap(), bytes32(0));
assertEq(_dynamicData, "");
}
Expand Down Expand Up @@ -339,7 +339,7 @@ contract StoreCoreTest is Test, StoreMock {
fieldLayout
);

assertTrue(Bytes.equals(staticData, loadedStaticData));
assertEq(staticData, loadedStaticData);
assertEq(_encodedLengths.unwrap(), bytes32(0));
assertEq(_dynamicData, "");
}
Expand Down

0 comments on commit ee19186

Please sign in to comment.