-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature / Keystore Extra Entropy #1295
base: v2
Are you sure you want to change the base?
Changes from all commits
1ed448a
8b4477b
f40742b
a65a59a
f1dfb5a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* eslint-disable no-bitwise */ | ||
import { getBytes, keccak256, randomBytes } from 'ethers' | ||
|
||
export class EntropyGenerator { | ||
#entropyPool: Uint8Array = new Uint8Array(0) | ||
|
||
generateRandomBytes(length: number, extraEntropy: string): Uint8Array { | ||
this.#resetEntropyPool() | ||
this.#collectSystemNoiseEntropy() | ||
this.#collectTimeEntropy() | ||
|
||
if (extraEntropy) { | ||
const encoder = new TextEncoder() | ||
const uint8Array = encoder.encode(extraEntropy) | ||
this.addEntropy(uint8Array) | ||
} | ||
|
||
if (this.#entropyPool.length === 0) throw new Error('Entropy pool is empty') | ||
|
||
const hash = getBytes(keccak256(this.#entropyPool)) | ||
const randomBytesGenerated = randomBytes(length) | ||
// ensures non-deterministic final output | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this comment is incorrect - not sure what you meant but it is deterministic - the same input will always generate the same output |
||
for (let i = 0; i < length; i++) { | ||
randomBytesGenerated[i] ^= hash[i % hash.length] | ||
} | ||
|
||
return randomBytesGenerated | ||
} | ||
|
||
#collectTimeEntropy(): void { | ||
const now = performance.now() | ||
|
||
if (!now) return | ||
|
||
const timeEntropy = new Uint8Array(new Float64Array([now]).buffer) | ||
this.addEntropy(timeEntropy) | ||
} | ||
|
||
#collectSystemNoiseEntropy(): void { | ||
const systemNoise = randomBytes(16) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that's not really system noise though |
||
this.addEntropy(systemNoise) | ||
} | ||
|
||
addEntropy(newEntropy: Uint8Array): void { | ||
const combined = new Uint8Array(this.#entropyPool.length + newEntropy.length) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there a better way to concat? |
||
combined.set(this.#entropyPool) | ||
combined.set(newEntropy, this.#entropyPool.length) | ||
this.#entropyPool = combined | ||
} | ||
|
||
#resetEntropyPool() { | ||
this.#entropyPool = new Uint8Array(0) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
where do we take the extra entropy from?