feat: add eth-address

This commit is contained in:
2025-06-25 00:34:06 +08:00
parent 9982409535
commit c966ad621d
4 changed files with 487 additions and 1 deletions

View File

@@ -36,6 +36,7 @@ Project or files:
│   ├── crypto2-demo │   ├── crypto2-demo
│   ├── curve25519 │   ├── curve25519
│   ├── efficient_sm2 │   ├── efficient_sm2
│   ├── eth-address
│   ├── highwayhash │   ├── highwayhash
│   ├── hmac_sha1 │   ├── hmac_sha1
│   ├── jose-test │   ├── jose-test
@@ -301,6 +302,6 @@ Project or files:
├── vec.rs ├── vec.rs
└── while.rs └── while.rs
273 directories, 38 files 274 directories, 38 files
``` ```

317
__crypto/eth-address/Cargo.lock generated Normal file
View File

@@ -0,0 +1,317 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "arrayvec"
version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50"
[[package]]
name = "bitcoin-io"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b47c4ab7a93edb0c7198c5535ed9b52b63095f4e9b45279c6736cec4b856baf"
[[package]]
name = "bitcoin_hashes"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb18c03d0db0247e147a21a6faafd5a7eb851c743db062de72018b6b7e8e4d16"
dependencies = [
"bitcoin-io",
"hex-conservative",
]
[[package]]
name = "bitflags"
version = "2.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967"
[[package]]
name = "byteorder"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
[[package]]
name = "cc"
version = "1.2.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d487aa071b5f64da6f19a3e848e3578944b726ee5a4854b82172f02aa876bfdc"
dependencies = [
"shlex",
]
[[package]]
name = "cfg-if"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9555578bc9e57714c812a1f84e4fc5b4d21fcb063490c624de019f7464c91268"
[[package]]
name = "crunchy"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "43da5946c66ffcc7745f48db692ffbb10a83bfe0afd96235c5c2a4fb23994929"
[[package]]
name = "eth-address"
version = "0.1.0"
dependencies = [
"hex",
"keccak-hash",
"num_cpus",
"rand",
"secp256k1",
]
[[package]]
name = "fixed-hash"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "835c052cb0c08c1acf6ffd71c022172e18723949c8282f2b9f27efbc51e64534"
dependencies = [
"static_assertions",
]
[[package]]
name = "getrandom"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26145e563e54f2cadc477553f1ec5ee650b00862f0a58bcd12cbdc5f0ea2d2f4"
dependencies = [
"cfg-if",
"libc",
"r-efi",
"wasi",
]
[[package]]
name = "hermit-abi"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc0fef456e4baa96da950455cd02c081ca953b141298e41db3fc7e36b1da849c"
[[package]]
name = "hex"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
[[package]]
name = "hex-conservative"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5313b072ce3c597065a808dbf612c4c8e8590bdbf8b579508bf7a762c5eae6cd"
dependencies = [
"arrayvec",
]
[[package]]
name = "keccak-hash"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3e1b8590eb6148af2ea2d75f38e7d29f5ca970d5a4df456b3ef19b8b415d0264"
dependencies = [
"primitive-types",
"tiny-keccak",
]
[[package]]
name = "libc"
version = "0.2.174"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1171693293099992e19cddea4e8b849964e9846f4acee11b3948bcc337be8776"
[[package]]
name = "num_cpus"
version = "1.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "91df4bbde75afed763b708b7eee1e8e7651e02d97f6d5dd763e89367e957b23b"
dependencies = [
"hermit-abi",
"libc",
]
[[package]]
name = "ppv-lite86"
version = "0.2.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9"
dependencies = [
"zerocopy",
]
[[package]]
name = "primitive-types"
version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d15600a7d856470b7d278b3fe0e311fe28c2526348549f8ef2ff7db3299c87f5"
dependencies = [
"fixed-hash",
"uint",
]
[[package]]
name = "proc-macro2"
version = "1.0.95"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d"
dependencies = [
"proc-macro2",
]
[[package]]
name = "r-efi"
version = "5.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f"
[[package]]
name = "rand"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9fbfd9d094a40bf3ae768db9361049ace4c0e04a4fd6b359518bd7b73a73dd97"
dependencies = [
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99d9a13982dcf210057a8a78572b2217b667c3beacbf3a0d8b454f6f82837d38"
dependencies = [
"getrandom",
]
[[package]]
name = "secp256k1"
version = "0.31.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c3c81b43dc2d8877c216a3fccf76677ee1ebccd429566d3e67447290d0c42b2"
dependencies = [
"bitcoin_hashes",
"rand",
"secp256k1-sys",
]
[[package]]
name = "secp256k1-sys"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcb913707158fadaf0d8702c2db0e857de66eb003ccfdda5924b5f5ac98efb38"
dependencies = [
"cc",
]
[[package]]
name = "shlex"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]]
name = "static_assertions"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
[[package]]
name = "syn"
version = "2.0.104"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17b6f705963418cdb9927482fa304bc562ece2fdd4f616084c50b7023b435a40"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "tiny-keccak"
version = "2.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c9d3793400a45f954c52e73d068316d76b6f4e36977e3fcebb13a2721e80237"
dependencies = [
"crunchy",
]
[[package]]
name = "uint"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "909988d098b2f738727b161a106cfc7cab00c539c2687a8836f8e565976fb53e"
dependencies = [
"byteorder",
"crunchy",
"hex",
"static_assertions",
]
[[package]]
name = "unicode-ident"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"
[[package]]
name = "wasi"
version = "0.14.2+wasi-0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9683f9a5a998d873c0d21fcbe3c083009670149a8fab228644b8bd36b2c48cb3"
dependencies = [
"wit-bindgen-rt",
]
[[package]]
name = "wit-bindgen-rt"
version = "0.39.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1"
dependencies = [
"bitflags",
]
[[package]]
name = "zerocopy"
version = "0.8.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1039dd0d3c310cf05de012d8a39ff557cb0d23087fd44cad61df08fc31907a2f"
dependencies = [
"zerocopy-derive",
]
[[package]]
name = "zerocopy-derive"
version = "0.8.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ecf5b4cc5364572d7f4c329661bcc82724222973f2cab6f050a4e5c22f75181"
dependencies = [
"proc-macro2",
"quote",
"syn",
]

