1
0

Code formatting

This commit is contained in:
Jon Michael Aanes 2024-03-10 20:53:56 +01:00
parent 8ff84c66db
commit b066738593
Signed by: Jmaa
SSH Key Fingerprint: SHA256:Ab0GfHGCblESJx7JRE4fj4bFy/KRpeLhi41y4pF3sNA

View File

@ -1,14 +1,12 @@
package jmaa.pbc;
import java.util.List;
import com.partisiablockchain.BlockchainAddress;
import com.partisiablockchain.crypto.KeyPair;
import java.math.BigInteger;
import java.util.ArrayList;
import com.partisiablockchain.BlockchainAddress;
import com.partisiablockchain.crypto.BlockchainPublicKey;
import com.partisiablockchain.crypto.KeyPair;
import java.util.List;
/** Search for PBC vanity addresses.
*/
/** Search for PBC vanity addresses. */
public final class PbcVanityAddressSearcher {
private static final int NUM_THREADS = 8;
@ -16,16 +14,17 @@ public final class PbcVanityAddressSearcher {
private static final long INITIAL_PING_INTERVAL = 10_000;
public static void main(String[] args) throws InterruptedException {
final AddressRequirements requirements = parseArgumentsToRequirements(args);
final AddressRequirements requirements = parseArgumentsToRequirements(args);
searchForPrivateKeyWithVanityAddress(requirements);
}
private static void searchForPrivateKeyWithVanityAddress(AddressRequirements addressRequirements) throws InterruptedException {
System.out.println("Searching for private key: %s".formatted(addressRequirements));
System.out.println("Difficulty: 1/%s".formatted(addressRequirements.difficulty()));
private static void searchForPrivateKeyWithVanityAddress(AddressRequirements addressRequirements)
throws InterruptedException {
System.out.println("Searching for private key: %s".formatted(addressRequirements));
System.out.println("Difficulty: 1/%s".formatted(addressRequirements.difficulty()));
List<Thread> searchers = new ArrayList<>();
for(int threadIdx=0;threadIdx<NUM_THREADS ;threadIdx++) {
for (int threadIdx = 0; threadIdx < NUM_THREADS; threadIdx++) {
final Thread searcher = new Thread(new AddressSearcherThread(threadIdx, addressRequirements));
searcher.start();
searchers.add(searcher);
@ -42,49 +41,58 @@ public final class PbcVanityAddressSearcher {
* @param threadIx Id of thread
* @param addressRequirements Requirements for the current search.
*/
private record AddressSearcherThread(int threadIdx, AddressRequirements addressRequirements) implements Runnable {
private record AddressSearcherThread(int threadIdx, AddressRequirements addressRequirements)
implements Runnable {
@Override
public void run() {
long start = System.nanoTime();
long pingInterval = INITIAL_PING_INTERVAL;
for(int iterationIdx=1;iterationIdx<MAX_ITERATIONS_PER_THREAD;iterationIdx++) {
// Generate random keypair
for (int iterationIdx = 1; iterationIdx < MAX_ITERATIONS_PER_THREAD; iterationIdx++) {
// Generate random keypair
final KeyPair keyPair = new KeyPair();
final BlockchainAddress address = keyPair.getPublic().createAddress();
// Output if vain enough
if(addressRequirements.satisfiedBy(address)) {
System.out.println("[%2d:%10d] match: addr=%s - pk=%s".formatted(threadIdx, iterationIdx, address.writeAsString(), keyPair.getPrivateKey().toString(16)));
if (addressRequirements.satisfiedBy(address)) {
System.out.println(
"[%2d:%10d] match: addr=%s - pk=%s"
.formatted(
threadIdx,
iterationIdx,
address.writeAsString(),
keyPair.getPrivateKey().toString(16)));
}
// Ping
if (iterationIdx % pingInterval == 0) {
if (iterationIdx % pingInterval == 0) {
final long now = System.nanoTime();
final long duration = (now - start) / 1_000_000_000;
System.out.println("[%2d:%10d] ping at %d sec".formatted(threadIdx, iterationIdx, duration));
System.out.println(
"[%2d:%10d] ping at %d sec".formatted(threadIdx, iterationIdx, duration));
pingInterval = pingInterval * 2;
}
}
}
}
private static AddressRequirements parseArgumentsToRequirements(String[] args) {
String prefix = "00";
String suffix = "";
for (int idx = 0; idx < args.length; idx++) {
final String word = args[idx];
System.out.println("%s : %s".formatted(idx, word));
if ("--prefix".equals(word)) {
prefix = args[++idx];;
} else if ("--suffix".equals(word)) {
suffix = args[++idx];
} else {
throw new RuntimeException("Unknown argument: " + word);
}
private static AddressRequirements parseArgumentsToRequirements(String[] args) {
String prefix = "00";
String suffix = "";
for (int idx = 0; idx < args.length; idx++) {
final String word = args[idx];
System.out.println("%s : %s".formatted(idx, word));
if ("--prefix".equals(word)) {
prefix = args[++idx];
;
} else if ("--suffix".equals(word)) {
suffix = args[++idx];
} else {
throw new RuntimeException("Unknown argument: " + word);
}
}
return new AddressRequirements(prefix, suffix);
return new AddressRequirements(prefix, suffix);
}
/**
@ -95,25 +103,21 @@ public final class PbcVanityAddressSearcher {
*/
private record AddressRequirements(String prefix, String suffix) {
public AddressRequirements {
if (!prefix.startsWith("00")) {
throw new IllegalArgumentException("Vanity prefix must start with 00.");
}
public AddressRequirements {
if (!prefix.startsWith("00")) {
throw new IllegalArgumentException("Vanity prefix must start with 00.");
}
}
/**
* Whether the given address satisfies the requirements.
*/
public boolean satisfiedBy(BlockchainAddress address) {
final String addressAsString = address.writeAsString();
return addressAsString.startsWith(prefix) && addressAsString.endsWith(suffix);
}
/** Whether the given address satisfies the requirements. */
public boolean satisfiedBy(BlockchainAddress address) {
final String addressAsString = address.writeAsString();
return addressAsString.startsWith(prefix) && addressAsString.endsWith(suffix);
}
/**
* Attempts to measure the "one-in-X" factor.
*/
public BigInteger difficulty() {
return BigInteger.ONE.shiftLeft(4*(prefix.length()-2 + suffix.length()));
}
/** Attempts to measure the "one-in-X" factor. */
public BigInteger difficulty() {
return BigInteger.ONE.shiftLeft(4 * (prefix.length() - 2 + suffix.length()));
}
}
}