Skip to content
Snippets Groups Projects
  • Simon Tatham's avatar
    6b1fbfe5
    PrimeCandidateSource: add Sophie Germain filtering. · 6b1fbfe5
    Simon Tatham authored
    A Sophie Germain prime is a prime p such that 2p+1 is also prime. The
    larger prime of the pair 2p+1 is also known as a 'safe prime', and is
    the preferred kind of modulus for conventional Diffie-Hellman.
    
    Generating these is harder work than normal prime generation. There's
    not really much of a technique except to just keep generating
    candidate primes p and then testing 2p+1. But what you _can_ do to
    speed things up is to get the prime-candidate generator to help a bit:
    it's already enforcing that no small prime divides p, and it's easy to
    get it to also enforce that no small prime divides 2p+1. That check
    can filter out a lot of bad candidates early, before you waste time on
    the more expensive checks, so you have a better chance of success with
    each number that gets as far as Miller-Rabin.
    
    Here I add an extra setup function for PrimeCandidateSource which
    enables those extra checks. After you call pcs_try_sophie_germain(),
    the PCS will only deliver you numbers for which both p and 2p+1 are
    free of small factors.
    6b1fbfe5
    History
    PrimeCandidateSource: add Sophie Germain filtering.
    Simon Tatham authored
    A Sophie Germain prime is a prime p such that 2p+1 is also prime. The
    larger prime of the pair 2p+1 is also known as a 'safe prime', and is
    the preferred kind of modulus for conventional Diffie-Hellman.
    
    Generating these is harder work than normal prime generation. There's
    not really much of a technique except to just keep generating
    candidate primes p and then testing 2p+1. But what you _can_ do to
    speed things up is to get the prime-candidate generator to help a bit:
    it's already enforcing that no small prime divides p, and it's easy to
    get it to also enforce that no small prime divides 2p+1. That check
    can filter out a lot of bad candidates early, before you waste time on
    the more expensive checks, so you have a better chance of success with
    each number that gets as far as Miller-Rabin.
    
    Here I add an extra setup function for PrimeCandidateSource which
    enables those extra checks. After you call pcs_try_sophie_germain(),
    the PCS will only deliver you numbers for which both p and 2p+1 are
    free of small factors.