View File

@@ -0,0 +1,11 @@
[package]
name = "eth-address"
version = "0.1.0"
edition = "2024"
[dependencies]
hex = "0.4.3"
keccak-hash = "0.11.0"
num_cpus = "1.17.0"
rand = "0.9.1"
secp256k1 = { version = "0.31.1", features = ["rand"] }

View File

@@ -0,0 +1,157 @@
use keccak_hash::keccak_256;
use secp256k1::{PublicKey, Secp256k1};
use std::{
sync::{Arc, Mutex},
thread,
};
fn main() {
let args = std::env::args();
if args.len() <= 1 {
println!("[ERROR] Need at least one argument, e.g. 0xff 0xffff ...");
return;
}
let continue_on_found = std::env::var("CONTINUE_ON_FOUND")
.map(|c| c.to_lowercase())
.map(|c| c == "1" || c == "true" || c == "yes" || c == "on")
.unwrap_or_else(|_| false);
let prefixes: Vec<String> = args.skip(1).collect();
println!("Prefixes: {:?}", prefixes);
if continue_on_found {
println!("CONTINUE_ON_FOUND is ON");
} else {
println!("CONTINUE_ON_FOUND is OFF");
}
let loop_count = std::env::var("LOOP_COUNT")
.map(|c| c.parse::<u64>())
.unwrap_or_else(|_| Ok(10_000_000_u64))
.unwrap_or_else(|_| 10_000_000_u64);
println!("LOOP_COUNT={}", loop_count);
let num_of_vcpus = num_cpus::get();
let num_of_phycpus = num_cpus::get_physical();
println!(
"You have {} vCPUs, from {} phyCPUs",
num_of_vcpus, num_of_phycpus
);
let concurrent_count = std::env::var("CONCURRENT_COUNT")
.map(|c| c.parse::<u64>())
.unwrap_or_else(|_| Ok(num_of_phycpus as u64))
.unwrap_or_else(|_| 1_u64);
println!("CONCURRENT_COUNT={}", concurrent_count);
let stop_flag = Arc::new(Mutex::new(false));
let mut handles = vec![];
for ind in 0..concurrent_count {
println!("- Running task {} of {}", ind, concurrent_count);
let the_stop_flag = Arc::clone(&stop_flag);
let prefixes = prefixes.clone();
let child = thread::spawn(move || {
run_one_task(prefixes, continue_on_found, loop_count, ind, the_stop_flag);
});
handles.push(child);
}
while let Some(h) = handles.pop() {
h.join().unwrap();
}
println!("Finished!");
}
fn run_one_task(
prefixes: Vec<String>,
continue_on_found: bool,
loop_count: u64,
ind: u64,
the_stop_flag: Arc<Mutex<bool>>,
) {
let secp = Secp256k1::new();
for i in 0..loop_count {
let (secret_key, public_key) = secp.generate_keypair(&mut rand::rng());
let s = make_eth_address(&public_key);
if i % 1_000 == 0 {
if *the_stop_flag.lock().unwrap() {
return;
}
if i > 0 && i % 100_000 == 0 {
println!("> {} - {}", ind, i);
}
}
let lower_s = s.to_ascii_lowercase();
if prefixes.iter().any(|p| lower_s.starts_with(p)) {
println!(">> {}\n{}", s, hex::encode(secret_key.as_ref()));
if !continue_on_found {
*the_stop_flag.lock().unwrap() = true;
break;
}
}
}
}
// reference: https://eips.ethereum.org/EIPS/eip-55
fn make_eth_address(public_key: &PublicKey) -> String {
let public_key_bytes = public_key.serialize_uncompressed().to_vec();
let mut output = [0_u8; 32];
keccak_256(&public_key_bytes[1..], &mut output);
let addr_without_prefix_0x = hex::encode(&output[32 - 20..]);
let mut addr_keccak_256_output = [0_u8; 32];
keccak_256(
addr_without_prefix_0x.as_bytes(),
&mut addr_keccak_256_output,
);
let addr_keccak_256_hex = hex::encode(&addr_keccak_256_output);
let mut addr = String::with_capacity(42);
addr.push_str("0x");
let addr_keccak_256_chars = addr_keccak_256_hex.chars().collect::<Vec<_>>();
let addr_chars = addr_without_prefix_0x.chars().collect::<Vec<_>>();
for i in 0..addr_chars.len() {
if addr_keccak_256_chars[i] >= '8' {
addr.push(addr_chars[i].to_ascii_uppercase());
} else {
addr.push(addr_chars[i])
}
}
addr
}
#[test]
fn test_make_eth_address() {
use secp256k1::{SecretKey};
for (private_key, eth_address) in [
(
"961fc1985ceca1e23b169103e041ecfa642ff8690a6ecc59d91f2dba648ab02b",
"0x0005119dEE1fD8763fE6ADBbc801484a3728F0db",
),
(
"2fa0db47c5939d7d4831d05dcdb329f6e7f7d5ca2c7247a666729aa513ea26bf",
"0x23aEf078e0997434Eb47E6d0B3C7770010473456",
),
(
"e6a8f02f9d308eca44b8437d6dc1d08101449ecef5851dfe5093bcf0bc21fa5b",
"0xef87E9712c412ef7B90804EbaB763FAe24E10123",
),
(
"3d541861865959e1e1c303d5c2a90084192415b61ead3fdb778047278011170b",
"0x1451449D05A3C232a543A995C2A38c07095f6666",
),
] {
let temp_key = hex::decode(private_key).unwrap();
let mut key_32_bytes = [0_u8; 32];
for i in 0..32 {
key_32_bytes[i] = temp_key[i];
}
let secp = Secp256k1::new();
let secret_key =
SecretKey::from_byte_array(key_32_bytes).expect("32 bytes, within curve order");
let public_key = PublicKey::from_secret_key(&secp, &secret_key);
assert_eq!(eth_address, make_eth_address(&public_key));
}
}