Last active
October 2, 2021 16:32
-
-
Save Anish-Agnihotri/e771d573df0ff1d5264acec386bfeffa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Emulate solidity keccak256(abi.encodepacked(string)) | |
const { soliditySha3 } = require("web3-utils"); | |
// Expected winning hash | |
const hash = | |
"0xf8eec0d6db1f110801e2771e5effbb2d209720f67e5608d521f96d516387e65b"; | |
// Potential guesses to test | |
const guesses = { | |
red: ["the old guitarist"], | |
yellow: ["lance"], | |
orange: ["nobody"], | |
black: ["the new york sun"], | |
blue: ["1518"], | |
green: ["rhinoceros horn"], | |
brown: ["auberge ravoux"], | |
purple: ["guerrilla girls"], | |
}; | |
(() => { | |
// Loop over potential guesses | |
for (let a = 0; a < guesses.red.length; a++) { | |
for (let b = 0; b < guesses.yellow.length; b++) { | |
for (let c = 0; c < guesses.orange.length; c++) { | |
for (let d = 0; d < guesses.black.length; d++) { | |
for (let e = 0; e < guesses.blue.length; e++) { | |
for (let f = 0; f < guesses.green.length; f++) { | |
for (let g = 0; g < guesses.brown.length; g++) { | |
for (let h = 0; h < guesses.purple.length; h++) { | |
// Assuming colored order | |
let answer = | |
guesses.red[a] + | |
guesses.yellow[b] + | |
guesses.orange[c] + | |
guesses.black[d] + | |
guesses.blue[e] + | |
guesses.green[f] + | |
guesses.brown[g] + | |
guesses.purple[h]; | |
// Generate answer like front-end | |
answer = answer.toLowerCase(); | |
answer = answer.replace(/ /g, ""); | |
// Hash | |
const resp = soliditySha3(answer); | |
console.log(resp); | |
// Check if hash matches | |
if (resp.toLowerCase() === hash.toLowerCase()) { | |
console.log("ANSWER FOUND!"); | |
process.exit(1); | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
})(); |
Rust unoptimized parallel with rayon (~4.2m hash/second versus ~600k/second for single threaded):
/*
1. Make new project (`cargo new greats`)
2. Add deps:
hex = "0.4"
rayon = "1.5"
ethers = { git = "https://github.com/gakonst/ethers-rs" }
3. Build release (`cargo build --release`)
4. Run (`./target/release/greats`)
*/
use hex;
use rayon::prelude::*;
use ethers::utils::keccak256;
fn main() {
// Expected winning hash
let expected_hash = "0xf8eec0d6db1f110801e2771e5effbb2d209720f67e5608d521f96d516387e65b";
// Options
let red = ["test", "test", "test", "test", "test", "test"];
let green = ["test", "test", "test", "test", "test", "test"];
let orange = ["test", "test", "test", "test", "test", "test"];
let black = ["test", "test", "test", "test", "test", "test"];
let purple = ["test", "test", "test", "test", "test", "test"];
let yellow = ["test", "test", "test", "test", "test", "test"];
let brown = ["test", "test", "test", "test", "test", "test"];
let blue = ["test", "test", "test", "test", "test", "test"];
(0..red.len()).into_par_iter().for_each(|r_i| {
(0..yellow.len()).into_par_iter().for_each(|r_y| {
(0..orange.len()).into_par_iter().for_each(|r_o| {
(0..black.len()).into_par_iter().for_each(|r_b| {
(0..blue.len()).into_par_iter().for_each(|r_bl| {
(0..green.len()).into_par_iter().for_each(|r_g| {
(0..brown.len()).into_par_iter().for_each(|r_br| {
(0..purple.len()).into_par_iter().for_each(|r_p| {
// Generate answer
let mut answer = format!(
"{}{}{}{}{}{}{}{}",
red[r_i],
yellow[r_y],
orange[r_o],
black[r_b],
blue[r_bl],
green[r_g],
brown[r_br],
purple[r_p]
);
// Lowercase
answer = answer.to_lowercase();
// Remove spaces
answer = answer.replace(" ", "");
// Keccak hash
let hash = keccak256(answer.as_bytes());
// Render as hexstring
let hex_hash = format!("0x{}", hex::encode(&hash));
if hex_hash == expected_hash {
println!("SUCCESS!");
std::process::exit(0);
}
});
});
});
});
});
});
});
});
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Single-threaded Rust: