/** * Generate a number that lies between 0 and q-1. * * @param \phpseclib\Math\BigInteger $q Max number. * * @return \phpseclib\Math\BigInteger Generated number. */ public static function randomNumber($q) { $bytes = strlen($q->toBytes()) + 8; $ints = $bytes + 1 >> 2; $cstring = Crypt\Random::String($ints); $random = ''; for ($i = 0; $i < $ints; ++$i) { $random .= pack('N', $cstring[$i]); } $c = new BigInteger(substr($random, 0, $bytes), 256); $one = new BigInteger(1); $result_base = $c->divide($q->subtract($one)); return $result_base[1]->add($one); }