From 26e37123f5c9ab55a01ab123b76dcef6b26571c1 Mon Sep 17 00:00:00 2001 From: b1ek Date: Tue, 1 Aug 2023 14:06:02 +1000 Subject: [PATCH] init repo --- .gitignore | 1 + Cargo.lock | 230 +++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 13 +++ examples/simple_pow.rs | 7 ++ src/gen.rs | 61 +++++++++++ src/hash.rs | 17 +++ src/lib.rs | 3 + src/num.rs | 25 +++++ 8 files changed, 357 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 examples/simple_pow.rs create mode 100644 src/gen.rs create mode 100644 src/hash.rs create mode 100644 src/lib.rs create mode 100644 src/num.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..d446d06 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,230 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "base64ct" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c3c1a368f70d6cf7302d78f8f7093da241fb8e8807c05cc9e51a125895a6d5b" + +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "cipher" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773f3b9af64447d2ce9850330c473515014aa235e6a783b02db81ff39e4a3dad" +dependencies = [ + "crypto-common", + "inout", +] + +[[package]] +name = "cpufeatures" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a17b76ff3a4162b0b27f354a0c87015ddad39d35f9c0c36607a3bdd175dde1f1" +dependencies = [ + "libc", +] + +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", + "subtle", +] + +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "getrandom" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be4136b2a15dd319360be1c07d9933517ccf0be8f16bf62a3bee4f0d618df427" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "hmac" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e" +dependencies = [ + "digest", +] + +[[package]] +name = "inout" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0c10553d664a4d0bcff9f4215d0aac67a639cc68ef660840afe309b807bc9f5" +dependencies = [ + "generic-array", +] + +[[package]] +name = "libc" +version = "0.2.147" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3" + +[[package]] +name = "password-hash" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "346f04948ba92c43e8469c1ee6736c7563d71012b17d40745260fe106aac2166" +dependencies = [ + "base64ct", + "rand_core", + "subtle", +] + +[[package]] +name = "pbkdf2" +version = "0.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8ed6a7761f76e3b9f92dfb0a60a6a6477c61024b775147ff0973a02653abaf2" +dependencies = [ + "digest", + "hmac", +] + +[[package]] +name = "powlib" +version = "0.1.0" +dependencies = [ + "rand", + "scrypt", +] + +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + +[[package]] +name = "salsa20" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97a22f5af31f73a954c10289c93e8a50cc23d971e80ee446f1f6f7137a088213" +dependencies = [ + "cipher", +] + +[[package]] +name = "scrypt" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0516a385866c09368f0b5bcd1caff3366aace790fcd46e2bb032697bb172fd1f" +dependencies = [ + "password-hash", + "pbkdf2", + "salsa20", + "sha2", +] + +[[package]] +name = "sha2" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "479fb9d862239e610720565ca91403019f2f00410f1864c5aa7479b950a76ed8" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + +[[package]] +name = "subtle" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc" + +[[package]] +name = "typenum" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba" + +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..a968d15 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "powlib" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[lib] +name = "powlib" + +[dependencies] +rand = "0.8.5" +scrypt = "0.11.0" diff --git a/examples/simple_pow.rs b/examples/simple_pow.rs new file mode 100644 index 0000000..0124a1b --- /dev/null +++ b/examples/simple_pow.rs @@ -0,0 +1,7 @@ + +use powlib::{self, gen::{POWRange, POWChallenge}}; + +fn main() { + let challenge = POWChallenge::make(POWRange::new(0, 20480)); + println!("{}", challenge.solve_singlethread()); +} \ No newline at end of file diff --git a/src/gen.rs b/src/gen.rs new file mode 100644 index 0000000..c9f4663 --- /dev/null +++ b/src/gen.rs @@ -0,0 +1,61 @@ + +use rand::random; + +use crate::{hash::hash_num, num::Num}; + +#[derive(Debug, Clone)] +pub struct POWRange { + min: u128, + max: u128 +} + +impl POWRange { + pub fn new(min: u128, max: u128) -> POWRange { + POWRange { min, max } + } +} + +#[derive(Debug)] +pub struct POWChallenge { + hash: [u8; 32], + range: POWRange +} + +impl POWChallenge { + pub fn new(hash: [u8; 32], range: POWRange) -> POWChallenge { + POWChallenge { hash, range } + } + + pub fn make(range: POWRange) -> POWChallenge { + let secret = (random::() % (range.max - range.min)) + range.min; + let secret = Num::new(secret); + POWChallenge { hash: hash_num(secret), range: range } + } + + pub fn make_from(num: Num, range: Option) -> POWChallenge { + POWChallenge { hash: hash_num(num), range: match range { + Some(v) => v, + None => POWRange { min: 0, max: num * 2 } + } } + } + + pub fn check(self: &POWChallenge, num: Num) -> bool { + hash_num(num) == self.hash + } + + pub fn chunk_solve(self: &POWChallenge, start: u128, end: u128) -> Option { + for i in start..end { + if self.check(Num::new(i)) { + return Some(i) + } + } + None + } + + pub fn solve_singlethread(self: &POWChallenge) -> u128 { + match self.chunk_solve(self.range.min, self.range.max) { + Some(v) => v, + None => panic!("Number not found in range") + } + } +} diff --git a/src/hash.rs b/src/hash.rs new file mode 100644 index 0000000..2723ab7 --- /dev/null +++ b/src/hash.rs @@ -0,0 +1,17 @@ +use scrypt::{scrypt, Params}; + +use crate::num::Num; + +const SALT: [u8; 0] = []; + +pub fn hash(data: &[u8], out: &mut [u8]) { + let params = Params::new(6, 6, 1, 32).unwrap(); + scrypt(data, &SALT.clone(), ¶ms, out).unwrap(); +} + +pub fn hash_num(num: Num) -> [u8; 32] { + let bnum = num.bytes(); + let mut out: [u8; 32] = [0; 32]; + hash(&bnum, &mut out); + out +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..59700e0 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,3 @@ +pub mod gen; +pub mod hash; +pub mod num; diff --git a/src/num.rs b/src/num.rs new file mode 100644 index 0000000..1997eac --- /dev/null +++ b/src/num.rs @@ -0,0 +1,25 @@ +#[derive(Clone, Copy)] +pub struct Num { + number: u128 +} + +impl Num { + pub fn new(num: u128) -> Num { + Num { number: num } + } + + pub fn bytes(self: &Num) -> [u8; 16] { + self.number.to_be_bytes() + } + + pub fn from_bytes(bytes: [u8; 16]) -> Num { + Num { number: u128::from_be_bytes(bytes) } + } +} + +impl std::ops::Mul for Num { + type Output = u128; + fn mul(self, rhs: u128) -> Self::Output { + self.number * rhs + } +} \ No newline at end of file