Commit 007e4fa3 authored by Alina's avatar Alina Committed by GitHub

OZ L09 Remove unnecessary use of assembly (#121)

* abi.encodePacked

* rm _malloc
parent 3e5fe501
...@@ -89,16 +89,7 @@ library Lib_RLPWriter { ...@@ -89,16 +89,7 @@ library Lib_RLPWriter {
bytes memory _out bytes memory _out
) )
{ {
_malloc(0x20); return writeBytes(abi.encodePacked(_in));
bytes memory inputBytes;
assembly {
let m := mload(0x40)
mstore(add(m, 20), xor(0x140000000000000000000000000000000000000000, _in))
mstore(0x40, add(m, 52))
inputBytes := m
}
return writeBytes(inputBytes);
} }
/** /**
...@@ -188,7 +179,7 @@ library Lib_RLPWriter { ...@@ -188,7 +179,7 @@ library Lib_RLPWriter {
* @return _binary RLP encoded bytes. * @return _binary RLP encoded bytes.
*/ */
function _toBinary( function _toBinary(
uint _x uint256 _x
) )
private private
pure pure
...@@ -196,12 +187,9 @@ library Lib_RLPWriter { ...@@ -196,12 +187,9 @@ library Lib_RLPWriter {
bytes memory _binary bytes memory _binary
) )
{ {
bytes memory b = new bytes(32); bytes memory b = abi.encodePacked(_x);
assembly {
mstore(add(b, 32), _x)
}
uint i = 0; uint256 i = 0;
for (; i < 32; i++) { for (; i < 32; i++) {
if (b[i] != 0) { if (b[i] != 0) {
break; break;
...@@ -209,7 +197,7 @@ library Lib_RLPWriter { ...@@ -209,7 +197,7 @@ library Lib_RLPWriter {
} }
bytes memory res = new bytes(32 - i); bytes memory res = new bytes(32 - i);
for (uint j = 0; j < res.length; j++) { for (uint256 j = 0; j < res.length; j++) {
res[j] = b[i++]; res[j] = b[i++];
} }
...@@ -292,22 +280,4 @@ library Lib_RLPWriter { ...@@ -292,22 +280,4 @@ library Lib_RLPWriter {
return flattened; return flattened;
} }
}
/** \ No newline at end of file
* Clears memory wherever the free memory is pointing.
* @param _numBytes Number of bytes to clear out (will be rounded up to nearest word).
*/
function _malloc(
uint _numBytes
)
private
pure
{
assembly {
let free_mem := mload(0x40)
for { let offset := 0x00 } lt(offset, _numBytes) { offset := add(offset, 0x20) } {
mstore(add(free_mem, offset), 0x00)
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment