HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

Generating very large prime numbers and their generator modulo N in php

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
phpnumbersgeneratorgeneratinglargeveryandprimemodulotheir

Problem

I recently came up with the idea to attempt generating ungodly large prime numbers and their generators modulo N in php. I don't know much about number theory and just wanted to get some comments to see if this code should be sucessful. The output is just directed to a file at the moment because I didn't want to wast time on making it nice until I got the fundamentals right.

failed to find generator modulo N. Please run setup again.");
        exit;
    }

    #we have both so lets do the setup
    $setupfile      = "Prime.inc";
    $safe_prime     = gmp_strval($safe_prime, 16);
    $g              = "$g\n";
    @touch($setupfile) or die("Unable to create setup file at this time. Please rerun setup.");
    @$fh = fopen($setupfile, 'x') or die("Unable to write to setup file/setup file already exists.(Prime.inc)");
    @fwrite($fh, $g) or die("Unable to write generator to setup file.(Prime.inc)");
    @fwrite($fh, $safe_prime) or die("Unable to write prime to setup file.(Prime.inc)");
    @fclose($fh);
    echo "Prime.inc created and populated sucessfully.";
}else{
    #we did not find the safe prime we were looking for
    echo "Setup Unsuccessful";
    echo "Please Rerun Setup";
}
exit;
?>

Solution

Instead of a global conditional operator is much more advantageous to use the boundary conditions

For example:

if ($prime_fail){
  message_error(PRIME_FAIL);
  exit;
}

$generator_found = false;
$g_upper_limit = 10; 
$onemod = gmp_intval(gmp_mod(1, $safe_prime));
// ...

Code Snippets

if ($prime_fail){
  message_error(PRIME_FAIL);
  exit;
}

$generator_found = false;
$g_upper_limit = 10; 
$onemod = gmp_intval(gmp_mod(1, $safe_prime));
// ...

Context

StackExchange Code Review Q#2215, answer score: 3

Revisions (0)

No revisions yet.