-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: incremental proof of work computing fn
- Loading branch information
1 parent
c0561b7
commit c2ec6c3
Showing
2 changed files
with
76 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ | |
* Copyright © 2021 Aravinth Manivnanan <[email protected]>. | ||
* | ||
* Use of this source code is governed by Apache 2.0 or MIT license. | ||
* You shoud have received a copy of MIT and Apache 2.0 along with | ||
* You should have received a copy of MIT and Apache 2.0 along with | ||
* this program. If not, see <https://spdx.org/licenses/MIT.html> for | ||
* MIT or <http://www.apache.org/licenses/LICENSE-2.0> for Apache. | ||
*/ | ||
|
@@ -51,7 +51,7 @@ export const digest = async (message: string): Promise<number[]> => { | |
* Calculate difficulty of a hash | ||
* | ||
* @param {number[]} hash - hash for which difficulty should be calculated | ||
* @returns {BigInt} - diffuclty of the given hash | ||
* @returns {BigInt} - difficulty of the given hash | ||
*/ | ||
export const score = (hash: number[]): BigInt => { | ||
let sum = BigInt(0); | ||
|
@@ -75,7 +75,7 @@ export type WasmWork = { | |
}; | ||
|
||
/** | ||
* Generate Proof-of-Work(PoW) according to the algorithim used in mCaptcha | ||
* Generate Proof-of-Work(PoW) according to the algorithm used in mCaptcha | ||
* | ||
* @param {string} salt - salt used in PoW computation. Will be provided in PoW requirement | ||
* @param {string} phrase - challenge phrase used in PoW computation. Will be provided in PoW requirement | ||
|
@@ -105,3 +105,46 @@ export const generate_work = async ( | |
}; | ||
return work; | ||
}; | ||
|
||
/** | ||
* Generate Proof-of-Work(PoW) according to the algorithm used in mCaptcha incrementally | ||
* | ||
* @param {string} salt - salt used in PoW computation. Will be provided in PoW requirement | ||
* @param {string} phrase - challenge phrase used in PoW computation. Will be provided in PoW requirement | ||
* @param {number} difficulty - target difficulty for which PoW should be generated. Will be provided in PoW requirement | ||
* @param {number} step - notify progress with nonce after 'n' number of steps | ||
* @param {(nonce: number) => void} fn - callback function to notify progress | ||
* | ||
* @returns {Promise<WasmWork>} - proof-of-work | ||
**/ | ||
export const stepped_generate_work = async ( | ||
salt: string, | ||
phrase: string, | ||
difficulty: number, | ||
step: number, | ||
fn: (nonce: number) => void, | ||
): Promise<WasmWork> => { | ||
const serialized_phrase = decoder.decode(serialize(phrase)); | ||
const base = salt + serialized_phrase; | ||
let nonce = 0; | ||
let result: BigInt = BigInt(0); | ||
const difficulty_new: BigInt = U128_MAX - U128_MAX / BigInt(difficulty); | ||
let count = 0; | ||
while (result < difficulty_new) { | ||
if (count < step) { | ||
nonce += 1; | ||
Check failure on line 135 in src/index.ts GitHub Actions / x86_64-unknown-linux-gnu
|
||
const hash = await digest(base + nonce.toString()); | ||
Check failure on line 136 in src/index.ts GitHub Actions / x86_64-unknown-linux-gnu
|
||
result = score(hash); | ||
Check failure on line 137 in src/index.ts GitHub Actions / x86_64-unknown-linux-gnu
|
||
count+=1; | ||
} else { | ||
fn(nonce); | ||
count = 0; | ||
} | ||
} | ||
|
||
const work: WasmWork = { | ||
result: result.toString(), | ||
nonce, | ||
}; | ||
return work; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters