Skip to content

Commit 3847050

Browse files
committed
Remove extra functions
1 parent e67e8b4 commit 3847050

File tree

3 files changed

+0
-141
lines changed

3 files changed

+0
-141
lines changed

contracts/utils/Memory.sol

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -30,57 +30,11 @@ library Memory {
3030
}
3131
}
3232

33-
/// @dev Returns a `Pointer` to the content of a `bytes` buffer. Skips the length word.
34-
function contentPointer(bytes memory buffer) internal pure returns (Pointer) {
35-
return addOffset(asPointer(buffer), 32);
36-
}
37-
38-
/**
39-
* @dev Copies `length` bytes from `srcPtr` to `destPtr`. Equivalent to https://www.evm.codes/?fork=cancun#5e[`mcopy`].
40-
*
41-
* WARNING: Reading or writing beyond the allocated memory bounds of either pointer
42-
* will result in undefined behavior and potential memory corruption.
43-
*/
44-
function copy(Pointer destPtr, Pointer srcPtr, uint256 length) internal pure {
45-
assembly ("memory-safe") {
46-
mcopy(destPtr, srcPtr, length)
47-
}
48-
}
49-
50-
/**
51-
* @dev Extracts a `bytes1` from a `Pointer`. `offset` starts from the most significant byte.
52-
*
53-
* NOTE: Will return `0x00` if `offset` is larger or equal to `32`.
54-
*/
55-
function loadByte(Pointer ptr, uint256 offset) internal pure returns (bytes1 v) {
56-
bytes32 word = load(ptr);
57-
assembly ("memory-safe") {
58-
v := byte(offset, word)
59-
}
60-
}
61-
62-
/// @dev Extracts a `bytes32` from a `Pointer`.
63-
function load(Pointer ptr) internal pure returns (bytes32 v) {
64-
assembly ("memory-safe") {
65-
v := mload(ptr)
66-
}
67-
}
68-
69-
/// @dev Adds an offset to a `Pointer`.
70-
function addOffset(Pointer ptr, uint256 offset) internal pure returns (Pointer) {
71-
return asPointer(bytes32(asUint256(ptr) + offset));
72-
}
73-
7433
/// @dev `Pointer` to `bytes32`. Expects a pointer to a properly ABI-encoded `bytes` object.
7534
function asBytes32(Pointer ptr) internal pure returns (bytes32) {
7635
return Pointer.unwrap(ptr);
7736
}
7837

79-
/// @dev `Pointer` to `uint256`. Expects a pointer to a properly ABI-encoded `bytes` object.
80-
function asUint256(Pointer ptr) internal pure returns (uint256) {
81-
return uint256(asBytes32(ptr));
82-
}
83-
8438
/// @dev `bytes32` to `Pointer`. Expects a pointer to a properly ABI-encoded `bytes` object.
8539
function asPointer(bytes32 value) internal pure returns (Pointer) {
8640
return Pointer.wrap(value);

test/utils/Memory.t.sol

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -18,50 +18,4 @@ contract MemoryTest is Test {
1818
ptr.asPointer().setFreeMemoryPointer();
1919
assertEq(Memory.getFreeMemoryPointer().asBytes32(), ptr);
2020
}
21-
22-
function testSymbolicContentPointer(uint256 seed) public pure {
23-
Memory.Pointer ptr = bytes32(bound(seed, START_PTR, END_PTR)).asPointer();
24-
assertEq(ptr.asBytes().contentPointer().asBytes32(), ptr.addOffset(32).asBytes32());
25-
}
26-
27-
function testCopy(bytes memory data, uint256 destSeed) public pure {
28-
uint256 minDestPtr = Memory.getFreeMemoryPointer().asUint256();
29-
Memory.Pointer destPtr = bytes32(bound(destSeed, minDestPtr, minDestPtr + END_PTR)).asPointer();
30-
destPtr.addOffset(data.length + 32).setFreeMemoryPointer();
31-
destPtr.copy(data.asPointer(), data.length + 32);
32-
bytes memory copiedData = destPtr.asBytes();
33-
assertEq(data.length, copiedData.length);
34-
for (uint256 i = 0; i < data.length; i++) {
35-
assertEq(data[i], copiedData[i]);
36-
}
37-
}
38-
39-
function testLoadByte(uint256 seed, uint256 index, bytes32 value) public pure {
40-
index = bound(index, 0, 31);
41-
Memory.Pointer ptr = bytes32(bound(seed, START_PTR, END_PTR)).asPointer();
42-
43-
assembly ("memory-safe") {
44-
mstore(ptr, value)
45-
}
46-
47-
bytes1 expected;
48-
assembly ("memory-safe") {
49-
expected := byte(index, value)
50-
}
51-
assertEq(ptr.loadByte(index), expected);
52-
}
53-
54-
function testLoad(uint256 seed, bytes32 value) public pure {
55-
Memory.Pointer ptr = bytes32(bound(seed, START_PTR, END_PTR)).asPointer();
56-
assembly ("memory-safe") {
57-
mstore(ptr, value)
58-
}
59-
assertEq(ptr.load(), value);
60-
}
61-
62-
function testSymbolicAddOffset(uint256 seed, uint256 offset) public pure {
63-
offset = bound(offset, 0, type(uint256).max - END_PTR);
64-
Memory.Pointer ptr = bytes32(bound(seed, START_PTR, END_PTR)).asPointer();
65-
assertEq(ptr.addOffset(offset).asUint256(), ptr.asUint256() + offset);
66-
}
6721
}

test/utils/Memory.test.js

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -26,39 +26,6 @@ describe('Memory', function () {
2626
});
2727
});
2828

29-
it('load extracts a word', async function () {
30-
const ptr = await this.mock.$getFreeMemoryPointer();
31-
await expect(this.mock.$load(ptr)).to.eventually.equal(ethers.toBeHex(0, 32));
32-
});
33-
34-
it('loadByte extracts a byte', async function () {
35-
const ptr = await this.mock.$getFreeMemoryPointer();
36-
await expect(this.mock.$loadByte(ptr, 0)).to.eventually.equal(ethers.toBeHex(0, 1));
37-
});
38-
39-
it('contentPointer', async function () {
40-
const data = ethers.toUtf8Bytes('hello world');
41-
const result = await this.mock.$contentPointer(data);
42-
expect(result).to.equal(ethers.toBeHex(0xa0, 32)); // 0x80 is the default free pointer (length)
43-
});
44-
45-
describe('addOffset', function () {
46-
it('addOffset', async function () {
47-
const basePtr = ethers.toBeHex(0x80, 32);
48-
const offset = 32;
49-
const expectedPtr = ethers.toBeHex(0xa0, 32);
50-
51-
await expect(this.mock.$addOffset(basePtr, offset)).to.eventually.equal(expectedPtr);
52-
});
53-
54-
it('addOffsetwraps around', async function () {
55-
const basePtr = ethers.toBeHex(0x80, 32);
56-
const offset = 256;
57-
const expectedPtr = ethers.toBeHex(0x180, 32);
58-
await expect(this.mock.$addOffset(basePtr, offset)).to.eventually.equal(expectedPtr);
59-
});
60-
});
61-
6229
describe('pointer conversions', function () {
6330
it('asBytes32 / asPointer', async function () {
6431
const ptr = ethers.toBeHex('0x1234', 32);
@@ -71,21 +38,5 @@ describe('Memory', function () {
7138
expect(ptr).to.equal(ethers.toBeHex(0x80, 32)); // Default free pointer
7239
await expect(this.mock.$asBytes(ptr)).to.eventually.equal(ethers.toBeHex(0x20, 32));
7340
});
74-
75-
it('asUint256', async function () {
76-
const value = 0x1234;
77-
const ptr = ethers.toBeHex(value, 32);
78-
await expect(this.mock.$asUint256(ptr)).to.eventually.equal(value);
79-
});
80-
});
81-
82-
describe('memory operations', function () {
83-
it('copy', async function () {
84-
await expect(this.mock.$copy(ethers.toBeHex(0x80, 32), ethers.toBeHex(0xc0, 32), 32)).to.not.be.reverted;
85-
});
86-
87-
it('copy with zero length', async function () {
88-
await expect(this.mock.$copy(ethers.toBeHex(0x80, 32), ethers.toBeHex(0xc0, 32), 0)).to.not.be.reverted;
89-
});
9041
});
9142
});

0 commit comments

Comments
 (0)