Skip to content

Conversation

@manishbista28
Copy link
Contributor

Current blake3_u4 implementation has an issue which generates incorrect output hash for some input message.

blake3_u4 operates over nibbles and looks up modulo and quotient values in a precompute table. The size of table depends upon the range of input index.
For example if you want to sum three number 0xfe + 0xff + 0xff, the impl proceeds in following steps:

  1. sum lower limb: 0xe + 0xf + 0xf => 0x2c (=44)
  2. find 44th index in quotient and modulo table to obtain 2 and c respectively. pass 2 as carry.
  3. sum higher limb: 0xf + 0xf + 0xf + 0x2 = 0x2f (=47)
  4. find 47th index in quotient and modulo table. But 47th index doesn't exist because the table has total size 47 starting from 0. As such any value above this stack position is returned, thus generating wrong result.

The fix adds an extra element on the precompute table making it of size 48. Since blake3_u4 only adds 3 elements at max, this new precompute table length suffices for the entire range of input.

A test has been added which fails if the fix is not present.

@jonasmartin
Copy link
Contributor

@manishbista28 Nice catch!
I miss calculated the maximun number possible to be added in a limb. The fix looks good to me.
👍

@lucidLuckylee
Copy link
Contributor

Nice work! Thank you for the PR!

@lucidLuckylee lucidLuckylee merged commit be9cf2b into BitVM:main Dec 16, 2024
stillsaiko pushed a commit that referenced this pull request Dec 16, 2024
* fix(blake3_u4): issue with insufficient size of precompute table

* chore: use get_script len function

---------

Co-authored-by: Manish Bista <[email protected]>
stillsaiko added a commit that referenced this pull request Dec 16, 2024
* Chore `fp_lc_mul` clippy lint `reversed_empty_ranges`

* Chore `#![allow(clippy::*)]`

* Chore `.div_ceil()` in `fq.rs`

* fix(blake3_u4): issue with insufficient size of precompute table (#154)

* fix(blake3_u4): issue with insufficient size of precompute table

* chore: use get_script len function

---------

Co-authored-by: Manish Bista <[email protected]>

* fix: issue where precompute p function bakes runtime input (#155)

* fix: issue where precompute p bakes runtime input

* chore: unchanged gitignore

---------

Co-authored-by: Manish Bista <[email protected]>

---------

Co-authored-by: manishbista28 <[email protected]>
Co-authored-by: Manish Bista <[email protected]>
@manishbista28 manishbista28 deleted the fix/issue_blake3_u4_precompute_table branch February 23, 2025 11:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants