Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1e4064d

Browse files
authored
Add solution for QuillCTF 2022 - Challenge 6 (minaminao#10)
1 parent 93b0128 commit 1e4064d

File tree

4 files changed

+120
-6
lines changed

4 files changed

+120
-6
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
}

src/QuillCTF2022/README.md

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ Here's the link to the challenges page: https://quillctf.super.site/challenges.
55
---
66

77
**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)
1414

1515

1616
---
@@ -76,4 +76,16 @@ Goerli link: https://goerli.etherscan.io/address/0x971e55f02367dcdd1535a7faed0a5
7676

7777
```
7878
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
7991
```

0 commit comments

Comments
 (0)