Skip to content
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

Open
thogiti opened this issue Oct 15, 2024 · 7 comments · May be fixed by #361
Open

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

thogiti opened this issue Oct 15, 2024 · 7 comments · May be fixed by #361
Assignees
Labels
audit 🔍 This issue is related to an audit. bug 🐛 Something isn't working good first issue Good for newcomers

Comments

@thogiti
Copy link

thogiti commented Oct 15, 2024

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

  • Functional Integrity: Incorrect carry values can corrupt the internal state, leading to wrong hash outputs.
  • Security Risks: The integrity of the hash function is compromised, potentially allowing for hash collisions or predictable outputs, which undermines the cryptographic strength of Blake2-512.

Recommendation

  • Modify the carry calculation to ensure that only a single carry bit (0 or 1) is propagated. For example:
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;
  • Alternatively, use BigInt for precise 64-bit arithmetic operations as in the original Blake implementation in the npm repo, which TypeScript supports, to handle carries correctly without manual intervention.
@thogiti thogiti added the bug 🐛 Something isn't working label Oct 15, 2024
@github-project-automation github-project-automation bot moved this to 📋 Backlog in ZK-Kit Oct 15, 2024
@cedoor cedoor added the audit 🔍 This issue is related to an audit. label Oct 15, 2024
@cedoor
Copy link
Member

cedoor commented Oct 24, 2024

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

@cedoor cedoor added the good first issue Good for newcomers label Oct 24, 2024
@hannahredler
Copy link
Contributor

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

@cedoor
Copy link
Member

cedoor commented Nov 22, 2024

@hannahredler sure, let us know if you'd like to work on this and we'll assign it to you.

@Arch0125
Copy link
Contributor

@cedoor Hey ive built a wrapper around the original blake-hash implementation, let me know if i can open a PR for review

@cedoor
Copy link
Member

cedoor commented Dec 10, 2024

Hi @Arch0125 , of course! I'll assign this issue to you

@cedoor cedoor moved this from ♻️ Grooming to 🏗 In Progress in ZK-Kit Dec 10, 2024
Arch0125 added a commit to Arch0125/zk-kit that referenced this issue Dec 12, 2024
…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
@Arch0125
Copy link
Contributor

@cedoor ive raised a PR here #361

@artwyman
Copy link
Collaborator

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:

  • Zupass and related apps have already issued signed data in the POD format which makes use of the hashes in zk-kit. I'd like to know how likely we are to deal with signatures failing after this update.
  • In fix(eddsa-poseidon): fixes carry handling of g function by introducing wrapper over original implementation #361 it would be great to validate the fix with a targeted unit test using an input which would produce incorrect results before the PR, and correct results after the PR. Assuming this bug doesn't exist in the circomlibjs implementation of blake512, such a test could fit in with the compatibility tests which compare results between the two libraries.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
audit 🔍 This issue is related to an audit. bug 🐛 Something isn't working good first issue Good for newcomers
Projects
Status: 🏗 In Progress
5 participants