privacy-scaling-explorations / zk-kit

A monorepo of reusable libraries for zero-knowledge technologies.
https://zkkit.pse.dev
MIT License
290 stars 74 forks source link

Fix carry handling in the g function in blake.ts #344

Open thogiti opened 1 month ago

thogiti commented 1 month ago

Incorrect Carry Handling in the g Function

The g function in Implementation in blake.ts uses ~~(lo / 0x0100000000) to compute the carry from the lower 32 bits of a 64-bit word.

Since lo can be up to 0x2FFFFFFFC (i.e., approximately 3 times 0x0100000000), the carry can erroneously be 2 or 3.

Impact

Recommendation

const carry = lo >= 0x100000000 ? 1 : 0;
v[a * 2] = (v[a * 2] + ((m[sigma[i][e] * 2] ^ u512[sigma[i][e + 1] * 2]) >>> 0) + v[b * 2] + carry) >>> 0;
cedoor commented 3 weeks ago

A better solution for this issue might be to use the original Blake implementation directly, wrapping it with a TS class.

hannahredler commented 1 week ago

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