Cayley
Pseudo-Random Bits from Finite Groups
Cayley.cpp
Go to the documentation of this file.
1 
4 #include "Includes.h"
5 #include "Cayley.h"
6 
8 //Useful constants
9 
10 #pragma region constants
11 
13 
14 extern const uint32_t g_nLandau[] = {
15  1, 1, 2, 3, 4, //g(0-4)
16  6, 6, 12, 15, 20, //g(5-9)
17  30, 30, 60, 60, 84, //g(10-14)
18  105, 140, 210, 210, 420, //g(15-19)
19  420, 420, 420, 840, 840, //g(20-24)
20  1260, 1260, 1540, 2310, 2520, //g(25-29)
21  4620, 4620, 5460, 5460, 9240, //g(30-34)
22  9240, 13860, 13860, 16380, 16380, //g(35-39)
23  27720, 30030, 32760, 60060, 60060, //g(40-44)
24  60060, 60060, 120120, 120120, 180180, //g(45-49)
25  180180, 180180, 180180, 360360, 360360, //g(50-54)
26  360360, 360360, 471240, 471240, 556920, //g(55-59)
27  1021020, 1021020, 1141140, 1141140, 2042040 //g(60-64)
28 }; //g_nLandau
29 
30 #pragma endregion constants
31 
33 //CCayley functions
34 
38 
39 CCayley::CCayley(uint32_t n):
40  m_nSize(n)
41 {
42  assert(n < 64); //for safety: this is the size of our Landau table.
43  m_nOrder = g_nLandau[n];
44  m_pCurPerm = new CPerm(n);
45 } //constructor
46 
48 
50  delete m_pCurPerm;
51 } //destructor
52 
56 
58  assert(i == 0 || i == 1);
59  return CPerm(m_nPower[i][1]);
60 } //Generator
61 
67 
68 void CCayley::ChooseGenerators(uint64_t (*rnd)(void)){
69  assert(rnd != nullptr); //safety
70 
71  CPerm p(m_nSize); //current permutation
72  bool ok = false; //whether chosen permutations are ok
73 
74  while(!ok){
75  do{ //choose the first generator; a max-order pseudo-random permutation
76  p.Randomize(rnd); //choose a pseudorandom permutation
77  m_nPower[0].Initialize(p); //initialize its power table and its order
78  }while(m_nPower[0].GetOrder() < m_nOrder); //insist on max order
79 
80  do{ //choose the second generator; a max-order pseudo-random odd permutation
81  p.RandomizeOdd(rnd); //choose a pseudorandom odd permutation
82  m_nPower[1].Initialize(p); //initialize its power table and its order
83  }while(m_nPower[1].GetOrder() < m_nOrder); //insist on max order
84 
85  //reject the generators if they have a common fixed point
86 
87  ok = true; //ok so far
88 
89  const CPerm& p0 = m_nPower[0][1]; //shorthand for first generator
90  const CPerm& q0 = m_nPower[1][1]; //shorthand for second generator
91 
92  for(uint32_t i=0; i<m_nSize; i++)
93  ok = ok && !(p0[i] == i && q0[i] == i);
94  } //while
95 } //ChooseGenerators
96 
100 
101 void CCayley::srand(uint64_t (*rand)(void)){
102  ChooseGenerators(rand); //random generators
103  m_pCurPerm->Randomize(rand); //random permutations
104 } //Initialize
105 
108 
109 const CPerm& CCayley::GetPerm() const{
110  return *m_pCurPerm;
111 } //GetPerm
112 
116 
117 const uint32_t CCayley::GetSize() const{
118  return m_nSize;
119 } //GetSize
120 
127 
129  static unsigned int i = 0; //generator parity; determines current generator
130  CPerm& perm = *m_pCurPerm; //shorthand for the current permutation
131  const uint32_t k = m_nDelayLine[m_nTail]%m_nOrder; //exponent
132 
133  perm *= m_nPower[i][k]; //multiply by generator i to the power k
134  i ^= 1; //flip generator parity
135  assert(i < 2); //safety
136 } //NextPerm
const uint32_t g_nLandau[]
Landau's function for .
const CPerm & GetPerm() const
Get current permutation.
Definition: Cayley.cpp:109
void ChooseGenerators(uint64_t(*rnd)(void))
Choose generators.
Definition: Cayley.cpp:68
Useful includes.
CPerm * m_pCurPerm
Current permutation.
Definition: Cayley.h:30
uint32_t m_nOrder
Order of generators.
Definition: Cayley.h:28
uint32_t m_nSize
Size of permutations.
Definition: Cayley.h:26
void NextPerm()
Compute next permutation.
Definition: Cayley.cpp:128
CPerm GetGenerator(int i) const
Get generator.
Definition: Cayley.cpp:57
Permutation.
Definition: Permutation.h:20
int m_nTail
Index of last element in delay line.
Definition: Cayley.h:53
CCayley(uint32_t n)
Constructor.
Definition: Cayley.cpp:39
const uint32_t GetSize() const
Get permutation size.
Definition: Cayley.cpp:117
CPowerTable m_nPower[2]
Power tables for a pair of generators.
Definition: Cayley.h:29
void Initialize(const CPerm &p)
Initialize.
Definition: PowerTable.cpp:17
virtual void srand(uint64_t(*rnd)(void))
Seed the generator.
Definition: Cayley.cpp:101
void RandomizeOdd(uint64_t(*rng)(void))
Set to random odd permutation.
Declaration of the Cayley pseudo-random number generator.
~CCayley()
Destructor.
Definition: Cayley.cpp:49
void Randomize(uint64_t(*rng)(void))
Set to random permutation.
uint64_t m_nDelayLine[m_nDelay]
Delay line.
Definition: Cayley.h:34