Skip to content

Commit 45b3fbc

Browse files
committed
Add fixture for double negation optimisation
1 parent 0a48bbc commit 45b3fbc

2 files changed

Lines changed: 71 additions & 0 deletions

File tree

‎packages/cashc/test/generation/fixtures.ts‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2010,4 +2010,53 @@ export const fixtures: Fixture[] = [
20102010
fingerprint: '3ab69a954ec4bf9ceeb58eceb8ead206195032edf6627155eb82f528403c94dd',
20112011
},
20122012
},
2013+
{
2014+
fn: 'double_negation.cash',
2015+
artifact: {
2016+
contractName: 'DoubleNegation',
2017+
constructorInputs: [{ name: 'flag', type: 'bool' }],
2018+
abi: [{ name: 'spend', inputs: [{ name: 'target', type: 'int' }] }],
2019+
bytecode:
2020+
// require(!!flag) - OP_NOT OP_NOT OP_VERIFY is optimised to OP_VERIFY
2021+
'OP_DUP OP_VERIFY '
2022+
// int i = 0; bool done = false;
2023+
+ 'OP_0 OP_0 '
2024+
// do { i = i + 1;
2025+
+ 'OP_BEGIN OP_OVER OP_1ADD OP_ROT OP_DROP OP_SWAP '
2026+
// done = i >= target;
2027+
+ 'OP_OVER OP_4 OP_PICK OP_GREATERTHANOREQUAL OP_NIP '
2028+
// } while (!done) - OP_NOT OP_NOT OP_UNTIL is optimised to OP_UNTIL
2029+
+ 'OP_DUP OP_UNTIL '
2030+
// if (!!flag) - OP_NOT OP_NOT OP_IF becomes OP_NOT OP_NOTIF, which is optimised to OP_IF
2031+
+ 'OP_ROT OP_IF '
2032+
// require(i == target); }
2033+
+ 'OP_OVER OP_3 OP_PICK OP_NUMEQUALVERIFY OP_ENDIF '
2034+
// require(i > 0)
2035+
+ 'OP_SWAP OP_0 OP_GREATERTHAN '
2036+
// clean up i and done
2037+
+ 'OP_NIP OP_NIP',
2038+
debug: {
2039+
bytecode: '7669000065788b7b757c785479a27776667b637853799d687c00a07777',
2040+
sourceMap: '4:18:4:22;:8::24:1;6:16:6:17:0;7:20:7:25;9:8:13:24;10:16:10:17;:::21:1;:12::22;;;11:19:11:20:0;:24::30;;:19:::1;:12::31;13:18:13:22:0;9:8::24:1;16:14:16:18:0;:12:18:9;17:20:17:21;:25::31;;:12::33:1;16:20:18:9;20:16:20:17:0;:20::21;:8::23:1;2:31:21:5;',
2041+
logs: [],
2042+
requires: [
2043+
{ ip: 2, line: 4 },
2044+
{ ip: 23, line: 17 },
2045+
{ ip: 28, line: 20 },
2046+
],
2047+
sourceTags: '27:28:sc',
2048+
},
2049+
source: fs.readFileSync(new URL('../valid-contract-files/double_negation.cash', import.meta.url), { encoding: 'utf-8' }),
2050+
compiler: {
2051+
name: 'cashc',
2052+
version,
2053+
options: {
2054+
enforceFunctionParameterTypes: true,
2055+
enforceLocktimeGuard: true,
2056+
},
2057+
},
2058+
updatedAt: '',
2059+
fingerprint: 'ed0b5bc35f0130fa04d1fce813fbd7c183bb5346006d10c0808f0d9872712ded',
2060+
},
2061+
},
20132062
];
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
contract DoubleNegation(bool flag) {
2+
function spend(int target) {
3+
// OP_NOT OP_NOT OP_VERIFY => OP_VERIFY
4+
require(!!flag);
5+
6+
int i = 0;
7+
bool done = false;
8+
9+
do {
10+
i = i + 1;
11+
done = i >= target;
12+
// OP_NOT OP_NOT OP_UNTIL => OP_UNTIL
13+
} while (!done);
14+
15+
// OP_NOT OP_NOT OP_IF => OP_NOT OP_NOTIF => OP_IF
16+
if (!!flag) {
17+
require(i == target);
18+
}
19+
20+
require(i > 0);
21+
}
22+
}

0 commit comments

Comments
 (0)