mirror of
https://github.com/lcn2/calc.git
synced 2025-08-16 01:03:29 +03:00
Release calc version 2.10.3t5.45
This commit is contained in:
@@ -22,27 +22,33 @@
|
||||
* chongo was here /\../\ chongo@toad.com
|
||||
*/
|
||||
|
||||
global lib_debug; /* 1 => print debug statements */
|
||||
|
||||
/*
|
||||
* seedrandom - seed the cryptographically strong Blum generator
|
||||
*
|
||||
* This function will seed the random() generator using a method
|
||||
* similar to method suggested for the paranoid in the zrand.c source
|
||||
* file and random help file.
|
||||
*
|
||||
* The period of a Blum generators with modulus 'n=p*q' (where p and
|
||||
* q are primes 3 mod 4) is:
|
||||
*
|
||||
* lambda(n) = lcm(factors of p-1 & q-1)
|
||||
*
|
||||
* One can construct a generator with a maximal period when
|
||||
* 'p' and 'q' have the fewest possible factors in common.
|
||||
* The quickest way to select such primes is only use 'p'
|
||||
* and 'q' when '(p-1)/2' and '(q-1)/2' are both primes.
|
||||
* This function will seed the random() generator that uses
|
||||
* such primes.
|
||||
*
|
||||
* given:
|
||||
* seed1 - a large random value (at least 10^20 and perhaps < 10^93)
|
||||
* seed2 - a large random value (at least 10^20 and perhaps < 10^93)
|
||||
* size - min Blum modulus as a power of 2 (at least 100, perhaps > 1024)
|
||||
* seed1 - a large random value (at least 10^20 and perhaps < 10^314)
|
||||
* seed2 - a large random value (at least 10^20 and perhaps < 10^314)
|
||||
* size - min Blum modulus as a power of 2 (at least 32, perhaps >= 512)
|
||||
* trials - number of ptest() trials (default 25)
|
||||
*
|
||||
* returns:
|
||||
* the previous random state
|
||||
*
|
||||
* NOTE: The [10^20, 10^93) range comes from [2^64, 2^64*fact(55)) range
|
||||
* where seeds are effective for srand(). All we really need to
|
||||
* do is to insist that a seed is > 2^64, which the 10^20 limit does.
|
||||
* NOTE: The [10^20, 10^314) range comes from the fact that the 13th internal
|
||||
* modulus is ~10^315. We want the lower bound seed to be reasonably big.
|
||||
*/
|
||||
define seedrandom(seed1, seed2, size, trials)
|
||||
{
|
||||
@@ -55,10 +61,9 @@ define seedrandom(seed1, seed2, size, trials)
|
||||
local n; /* Blum modulus */
|
||||
local binsize; /* smallest power of 2 > n=p*q */
|
||||
local r; /* initial quadratic residue */
|
||||
local rand_state; /* the initial rand state */
|
||||
local rand_junk; /* rand state that is not needed */
|
||||
local random_state; /* the initial rand state */
|
||||
local random_junk; /* rand state that is not needed */
|
||||
local old_state; /* old random state to return */
|
||||
local random_cfg; /* old srandom configuration value */
|
||||
|
||||
/*
|
||||
* firewall
|
||||
@@ -76,14 +81,13 @@ define seedrandom(seed1, seed2, size, trials)
|
||||
trials = 25;
|
||||
}
|
||||
if (digits(seed1) <= 20) {
|
||||
quit "1st arg (seed1) must be > 10^20 and perhaps < 10^93";
|
||||
quit "1st arg (seed1) must be > 10^20 and perhaps < 10^314";
|
||||
}
|
||||
if (digits(seed2) <= 20) {
|
||||
quit "2nd arg (seed2) must be > 10^20 and perhaps < 10^93";
|
||||
quit "2nd arg (seed2) must be > 10^20 and perhaps < 10^314";
|
||||
}
|
||||
if (size < 100) {
|
||||
/* 3% of 100 is 2.97 < 3 whereas 3% of 100 is 3 */
|
||||
quit "3rd arg (size) needs to be > 66 (perhaps >= 1024)";
|
||||
if (size < 32) {
|
||||
quit "3rd arg (size) needs to be >= 32 (perhaps >= 512)";
|
||||
}
|
||||
if (trials < 1) {
|
||||
quit "4th arg (trials) must be > 0";
|
||||
@@ -99,38 +103,54 @@ define seedrandom(seed1, seed2, size, trials)
|
||||
/*
|
||||
* find the first Blum prime
|
||||
*/
|
||||
rand_state = srand(seed1);
|
||||
random_state = srandom(seed1, 13);
|
||||
do {
|
||||
fp = nextcand(2^sp+randbit(sp), trials, 0, 3, 4);
|
||||
p = 2*fp+1;
|
||||
} while (ptest(p,trials) == 0);
|
||||
do {
|
||||
fp = nextcand(2^sp+randombit(sp), 1, 1, 3, 4);
|
||||
p = 2*fp+1;
|
||||
} while (ptest(p,1,0) == 0);
|
||||
} while(ptest(p, trials) == 0 || ptest(fp, trials) == 0);
|
||||
if (config("lib_debug") > 0) {
|
||||
print "/* 1st Blum prime */ p=", p;
|
||||
}
|
||||
|
||||
/*
|
||||
* find the 2nd Blum prime
|
||||
*/
|
||||
rand_junk = srand(seed2);
|
||||
random_junk = srandom(seed2, 13);
|
||||
do {
|
||||
fq = nextcand(2^sq+randbit(sq), trials, 0, 3, 4);
|
||||
q = 2*fq+1;
|
||||
} while (ptest(q,trials) == 0);
|
||||
do {
|
||||
fq = nextcand(2^sq+randombit(sq), 1, 1, 3, 4);
|
||||
q = 2*fq+1;
|
||||
} while (ptest(q,1,0) == 0);
|
||||
} while(ptest(q, trials) == 0 || ptest(fq, trials) == 0);
|
||||
if (config("lib_debug") > 0) {
|
||||
print "/* 2nd Blum prime */ q=", q;
|
||||
}
|
||||
|
||||
/*
|
||||
* seed the Blum generator
|
||||
*/
|
||||
n = p*q; /* the Blum modulus */
|
||||
binsize = higbbit(n)+1; /* smallest power of 2 > p*q */
|
||||
binsize = highbit(n)+1; /* smallest power of 2 > p*q */
|
||||
r = pmod(rand(1<<ceil(binsize*4/5), 1<<(binsize-2)), 2, n);
|
||||
random_cfg = config("srandom", 0); /* no checks are needed */
|
||||
if (config("lib_debug") >= 0) {
|
||||
print "/* seed quadratic residue */ r=", r;
|
||||
print "/* newn", binsize, "bit quadratic residue*/ newn=", n;
|
||||
}
|
||||
old_state = srandom(r, n);
|
||||
|
||||
/*
|
||||
* restore other states that we altered
|
||||
*/
|
||||
rand_junk = srand(rand_state);
|
||||
rand_junk = config("srandom", random_cfg);
|
||||
random_junk = srandom(random_state);
|
||||
|
||||
/*
|
||||
* return the previous random state
|
||||
*/
|
||||
return old_state;
|
||||
}
|
||||
|
||||
if (config("lib_debug") >= 0) {
|
||||
print "seedrandom(seed1, seed2, size [, trials]) defined";
|
||||
}
|
||||
|
Reference in New Issue
Block a user