import { blake2bResetForShortMessage , blake2bCompress , Context } from "./blake2b/blake2b" ; import { CHALLENGE_SIZE_BYTES } from "./constants" ; export const HASH_SIZE_BYTES = 32 ; param input param threshold export function solveBlake2bEfficient ( input , threshold , n ) { if ( input . length != CHALLENGE_SIZE_BYTES ) { throw Error ( "Invalid input" ) ; } const buf = input . buffer ; const view = new DataView ( buf ) ; const ctx = new Context ( HASH_SIZE_BYTES ) ; ctx . t = CHALLENGE_SIZE_BYTES ; const start = view . getUint32 ( 124 , true ) ; const end = start + n ; for ( let i = start ; i < end ; i ++ ) { view . setUint32 ( 124 , i , true ) ; blake2bResetForShortMessage ( ctx , input ) ; blake2bCompress ( ctx , true ) ; if ( ctx . h [ 0 ] < threshold ) { if ( ASC_TARGET == 0 ) { return new Uint8Array ( ctx . h . buffer ) ; } return Uint8Array . wrap ( ctx . h . buffer ) ; } } return new Uint8Array ( 0 ) ; }