I have the following structure and want to compute its hash:
struct SomeStruct {
uint8 field1;
int24 field2;
uint256 field3;
}
I was expecting that due to packing the size of this structure is 64 bytes, as the first two fields fit in a single 32-byte slot. However I tested it with this code:
SomeStruct memory s = SomeStruct(0x13, 0x567890, type(uint256).max - 100000000000);
bytes32 h1 = keccak256(abi.encode(s));
bytes32 h2;
assembly("memory-safe") {
h2 := keccak256(s, mul(32, 3))
}
After this, both hashes h1
and h2
are equal. Does packing only apply to structs in storage rather than structs in other memory types?