File tree Expand file tree Collapse file tree 4 files changed +120
-6
lines changed Expand file tree Collapse file tree 4 files changed +120
-6
lines changed Original file line number Diff line number Diff line change
1
+ #define macro MAIN() = takes (0) returns (0) {
2
+ 0x04 calldataload // [n]
3
+ 0x02 // [2,n]
4
+ dup2 // [n,2,n]
5
+ mod // [n%2,n]
6
+ iszero // [n%2==0,n]
7
+ handleEvenCase jumpi
8
+ 0x03 // [3, n]
9
+ mul // [3*n]
10
+ 0x1 // [1,3*n]
11
+ add // [1+3*n]
12
+ returnResult jump // return result
13
+
14
+ handleEvenCase:
15
+ 0x01 // [1,n]
16
+ shr // [n>>1] (div by 2)
17
+
18
+ returnResult:
19
+ returndatasize mstore
20
+ calldatasize returndatasize return
21
+
22
+ }
Original file line number Diff line number Diff line change
1
+ // SPDX-License-Identifier: MIT
2
+ pragma solidity ^ 0.8.15 ;
3
+
4
+ import "foundry-huff/HuffDeployer.sol " ;
5
+ import "forge-std/Test.sol " ;
6
+ import "forge-std/console.sol " ;
7
+
8
+ import {CollatzPuzzle} from "./challenge/CollatzPuzzle.sol " ;
9
+
10
+ interface ICollatzPuzzle {
11
+ function collatzIteration (uint256 n ) external pure returns (uint256 );
12
+ }
13
+
14
+ contract CollatzPuzzleSolved is Test {
15
+ CollatzPuzzle public puzzle;
16
+ ICollatzPuzzle public solution;
17
+
18
+ /// @dev Setup the testing environment.
19
+ function setUp () public {
20
+ puzzle = new CollatzPuzzle ();
21
+ solution = ICollatzPuzzle (
22
+ HuffDeployer.deploy (
23
+ "/QuillCTF2022/CollatzPuzzle/CollatzPuzzleSolution "
24
+ )
25
+ );
26
+ }
27
+
28
+ function testSolution () public {
29
+ assertEq (puzzle.callMe (address (solution)), true );
30
+ }
31
+ }
Original file line number Diff line number Diff line change
1
+ // SPDX-License-Identifier: MIT
2
+ pragma solidity ^ 0.8.0 ;
3
+
4
+ interface ICollatz {
5
+ function collatzIteration (uint256 n ) external pure returns (uint256 );
6
+ }
7
+
8
+ contract CollatzPuzzle is ICollatz {
9
+ function collatzIteration (uint256 n )
10
+ public
11
+ pure
12
+ override
13
+ returns (uint256 )
14
+ {
15
+ if (n % 2 == 0 ) {
16
+ return n / 2 ;
17
+ } else {
18
+ return 3 * n + 1 ;
19
+ }
20
+ }
21
+
22
+ function callMe (address addr ) external view returns (bool ) {
23
+ // check code size
24
+ uint256 size;
25
+ assembly {
26
+ size := extcodesize (addr)
27
+ }
28
+ require (size > 0 && size <= 32 , "bad code size! " );
29
+
30
+ // check results to be matching
31
+ uint256 p;
32
+ uint256 q;
33
+ for (uint256 n = 1 ; n < 200 ; n++ ) {
34
+ // local result
35
+ p = n;
36
+ for (uint256 i = 0 ; i < 5 ; i++ ) {
37
+ p = collatzIteration (p);
38
+ }
39
+ // your result
40
+ q = n;
41
+ for (uint256 i = 0 ; i < 5 ; i++ ) {
42
+ q = ICollatz (addr).collatzIteration {gas: 100 }(q);
43
+ }
44
+ require (p == q, "result mismatch! " );
45
+ }
46
+
47
+ return true ;
48
+ }
49
+ }
Original file line number Diff line number Diff line change @@ -5,12 +5,12 @@ Here's the link to the challenges page: https://quillctf.super.site/challenges.
5
5
---
6
6
7
7
** Table of Contents**
8
- - [ QuillHash CTF 2022 Solutions ] ( #quillhash-ctf-2022-solutions )
9
- - [ Road closed :] ( #road-closed )
10
- - [ VIP Bank :] ( #vip-bank )
11
- - [ Confidential Hash :] ( #confidential-hash )
12
- - [ SafeNFT :] ( #safenft )
13
- - [ D3l3g4t3 :] ( #d3l3g4t3 )
8
+ - [ Road closed: ] ( #road-closed )
9
+ - [ VIP Bank :] ( #vip-bank )
10
+ - [ Confidential Hash :] ( #confidential-hash )
11
+ - [ SafeNFT :] ( #safenft )
12
+ - [ D3l3g4t3 :] ( #d3l3g4t3 )
13
+ - [ CollatzPuzzle :] ( #collatzpuzzle )
14
14
15
15
16
16
---
@@ -76,4 +76,16 @@ Goerli link: https://goerli.etherscan.io/address/0x971e55f02367dcdd1535a7faed0a5
76
76
77
77
```
78
78
forge test --match-contract D3l3g4t3Solved -vvvv
79
+ ```
80
+
81
+ ## CollatzPuzzle:
82
+ https://quillctf.super.site/challenges/quillctf-challenges/collatz-puzzle
83
+
84
+ ** Objective** :
85
+ - Make a successful call to the callMe function.
86
+ - You should be the deployer of the contract at the given addr parameter!
87
+
88
+
89
+ ```
90
+ forge test -vvvv --match-contract CollatzPuzzleSolved
79
91
```
You can’t perform that action at this time.
0 commit comments