Skip to content

Commit

Permalink
benchmark: add atomic counter
Browse files Browse the repository at this point in the history
  • Loading branch information
hinto-janai committed Sep 30, 2022
1 parent dd9ca58 commit ae5e548
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 59 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "monero-vanity"
version = "0.2.0"
version = "0.3.0"
edition = "2021"

# Build with [RUSTFLAGS="-C target-cpu=native" cargo build --profile optimized]
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Enter the private key and the generated wallet will have the address found.
- [Rust regex is allowed in any mode](https://docs.rs/regex/latest/regex/#syntax)
- All characters must be ASCII, Unicode, or a regex pattern
- `I`, `O`, `l`, `0`, `+`, `/` are invalid characters in [Monero addresses](https://monerodocs.org/cryptography/base58)
- Using slightly less than max threads might be faster (see `Benchmark` below for more info)

**Safety:**
- Normal: Private key is generated by reducing a 256-bit integer
Expand All @@ -67,15 +68,17 @@ Enter the private key and the generated wallet will have the address found.
- Full: `4...hinto` would match if `^.*hinto$` was typed

## Benchmark
Start with this flag to start benchmark mode:
For speed reasons, monero-vanity does lazy iteration counting (current thread tries * threads used). This can be inaccurate because threads can run at very different speeds. This benchmark mode will use an atomic counter, giving 100% accuracy. You can use this mode to more accurately gauge your CPUs performance using different amount of threads. Using slightly less than max threads may actually be faster (28 threads seems like the sweet spot for a Ryzen 5950x).

Start benchmark mode with this flag:
```
./monero-vanity --benchmark
```
**Then enter:**
1. How many threads to use
4. How many iterations

monero-vanity will look for an impossible address (`^..l.*$`) using 1-43th character + 256-bit RNG settings until a thread has reached the target iteration and print some benchmark stats.
monero-vanity will look for an impossible address (`^..l.*$`) using 1-43th character + 256-bit RNG settings until all threads have reached the target iteration, and print some benchmark stats.

## Build
Normal:
Expand Down
47 changes: 0 additions & 47 deletions build_all.sh

This file was deleted.

26 changes: 26 additions & 0 deletions build_sign.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/usr/bin/env bash

set -e

[[ $PWD = */monero-vanity ]]

echo "=== Removing old /tmp/ build folders ==="
rm -rf /tmp/monero-vanity-${VER}*

VER="v$(grep "version" Cargo.toml | awk '{print $3}' | tr -d '"')"
TMP=$(mktemp -d /tmp/monero-vanity-${VER}.XXXX)
echo "=== \$TMP: $TMP ==="
echo "=== \$VER: $VER ==="

echo "=== Building Linux x86_64 ==="
cargo build --profile optimized
mv target/optimized/monero-vanity $TMP/monero-vanity-${VER}-linux-x64

echo "=== Building Windows x86_64 ==="
cargo build --profile optimized --target x86_64-pc-windows-gnu
mv target/x86_64-pc-windows-gnu/optimized/monero-vanity.exe $TMP/monero-vanity-${VER}-windows-x64.exe

cd $TMP
echo "=== Hashing/Signing ==="
sha256sum monero-vanity-${VER}-linux-x64 monero-vanity-${VER}-windows-x64.exe | gpg --clearsign > $TMP/SHA256SUM
echo "Done."
27 changes: 19 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
use std::{io,env,thread};
use std::io::Write;
use std::process::exit;
//use std::sync::{Arc, Mutex};
use std::time::{Instant,Duration};
use std::sync::{Arc,Mutex};
use std::time::Instant;
use num_cpus;
use regex::Regex;
use rand::Rng;
use rand::rngs::OsRng;
use num_format::{Locale, ToFormattedString};
use num_format::{Locale,ToFormattedString};
use monero::{PrivateKey,PublicKey,Address,Network,ViewPair,Hash};
use curve25519_dalek::scalar::Scalar;
use curve25519_dalek::constants::ED25519_BASEPOINT_TABLE;
Expand Down Expand Up @@ -258,7 +258,7 @@ fn test() {
// Ask user for interation count
let mut input = String::new();
let iter: u64;
print!("How many interations? ");
print!("How many iterations? (0 = 1,000,000) ");
io::stdout().flush().unwrap();
io::stdin().read_line(&mut input).expect("Failed read line");
// Handle input
Expand All @@ -278,9 +278,17 @@ fn test() {
let now = Instant::now(); // Set timer
let locale = &Locale::en; // 1000 formatter

// Atomic counters
let total_z: u64 = 0;
let done_z: u32 = 0;
let total = Arc::new(Mutex::new(total_z));
let done = Arc::new(Mutex::new(done_z));

println!("Testing [{}] iterations on [{}] threads...\n", iter.to_formatted_string(locale), threads.to_formatted_string(locale));
// Test
for i in 1..=threads {
let total_clone = Arc::clone(&total);
let done_clone = Arc::clone(&done);
thread::spawn(move|| {
let regex = &Regex::new("^..l.*$").unwrap(); // impossible pattern
base = base + (&Scalar::from(i) * &ED25519_BASEPOINT_TABLE);
Expand All @@ -293,11 +301,14 @@ fn test() {
base += offset;
}
let speed = iter as f64 / now.elapsed().as_secs_f64();
let full_speed = (speed * threads as f64) as u64;
println!("Thread: [{}], Time: [{:?}], Speed: [{} keys/sec]", i, now.elapsed().as_secs_f32(), (speed as u64).to_formatted_string(locale));
thread::sleep(Duration::from_millis(1000));
println!("\nFull speed: [{} keys/sec]", full_speed.to_formatted_string(locale));
exit(0);
*total_clone.lock().unwrap() += speed as u64;
*done_clone.lock().unwrap() += 1;
if *done_clone.lock().unwrap() == threads {
let full = *total_clone.lock().unwrap();
println!("\nFull speed: [{} keys/sec]", full.to_formatted_string(locale));
exit(0);
}
});
}
thread::park();
Expand Down

0 comments on commit ae5e548

Please sign in to comment.