/usr/include/linbox/randiter/random-prime.h is in liblinbox-dev 1.4.2-5build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 | /* Copyright (C) 2007,2010 LinBox
* Written by <Jean-Guillaume.Dumas@imag.fr>
* Modified by Brice Boyer (briceboyer) <boyer.brice@gmail.com> (RandomPrimeIter)
*
*
*
* ========LICENCE========
* This file is part of the library LinBox.
*
* LinBox is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
* ========LICENCE========
*/
/*! @file randiter/random-prime.h
* @ingroup randiter
* @brief Generates random positive prime \ref integers.
*/
#ifndef __LINBOX_random_prime_iterator_H
#define __LINBOX_random_prime_iterator_H
#include <givaro/givinteger.h>
#include <givaro/givintprime.h>
#include "linbox/util/timer.h"
#include "linbox/util/debug.h"
#include "linbox/field/field-traits.h"
namespace LinBox
{
/*! @brief Random Prime Generator.
* @ingroup primes
* @ingroup randiter
*
* Generates prime of specified length.
* @internal
* It is given by <code>nextprime(2^_bits-p)</code> where <code>size(p) < _bits</code>.
* @todo
* One could use Integer::random_exact.
*/
//template<class Prime_Type = integer>
class RandomPrimeIterator {
private:
uint64_t _bits; //!< common lenght of all primes
integer _shift; //!< @internal used to set proper bit size
integer _prime; //!< the generated prime.
Givaro::IntPrimeDom _IPD; //!< empty struct dealing with primality.
public:
/*! Constructor.
* @param bits size of primes (in bits). Default is 27 so it
* can fit in a <code>Linbox::Modular<double></code>.
* @param seed if \c 0 a seed will be generated, otherwise, the
* provided seed will be use.
*/
RandomPrimeIterator(uint64_t bits = 27, uint64_t seed = 0) :
_bits(bits), _shift(integer(1)<<_bits)
{
linbox_check(bits >1);
if (! seed)
setSeed( BaseTimer::seed() );
else
setSeed( seed );
integer::random(_prime,_bits-1);
_prime = _shift - _prime;
_IPD.nextprimein(_prime);
}
typedef integer Prime_Type ;
/** @brief operator++()
* creates a new random prime.
*/
inline RandomPrimeIterator &operator ++ ()
{
integer::random(_prime,_bits-1);
_prime = _shift - _prime;
_IPD.nextprimein(_prime);
return *this;
}
/** @brief get the random prime.
* returns the actual prime.
*/
const Prime_Type &operator * () const
{
return _prime;
}
/** @brief get the random prime.
* returns the actual prime.
* @warning a new prime is not generated.
*/
const Prime_Type & randomPrime() const
{
return _prime;
}
/** @brief Sets the seed.
* Set the random seed to be \p ul.
* @param ul the new seed.
*/
void static setSeed(uint64_t ul)
{
integer::seeding(ul);
}
void setBits(uint64_t bits) {
_bits = bits;
_shift=(integer(1)<<_bits);
linbox_check(bits >1);
integer::random(_prime,_bits-1);
_prime = _shift - _prime;
_IPD.nextprimein(_prime);
}
template<class _ModField>
void setBitsField()
{
// std::cout << _bits << std::endl;
integer k = FieldTraits<_ModField >::maxModulus();
// std::cout << k << std::endl;
uint64_t bits = (uint64_t)(k.bitsize());
if (!bits)
throw("weird");
--bits;
// std::cout << bits << std::endl;
if (bits < _bits)
setBits(bits);
// std::cout << _bits << std::endl;
}
template<class _ModField>
bool setBitsDelayedField(size_t n)
{
integer k = FieldTraits<_ModField >::maxModulus();
int bits = (int)(k.bitsize());
//std::cout << "maxmodbits: " << bits << std::endl;
if (!bits) throw("weird");
--bits;
bits -= (int)((log((double)n)/2./M_LN2));
//std::cout << "delcorrect: " << bits << std::endl;
if (bits < 0) return false;
if (bits < (int64_t)_bits) setBits((uint64_t)bits);
return true;
}
};
/*! @brief Random Prime Iterator.
* @ingroup primes
* @ingroup randiter
*
* Generates prime of size smaller than a prescribed one.
* This class is closer to the LinBox::RandIterArchetype.
* @todo
* one could create the same one on a LinBox::PID_double ?
*/
class RandomPrimeIter {
uint64_t _bits; //!< max length for all primes
integer _seed; //!< the generated prime.
Givaro::GivRandom _generator;
Givaro::IntPrimeDom _IPD;
public:
/*! Constructor.
* @param bits max size of primes (in bits). Default is 30 so it
* can fit in a <code>Linbox::Modular<double></code>.
* @param seed if \c 0 a seed will be generated, otherwise, the
* provided seed will be use.
*/
RandomPrimeIter(uint64_t bits = 30, uint64_t seed = 0) :
_bits(bits), _generator(seed)
{
linbox_check(bits >1);
if (! seed)
seed = BaseTimer::seed() ;
else
_seed = seed ;
integer::seeding(_seed);
}
/// destructor.
~RandomPrimeIter() {}
/// prime type
typedef integer Prime_Type ;
/// copy constructor.
/// @param R random iterator to be copied.
RandomPrimeIter (const RandomPrimeIter &R) :
_bits(R._bits), _seed(R._seed), _generator(R._generator)
{}
typedef integer Element ;
/// copy.
/// @param R random iterator to be copied.
RandomPrimeIter &operator=(const RandomPrimeIter &R)
{
if (this != &R) {
_bits = R._bits;
_seed = R._seed;
}
return *this;
}
/** @brief get a random prime of maximum size \c _bits .
* @param[out] a a prime number
*/
integer & random (integer & a) const
{
integer::random(a,_bits);
_IPD.nextprimein(a);
while (a.bitsize()>_bits)
_IPD.prevprimein(a);
return a;
}
integer random () const
{
integer a ;
return random(a);
}
integer & random_exact (integer & a) const
{
// integer::random_exact(a,_bits);
integer::random(a,_bits-1); //!@todo uses random_exact when givaro is released.
a = (integer(1)<<_bits) - a;
_IPD.nextprimein(a);
while (a.bitsize()>_bits)
_IPD.prevprimein(a);
return a;
}
integer random_exact () const
{
integer a ;
return random_exact(a);
}
integer & random_between (integer & a, uint64_t _low_bits) const
{
linbox_check(_low_bits < _bits);
// integer::random_exact(a,_bits);
uint64_t ze_bits = (_low_bits + ((_bits - _low_bits)*_generator())) ;
linbox_check (!(ze_bits<_low_bits) && !(ze_bits>_bits));
integer::random(a,ze_bits-1); //!@todo uses random_between when givaro is released.
a = (integer(1)<<ze_bits) - a;
_IPD.nextprimein(a);
while (a.bitsize()>_bits)
_IPD.prevprimein(a);
linbox_check(a.bitsize() >= _low_bits && a.bitsize() <= _bits) ;
return a;
}
integer random_between ( uint64_t _low_bits) const
{
// std::cout << "random between " << _low_bits << " and " << _bits << std::endl;
integer a ;
random_between(a,_low_bits);
// std::cout << a << std::endl;
return a ;
}
void setBits (uint64_t bits)
{
_bits = bits;
}
};
}
#endif //__LINBOX_random_prime_iterator_H
// Local Variables:
// mode: C++
// tab-width: 8
// indent-tabs-mode: nil
// c-basic-offset: 8
// End:
// vim:sts=8:sw=8:ts=8:noet:sr:cino=>s,f0,{0,g0,(0,\:0,t0,+0,=s
|