Skip to content

Commit

Permalink
v 0.14.0, threads for PERMUTATION_CHECK, minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
PawelGorny committed Oct 16, 2021
1 parent ddf2f1f commit 78d11a7
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 18 deletions.
13 changes: 13 additions & 0 deletions examples/example_22.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
PERMUTATION_CHECK
bc1q9fdjavqhzdttar9kjchpw3rerkzfs3zvgrwg9e
9
lady
oblige
cable
hidden
surface
marriage
grit
priority
forget
m/84'/0'/0'/0/0
17 changes: 8 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.pawelgorny</groupId>
<artifactId>lostword</artifactId>
<version>0.13.3</version>
<version>0.14.0</version>
<packaging>jar</packaging>

<dependencies>
Expand All @@ -23,17 +23,17 @@
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>30.0-jre</version>
<version>31.0.1-jre</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.26</version>
<version>1.7.32</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
<version>2.8.8</version>
</dependency>

<dependency>
Expand All @@ -50,21 +50,21 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<finalName>lostWord</finalName>
</configuration>
<version>3.2.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
Expand All @@ -74,7 +74,6 @@
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>lostWord</finalName>
</configuration>
<executions>
<execution>
Expand Down
76 changes: 67 additions & 9 deletions src/main/java/com/pawelgorny/lostword/WorkerPermutationCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@

import org.bitcoinj.core.Utils;
import org.bitcoinj.crypto.MnemonicException;
import org.bouncycastle.crypto.macs.HMac;

import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class WorkerPermutationCheck extends Worker{

Expand All @@ -28,15 +33,67 @@ public void run() throws InterruptedException, MnemonicException {
String[] words = new String[0];
words = configuration.getWORDS().toArray(words);
start = System.currentTimeMillis();
printAllRecursive(configuration.getSIZE(), words);
THREADS = Math.min(THREADS, configuration.getSIZE());
List<List<String>> workPerThread = new ArrayList<>(THREADS);
int tIx=0;
for(String w:words){
if (workPerThread.size()==tIx){
workPerThread.add(new ArrayList<>(1));
}
workPerThread.get(tIx).add(w);
tIx=(++tIx%THREADS);
}
final List<MessageDigest> SHA_256_DIGESTS= new ArrayList<>(THREADS);
final List<HMac> SHA_512_DIGESTS= new ArrayList<>(THREADS);

final CountDownLatch latch = new CountDownLatch(THREADS);
final ExecutorService executorService = Executors.newFixedThreadPool(THREADS);

for(tIx=0; tIx<THREADS; tIx++){
try {
SHA_512_DIGESTS.add(createHmacSha512Digest());
SHA_256_DIGESTS.add(MessageDigest.getInstance("SHA-256"));
}catch (Exception e){
}
final boolean REPORTER = tIx==0;
final int T_NUMBER = tIx;
final List<String> WORDS_TO_WORK = workPerThread.get(tIx);
executorService.submit(() -> {
final HMac LOCAL_SHA_512_DIGEST = SHA_512_DIGESTS.get(T_NUMBER);
final MessageDigest LOCAL_SHA_256_DIGEST = SHA_256_DIGESTS.get(T_NUMBER);
if (REPORTER) {
start = System.currentTimeMillis();
}
for (String PREFIX : WORDS_TO_WORK) {
if (RESULT != null) {
break;
}
List<String> seedToProcess = new ArrayList<>(configuration.getWORDS());
for (int i = 0; i < configuration.getSIZE(); i++) {
if (seedToProcess.get(i).equals(PREFIX)) {
seedToProcess.remove(i);
break;
}
}
String[] target = new String[configuration.getSIZE()];
String[] toProcess = new String[configuration.getSIZE() - 1];
toProcess = seedToProcess.toArray(toProcess);
target[0] = PREFIX;
checkAllRecursive(configuration.getSIZE() - 1, toProcess, target, LOCAL_SHA_512_DIGEST, LOCAL_SHA_256_DIGEST, REPORTER);
}
latch.countDown();
});
}
latch.await();
executorService.shutdown();
}

private boolean printAllRecursive(int n, String[] elements) {
private boolean checkAllRecursive(int n, String[] elements, final String[] target, HMac LOCAL_SHA_512_DIGEST, MessageDigest LOCAL_SHA_256_DIGEST, Boolean REPORTER) {
if(n == 1) {
return checkElements(elements);
return checkElements(target, elements, LOCAL_SHA_512_DIGEST, LOCAL_SHA_256_DIGEST, REPORTER);
} else {
for(int i = 0; i < n-1; i++) {
if (printAllRecursive(n - 1, elements)){
if (checkAllRecursive(n - 1, elements, target, LOCAL_SHA_512_DIGEST, LOCAL_SHA_256_DIGEST, REPORTER)){
return true;
}
if(n % 2 == 0) {
Expand All @@ -45,24 +102,25 @@ private boolean printAllRecursive(int n, String[] elements) {
swap(elements, 0, n - 1);
}
}
if (printAllRecursive(n - 1, elements)){
if (checkAllRecursive(n - 1, elements, target, LOCAL_SHA_512_DIGEST, LOCAL_SHA_256_DIGEST, REPORTER)){
return true;
}
}
return RESULT!=null;
}

private boolean checkElements(String[] input) {
List<String> mnemonic = Arrays.asList(input);
private boolean checkElements(String[] target, String[] input, HMac LOCAL_SHA_512_DIGEST, MessageDigest LOCAL_SHA_256_DIGEST, Boolean REPORTER) {
System.arraycopy(input,0, target, 1, input.length);
List<String> mnemonic = Arrays.asList(target);
try {
boolean result = check(mnemonic, null, SHA_256_DIGEST);
boolean result = check(mnemonic, LOCAL_SHA_512_DIGEST, LOCAL_SHA_256_DIGEST);
if (result){
RESULT = new Result(mnemonic);
}
}catch (MnemonicException e){
System.out.println(e.getLocalizedMessage());
}
if (System.currentTimeMillis()-start > STATUS_PERIOD){
if (REPORTER && (System.currentTimeMillis()-start > STATUS_PERIOD)){
System.out.println(SDTF.format(new Date())+ " Alive!");
start = System.currentTimeMillis();
}
Expand Down

0 comments on commit 78d11a7

Please sign in to comment.