-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SMTChecker: Fix equality of array literals
There are two kinds of array literals in Solidity: string literals (arrays of characters/bytes) and proper array literals (e.g., [1,2,3]). While array literals cannot be directly tested for equality in Solidity, it is possible to compute hash of these values and compare hashes. The expectation is that hashes of the same array literals would be the same, but previously SMTChecker returned false positive in this case, saying that they don't have to be equal. The reason for the false positive was the following. We represent Solidity array literal as a tuple `(elements, length)` where `length` is an integer representing the actualy length of the array literal, and `elements` are an SMT-LIB array, where the first `length` elements represent the actual content of the array literal. However, SMT-LIB arrays are infinite objects (more like functions from indices to elements). Previously, we left the part after `length`-th element unspecified. For the solver this meant that two array literals equal at the Solidity level were represented with two different SMT-LIB arrays. The proposed solution is to always start from a constant-zero array, and use store operations to build an SMT-LIB array that matches the Solidity array literal.
- Loading branch information
Showing
6 changed files
with
69 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
test/libsolidity/smtCheckerTests/crypto/crypto_functions_same_array.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
contract C { | ||
function c1() public pure { | ||
bytes32 k1 = keccak256(abi.encode([1])); | ||
bytes32 k2 = keccak256(abi.encode([1])); | ||
assert(k1 == k2); | ||
} | ||
|
||
function c2() public pure { | ||
bytes32 s1 = sha256(abi.encode([1,2])); | ||
bytes32 s2 = sha256(abi.encode([1,2])); | ||
assert(s1 == s2); | ||
} | ||
|
||
function c3() public pure { | ||
bytes32 r1 = ripemd160(abi.encode([1,2,3])); | ||
bytes32 r2 = ripemd160(abi.encode([1,2,3])); | ||
assert(r1 == r2); | ||
} | ||
} | ||
// ==== | ||
// SMTEngine: chc | ||
// ---- | ||
// Info 1391: CHC: 3 verification condition(s) proved safe! Enable the model checker option "show proved safe" to see all of them. |
23 changes: 23 additions & 0 deletions
23
test/libsolidity/smtCheckerTests/crypto/crypto_functions_same_string_literal.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
contract C { | ||
function c1() public pure { | ||
bytes32 k1 = keccak256("1"); | ||
bytes32 k2 = keccak256("1"); | ||
assert(k1 == k2); | ||
} | ||
|
||
function c2() public pure { | ||
bytes32 s1 = sha256("10"); | ||
bytes32 s2 = sha256("10"); | ||
assert(s1 == s2); | ||
} | ||
|
||
function c3() public pure { | ||
bytes32 r1 = ripemd160("100"); | ||
bytes32 r2 = ripemd160("100"); | ||
assert(r1 == r2); | ||
} | ||
} | ||
// ==== | ||
// SMTEngine: chc | ||
// ---- | ||
// Info 1391: CHC: 3 verification condition(s) proved safe! Enable the model checker option "show proved safe" to see all of them. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters