[Cryptographic_Clang] Fix lost borrow in bignum_subtract when a limb is 0xFFFFFFFF

Open Beginner friendly
#417 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
79/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
cpp
Domain
cryptography

Research direction

Read Security_Algorithms/Cryptographic_Clang/bignum.c, focusing on bignum_subtract and how it computes each limb’s difference and borrow. Reproduce the failure with operands whose limbs include 0xFFFFFFFF, then run the random identity and Python differential checks described in the issue. Done means both checks pass with no incorrect subtractions.

Written by the indexing model from the issue text.

Description

area: security-algorithms priority: P1 size: XS type: bug

Summary

bignum_subtract computes the borrow from temp = carry + b2->data[i] in 32 bits. When the incoming borrow is 1 and the limb of b2 is 0xFFFFFFFF, temp wraps to 0, the limb difference is computed against 0 and the borrow is lost. RSA key generation uses subtraction (p - 1, q - 1), so wrong results can propagate into phi.

Location

  • Security_Algorithms/Cryptographic_Clang/bignum.c (bignum_subtract)

Evidence

Differential test against Python integers, 3,000 random operands biased towards 0xFFFFFFFF limbs: 379 subtractions wrong. Found while fixing #136 and #137.

Expected

(a + b) - b == a for all operands with a >= b.

Proposed fix

Accumulate the subtrahend limb plus borrow in 64 bits and derive the borrow from that comparison.

Acceptance criteria

  • Random identity test (a + b) - b == a passes for 20,000 operands
  • Differential test against Python reports no wrong subtraction

Found while fixing the bignum issues · Phase 3 · Build & correctness


Generated by Claude Code

Dominant language
C++
Stars
2
Forks
1
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ibra-kdbra/Math-Problems

All issues in ibra-kdbra/Math-Problems

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.