Skip to content

Commit

Permalink
v 0.14.1, minor improvement for KNOWN_POSITION
Browse files Browse the repository at this point in the history
(checksum limit)
  • Loading branch information
PawelGorny committed Nov 17, 2021
1 parent 78d11a7 commit 7ced649
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 17 deletions.
17 changes: 17 additions & 0 deletions examples/example_23.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
KNOWN_POSITION
#dry acquire error joy series wheat negative top blossom able car frown
0x27c813678bD8A1C90bEB8e75F6c7972eb045E3dF
12
dry
acquire
error
joy
series
wheat
negative
top
blossom
able
?
?
m/44'/60'/0'/0/0
29 changes: 29 additions & 0 deletions examples/example_24.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
KNOWN_POSITION
#fat pupil find depart blossom steak ladder account surprise chase visit memory sausage oyster melt maple awake fox rate egg arch ability ability blanket
0xa8A8318BB27abB025aCFB54e8AD8E5463C9f2987
24
fat
pupil
find
depart
blossom
steak
ladder
account
surprise
chase
visit
memory
sausage
oyster
melt
maple
awake
fox
rate
egg
arch
?
?
?
m/44'/60'/0'/0/0
2 changes: 1 addition & 1 deletion 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.14.0</version>
<version>0.14.1</version>
<packaging>jar</packaging>

