-
Notifications
You must be signed in to change notification settings - Fork 92
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
Fix carry handling in the g function in blake.ts #344
Comments
A better solution for this issue might be to use the original Blake implementation directly, wrapping it with a TS class. |
Hey, if you need someone to work on this I would be happy to do so and replace the custom implementation with a wrapper over this implementation |
@hannahredler sure, let us know if you'd like to work on this and we'll assign it to you. |
@cedoor Hey ive built a wrapper around the original blake-hash implementation, let me know if i can open a PR for review |
Hi @Arch0125 , of course! I'll assign this issue to you |
…g original blake-hash imp This commit replaces the existing implementation of blake-hash with a TS wrapper over original blake-hash package and exposing required methods along with specified types for each re privacy-scaling-explorations#344
Is there a known set of real inputs which can demonstrate this error? What's the likelihood of this occurring in the wild with non-malicious inputs? Two contexts in which I ask these questions:
|
Crafting specific inputs that cause the internal state to reach conditions where lo exceeds 0x100000000 (causing multiple carries) is non-trivial due to the complexity of the Blake2-512 algorithm. But, may be you can implement a targeted unit test that artificially manipulates the internal state to simulate the buggy carry handling. import { Blake512 } from './Blake512'; // Path to Implementation
// Mock the 'g' function to force 'lo' to exceed the carry threshold
function mockGWithHighLo(v: number[], m: number[], i: number, a: number, b: number, c: number, d: number, e: number): void {
let lo = 0x2FFFFFFFC; // Force 'lo' to a high value
console.log(`Mocked lo: ${lo.toString(16)}`); // Logging
v[a * 2] = (v[a * 2] + ((m[sigma[i][e] * 2] ^ u512[sigma[i][e + 1] * 2]) >>> 0) + v[b * 2] + ~~(lo / 0x0100000000)) >>> 0;
v[a * 2 + 1] = lo >>> 0;
// Continue with the rest of the function as per the original 'g'
// ...
}
// Replace the original 'g' function with the mocked version for testing
Blake512.prototype.g = mockGWithHighLo;
// Create a hash instance and perform updates
const blake = new Blake512();
blake.update(Buffer.from('test input'));
// Finalize the hash
const hash = blake.digest();
// Verify that the carry handling was incorrect
console.log(hash.toString('hex')); |
Incorrect Carry Handling in the
g
FunctionThe
g
function in Implementation inblake.ts
uses~~(lo / 0x0100000000)
to compute the carry from the lower 32 bits of a 64-bit word.Since
lo
can be up to0x2FFFFFFFC
(i.e., approximately 3 times0x0100000000
), the carry can erroneously be 2 or 3.Impact
Recommendation
The text was updated successfully, but these errors were encountered: