Skip to content

Conversation

@nkrishang
Copy link
Contributor

@nkrishang nkrishang commented Nov 20, 2023

See #563

Comment on lines +72 to +95
function toHexStringChecksummed(address value) internal pure returns (string memory str) {
str = toHexString(value);
/// @solidity memory-safe-assembly
assembly {
let mask := shl(6, div(not(0), 255)) // `0b010000000100000000 ...`
let o := add(str, 0x22)
let hashed := and(keccak256(o, 40), mul(34, mask)) // `0b10001000 ... `
let t := shl(240, 136) // `0b10001000 << 240`
for {
let i := 0
} 1 {

} {
mstore(add(i, i), mul(t, byte(i, hashed)))
i := add(i, 1)
if eq(i, 20) {
break
}
}
mstore(o, xor(mload(o), shr(1, and(mload(0x00), and(mload(o), mask)))))
o := add(o, 0x20)
mstore(o, xor(mload(o), shr(1, and(mload(0x20), and(mload(o), mask)))))
}
}

Check warning

Code scanning / Slither

Assembly usage

TWStrings.toHexStringChecksummed(address) (contracts/lib/TWStrings.sol#72-95) uses assembly - INLINE ASM (contracts/lib/TWStrings.sol#75-94)
Comment on lines +168 to +194
function toHexStringNoPrefix(bytes memory raw) internal pure returns (string memory str) {
/// @solidity memory-safe-assembly
assembly {
let length := mload(raw)
str := add(mload(0x40), 2) // Skip 2 bytes for the optional prefix.
mstore(str, add(length, length)) // Store the length of the output.

// Store "0123456789abcdef" in scratch space.
mstore(0x0f, 0x30313233343536373839616263646566)

let o := add(str, 0x20)
let end := add(raw, length)

for {

} iszero(eq(raw, end)) {

} {
raw := add(raw, 1)
mstore8(add(o, 1), mload(and(mload(raw), 15)))
mstore8(o, mload(and(shr(4, mload(raw)), 15)))
o := add(o, 2)
}
mstore(o, 0) // Zeroize the slot after the string.
mstore(0x40, add(o, 0x20)) // Allocate the memory.
}
}

Check warning

Code scanning / Slither

Assembly usage

TWStrings.toHexStringNoPrefix(bytes) (contracts/lib/TWStrings.sol#168-194) uses assembly - INLINE ASM (contracts/lib/TWStrings.sol#170-193)
Comment on lines +99 to +108
function toHexString(address value) internal pure returns (string memory str) {
str = toHexStringNoPrefix(value);
/// @solidity memory-safe-assembly
assembly {
let strLength := add(mload(str), 2) // Compute the length.
mstore(str, 0x3078) // Write the "0x" prefix.
str := sub(str, 2) // Move the pointer.
mstore(str, strLength) // Write the length.
}
}

Check warning

Code scanning / Slither

Assembly usage

TWStrings.toHexString(address) (contracts/lib/TWStrings.sol#99-108) uses assembly - INLINE ASM (contracts/lib/TWStrings.sol#102-107)
Comment on lines +155 to +164
function toHexString(bytes memory raw) internal pure returns (string memory str) {
str = toHexStringNoPrefix(raw);
/// @solidity memory-safe-assembly
assembly {
let strLength := add(mload(str), 2) // Compute the length.
mstore(str, 0x3078) // Write the "0x" prefix.
str := sub(str, 2) // Move the pointer.
mstore(str, strLength) // Write the length.
}
}

Check warning

Code scanning / Slither

Assembly usage

TWStrings.toHexString(bytes) (contracts/lib/TWStrings.sol#155-164) uses assembly - INLINE ASM (contracts/lib/TWStrings.sol#158-163)
Comment on lines +112 to +151
function toHexStringNoPrefix(address value) internal pure returns (string memory str) {
/// @solidity memory-safe-assembly
assembly {
str := mload(0x40)

// Allocate the memory.
// We need 0x20 bytes for the trailing zeros padding, 0x20 bytes for the length,
// 0x02 bytes for the prefix, and 0x28 bytes for the digits.
// The next multiple of 0x20 above (0x20 + 0x20 + 0x02 + 0x28) is 0x80.
mstore(0x40, add(str, 0x80))

// Store "0123456789abcdef" in scratch space.
mstore(0x0f, 0x30313233343536373839616263646566)

str := add(str, 2)
mstore(str, 40)

let o := add(str, 0x20)
mstore(add(o, 40), 0)

value := shl(96, value)

// We write the string from rightmost digit to leftmost digit.
// The following is essentially a do-while loop that also handles the zero case.
for {
let i := 0
} 1 {

} {
let p := add(o, add(i, i))
let temp := byte(i, value)
mstore8(add(p, 1), mload(and(temp, 15)))
mstore8(p, mload(shr(4, temp)))
i := add(i, 1)
if eq(i, 20) {
break
}
}
}
}

Check warning

Code scanning / Slither

Assembly usage

TWStrings.toHexStringNoPrefix(address) (contracts/lib/TWStrings.sol#112-151) uses assembly - INLINE ASM (contracts/lib/TWStrings.sol#114-150)
@nkrishang nkrishang enabled auto-merge (squash) November 20, 2023 19:52
@nkrishang nkrishang merged commit 49a573c into main Nov 20, 2023
@nkrishang nkrishang deleted the AA-benchmark-test branch November 20, 2023 20:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants