Skip to content

goodkey: rsa: detect n = p^2 - #9110

Merged
ezekiel merged 1 commit into
mainfrom
inahga/rsa
Oct 20, 2026
Merged

ezekiel merged 1 commit into
mainfrom
inahga/rsa

Conversation

@inahga

@inahga inahga commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

checkPrimeFactorsTooClose() doesn't check the ultimate case of factors being too close.

@inahga
inahga requested a review from a team as a code owner October 20, 2026 20:04

@aarongable aarongable left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Wish we could do this by just changing the start point of fermat's algorithm, but I don't think there's a clean way to do that due to BigInt.Sqrt taking the floor.

@aarongable
aarongable requested review from a team and jsha and removed request for a team October 20, 2026 21:15
@inahga

inahga commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor Author

It can be done, but it harms comprehensibility IMO

      399 -  a := new(big.Int)                                                                                                  
      400 -  a.Sqrt(n).Add(a, one)                                                                                                                                                                             
      400 +  a := new(big.Int).Sqrt(n)                                                                                          
      401 +  b2 := new(big.Int).Mul(a, a)                                                                                       
      402 +  if b2.Cmp(n) != 0 {                                                                                                
      403 +    a.Add(a, one)                                                                                                    
      404 +    b2.Mul(a, a)                                                                                                     
      405 +  }                                                                                                                  
      406  
      407    // We calculate b2 to see if it is a perfect square (i.e. b^2), and therefore
      408    // b is an integer. Specifically, b2 = a^2 - n.
      404 -  b2 := new(big.Int)                                                                                                 
      405 -  b2.Mul(a, a).Sub(b2, n)                                                                                            
      409 +  b2.Sub(b2, n)                       

@aarongable

Copy link
Copy Markdown
Contributor

Yep agreed, the cleanliness is critical for code that's already this unreadable.

@ezekiel
ezekiel merged commit d102e3b into main Oct 20, 2026
19 checks passed
@ezekiel
ezekiel deleted the inahga/rsa branch October 20, 2026 21:55
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