<dependencies>
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/pawelgorny/lostword/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public final class Configuration {

private final WORK work;

private boolean missingChecksum = false;
private int missingChecksumLimit = -1;

public Configuration(WORK work, String targetAddress, String path, List<String> words, int knownStarter) {
this.work = work;
this.WORDS = words;
Expand Down Expand Up @@ -256,4 +259,20 @@ public String getEthereumAddress() {
public String getDerivationPathFull() {
return derivationPathFull;
}

public boolean isMissingChecksum() {
return missingChecksum;
}

public void setMissingChecksum(boolean missingChecksum) {
this.missingChecksum = missingChecksum;
}

public int getMissingChecksumLimit() {
return missingChecksumLimit;
}

public void setMissingChecksumLimit(int missingChecksumLimit) {
this.missingChecksumLimit = missingChecksumLimit;
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/pawelgorny/lostword/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,15 @@ private static Configuration readConfiguration(String file) {
if (WORK.POOL.equals(work)){
configuration.setWORDS_POOL(wordsPool);
}
if (WORK.KNOWN_POSITION.equals(work) && words.get(size-1).equalsIgnoreCase("?")){
for (int s=9, b=3; s<=24; s+=3, b++){
if (s==size){
configuration.setMissingChecksum(true);
configuration.setMissingChecksumLimit((int)Math.pow(2,(11-b)));
break;
}
}
}
return configuration;
}
}
21 changes: 13 additions & 8 deletions src/main/java/com/pawelgorny/lostword/Worker.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,16 @@ public class Worker {
private final long CREATION_SECONDS = Utils.currentTimeSeconds();

private final int CONCAT_LEN_BITS;
private final int CONCAT_LEN_BITS_DIV_33;
private final int CONCAT_LEN_BITS_MINUS_CONCAT_LEN_BITS_DIV_33;
private final int CONCAT_LEN_BITS_MINUS_CONCAT_LEN_BITS_DIV_33__DIV8;

public Worker(Configuration configuration) {
this.configuration = configuration;
this.CONCAT_LEN_BITS = 11 * configuration.getSIZE();
this.CONCAT_LEN_BITS_DIV_33 = CONCAT_LEN_BITS/33;
this.CONCAT_LEN_BITS_MINUS_CONCAT_LEN_BITS_DIV_33 = CONCAT_LEN_BITS - CONCAT_LEN_BITS_DIV_33;
this.CONCAT_LEN_BITS_MINUS_CONCAT_LEN_BITS_DIV_33__DIV8 = CONCAT_LEN_BITS_MINUS_CONCAT_LEN_BITS_DIV_33/8;
int procs = Runtime.getRuntime().availableProcessors();
if (procs > 1) {
if (procs % 2 == 1) {
Expand Down Expand Up @@ -117,9 +123,9 @@ protected boolean check(final List<String> mnemonic) throws MnemonicException {
return check(mnemonic, null, null);
}

protected boolean check(final List<String> mnemonic, HMac SHA512DIGEST, MessageDigest sha256) throws MnemonicException {
protected Boolean check(final List<String> mnemonic, HMac SHA512DIGEST, MessageDigest sha256) throws MnemonicException {
if (!checksumCheck(mnemonic, sha256)){
return false;
return configuration.isMissingChecksum()?null:false;
}

if (Configuration.ETHEREUM.equals(configuration.getCoin())){
Expand Down Expand Up @@ -202,9 +208,7 @@ public boolean checksumCheckBcJ(List<String> words, MessageDigest sha256){
}
}

int var11 = CONCAT_LEN_BITS / 33;
int var12 = CONCAT_LEN_BITS - var11;
byte[] var13 = new byte[var12 / 8];
byte[] var13 = new byte[CONCAT_LEN_BITS_MINUS_CONCAT_LEN_BITS_DIV_33__DIV8];

for(hash = 0; hash < var13.length; ++hash) {
for(int hashBits = 0; hashBits < 8; ++hashBits) {
Expand All @@ -217,8 +221,8 @@ public boolean checksumCheckBcJ(List<String> words, MessageDigest sha256){
byte[] var14 = hash(var13, 0, var13.length, sha256);
boolean[] var15 = bytesToBits(var14);

for(int i = 0; i < var11; ++i) {
if(concatBits[var12 + i] != var15[i]) {
for(int i = 0; i < CONCAT_LEN_BITS_DIV_33; ++i) {
if(concatBits[CONCAT_LEN_BITS_MINUS_CONCAT_LEN_BITS_DIV_33 + i] != var15[i]) {
return false;
}
}
Expand Down Expand Up @@ -309,7 +313,8 @@ protected void processPosition(int position) throws InterruptedException {
final HMac LOCAL_SHA_512_DIGEST = createHmacSha512Digest();
for (int bipPosition = 0; RESULT == null && bipPosition < WORDS_TO_WORK.size(); bipPosition++) {
SEED.set(WORKING_POSITION, WORDS_TO_WORK.get(bipPosition));
if (check(SEED, LOCAL_SHA_512_DIGEST, LOCAL_SHA_256_DIGEST)) {
Boolean checkResult = check(SEED, LOCAL_SHA_512_DIGEST, LOCAL_SHA_256_DIGEST);
if (checkResult!=null && checkResult) {
RESULT = new Result(1 + WORKING_POSITION, WORDS_TO_WORK.get(bipPosition), SEED);
}
}
Expand Down
23 changes: 16 additions & 7 deletions src/main/java/com/pawelgorny/lostword/WorkerKnownPosition.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -78,7 +79,7 @@ private void checkUnknown(int position) throws InterruptedException {
final ExecutorService executorService = Executors.newFixedThreadPool(THREADS);
for (int t = 0; t < THREADS; t++) {
final int WORKING_POSITION = nextPosition;
final List<String> SEED = new ArrayList<>(mnemonic);
final List<String> SEED = new LinkedList<>(mnemonic);
final List<String> WORDS_TO_WORK = DICTIONARY.get(t);
final boolean REPORTER = t==0;
final int T_NUMBER=t;
Expand All @@ -105,37 +106,45 @@ private void checkUnknown(int position) throws InterruptedException {
}
}

private void processSeed(final List<String> seed, int depth, int positionStartSearch, boolean reporter, HMac SHA_512_DIGEST, MessageDigest SHA_256_DIGEST) throws MnemonicException {
private Boolean processSeed(final List<String> seed, int depth, int positionStartSearch, boolean reporter, HMac SHA_512_DIGEST, MessageDigest SHA_256_DIGEST) throws MnemonicException {
if (NUMBER_UNKNOWN==depth){
if (check(seed, SHA_512_DIGEST, SHA_256_DIGEST)){
Boolean checkResult = check(seed, SHA_512_DIGEST, SHA_256_DIGEST);
if (checkResult!=null&&checkResult){
System.out.println(seed);
RESULT = new Result(new ArrayList<>(seed));
return;
return true;
}
if (reporter && (System.currentTimeMillis()-start > STATUS_PERIOD)){
System.out.println(SDTF.format(new Date())+ " Alive!");
start = System.currentTimeMillis();
}
return configuration.isMissingChecksum()?(checkResult==null?false:null):checkResult;
}else{
int nextDepth = depth + 1;
int position = getNextUnknown(positionStartSearch, seed);
if(position == -1){
if (check(seed, SHA_512_DIGEST, SHA_256_DIGEST)){
Boolean checkResult = check(seed, SHA_512_DIGEST, SHA_256_DIGEST);
if (checkResult!=null&&checkResult){
System.out.println(seed);
RESULT = new Result(new ArrayList<>(seed));
}
return;
return false;
}
int positionStartNextSearch = 0;
if (nextDepth <NUMBER_UNKNOWN ){
positionStartNextSearch = position+1;
}
int checksumCheckLimit = configuration.getMissingChecksumLimit();
for (int w = 0; RESULT==null && w<DICTIONARY_SIZE; w++){
seed.set(position, Configuration.MNEMONIC_CODE.getWordList().get(w));
processSeed(seed, nextDepth, positionStartNextSearch, reporter, SHA_512_DIGEST, SHA_256_DIGEST);
Boolean result = processSeed(seed, nextDepth, positionStartNextSearch, reporter, SHA_512_DIGEST, SHA_256_DIGEST);
if(result == null && --checksumCheckLimit==0){
break;
}
}
seed.set(position, Configuration.UNKNOWN_CHAR);
}
return false;
}

private void checkOne(int position) throws InterruptedException {
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ protected boolean check(List<String> mnemonic) throws MnemonicException {
}

@Override
protected boolean check(List<String> mnemonic, HMac SHA512DIGEST, MessageDigest sha256) throws MnemonicException {
protected Boolean check(List<String> mnemonic, HMac SHA512DIGEST, MessageDigest sha256) throws MnemonicException {
return super.check(mnemonic, SHA512DIGEST, sha256);
}

Expand Down

0 comments on commit 7ced649

Please sign in to comment.