Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/rings/bernmm/bern_modp_util.h
4100 views
1
/*
2
bern_modp_util.h: number-theoretic utility functions
3
4
Copyright (C) 2008, 2009, David Harvey
5
6
This file is part of the bernmm package (version 1.1).
7
8
bernmm is released under a BSD-style license. See the README file in
9
the source distribution for details.
10
*/
11
12
13
#ifndef BERNMM_BERN_MODP_UTIL_H
14
#define BERNMM_BERN_MODP_UTIL_H
15
16
17
#include <vector>
18
#include <cassert>
19
#include <climits>
20
21
22
#if ULONG_MAX == 4294967295U
23
#define ULONG_BITS 32
24
#elif ULONG_MAX == 18446744073709551615U
25
#define ULONG_BITS 64
26
#else
27
#error Oops! Unsigned long is neither 32 nor 64 bits.
28
#error You need to update bern_modp_util.h.
29
#endif
30
31
32
namespace bernmm {
33
34
35
/*
36
Same as NTL's PowerMod, but also accepts an _ninv_ parameter, which is the
37
same as the ninv parameter for NTL's MulMod routines, i.e. should have
38
ninv = 1 / ((double) n).
39
40
(Implementation is adapted from ZZ.c in NTL 5.4.1.)
41
*/
42
long PowerMod(long a, long ee, long n, double ninv);
43
44
45
/*
46
Represents the factorisation of an integer n into distinct prime factors.
47
48
(Very naive implementation!)
49
*/
50
class Factorisation
51
{
52
protected:
53
/*
54
Finds distinct prime factors of m in the range k < p <= m.
55
Assumes that m does not have any prime factors p <= k.
56
Appends factors found to _factors_.
57
*/
58
void helper(long k, long m);
59
60
public:
61
// the integer
62
long n;
63
64
// the distinct factors (in increasing order)
65
std::vector<long> factors;
66
67
// initialises with given integer
68
Factorisation(long n);
69
};
70
71
72
73
class PrimeTable
74
{
75
private:
76
std::vector<long> data; // bit-vector; 0 means prime, 1 means composite
77
78
// read bit from index i
79
inline bool get(long i) const
80
{
81
return (data[i / ULONG_BITS] >> (i % ULONG_BITS)) & 1;
82
}
83
84
// set bit at index i
85
inline void set(long i)
86
{
87
data[i / ULONG_BITS] |= (1L << (i % ULONG_BITS));
88
}
89
90
91
public:
92
// initialise with primes up to given bound
93
PrimeTable(long bound);
94
95
// test whether n is prime by table lookup
96
inline bool is_prime(long n) const
97
{
98
return !get(n);
99
}
100
101
// returns smallest prime p that is larger than n
102
long next_prime(long n) const
103
{
104
for (n++; !is_prime(n); n++);
105
return n;
106
}
107
};
108
109
110
111
/*
112
Returns 1 if n is prime.
113
*/
114
int is_prime(long n);
115
116
117
/*
118
Returns smallest prime larger than p.
119
*/
120
long next_prime(long p);
121
122
123
/*
124
Computes order of x mod p, given the factorisation F of p-1.
125
*/
126
long order(long x, long p, double pinv, const Factorisation& F);
127
128
129
/*
130
Finds the smallest primitive root mod p, given the factorisation F of p-1.
131
*/
132
long primitive_root(long p, double pinv, const Factorisation& F);
133
134
135
}; // end namespace
136
137
138
#endif
139
140
// end of file ================================================================
141
142