Skip to content

Commit

Permalink
task7, task8
Browse files Browse the repository at this point in the history
  • Loading branch information
hdbejxnsk committed Aug 2, 2024
1 parent 9cd71ca commit 397864e
Show file tree
Hide file tree
Showing 8 changed files with 341 additions and 0 deletions.
34 changes: 34 additions & 0 deletions mover/djendjcn/code/task7/check_in/Move.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# @generated by Move, please check-in and do not edit manually.

[move]
version = 2
manifest_digest = "60ED2B08562863E03B32E589CB9D813832DBE08E722E777386028AACAA9C2B4A"
deps_digest = "F8BBB0CCB2491CA29A3DF03D6F92277A4F3574266507ACD77214D37ECA3F3082"
dependencies = [
{ name = "Sui" },
]

[[move.package]]
name = "MoveStdlib"
source = { git = "https://gitee.com/MystenLabs/sui.git", rev = "framework/testnet", subdir = "crates/sui-framework/packages/move-stdlib" }

[[move.package]]
name = "Sui"
source = { git = "https://gitee.com/MystenLabs/sui.git", rev = "framework/testnet", subdir = "crates/sui-framework/packages/sui-framework" }

dependencies = [
{ name = "MoveStdlib" },
]

[move.toolchain-version]
compiler-version = "1.27.2"
edition = "2024.beta"
flavor = "sui"

[env]

[env.testnet]
chain-id = "4c78adac"
original-published-id = "0x914099b4d1b4f5513acc8aaa4fdc1f67578522b81d818f61bae527d590c6d87d"
latest-published-id = "0x914099b4d1b4f5513acc8aaa4fdc1f67578522b81d818f61bae527d590c6d87d"
published-version = "1"
36 changes: 36 additions & 0 deletions mover/djendjcn/code/task7/check_in/Move.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[package]
name = "check_in"
edition = "2024.beta" # edition = "legacy" to use legacy (pre-2024) Move
# license = "" # e.g., "MIT", "GPL", "Apache 2.0"
# authors = ["..."] # e.g., ["Joe Smith ([email protected])", "John Snow ([email protected])"]

[dependencies]
Sui = { git = "https://gitee.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]
check_in = "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"
77 changes: 77 additions & 0 deletions mover/djendjcn/code/task7/check_in/sources/check_in.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
module check_in::check_in {
use std::ascii::{String, string};
use std::bcs;
use std::hash::sha3_256;
use std::vector;
use sui::event;
use sui::object;
use sui::random;
use sui::random::Random;
use sui::transfer::share_object;
use sui::tx_context::{Self, TxContext};

const ESTRING: u64 = 0;

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

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

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


entry fun get_flag(
flag: vector<u8>,
github_id: String,
flag_str: &mut FlagString,
rand: &Random,
ctx: &mut TxContext
) {
let mut bcs_flag = bcs::to_bytes(&flag_str.str);
vector::append<u8>(&mut bcs_flag, *github_id.as_bytes());

assert!(flag == sha3_256(bcs_flag), ESTRING);

flag_str.str = getRandomString(rand, ctx);

flag_str.ture_num = flag_str.ture_num + 1;

event::emit(Flag {
sender: tx_context::sender(ctx),
flag: true,
ture_num: flag_str.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)
}
}
19 changes: 19 additions & 0 deletions mover/djendjcn/code/task7/check_in/tests/check_in_tests.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
#[test_only]
module check_in::check_in_tests {
// uncomment this line to import the module
// use check_in::check_in;
const ENotImplemented: u64 = 0;
#[test]
fun test_check_in() {
// pass
}
#[test, expected_failure(abort_code = ::check_in::check_in_tests::ENotImplemented)]
fun test_check_in_fail() {
abort ENotImplemented
}
}
*/
34 changes: 34 additions & 0 deletions mover/djendjcn/code/task8/lets_move/Move.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# @generated by Move, please check-in and do not edit manually.

[move]
version = 2
manifest_digest = "786B91A5B97E30CFE4109DB806C7FDAA208AD34906C77AB899770D4D0AEB5DB1"
deps_digest = "F8BBB0CCB2491CA29A3DF03D6F92277A4F3574266507ACD77214D37ECA3F3082"
dependencies = [
{ name = "Sui" },
]

[[move.package]]
name = "MoveStdlib"
source = { git = "https://github.com/MystenLabs/sui.git", rev = "framework/testnet", subdir = "crates/sui-framework/packages/move-stdlib" }

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

dependencies = [
{ name = "MoveStdlib" },
]

[move.toolchain-version]
compiler-version = "1.26.2"
edition = "2024.beta"
flavor = "sui"

[env]

[env.testnet]
chain-id = "4c78adac"
original-published-id = "0x097a3833b6b5c62ca6ad10f0509dffdadff7ce31e1d86e63e884a14860cedc0f"
latest-published-id = "0x097a3833b6b5c62ca6ad10f0509dffdadff7ce31e1d86e63e884a14860cedc0f"
published-version = "1"
36 changes: 36 additions & 0 deletions mover/djendjcn/code/task8/lets_move/Move.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[package]
name = "lets_move"
edition = "2024.beta" # edition = "legacy" to use legacy (pre-2024) Move
# license = "" # e.g., "MIT", "GPL", "Apache 2.0"
# authors = ["..."] # e.g., ["Joe Smith ([email protected])", "John Snow ([email protected])"]

[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]
lets_move = "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"
86 changes: 86 additions & 0 deletions mover/djendjcn/code/task8/lets_move/sources/lets_move.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
module lets_move::lets_move {
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;

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)
}
}
19 changes: 19 additions & 0 deletions mover/djendjcn/code/task8/lets_move/tests/lets_move_tests.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
#[test_only]
module lets_move::lets_move_tests {
// uncomment this line to import the module
// use lets_move::lets_move;
const ENotImplemented: u64 = 0;
#[test]
fun test_lets_move() {
// pass
}
#[test, expected_failure(abort_code = ::lets_move::lets_move_tests::ENotImplemented)]
fun test_lets_move_fail() {
abort ENotImplemented
}
}
*/

0 comments on commit 397864e

Please sign in to comment.