Skip to content

Commit

Permalink
add prefix pattern search to nickminer algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
pk910 committed Sep 26, 2024
1 parent 49e493d commit 6c45118
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 25 deletions.
2 changes: 1 addition & 1 deletion faucet-client/src/worker/worker-nickminer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { PoWHashAlgo } from "../types/PoWMinerSrc";
configFn: (preimg, params) => {
if(params.a !== PoWHashAlgo.NICKMINER)
return null;
nickMiner.miner_set_config(params.i, params.r, params.v, params.s, params.c, preimg);
nickMiner.miner_set_config(params.i, params.r, params.v, params.s, params.p, params.c, preimg);
}
});

Expand Down
24 changes: 23 additions & 1 deletion faucet-wasm/nickminer/miner/nickminer.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ unsigned char inputSigR[32];
unsigned char inputSigV;
unsigned char preimageHash[32];
unsigned char outputSuffix[20];
unsigned char outputPrefix[20];
int outputSuffixLen;
int outputPrefixLen;
int maxRounds;

void miner_init() {
Expand Down Expand Up @@ -54,7 +56,7 @@ static void get_create_addr(unsigned char *deployer, unsigned char *addrBuf) {
memcpy(addrBuf, hashout+12, 20);
}

void miner_set_config(unsigned char *input_hex, unsigned char *input_sigr, int input_sig_v, unsigned char *output_suffix, int max_rounds, unsigned char *preimageHex) {
void miner_set_config(unsigned char *input_hex, unsigned char *input_sigr, int input_sig_v, unsigned char *output_suffix, unsigned char *output_prefix, int max_rounds, unsigned char *preimageHex) {
unsigned char *pos;
size_t i;

Expand All @@ -67,6 +69,11 @@ void miner_set_config(unsigned char *input_hex, unsigned char *input_sigr, int i
for(i = 0; i < output_len; i++) { sscanf(pos, "%2hhx", &outputSuffix[i]); pos += 2; }
outputSuffixLen = output_len;

output_len = strlen(output_prefix) / 2;
pos = output_prefix;
for(i = 0; i < output_len; i++) { sscanf(pos, "%2hhx", &outputPrefix[i]); pos += 2; }
outputPrefixLen = output_len;

maxRounds = max_rounds;

int preimage_len = strlen(preimageHex) / 2;
Expand Down Expand Up @@ -198,6 +205,21 @@ unsigned char* miner_run(unsigned char *nonceHex) {
if(diff & 0x80) { break; } else { score++; }
}

if (score == outputSuffixLen * 8) {
// suffix matches completely, check prefix
for (j = 0; j < outputPrefixLen; j++) {
diff = addr[j] ^ outputPrefix[j];
if(diff & 0x80) { break; } else { score++; }
if(diff & 0x40) { break; } else { score++; }
if(diff & 0x20) { break; } else { score++; }
if(diff & 0x10) { break; } else { score++; }
if(diff & 0x08) { break; } else { score++; }
if(diff & 0x04) { break; } else { score++; }
if(diff & 0x02) { break; } else { score++; }
if(diff & 0x01) { break; } else { score++; }
}
}

if (score > bestScore) {
bestScore = score;
memcpy(bestAddr, addr, 20);
Expand Down
2 changes: 1 addition & 1 deletion faucet-wasm/nickminer/wasm-pre.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Module["locateFile"] = function() {};
Module["onRuntimeInitialized"] = function() {
nickMiner = {
miner_init: cwrap("miner_init", "void", []),
miner_set_config: cwrap("miner_set_config", "void", ["string", "string", "number", "string", "number", "string"]),
miner_set_config: cwrap("miner_set_config", "void", ["string", "string", "number", "string", "string", "number", "string"]),
miner_get_input: cwrap("miner_get_input", "string", []),
miner_get_sigrv: cwrap("miner_get_sigrv", "string", []),
miner_get_suffix: cwrap("miner_get_suffix", "string", []),
Expand Down
4 changes: 2 additions & 2 deletions libs/nickminer_wasm.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion libs/nickminer_wasm.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

interface NickMiner {
miner_init(): void;
miner_set_config(inputHash: string, sigR: string, sigV: number, suffixMask: string, rounds: number, preimage: string): void;
miner_set_config(inputHash: string, sigR: string, sigV: number, suffixMask: string, prefixMask: string, rounds: number, preimage: string): void;
miner_run(nonce: string): string;
}

Expand Down
18 changes: 0 additions & 18 deletions src/modules/pow/PoWShareVerification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,24 +214,6 @@ export class PoWShareVerification {
if(powConfig.powHashAlgo === PoWHashAlgo.NICKMINER && powConfig.powNickMinerParams.relevantFile) {
// check for relevant results
let difficulty = parseInt(this.data.substring(2, 4), 16);
if(difficulty >= powConfig.powNickMinerParams.suffix.length * 4) {
// check for valid prefix
for(let i = 0; i < powConfig.powNickMinerParams.prefix.length; i++) {
let addrByte = parseInt(this.data.substring(4 + i * 2, 6 + i * 2), 16);
let prefixByte = parseInt(powConfig.powNickMinerParams.prefix.substring(i*2, (i*2)+2), 16);

let diffByte = addrByte ^ prefixByte;
if(diffByte & 0x80) break; difficulty++;
if(diffByte & 0x40) break; difficulty++;
if(diffByte & 0x20) break; difficulty++;
if(diffByte & 0x10) break; difficulty++;
if(diffByte & 0x08) break; difficulty++;
if(diffByte & 0x04) break; difficulty++;
if(diffByte & 0x02) break; difficulty++;
if(diffByte & 0x01) break; difficulty++;
}
}

if(difficulty >= powConfig.powNickMinerParams.relevantDifficulty) {
let line = "0x" + this.data.substring(4, 44) + " (d: " + difficulty + "): hash: 0x" + powConfig.powNickMinerParams.hash + ", sigR: 0x" + powConfig.powNickMinerParams.sigR + ", sigS: 0x" + this.data.substring(44, this.data.length);
fs.appendFileSync(resolveRelativePath(powConfig.powNickMinerParams.relevantFile), line + "\n")
Expand Down
2 changes: 1 addition & 1 deletion src/modules/pow/validator/PoWValidatorWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class PoWValidatorWorker {
await module.default.getNickMinerReadyPromise();
let nickMiner = module.default.getNickMiner();
return (nonce, preimg, params: IPoWNickMinerParams) => {
nickMiner.miner_set_config(params.hash, params.sigR, params.sigV, params.suffix, params.count, preimg);
nickMiner.miner_set_config(params.hash, params.sigR, params.sigV, params.suffix, params.prefix, params.count, preimg);
return nickMiner.miner_run(nonce);
};
})();
Expand Down

0 comments on commit 6c45118

Please sign in to comment.