Skip to content

security(ntt): make Barrett reduction branchless, closing the constant-time gap#55

Merged
erfanjazebnikoo merged 1 commit into
devfrom
fix/ntt-branchless-barrett-reduction
Jul 10, 2026
Merged

security(ntt): make Barrett reduction branchless, closing the constant-time gap#55
erfanjazebnikoo merged 1 commit into
devfrom
fix/ntt-branchless-barrett-reduction

Conversation

@erfanjazebnikoo

Copy link
Copy Markdown
Member

Summary

Direct response to feedback on r/C_Programming (Bman1296, robchroma, AttractiveDaddy) on the constant-time caveat documented in docs/algorithms/ntt.md. u/robchroma proposed a branchless canonicalization for priv_barrett that avoids the implementation-defined-behavior problem with the reference Kyber technique.

Verification

  • Exhaustively checked against a % q for all 22,164,483 valid inputs in [0, 2*q^2], zero mismatches (not spot-checked)
  • Full test suite: 335/335 passing
  • AddressSanitizer + UndefinedBehaviorSanitizer: clean
  • -std=c99 -pedantic-errors -Wall -Wextra -Werror on both gcc and clang: zero warnings

Docs

Updates the constant-time warning in docs/algorithms/ntt.md and README.md to reflect the fix, while keeping the honest caveat that this isn't a formally audited cryptographic primitive. Credits robchroma in CONTRIBUTORS.md, adds a ### Security entry to CHANGELOG.md.

Not done here (separate, larger decision)

This closes the specific constant-time gap that was documented and publicly flagged. It does not constitute a security audit of the NTT module as a whole.

…t-time gap

Replaces priv_barrett's data-dependent canonicalization (if r<0 ... if r>=q ...)
with a branchless version using only unsigned arithmetic and a well-defined C99
boolean comparison, contributed by u/robchroma on r/C_Programming in response to
the constant-time caveat this project had documented.

Exhaustively verified against a % q for all 22,164,483 valid inputs in [0, 2*q^2]
before adoption, not spot-checked. Rebuilt and re-ran the full test suite (335
tests) plus AddressSanitizer/UndefinedBehaviorSanitizer, clean under both, and
recompiled under -std=c99 -pedantic-errors -Wall -Wextra -Werror on gcc and
clang with zero warnings.

Updates docs/algorithms/ntt.md and README.md to reflect that no known
data-dependent branches remain in the transform, while keeping the honest
caveat that this is not a formally audited cryptographic primitive. Credits
robchroma in CONTRIBUTORS.md and adds a CHANGELOG entry.
@erfanjazebnikoo
erfanjazebnikoo merged commit d90ce5f into dev Jul 10, 2026
@erfanjazebnikoo erfanjazebnikoo self-assigned this Jul 10, 2026
@erfanjazebnikoo
erfanjazebnikoo deleted the fix/ntt-branchless-barrett-reduction branch July 10, 2026 17:08
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.

1 participant