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