Clean dirty addresses and booleans (#5195)

Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com>
This commit is contained in:
cairo
2024-09-18 18:21:17 +02:00
committed by GitHub
parent 809ded806f
commit 3f901696f7
7 changed files with 50 additions and 7 deletions

View File

@ -1,4 +1,5 @@
const format = require('../format-lines');
const sanitize = require('../helpers/sanitize');
const { product } = require('../../helpers');
const { SIZES } = require('./Packing.opts');
@ -44,8 +45,8 @@ function pack_${left}_${right}(bytes${left} left, bytes${right} right) internal
left + right
} result) {
assembly ("memory-safe") {
left := and(left, shl(${256 - 8 * left}, not(0)))
right := and(right, shl(${256 - 8 * right}, not(0)))
left := ${sanitize.bytes('left', left)}
right := ${sanitize.bytes('right', right)}
result := or(left, shr(${8 * left}, right))
}
}
@ -55,7 +56,7 @@ const extract = (outer, inner) => `\
function extract_${outer}_${inner}(bytes${outer} self, uint8 offset) internal pure returns (bytes${inner} result) {
if (offset > ${outer - inner}) revert OutOfRangeAccess();
assembly ("memory-safe") {
result := and(shl(mul(8, offset), self), shl(${256 - 8 * inner}, not(0)))
result := ${sanitize.bytes('shl(mul(8, offset), self)', inner)}
}
}
`;
@ -64,7 +65,7 @@ const replace = (outer, inner) => `\
function replace_${outer}_${inner}(bytes${outer} self, bytes${inner} value, uint8 offset) internal pure returns (bytes${outer} result) {
bytes${inner} oldValue = extract_${outer}_${inner}(self, offset);
assembly ("memory-safe") {
value := and(value, shl(${256 - 8 * inner}, not(0)))
value := ${sanitize.bytes('value', inner)}
result := xor(self, shr(mul(8, offset), xor(oldValue, value)))
}
}