@ethereumjs/ethash
Advanced tools
Comparing version 1.1.0 to 2.0.0-beta.1
/// <reference types="node" /> | ||
import type { LevelUp } from 'levelup'; | ||
import { Block, BlockHeader } from '@ethereumjs/block'; | ||
declare type Solution = { | ||
import { AbstractLevel } from 'abstract-level'; | ||
export declare type Solution = { | ||
mixHash: Buffer; | ||
nonce: Buffer; | ||
}; | ||
declare class Miner { | ||
export declare class Miner { | ||
private blockHeader; | ||
@@ -40,5 +40,11 @@ private block?; | ||
} | ||
export declare type EthashCacheDB = AbstractLevel<string | Buffer | Uint8Array, string | Buffer, { | ||
cache: Buffer[]; | ||
fullSize: number; | ||
cacheSize: number; | ||
seed: Buffer; | ||
}>; | ||
export default class Ethash { | ||
dbOpts: Object; | ||
cacheDB?: LevelUp; | ||
cacheDB?: EthashCacheDB; | ||
cache: Buffer[]; | ||
@@ -49,3 +55,3 @@ epoc?: number; | ||
seed?: Buffer; | ||
constructor(cacheDB?: LevelUp); | ||
constructor(cacheDB?: EthashCacheDB); | ||
mkcache(cacheSize: number, seed: Buffer): Buffer[]; | ||
@@ -62,3 +68,3 @@ calcDatasetItem(i: number): Buffer; | ||
*/ | ||
loadEpoc(number: number): Promise<void>; | ||
loadEpoc(number: bigint): Promise<void>; | ||
/** | ||
@@ -74,2 +80,2 @@ * Returns a `Miner` object | ||
} | ||
export {}; | ||
//# sourceMappingURL=index.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const ethereumjs_util_1 = require("ethereumjs-util"); | ||
const util_1 = require("./util"); | ||
exports.Miner = void 0; | ||
const keccak_1 = require("ethereum-cryptography/keccak"); | ||
const util_1 = require("@ethereumjs/util"); | ||
const rlp_1 = require("rlp"); | ||
const util_2 = require("./util"); | ||
const block_1 = require("@ethereumjs/block"); | ||
@@ -24,3 +27,3 @@ const xor = require('buffer-xor'); | ||
} | ||
this.currentNonce = new ethereumjs_util_1.BN(0); | ||
this.currentNonce = BigInt(0); | ||
this.ethash = ethash; | ||
@@ -72,13 +75,12 @@ this.stopMining = false; | ||
const { number, difficulty } = this.blockHeader; | ||
await this.ethash.loadEpoc(number.toNumber()); | ||
const self = this; | ||
while (iterations != 0 && !this.stopMining) { | ||
await this.ethash.loadEpoc(number); | ||
while (iterations !== 0 && !this.stopMining) { | ||
// The promise/setTimeout construction is necessary to ensure we jump out of the event loop | ||
// Without this, for high-difficulty blocks JS never jumps out of the Promise | ||
const solution = await new Promise((resolve) => { | ||
setTimeout(function () { | ||
const nonce = self.currentNonce.toBuffer(undefined, 8); | ||
const a = self.ethash.run(headerHash, nonce); | ||
const result = new ethereumjs_util_1.BN(a.hash); | ||
if (ethereumjs_util_1.TWO_POW256.div(difficulty).cmp(result) === 1) { | ||
setTimeout(() => { | ||
const nonce = (0, util_1.setLengthLeft)((0, util_1.bigIntToBuffer)(this.currentNonce), 8); | ||
const a = this.ethash.run(headerHash, nonce); | ||
const result = (0, util_1.bufferToBigInt)(a.hash); | ||
if (util_1.TWO_POW256 / difficulty > result) { | ||
const solution = { | ||
@@ -88,7 +90,7 @@ mixHash: a.mix, | ||
}; | ||
self.solution = solution; | ||
this.solution = solution; | ||
resolve(solution); | ||
return; | ||
} | ||
self.currentNonce.iaddn(1); | ||
this.currentNonce++; | ||
iterations--; | ||
@@ -104,2 +106,3 @@ resolve(null); | ||
} | ||
exports.Miner = Miner; | ||
class Ethash { | ||
@@ -114,15 +117,13 @@ constructor(cacheDB) { | ||
mkcache(cacheSize, seed) { | ||
// console.log('generating cache') | ||
// console.log('size: ' + cacheSize) | ||
// console.log('seed: ' + seed.toString('hex')) | ||
const n = Math.floor(cacheSize / util_1.params.HASH_BYTES); | ||
const o = [(0, ethereumjs_util_1.keccak)(seed, 512)]; | ||
// console.log(`generating cache\nsize: ${cacheSize}\nseed: ${seed.toString('hex')}`) | ||
const n = Math.floor(cacheSize / util_2.params.HASH_BYTES); | ||
const o = [Buffer.from((0, keccak_1.keccak512)(seed))]; | ||
let i; | ||
for (i = 1; i < n; i++) { | ||
o.push((0, ethereumjs_util_1.keccak)(o[o.length - 1], 512)); | ||
o.push(Buffer.from((0, keccak_1.keccak512)(o[o.length - 1]))); | ||
} | ||
for (let _ = 0; _ < util_1.params.CACHE_ROUNDS; _++) { | ||
for (let _ = 0; _ < util_2.params.CACHE_ROUNDS; _++) { | ||
for (i = 0; i < n; i++) { | ||
const v = o[i].readUInt32LE(0) % n; | ||
o[i] = (0, ethereumjs_util_1.keccak)(xor(o[(i - 1 + n) % n], o[v]), 512); | ||
o[i] = Buffer.from((0, keccak_1.keccak512)(xor(o[(i - 1 + n) % n], o[v]))); | ||
} | ||
@@ -135,11 +136,11 @@ } | ||
const n = this.cache.length; | ||
const r = Math.floor(util_1.params.HASH_BYTES / util_1.params.WORD_BYTES); | ||
const r = Math.floor(util_2.params.HASH_BYTES / util_2.params.WORD_BYTES); | ||
let mix = Buffer.from(this.cache[i % n]); | ||
mix.writeInt32LE(mix.readUInt32LE(0) ^ i, 0); | ||
mix = (0, ethereumjs_util_1.keccak)(mix, 512); | ||
for (let j = 0; j < util_1.params.DATASET_PARENTS; j++) { | ||
const cacheIndex = (0, util_1.fnv)(i ^ j, mix.readUInt32LE((j % r) * 4)); | ||
mix = (0, util_1.fnvBuffer)(mix, this.cache[cacheIndex % n]); | ||
mix = Buffer.from((0, keccak_1.keccak512)(mix)); | ||
for (let j = 0; j < util_2.params.DATASET_PARENTS; j++) { | ||
const cacheIndex = (0, util_2.fnv)(i ^ j, mix.readUInt32LE((j % r) * 4)); | ||
mix = (0, util_2.fnvBuffer)(mix, this.cache[cacheIndex % n]); | ||
} | ||
return (0, ethereumjs_util_1.keccak)(mix, 512); | ||
return Buffer.from((0, keccak_1.keccak512)(mix)); | ||
} | ||
@@ -153,10 +154,10 @@ run(val, nonce, fullSize) { | ||
} | ||
const n = Math.floor(fullSize / util_1.params.HASH_BYTES); | ||
const w = Math.floor(util_1.params.MIX_BYTES / util_1.params.WORD_BYTES); | ||
const s = (0, ethereumjs_util_1.keccak)(Buffer.concat([val, (0, util_1.bufReverse)(nonce)]), 512); | ||
const mixhashes = Math.floor(util_1.params.MIX_BYTES / util_1.params.HASH_BYTES); | ||
const n = Math.floor(fullSize / util_2.params.HASH_BYTES); | ||
const w = Math.floor(util_2.params.MIX_BYTES / util_2.params.WORD_BYTES); | ||
const s = Buffer.from((0, keccak_1.keccak512)(Buffer.concat([val, (0, util_2.bufReverse)(nonce)]))); | ||
const mixhashes = Math.floor(util_2.params.MIX_BYTES / util_2.params.HASH_BYTES); | ||
let mix = Buffer.concat(Array(mixhashes).fill(s)); | ||
let i; | ||
for (i = 0; i < util_1.params.ACCESSES; i++) { | ||
const p = ((0, util_1.fnv)(i ^ s.readUInt32LE(0), mix.readUInt32LE((i % w) * 4)) % Math.floor(n / mixhashes)) * | ||
for (i = 0; i < util_2.params.ACCESSES; i++) { | ||
const p = ((0, util_2.fnv)(i ^ s.readUInt32LE(0), mix.readUInt32LE((i % w) * 4)) % Math.floor(n / mixhashes)) * | ||
mixhashes; | ||
@@ -167,9 +168,9 @@ const newdata = []; | ||
} | ||
mix = (0, util_1.fnvBuffer)(mix, Buffer.concat(newdata)); | ||
mix = (0, util_2.fnvBuffer)(mix, Buffer.concat(newdata)); | ||
} | ||
const cmix = Buffer.alloc(mix.length / 4); | ||
for (i = 0; i < mix.length / 4; i = i + 4) { | ||
const a = (0, util_1.fnv)(mix.readUInt32LE(i * 4), mix.readUInt32LE((i + 1) * 4)); | ||
const b = (0, util_1.fnv)(a, mix.readUInt32LE((i + 2) * 4)); | ||
const c = (0, util_1.fnv)(b, mix.readUInt32LE((i + 3) * 4)); | ||
const a = (0, util_2.fnv)(mix.readUInt32LE(i * 4), mix.readUInt32LE((i + 1) * 4)); | ||
const b = (0, util_2.fnv)(a, mix.readUInt32LE((i + 2) * 4)); | ||
const c = (0, util_2.fnv)(b, mix.readUInt32LE((i + 3) * 4)); | ||
cmix.writeUInt32LE(c, i); | ||
@@ -179,10 +180,10 @@ } | ||
mix: cmix, | ||
hash: (0, ethereumjs_util_1.keccak256)(Buffer.concat([s, cmix])), | ||
hash: Buffer.from((0, keccak_1.keccak256)(Buffer.concat([s, cmix]))), | ||
}; | ||
} | ||
cacheHash() { | ||
return (0, ethereumjs_util_1.keccak256)(Buffer.concat(this.cache)); | ||
return Buffer.from((0, keccak_1.keccak256)(Buffer.concat(this.cache))); | ||
} | ||
headerHash(rawHeader) { | ||
return (0, ethereumjs_util_1.rlphash)(rawHeader.slice(0, -2)); | ||
return Buffer.from((0, keccak_1.keccak256)(rlp_1.default.encode((0, util_1.bufArrToArr)(rawHeader.slice(0, -2))))); | ||
} | ||
@@ -193,3 +194,3 @@ /** | ||
async loadEpoc(number) { | ||
const epoc = (0, util_1.getEpoc)(number); | ||
const epoc = (0, util_2.getEpoc)(number); | ||
if (this.epoc === epoc) { | ||
@@ -205,3 +206,3 @@ return; | ||
if (epoc === 0) { | ||
return [(0, ethereumjs_util_1.zeros)(32), 0]; | ||
return [(0, util_1.zeros)(32), 0]; | ||
} | ||
@@ -213,3 +214,3 @@ let data; | ||
catch (error) { | ||
if (error.type !== 'NotFoundError') { | ||
if (error.code !== 'LEVEL_NOT_FOUND') { | ||
throw error; | ||
@@ -230,3 +231,3 @@ } | ||
catch (error) { | ||
if (error.type !== 'NotFoundError') { | ||
if (error.code !== 'LEVEL_NOT_FOUND') { | ||
throw error; | ||
@@ -236,6 +237,6 @@ } | ||
if (!data) { | ||
this.cacheSize = (0, util_1.getCacheSize)(epoc); | ||
this.fullSize = (0, util_1.getFullSize)(epoc); | ||
this.cacheSize = await (0, util_2.getCacheSize)(epoc); | ||
this.fullSize = await (0, util_2.getFullSize)(epoc); | ||
const [seed, foundEpoc] = await findLastSeed(epoc); | ||
this.seed = (0, util_1.getSeed)(seed, foundEpoc, epoc); | ||
this.seed = (0, util_2.getSeed)(seed, foundEpoc, epoc); | ||
const cache = this.mkcache(this.cacheSize, this.seed); | ||
@@ -251,3 +252,2 @@ // store the generated cache | ||
else { | ||
// Object.assign(this, data) | ||
this.cache = data.cache.map((a) => { | ||
@@ -273,6 +273,6 @@ return Buffer.from(a); | ||
const { number, difficulty, mixHash, nonce } = header; | ||
await this.loadEpoc(number.toNumber()); | ||
await this.loadEpoc(number); | ||
const a = this.run(headerHash, nonce); | ||
const result = new ethereumjs_util_1.BN(a.hash); | ||
return a.mix.equals(mixHash) && ethereumjs_util_1.TWO_POW256.div(difficulty).cmp(result) === 1; | ||
const result = (0, util_1.bufferToBigInt)(a.hash); | ||
return a.mix.equals(mixHash) && util_1.TWO_POW256 / difficulty > result; | ||
} | ||
@@ -279,0 +279,0 @@ async verifyPOW(block) { |
@@ -16,5 +16,5 @@ /// <reference types="node" /> | ||
}; | ||
export declare function getCacheSize(epoc: number): number; | ||
export declare function getFullSize(epoc: number): number; | ||
export declare function getEpoc(blockNumber: number): number; | ||
export declare function getCacheSize(epoc: number): Promise<number>; | ||
export declare function getFullSize(epoc: number): Promise<number>; | ||
export declare function getEpoc(blockNumber: bigint): number; | ||
/** | ||
@@ -32,1 +32,2 @@ * Generates a seed give the end epoc and optional the begining epoc and the | ||
export declare function bufReverse(a: Buffer): Buffer; | ||
//# sourceMappingURL=util.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.bufReverse = exports.fnvBuffer = exports.fnv = exports.getSeed = exports.getEpoc = exports.getFullSize = exports.getCacheSize = exports.params = void 0; | ||
const ethereumjs_util_1 = require("ethereumjs-util"); | ||
const MR = require('miller-rabin'); | ||
const bigint_crypto_utils_1 = require("bigint-crypto-utils"); | ||
const keccak_1 = require("ethereum-cryptography/keccak"); | ||
exports.params = { | ||
@@ -20,9 +20,8 @@ DATASET_BYTES_INIT: 1073741824, | ||
}; | ||
function getCacheSize(epoc) { | ||
const mr = new MR(); | ||
let sz = exports.params.CACHE_BYTES_INIT + | ||
exports.params.CACHE_BYTES_GROWTH * epoc; | ||
sz -= exports.params.HASH_BYTES; | ||
while (!mr.test(new ethereumjs_util_1.BN(sz / exports.params.HASH_BYTES))) { | ||
sz -= 2 * exports.params.HASH_BYTES; | ||
async function getCacheSize(epoc) { | ||
const { CACHE_BYTES_INIT, CACHE_BYTES_GROWTH, HASH_BYTES } = exports.params; | ||
let sz = CACHE_BYTES_INIT + CACHE_BYTES_GROWTH * epoc; | ||
sz -= HASH_BYTES; | ||
while (!(await (0, bigint_crypto_utils_1.isProbablyPrime)(sz / HASH_BYTES, undefined, true))) { | ||
sz -= 2 * HASH_BYTES; | ||
} | ||
@@ -32,9 +31,8 @@ return sz; | ||
exports.getCacheSize = getCacheSize; | ||
function getFullSize(epoc) { | ||
const mr = new MR(); | ||
let sz = exports.params.DATASET_BYTES_INIT + | ||
exports.params.DATASET_BYTES_GROWTH * epoc; | ||
sz -= exports.params.MIX_BYTES; | ||
while (!mr.test(new ethereumjs_util_1.BN(sz / exports.params.MIX_BYTES))) { | ||
sz -= 2 * exports.params.MIX_BYTES; | ||
async function getFullSize(epoc) { | ||
const { DATASET_BYTES_INIT, DATASET_BYTES_GROWTH, MIX_BYTES } = exports.params; | ||
let sz = DATASET_BYTES_INIT + DATASET_BYTES_GROWTH * epoc; | ||
sz -= MIX_BYTES; | ||
while (!(await (0, bigint_crypto_utils_1.isProbablyPrime)(sz / MIX_BYTES, undefined, true))) { | ||
sz -= 2 * MIX_BYTES; | ||
} | ||
@@ -45,3 +43,3 @@ return sz; | ||
function getEpoc(blockNumber) { | ||
return Math.floor(blockNumber / exports.params.EPOCH_LENGTH); | ||
return Number(blockNumber / BigInt(exports.params.EPOCH_LENGTH)); | ||
} | ||
@@ -59,3 +57,3 @@ exports.getEpoc = getEpoc; | ||
for (let i = begin; i < end; i++) { | ||
seed = (0, ethereumjs_util_1.keccak256)(seed); | ||
seed = Buffer.from((0, keccak_1.keccak256)(seed)); | ||
} | ||
@@ -62,0 +60,0 @@ return seed; |
{ | ||
"name": "@ethereumjs/ethash", | ||
"version": "1.1.0", | ||
"version": "2.0.0-beta.1", | ||
"description": "An ethash implementation in JavaScript", | ||
@@ -14,3 +14,2 @@ "license": "MPL-2.0", | ||
"dist", | ||
"dist.browser", | ||
"src" | ||
@@ -20,7 +19,4 @@ ], | ||
"types": "dist/index.d.ts", | ||
"browser": "dist.browser/index.js", | ||
"scripts": { | ||
"build": "npm run build:node && npm run build:browser", | ||
"build:node": "../../config/cli/ts-build.sh node", | ||
"build:browser": "../../config/cli/ts-build.sh browser", | ||
"build": "../../config/cli/ts-build.sh", | ||
"tsc": "../../config/cli/ts-compile.sh", | ||
@@ -36,18 +32,21 @@ "prepublishOnly": "../../config/cli/prepublish.sh", | ||
"dependencies": { | ||
"@ethereumjs/block": "^3.5.0", | ||
"@types/levelup": "^4.3.0", | ||
"@ethereumjs/block": "4.0.0-beta.1", | ||
"@ethereumjs/util": "8.0.0-beta.1", | ||
"abstract-level": "^1.0.3", | ||
"bigint-crypto-utils": "^3.0.23", | ||
"buffer-xor": "^2.0.1", | ||
"ethereumjs-util": "^7.1.1", | ||
"miller-rabin": "^4.0.0" | ||
"ethereum-cryptography": "^1.0.3", | ||
"miller-rabin": "^4.0.0", | ||
"rlp": "4.0.0-beta.1" | ||
}, | ||
"devDependencies": { | ||
"@ethereumjs/common": "^2.5.0", | ||
"@ethereumjs/common": "3.0.0-beta.1", | ||
"@types/tape": "^4.13.2", | ||
"eslint": "^6.8.0", | ||
"level-mem": "^5.0.1", | ||
"typedoc": "^0.22.4", | ||
"nyc": "^14.0.0", | ||
"memory-level": "^1.0.0", | ||
"nyc": "^15.1.0", | ||
"prettier": "^2.0.5", | ||
"tape": "^5.3.1", | ||
"ts-node": "^10.2.1", | ||
"typedoc": "^0.22.4", | ||
"typescript": "^4.4.2" | ||
@@ -54,0 +53,0 @@ }, |
@@ -10,3 +10,3 @@ # @ethereumjs/ethash | ||
| [Ethash](https://github.com/ethereum/wiki/wiki/Ethash) implementation in TypeScript. | | ||
| --- | | ||
| ------------------------------------------------------------------------------------ | | ||
@@ -26,3 +26,3 @@ Note: this `README` reflects the state of the library from `v1.0.0` onwards. See `README` from the [standalone repository](https://github.com/ethereumjs/ethashjs) for an introduction on the last preceding release. | ||
import { Block } from '@ethereumjs/block' | ||
const level = require('level-mem') | ||
import { MemoryLevel } from 'memory-level' | ||
@@ -52,9 +52,9 @@ const cacheDB = level() | ||
import { BN } from 'ethereumjs-util' | ||
const level = require('level-mem') | ||
import { MemoryLevel } from 'memory-level' | ||
const cacheDB = level() | ||
const cacheDB = new MemoryLevel() | ||
const block = Block.fromBlockData({ | ||
header: { | ||
difficulty: new BN(100), | ||
number: new BN(1), | ||
difficulty: BigInt(100), | ||
number: BigInt(1), | ||
}, | ||
@@ -61,0 +61,0 @@ }) |
@@ -1,3 +0,12 @@ | ||
import { BN, keccak, keccak256, rlphash, zeros, TWO_POW256 } from 'ethereumjs-util' | ||
import { keccak256, keccak512 } from 'ethereum-cryptography/keccak' | ||
import { | ||
zeros, | ||
TWO_POW256, | ||
bigIntToBuffer, | ||
bufArrToArr, | ||
bufferToBigInt, | ||
setLengthLeft, | ||
} from '@ethereumjs/util' | ||
import RLP from 'rlp' | ||
import { | ||
params, | ||
@@ -12,8 +21,7 @@ fnv, | ||
} from './util' | ||
// eslint-disable-next-line implicit-dependencies/no-implicit | ||
import type { LevelUp } from 'levelup' | ||
import { Block, BlockData, BlockHeader, HeaderData } from '@ethereumjs/block' | ||
import { AbstractLevel } from 'abstract-level' | ||
const xor = require('buffer-xor') | ||
type Solution = { | ||
export type Solution = { | ||
mixHash: Buffer | ||
@@ -23,3 +31,3 @@ nonce: Buffer | ||
class Miner { | ||
export class Miner { | ||
private blockHeader: BlockHeader | ||
@@ -31,3 +39,3 @@ private block?: Block | ||
private currentNonce: BN | ||
private currentNonce: bigint | ||
private headerHash?: Buffer | ||
@@ -51,3 +59,3 @@ private stopMining: boolean | ||
} | ||
this.currentNonce = new BN(0) | ||
this.currentNonce = BigInt(0) | ||
this.ethash = ethash | ||
@@ -103,20 +111,20 @@ this.stopMining = false | ||
await this.ethash.loadEpoc(number.toNumber()) | ||
const self = this | ||
while (iterations != 0 && !this.stopMining) { | ||
await this.ethash.loadEpoc(number) | ||
while (iterations !== 0 && !this.stopMining) { | ||
// The promise/setTimeout construction is necessary to ensure we jump out of the event loop | ||
// Without this, for high-difficulty blocks JS never jumps out of the Promise | ||
const solution = await new Promise((resolve) => { | ||
setTimeout(function () { | ||
const nonce = self.currentNonce.toBuffer(undefined, 8) | ||
setTimeout(() => { | ||
const nonce = setLengthLeft(bigIntToBuffer(this.currentNonce), 8) | ||
const a = self.ethash.run(headerHash, nonce) | ||
const result = new BN(a.hash) | ||
const a = this.ethash.run(headerHash, nonce) | ||
const result = bufferToBigInt(a.hash) | ||
if (TWO_POW256.div(difficulty).cmp(result) === 1) { | ||
if (TWO_POW256 / difficulty > result) { | ||
const solution: Solution = { | ||
mixHash: <Buffer>a.mix, | ||
mixHash: a.mix, | ||
nonce, | ||
} | ||
self.solution = solution | ||
this.solution = solution | ||
resolve(solution) | ||
@@ -126,3 +134,3 @@ return | ||
self.currentNonce.iaddn(1) | ||
this.currentNonce++ | ||
iterations-- | ||
@@ -141,5 +149,16 @@ | ||
export type EthashCacheDB = AbstractLevel< | ||
string | Buffer | Uint8Array, | ||
string | Buffer, | ||
{ | ||
cache: Buffer[] | ||
fullSize: number | ||
cacheSize: number | ||
seed: Buffer | ||
} | ||
> | ||
export default class Ethash { | ||
dbOpts: Object | ||
cacheDB?: LevelUp | ||
cacheDB?: EthashCacheDB | ||
cache: Buffer[] | ||
@@ -151,3 +170,3 @@ epoc?: number | ||
constructor(cacheDB?: LevelUp) { | ||
constructor(cacheDB?: EthashCacheDB) { | ||
this.dbOpts = { | ||
@@ -161,11 +180,9 @@ valueEncoding: 'json', | ||
mkcache(cacheSize: number, seed: Buffer) { | ||
// console.log('generating cache') | ||
// console.log('size: ' + cacheSize) | ||
// console.log('seed: ' + seed.toString('hex')) | ||
// console.log(`generating cache\nsize: ${cacheSize}\nseed: ${seed.toString('hex')}`) | ||
const n = Math.floor(cacheSize / params.HASH_BYTES) | ||
const o = [keccak(seed, 512)] | ||
const o = [Buffer.from(keccak512(seed))] | ||
let i | ||
for (i = 1; i < n; i++) { | ||
o.push(keccak(o[o.length - 1], 512)) | ||
o.push(Buffer.from(keccak512(o[o.length - 1]))) | ||
} | ||
@@ -176,3 +193,3 @@ | ||
const v = o[i].readUInt32LE(0) % n | ||
o[i] = keccak(xor(o[(i - 1 + n) % n], o[v]), 512) | ||
o[i] = Buffer.from(keccak512(xor(o[(i - 1 + n) % n], o[v]))) | ||
} | ||
@@ -190,3 +207,3 @@ } | ||
mix.writeInt32LE(mix.readUInt32LE(0) ^ i, 0) | ||
mix = keccak(mix, 512) | ||
mix = Buffer.from(keccak512(mix)) | ||
for (let j = 0; j < params.DATASET_PARENTS; j++) { | ||
@@ -196,3 +213,3 @@ const cacheIndex = fnv(i ^ j, mix.readUInt32LE((j % r) * 4)) | ||
} | ||
return keccak(mix, 512) | ||
return Buffer.from(keccak512(mix)) | ||
} | ||
@@ -209,3 +226,3 @@ | ||
const w = Math.floor(params.MIX_BYTES / params.WORD_BYTES) | ||
const s = keccak(Buffer.concat([val, bufReverse(nonce)]), 512) | ||
const s = Buffer.from(keccak512(Buffer.concat([val, bufReverse(nonce)]))) | ||
const mixhashes = Math.floor(params.MIX_BYTES / params.HASH_BYTES) | ||
@@ -236,3 +253,3 @@ let mix = Buffer.concat(Array(mixhashes).fill(s)) | ||
mix: cmix, | ||
hash: keccak256(Buffer.concat([s, cmix])), | ||
hash: Buffer.from(keccak256(Buffer.concat([s, cmix]))), | ||
} | ||
@@ -242,7 +259,7 @@ } | ||
cacheHash() { | ||
return keccak256(Buffer.concat(this.cache)) | ||
return Buffer.from(keccak256(Buffer.concat(this.cache))) | ||
} | ||
headerHash(rawHeader: Buffer[]) { | ||
return rlphash(rawHeader.slice(0, -2)) | ||
return Buffer.from(keccak256(RLP.encode(bufArrToArr(rawHeader.slice(0, -2))))) | ||
} | ||
@@ -253,3 +270,3 @@ | ||
*/ | ||
async loadEpoc(number: number) { | ||
async loadEpoc(number: bigint) { | ||
const epoc = getEpoc(number) | ||
@@ -276,3 +293,3 @@ | ||
} catch (error: any) { | ||
if (error.type !== 'NotFoundError') { | ||
if (error.code !== 'LEVEL_NOT_FOUND') { | ||
throw error | ||
@@ -292,3 +309,3 @@ } | ||
} catch (error: any) { | ||
if (error.type !== 'NotFoundError') { | ||
if (error.code !== 'LEVEL_NOT_FOUND') { | ||
throw error | ||
@@ -299,4 +316,4 @@ } | ||
if (!data) { | ||
this.cacheSize = getCacheSize(epoc) | ||
this.fullSize = getFullSize(epoc) | ||
this.cacheSize = await getCacheSize(epoc) | ||
this.fullSize = await getFullSize(epoc) | ||
@@ -318,3 +335,2 @@ const [seed, foundEpoc] = await findLastSeed(epoc) | ||
} else { | ||
// Object.assign(this, data) | ||
this.cache = data.cache.map((a: Buffer) => { | ||
@@ -343,7 +359,7 @@ return Buffer.from(a) | ||
await this.loadEpoc(number.toNumber()) | ||
await this.loadEpoc(number) | ||
const a = this.run(headerHash, nonce) | ||
const result = new BN(a.hash) | ||
const result = bufferToBigInt(a.hash) | ||
return a.mix.equals(mixHash) && TWO_POW256.div(difficulty).cmp(result) === 1 | ||
return a.mix.equals(mixHash) && TWO_POW256 / difficulty > result | ||
} | ||
@@ -350,0 +366,0 @@ |
@@ -1,3 +0,3 @@ | ||
import { BN, keccak256 } from 'ethereumjs-util' | ||
const MR = require('miller-rabin') | ||
import { isProbablyPrime } from 'bigint-crypto-utils' | ||
import { keccak256 } from 'ethereum-cryptography/keccak' | ||
@@ -19,10 +19,8 @@ export const params = { | ||
export function getCacheSize(epoc: number) { | ||
const mr = new MR() | ||
let sz = | ||
(exports.params.CACHE_BYTES_INIT as number) + | ||
(exports.params.CACHE_BYTES_GROWTH as number) * epoc | ||
sz -= exports.params.HASH_BYTES | ||
while (!mr.test(new BN(sz / exports.params.HASH_BYTES))) { | ||
sz -= 2 * exports.params.HASH_BYTES | ||
export async function getCacheSize(epoc: number) { | ||
const { CACHE_BYTES_INIT, CACHE_BYTES_GROWTH, HASH_BYTES } = params | ||
let sz = CACHE_BYTES_INIT + CACHE_BYTES_GROWTH * epoc | ||
sz -= HASH_BYTES | ||
while (!(await isProbablyPrime(sz / HASH_BYTES, undefined, true))) { | ||
sz -= 2 * HASH_BYTES | ||
} | ||
@@ -32,10 +30,8 @@ return sz | ||
export function getFullSize(epoc: number) { | ||
const mr = new MR() | ||
let sz = | ||
(exports.params.DATASET_BYTES_INIT as number) + | ||
(exports.params.DATASET_BYTES_GROWTH as number) * epoc | ||
sz -= exports.params.MIX_BYTES | ||
while (!mr.test(new BN(sz / exports.params.MIX_BYTES))) { | ||
sz -= 2 * exports.params.MIX_BYTES | ||
export async function getFullSize(epoc: number) { | ||
const { DATASET_BYTES_INIT, DATASET_BYTES_GROWTH, MIX_BYTES } = params | ||
let sz = DATASET_BYTES_INIT + DATASET_BYTES_GROWTH * epoc | ||
sz -= MIX_BYTES | ||
while (!(await isProbablyPrime(sz / MIX_BYTES, undefined, true))) { | ||
sz -= 2 * MIX_BYTES | ||
} | ||
@@ -45,4 +41,4 @@ return sz | ||
export function getEpoc(blockNumber: number) { | ||
return Math.floor(blockNumber / exports.params.EPOCH_LENGTH) | ||
export function getEpoc(blockNumber: bigint) { | ||
return Number(blockNumber / BigInt(params.EPOCH_LENGTH)) | ||
} | ||
@@ -60,3 +56,3 @@ | ||
for (let i = begin; i < end; i++) { | ||
seed = keccak256(seed) | ||
seed = Buffer.from(keccak256(seed)) | ||
} | ||
@@ -63,0 +59,0 @@ return seed |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
69207
8
66194
13
854
1
4
+ Addedabstract-level@^1.0.3
+ Addedbigint-crypto-utils@^3.0.23
+ Addedethereum-cryptography@^1.0.3
+ Addedrlp@4.0.0-beta.1
+ Added@ethereumjs/block@4.0.0-beta.1(transitive)
+ Added@ethereumjs/common@3.0.0-beta.1(transitive)
+ Added@ethereumjs/trie@5.0.0-beta.1(transitive)
+ Added@ethereumjs/tx@4.0.0-beta.1(transitive)
+ Added@ethereumjs/util@8.0.0-beta.1(transitive)
+ Added@noble/hashes@1.2.0(transitive)
+ Added@noble/secp256k1@1.7.1(transitive)
+ Added@scure/base@1.1.9(transitive)
+ Added@scure/bip32@1.1.5(transitive)
+ Added@scure/bip39@1.1.1(transitive)
+ Addedabstract-level@1.0.4(transitive)
+ Addedbigint-crypto-utils@3.3.0(transitive)
+ Addedbrowser-level@1.0.1(transitive)
+ Addedbuffer@6.0.3(transitive)
+ Addedcatering@2.1.1(transitive)
+ Addedclassic-level@1.4.1(transitive)
+ Addedethereum-cryptography@1.2.0(transitive)
+ Addedis-buffer@2.0.5(transitive)
+ Addedlevel@8.0.1(transitive)
+ Addedlevel-supports@4.0.1(transitive)
+ Addedlevel-transcoder@1.0.1(transitive)
+ Addedmemory-level@1.0.0(transitive)
+ Addedmodule-error@1.0.2(transitive)
+ Addednapi-macros@2.2.2(transitive)
+ Addedqueue-microtask@1.2.3(transitive)
+ Addedrlp@4.0.0-beta.1(transitive)
+ Addedrun-parallel-limit@1.1.0(transitive)
- Removed@types/levelup@^4.3.0
- Removedethereumjs-util@^7.1.1
- Removed@ethereumjs/block@3.6.3(transitive)
- Removed@ethereumjs/common@2.6.5(transitive)
- Removed@ethereumjs/tx@3.5.2(transitive)
- Removed@types/abstract-leveldown@7.2.5(transitive)
- Removed@types/bn.js@5.1.6(transitive)
- Removed@types/level-errors@3.0.2(transitive)
- Removed@types/levelup@4.3.3(transitive)
- Removed@types/node@22.9.0(transitive)
- Removed@types/pbkdf2@3.1.2(transitive)
- Removed@types/secp256k1@4.0.6(transitive)
- Removedabstract-leveldown@6.2.36.3.0(transitive)
- Removedbase-x@3.0.10(transitive)
- Removedblakejs@1.2.1(transitive)
- Removedbn.js@5.2.1(transitive)
- Removedbrowserify-aes@1.2.0(transitive)
- Removedbs58@4.0.1(transitive)
- Removedbs58check@2.1.2(transitive)
- Removedbuffer@5.7.1(transitive)
- Removedbuffer-xor@1.0.3(transitive)
- Removedcipher-base@1.0.4(transitive)
- Removedcreate-hash@1.2.0(transitive)
- Removedcreate-hmac@1.1.7(transitive)
- Removeddeferred-leveldown@5.3.0(transitive)
- Removedelliptic@6.6.0(transitive)
- Removedencoding-down@6.3.0(transitive)
- Removederrno@0.1.8(transitive)
- Removedethereum-cryptography@0.1.3(transitive)
- Removedethereumjs-util@7.1.5(transitive)
- Removedevp_bytestokey@1.0.3(transitive)
- Removedhash-base@3.1.0(transitive)
- Removedhash.js@1.1.7(transitive)
- Removedhmac-drbg@1.0.1(transitive)
- Removedimmediate@3.2.33.3.0(transitive)
- Removedkeccak@3.0.4(transitive)
- Removedlevel-codec@9.0.2(transitive)
- Removedlevel-concat-iterator@2.0.1(transitive)
- Removedlevel-errors@2.0.1(transitive)
- Removedlevel-iterator-stream@4.0.2(transitive)
- Removedlevel-mem@5.0.1(transitive)
- Removedlevel-packager@5.1.1(transitive)
- Removedlevel-supports@1.0.1(transitive)
- Removedlevel-ws@2.0.0(transitive)
- Removedlevelup@4.4.0(transitive)
- Removedltgt@2.2.1(transitive)
- Removedmd5.js@1.3.5(transitive)
- Removedmemdown@5.1.0(transitive)
- Removedmerkle-patricia-tree@4.2.4(transitive)
- Removedminimalistic-assert@1.0.1(transitive)
- Removedminimalistic-crypto-utils@1.0.1(transitive)
- Removednode-addon-api@2.0.25.1.0(transitive)
- Removedpbkdf2@3.1.2(transitive)
- Removedprr@1.0.1(transitive)
- Removedrandombytes@2.1.0(transitive)
- Removedripemd160@2.0.2(transitive)
- Removedrlp@2.2.7(transitive)
- Removedscrypt-js@3.0.1(transitive)
- Removedsecp256k1@4.0.4(transitive)
- Removedsetimmediate@1.0.5(transitive)
- Removedsha.js@2.4.11(transitive)
- Removedundici-types@6.19.8(transitive)
- Removedxtend@4.0.2(transitive)