/** * Generate modulated number * * Generates a number that lies between 0 and q-1 * * @access public * @static * @staticvar MathBigInteger $one Constant one * @param MathBigInteger $q Modulation * @return MathBigInteger Generated number */ public static function randomNumberMod($q) { // do a few more bits than q so we can wrap around with not too much bias // wow, turns out this was actually not far off from FIPS186-3, who knew? // FIPS186-3 says to generate 64 more bits than needed into "c", then to do: // result = (c mod (q-1)) + 1 static $one; if (!isset($one)) { $one = new MathBigInteger(1); } $c = self::_os2ip(self::_random(strlen($q->toBytes()) + 8)); $result_base = $c->divide($q->subtract($one)); return $result_base[1]->add($one); }