Skip to content

Commit

Permalink
got 'em 😮‍💨
Browse files Browse the repository at this point in the history
  • Loading branch information
0age committed Oct 20, 2024
1 parent 4cac73e commit a833675
Show file tree
Hide file tree
Showing 7 changed files with 590 additions and 150 deletions.
34 changes: 17 additions & 17 deletions src/TheCompact.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1044,7 +1044,7 @@ contract TheCompact is ITheCompact, ERC6909, Extsload {
uint256 nonce;
uint256 expires;

assembly {
assembly ("memory-safe") {
let allocatorSignaturePtr := add(calldataPointer, calldataload(calldataPointer))
allocatorSignature.offset := add(0x20, allocatorSignaturePtr)
allocatorSignature.length := calldataload(allocatorSignaturePtr)
Expand All @@ -1063,7 +1063,7 @@ contract TheCompact is ITheCompact, ERC6909, Extsload {
address allocator = allocatorId.fromRegisteredAllocatorIdWithConsumed(nonce);

bytes32 domainSeparator = _INITIAL_DOMAIN_SEPARATOR.toLatest(_INITIAL_CHAIN_ID);
assembly {
assembly ("memory-safe") {
sponsorDomainSeparator := add(sponsorDomainSeparator, mul(iszero(sponsorDomainSeparator), domainSeparator))
}

Expand Down Expand Up @@ -1116,7 +1116,7 @@ contract TheCompact is ITheCompact, ERC6909, Extsload {
address claimant;
uint256 amount;

assembly {
assembly ("memory-safe") {
let calldataPointerWithOffset := add(calldataPointer, offsetToId)
id := calldataload(calldataPointerWithOffset)
allocatedAmount := calldataload(add(calldataPointerWithOffset, 0x20))
Expand All @@ -1143,7 +1143,7 @@ contract TheCompact is ITheCompact, ERC6909, Extsload {
uint256 allocatedAmount;
SplitComponent[] calldata claimants;

assembly {
assembly ("memory-safe") {
let calldataPointerWithOffset := add(calldataPointer, offsetToId)
id := calldataload(calldataPointerWithOffset)
allocatedAmount := calldataload(add(calldataPointerWithOffset, 0x20))
Expand All @@ -1161,7 +1161,7 @@ contract TheCompact is ITheCompact, ERC6909, Extsload {
}

function _ensureValidScope(bytes32 sponsorDomainSeparator, uint256 id) internal pure {
assembly {
assembly ("memory-safe") {
if iszero(or(iszero(sponsorDomainSeparator), iszero(shr(255, id)))) {
// revert InvalidScope(id)
mstore(0, 0xa06356f5)
Expand All @@ -1181,7 +1181,7 @@ contract TheCompact is ITheCompact, ERC6909, Extsload {
) internal returns (bool) {
BatchClaimComponent[] calldata claims;
address claimant;
assembly {
assembly ("memory-safe") {
let calldataPointerWithOffset := add(calldataPointer, offsetToId)
let claimsPtr := add(calldataPointer, calldataload(calldataPointerWithOffset))
claims.offset := add(0x20, claimsPtr)
Expand All @@ -1196,7 +1196,7 @@ contract TheCompact is ITheCompact, ERC6909, Extsload {

uint256 totalClaims = claims.length;

assembly {
assembly ("memory-safe") {
if iszero(totalClaims) {
// revert InvalidBatchAllocation()
mstore(0, 0x3a03d3bb)
Expand Down Expand Up @@ -1231,7 +1231,7 @@ contract TheCompact is ITheCompact, ERC6909, Extsload {
_ensureValidScope(sponsorDomainSeparator, component.id);
}

assembly {
assembly ("memory-safe") {
// revert InvalidBatchAllocation()
mstore(0, 0x3a03d3bb)
revert(0x1c, 0x04)
Expand All @@ -1251,7 +1251,7 @@ contract TheCompact is ITheCompact, ERC6909, Extsload {
function(address, address, uint256, uint256) internal returns (bool) operation
) internal returns (bool) {
SplitBatchClaimComponent[] calldata claims;
assembly {
assembly ("memory-safe") {
let claimsPtr := add(calldataPointer, calldataload(add(calldataPointer, offsetToId)))
claims.offset := add(0x20, claimsPtr)
claims.length := calldataload(claimsPtr)
Expand Down Expand Up @@ -1279,7 +1279,7 @@ contract TheCompact is ITheCompact, ERC6909, Extsload {
_ensureValidScope(sponsorDomainSeparator, claims[i].id);
}

assembly {
assembly ("memory-safe") {
// revert InvalidBatchAllocation()
mstore(0, 0x3a03d3bb)
revert(0x1c, 0x04)
Expand Down Expand Up @@ -1394,22 +1394,22 @@ contract TheCompact is ITheCompact, ERC6909, Extsload {
returns (bool)
{
(bytes32 messageHash, bytes32 qualificationMessageHash) = claimPayload.toMessageHash();
return _processBatchClaimWithQualification.usingQualifiedBatchMultichainClaim()(messageHash, qualificationMessageHash, claimPayload, 0xe0, operation);
return _processBatchClaimWithQualification.usingQualifiedBatchMultichainClaim()(messageHash, qualificationMessageHash, claimPayload, 0x100, operation);
}

function _processBatchMultichainClaimWithWitness(BatchMultichainClaimWithWitness calldata claimPayload, function(address, address, uint256, uint256) internal returns (bool) operation)
internal
returns (bool)
{
return _processSimpleBatchClaim.usingBatchMultichainClaimWithWitness()(claimPayload.toMessageHash(), claimPayload, 0xe0, operation);
return _processSimpleBatchClaim.usingBatchMultichainClaimWithWitness()(claimPayload.toMessageHash(), claimPayload, 0x100, operation);
}

function _processQualifiedBatchMultichainClaimWithWitness(
QualifiedBatchMultichainClaimWithWitness calldata claimPayload,
function(address, address, uint256, uint256) internal returns (bool) operation
) internal returns (bool) {
(bytes32 messageHash, bytes32 qualificationMessageHash) = claimPayload.toMessageHash();
return _processBatchClaimWithQualification.usingQualifiedBatchMultichainClaimWithWitness()(messageHash, qualificationMessageHash, claimPayload, 0x120, operation);
return _processBatchClaimWithQualification.usingQualifiedBatchMultichainClaimWithWitness()(messageHash, qualificationMessageHash, claimPayload, 0x140, operation);
}

function _processExogenousQualifiedBatchMultichainClaim(
Expand All @@ -1418,7 +1418,7 @@ contract TheCompact is ITheCompact, ERC6909, Extsload {
) internal returns (bool) {
(bytes32 messageHash, bytes32 qualificationMessageHash) = claimPayload.toMessageHash();
return _processBatchClaimWithQualificationAndSponsorDomain.usingExogenousQualifiedBatchMultichainClaim()(
messageHash, qualificationMessageHash, claimPayload, 0x120, claimPayload.notarizedChainId.toNotarizedDomainSeparator(), operation
messageHash, qualificationMessageHash, claimPayload, 0x140, claimPayload.notarizedChainId.toNotarizedDomainSeparator(), operation
);
}

Expand All @@ -1427,7 +1427,7 @@ contract TheCompact is ITheCompact, ERC6909, Extsload {
function(address, address, uint256, uint256) internal returns (bool) operation
) internal returns (bool) {
return _processBatchClaimWithSponsorDomain.usingExogenousBatchMultichainClaimWithWitness()(
claimPayload.toMessageHash(), claimPayload, 0x120, claimPayload.notarizedChainId.toNotarizedDomainSeparator(), operation
claimPayload.toMessageHash(), claimPayload, 0x140, claimPayload.notarizedChainId.toNotarizedDomainSeparator(), operation
);
}

Expand All @@ -1437,7 +1437,7 @@ contract TheCompact is ITheCompact, ERC6909, Extsload {
) internal returns (bool) {
(bytes32 messageHash, bytes32 qualificationMessageHash) = claimPayload.toMessageHash();
return _processBatchClaimWithQualificationAndSponsorDomain.usingExogenousQualifiedBatchMultichainClaimWithWitness()(
messageHash, qualificationMessageHash, claimPayload, 0x160, claimPayload.notarizedChainId.toNotarizedDomainSeparator(), operation
messageHash, qualificationMessageHash, claimPayload, 0x180, claimPayload.notarizedChainId.toNotarizedDomainSeparator(), operation
);
}

Expand Down Expand Up @@ -1761,7 +1761,7 @@ contract TheCompact is ITheCompact, ERC6909, Extsload {
}
}

assembly {
assembly ("memory-safe") {
if errorBuffer {
// revert InvalidBatchAllocation()
mstore(0, 0x3a03d3bb)
Expand Down
4 changes: 2 additions & 2 deletions src/lib/ConsumerLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ library ConsumerLib {
// specific nonces are consumed for each account and can only be used once.
// NOTE: this function temporarily overwrites the free memory pointer, but
// restores it before returning.
assembly {
assembly ("memory-safe") {
let freeMemoryPointer := mload(0x40)

// slot: keccak256(_CONSUMER_NONCE_SCOPE ++ account ++ nonce[0:31])
Expand All @@ -35,7 +35,7 @@ library ConsumerLib {
}

function isConsumedBy(uint256 nonceToCheck, address account) internal view returns (bool consumed) {
assembly {
assembly ("memory-safe") {
let freeMemoryPointer := mload(0x40)

// slot: keccak256(_CONSUMER_NONCE_SCOPE ++ account ++ nonce[0:31])
Expand Down
22 changes: 11 additions & 11 deletions src/lib/EfficiencyLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ library EfficiencyLib {
// around the fact that solidity only evaluates both expressions in an && if
// the first expression evaluates to true, which requires a conditional jump.
function and(bool a, bool b) internal pure returns (bool c) {
assembly {
assembly ("memory-safe") {
c := and(a, b)
}
}
Expand All @@ -20,64 +20,64 @@ library EfficiencyLib {
// around the fact that solidity only evaluates both expressions in an || if
// the first expression evaluates to false, which requires a conditional jump.
function or(bool a, bool b) internal pure returns (bool c) {
assembly {
assembly ("memory-safe") {
c := or(a, b)
}
}

// NOTE: this function is only safe if the supplied uint256 is known to not
// have any dirty bits set (i.e. it is either 0 or 1).
function asBool(uint256 a) internal pure returns (bool b) {
assembly {
assembly ("memory-safe") {
b := a
}
}

function asSanitizedAddress(uint256 accountValue) internal pure returns (address account) {
// extract last 160 bits (can also just use a mask if contract size permits)
assembly {
assembly ("memory-safe") {
account := shr(96, shl(96, accountValue))
}
}

function isNullAddress(address account) internal pure returns (bool isNull) {
assembly {
assembly ("memory-safe") {
isNull := iszero(shl(96, account))
}
}

function asUint256(bool a) internal pure returns (uint256 b) {
assembly {
assembly ("memory-safe") {
b := a
}
}

function asUint256(uint8 a) internal pure returns (uint256 b) {
assembly {
assembly ("memory-safe") {
b := a
}
}

function asUint256(uint96 a) internal pure returns (uint256 b) {
assembly {
assembly ("memory-safe") {
b := a
}
}

function asUint256(Scope a) internal pure returns (uint256 b) {
assembly {
assembly ("memory-safe") {
b := a
}
}

function asUint256(address a) internal pure returns (uint256 b) {
assembly {
assembly ("memory-safe") {
b := a
}
}

function asUint256(ResetPeriod a) internal pure returns (uint256 b) {
assembly {
assembly ("memory-safe") {
b := a
}
}
Expand Down
Loading

0 comments on commit a833675

Please sign in to comment.