Integer
LWR_rounding(a, q, p)
LWR rounding function, that maps elements from \(\mathbb{Z}_q\) to elements from \(\mathbb{Z}_p\) for some positive integers \(p\) and \(q\).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
a
|
int
|
Integer to be rounded. |
required |
q
|
int
|
Modulus of domain ring \(\mathbb{Z}_q\). |
required |
p
|
int
|
Modulus of codomain ring \(\mathbb{Z}_p\). |
required |
Returns:
Type | Description |
---|---|
ModInt
|
Integer \(c \in [0,p)\). |
Source code in src/lbpqc/primitives/integer/integer_ring.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
center_mod_reduce(a, m, right_closed=True)
Reduces integer \(a\) to it's equivalence class modulo \(m\) represented as an interval centered around zero.
Depending on the right_closed
parameter, the interval is either
$$
\left(-\frac{m}{2}, \frac{m}{2}\right]
$$
or
$$
\left[-\frac{m}{2}, \frac{m}{2}\right)
$$
Parameters:
Name | Type | Description | Default |
---|---|---|---|
a
|
int
|
integer or numpy array to be reduced. |
required |
m
|
int
|
positive modulus for the congruence relation. |
required |
right_closed
|
bool
|
parameter deciding which side of half-open interval is closed. |
True
|
Returns:
Type | Description |
---|---|
CenteredModInt
|
Integer or numpy array with entries reduced around zero. |
Source code in src/lbpqc/primitives/integer/integer_ring.py
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 |
|
eea(a, b)
Implementation of extended Euclidean algorithm, i.e. algorithm that for integers \(a\) and \(b\) computes their \(\gcd\) and coefficients of Bézout's identity, which are integers \(s\) and \(t\) such that $$ sa + tb = \gcd(a,b) $$
Parameters:
Name | Type | Description | Default |
---|---|---|---|
a
|
int
|
integer a. |
required |
b
|
int
|
integer b. |
required |
Returns:
Type | Description |
---|---|
Tuple[int, int, int]
|
Tuple (gcd(a,b), s, t). |
Source code in src/lbpqc/primitives/integer/integer_ring.py
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 |
|
mod_reduce(a, m)
Reduces integer \(a\) to it's equivalence class modulo \(m\) represented as integer in the interval \([0,m)\).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
a
|
int
|
integer or numpy array to be reduced. |
required |
m
|
int
|
positive modulus for the congruence relation. |
required |
Returns:
Type | Description |
---|---|
ModInt
|
Integer or numpy array with entries from interval \([0, \text{m})\). |
Source code in src/lbpqc/primitives/integer/integer_ring.py
23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
modinv(a, modulus)
Computes multiplicative modular inverse of integer \(a\) for a given modulus.
If the inverse does not exists, i.e. \(\gcd(a, \text{modulus}) \neq 1\) then raises ValueError
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
a
|
int
|
integer, which inverse we want to calculate. |
required |
modulus
|
int
|
positive modulus. |
required |
Returns:
Type | Description |
---|---|
ModInt
|
Integer \(r\) from interval \([0, \text{modulus})\) that satisfies \(a \cdot r \equiv 1 \mod \text{modulus}\). |
Raises:
Type | Description |
---|---|
ValueError
|
If \(\gcd(a, \text{modulus}) \neq 1\). |
Source code in src/lbpqc/primitives/integer/integer_ring.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
|
modpow(a, r, modulus)
Computes $$ a^{r} \mod \text{modulus} $$ using multiply and halve powering algorithm for groups.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
a
|
int
|
Base. |
required |
r
|
int
|
Exponent. |
required |
modulus
|
int
|
Positive modulus. |
required |
Returns:
Type | Description |
---|---|
ModInt
|
Integer \(c \in [0, \text{modulus})\) such that \(c \equiv a^r \mod \text{modulus}\). |
Source code in src/lbpqc/primitives/integer/integer_ring.py
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 |
|
fermat_primality_test(p, s, int_gen=None)
Fermat's primality test
Parameters:
Name | Type | Description | Default |
---|---|---|---|
p
|
int
|
. |
required |
s
|
int
|
. |
required |
Source code in src/lbpqc/primitives/integer/prime.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
is_prime(p)
Equivalent to miller_rabin_primality_test(p, 20)
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
p
|
int
|
Prime candidate. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if p is prime (at least according to miller rabin test) False otherwise. |
Source code in src/lbpqc/primitives/integer/prime.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
|
miller_rabin_primality_test(p, s, int_gen=None)
Miller-Rabin primality test
Parameters:
Name | Type | Description | Default |
---|---|---|---|
p
|
int
|
. |
required |
s
|
int
|
. |
required |
int_gen
|
. |
None
|
Returns:
Type | Description |
---|---|
. |
Source code in src/lbpqc/primitives/integer/prime.py
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 |
|