Skip to content

Commit

Permalink
feat: finish task8.
Browse files Browse the repository at this point in the history
  • Loading branch information
Heemale committed Dec 7, 2024
1 parent 422436a commit 6acf54e
Show file tree
Hide file tree
Showing 9 changed files with 847 additions and 1 deletion.
1 change: 1 addition & 0 deletions mover/Heemale/code/task8/call.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sui client call --package 0x097a3833b6b5c62ca6ad10f0509dffdadff7ce31e1d86e63e884a14860cedc0f --module lets_move --function get_flag --args " 31591919" "Heemale" 0x19e76ca504c5a5fa5e214a45fca6c058171ba333f6da897b82731094504d5ab9 0x8
37 changes: 37 additions & 0 deletions mover/Heemale/code/task8/contract/Move.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[package]
name = "task8"
edition = "2024.beta" # edition = "legacy" to use legacy (pre-2024) Move
# license = "" # e.g., "MIT", "GPL", "Apache 2.0"
# authors = ["..."] # e.g., ["Joe Smith (joesmith@noemail.com)", "John Snow (johnsnow@noemail.com)"]

[dependencies]
Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "framework/testnet" }

# For remote import, use the `{ git = "...", subdir = "...", rev = "..." }`.
# Revision can be a branch, a tag, and a commit hash.
# MyRemotePackage = { git = "https://some.remote/host.git", subdir = "remote/path", rev = "main" }

# For local dependencies use `local = path`. Path is relative to the package root
# Local = { local = "../path/to" }

# To resolve a version conflict and force a specific version for dependency
# override use `override = true`
# Override = { local = "../conflicting/version", override = true }

[addresses]
task8 = "0x0"

# Named addresses will be accessible in Move as `@name`. They're also exported:
# for example, `std = "0x1"` is exported by the Standard Library.
# alice = "0xA11CE"

[dev-dependencies]
# The dev-dependencies section allows overriding dependencies for `--test` and
# `--dev` modes. You can introduce test-only dependencies here.
# Local = { local = "../path/to/dev-build" }

[dev-addresses]
# The dev-addresses section allows overwriting named addresses for the `--test`
# and `--dev` modes.
# alice = "0xB0B"

101 changes: 101 additions & 0 deletions mover/Heemale/code/task8/contract/sources/task8.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
module task8::task8 {
use std::ascii::{String, string};
use std::hash;
use sui::event;
use sui::bcs;
use sui::random;
use sui::random::Random;
use sui::transfer::share_object;
#[test_only]
use std::debug::print;

const EPROOF: u64 = 0;

public struct Flag has copy, drop {
sender: address,
flag: bool,
ture_num: u64,
github_id: String
}

public struct Challenge has key {
id: UID,
str: String,
difficulity: u64,
ture_num: u64
}

fun init(ctx: &mut TxContext) {
let flag_str = Challenge {
id: object::new(ctx),
str: string(b"LetsMoveCTF"),
difficulity: 3,
ture_num: 0,
};
share_object(flag_str);
}


entry fun get_flag(
proof: vector<u8>,
github_id: String,
challenge: &mut Challenge,
rand: &Random,
ctx: &mut TxContext
) {
let mut full_proof: vector<u8> = vector::empty<u8>();
vector::append<u8>(&mut full_proof, proof);
vector::append<u8>(&mut full_proof, tx_context::sender(ctx).to_bytes());
vector::append<u8>(&mut full_proof, bcs::to_bytes(challenge));

let hash: vector<u8> = hash::sha3_256(full_proof);

let mut prefix_sum: u32 = 0;
let mut i: u64 = 0;
while (i < challenge.difficulity) {
prefix_sum = prefix_sum + (*vector::borrow(&hash, i) as u32);
i = i + 1;
};

assert!(prefix_sum == 0, EPROOF);

challenge.str = getRandomString(rand, ctx);
challenge.ture_num = challenge.ture_num + 1;

event::emit(Flag {
sender: tx_context::sender(ctx),
flag: true,
ture_num: challenge.ture_num,
github_id
});
}


fun getRandomString(rand: &Random, ctx: &mut TxContext): String {
let mut gen = random::new_generator(rand, ctx);

let mut str_len = random::generate_u8_in_range(&mut gen, 4, 30);

let mut rand: vector<u8> = b"";
while (str_len != 0) {
let rand_num = random::generate_u8_in_range(&mut gen, 34, 126);
vector::push_back(&mut rand, rand_num);
str_len = str_len - 1;
};

string(rand)
}

#[test_only]
public fun init_for_testing(ctx: &mut TxContext) {
let id = object::new(ctx);
print(&object::uid_to_address(&id));
let flag_str = Challenge {
id,
str: string(b"kR}Qd&*h]Ub?.ld}"),
difficulity: 3,
ture_num: 101,
};
share_object(flag_str);
}
}
56 changes: 56 additions & 0 deletions mover/Heemale/code/task8/contract/tests/task8_tests.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#[test_only]
module task8::task8_tests {
use sui::test_scenario::{Self, ctx};
use std::debug;
use std::bcs::{Self};
use std::hash::{Self};
use task8::task8::{Self, Challenge};

const ADMIN: address = @ADMIN;

#[test]
fun process_for_testing() {
let mut scenario = test_scenario::begin(ADMIN);

task8::init_for_testing(ctx(&mut scenario));
test_scenario::next_tx(&mut scenario, ADMIN);

let challenge = test_scenario::take_shared<Challenge>(&scenario);

let mut proof = b"31591919";

let proof_data = bcs::to_bytes(&proof);
let admin_data = ADMIN.to_bytes();
let challenge_data = bcs::to_bytes(&challenge);
debug::print(&proof_data);
debug::print(&admin_data);
debug::print(&challenge_data);

let mut full_proof: vector<u8> = vector::empty<u8>();
vector::append<u8>(&mut full_proof, proof_data);
vector::append<u8>(&mut full_proof, ADMIN.to_bytes());
vector::append<u8>(&mut full_proof, challenge_data);
debug::print(&full_proof);

let hash: vector<u8> = hash::sha3_256(full_proof);
debug::print(&hash);

let mut prefix_sum: u32 = 0;
let mut i: u64 = 0;
while (i < 3) {
// 需要前三位是0
prefix_sum = prefix_sum + (*vector::borrow(&hash, i) as u32);
debug::print(&prefix_sum);
i = i + 1;
};

if (prefix_sum == 0) {
debug::print(&1);
} else {
debug::print(&0);
};

test_scenario::return_shared(challenge);
test_scenario::end(scenario);
}
}
23 changes: 23 additions & 0 deletions mover/Heemale/code/task8/ts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "task8-ts",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "npx ts-node -r tsconfig-paths/register ./src/index.ts"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@mysten/bcs": "^1.1.1",
"@mysten/sui": "^1.16.0",
"@types/node": "^22.10.1",
"js-sha3": "^0.9.3",
"ts-node": "^10.9.2",
"typescript": "^5.7.2"
},
"devDependencies": {
"tsconfig-paths": "^4.2.0"
}
}
Loading

0 comments on commit 6acf54e

Please sign in to comment.