Polynomial
add(p, q)
Adds two polynomials.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
p
|
VectorInt
|
polynomial's \(p\) coefficients. |
required |
q
|
VectorInt
|
polynomial's \(q\) coefficients. |
required |
Returns:
Type | Description |
---|---|
VectorInt
|
Coefficients array of polynomial \(p + q\). |
Source code in src/lbpqc/primitives/polynomial/poly.py
148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
|
deg(p, *, error_if_zero_poly=False)
Returns degree of a given polynomial calculated as an index of the last nonzero ceofficient.
Returns -1 as a degree of zero polynomial if error_if_zero_poly
is set to False
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
p
|
VectorInt
|
Polynomial's coefficients. |
required |
error_if_zero_poly
|
bool
|
Parameter deciding how to treat degree of zero polynomial. |
False
|
Returns:
Type | Description |
---|---|
int
|
Degree of the given polynomial. |
Raises:
Type | Description |
---|---|
ValueError
|
If given empty numpy array. |
ValueError
|
If given zero polynomial and error_if_zero_poly is set to True. |
Source code in src/lbpqc/primitives/polynomial/poly.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
|
is_zero_poly(p)
Checks if given polynomial is zero polynomial.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
p
|
VectorInt
|
Polynomial's coefficients. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if \(p \equiv 0\), False otherwise |
Source code in src/lbpqc/primitives/polynomial/poly.py
14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
monomial(coeff, degree)
For given degree \(d\) and coefficient \(c\), constructs a monomial $$ cX^{d - 1} $$
Parameters:
Name | Type | Description | Default |
---|---|---|---|
coeff
|
int
|
Monomial's coefficient. |
required |
degree
|
int
|
Monomial's degree. |
required |
Returns:
Type | Description |
---|---|
VectorInt
|
Coefficients' array with only nonzero entry |
Examples:
\(7X^5\)
>>> monomial(7, 5)
array([0,0,0,0,0,5])
Source code in src/lbpqc/primitives/polynomial/poly.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
|
mul(p, q)
Multiplies polynomials \(p\) and \(q\).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
p
|
Vector
|
polynomial's \(p\) coefficients. |
required |
q
|
Vector
|
polynomial's \(q\) coefficients. |
required |
Returns:
Type | Description |
---|---|
Vector
|
Coefficients array of polynomial \(p \cdot q\). |
Source code in src/lbpqc/primitives/polynomial/poly.py
180 181 182 183 184 185 186 187 188 189 190 191 192 |
|
pad(p, max_deg)
Pad's polynomial's coefficient's array with zero entries for powers higher than polynomial's degree, so that length of resulting array is equal to max_deg + 1.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
p
|
VectorInt
|
Polynomial's coefficients. |
required |
max_deg
|
int
|
Degree that \(p\) is to be expanded to. |
required |
Returns:
Type | Description |
---|---|
VectorInt
|
Coefficient's array with length equal to |
Examples:
\(p = X^3 + 7X - 2\)
>>> p = np.array([-2, 7, 0, 1])
>>> pad(p, 5)
array([-2, 7, 0, 1, 0, 0])
Source code in src/lbpqc/primitives/polynomial/poly.py
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 |
|
sub(p, q)
Subtract polynomial \(q\) from polynomial \(p\).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
p
|
VectorInt
|
polynomial's \(p\) coefficients. |
required |
q
|
VectorInt
|
polynomial's \(q\) coefficients. |
required |
Returns:
Type | Description |
---|---|
VectorInt
|
Coefficients array of polynomial \(p - q\). |
Source code in src/lbpqc/primitives/polynomial/poly.py
164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
|
trim(p)
Trims zero coefficients of powers higher than polynomial's degree, so that resulting coefficient's arrray has length of \(\deg(p) + 1\).
If p is zero polynomial, then returns np.array([0], dtype=int)
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
p
|
VectorInt
|
Polynomial's coefficients. |
required |
Returns:
Type | Description |
---|---|
VectorInt
|
Coefficient's array of length at most \(\deg(p)\). |
Examples:
>>> p = np.array([1,0,2,3,0,0,0])
>>> trim(p)
array([1,0,2,3])
Source code in src/lbpqc/primitives/polynomial/poly.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
|
zero_poly(max_deg=0)
Explicitly constructs zero polynomial, i.e. a coefficient's array of length max_deg
+ 1 filled with zeros.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
max_deg
|
int
|
. |
0
|
Returns:
Type | Description |
---|---|
VectorInt
|
Coefficients' array of length |
Source code in src/lbpqc/primitives/polynomial/poly.py
133 134 135 136 137 138 139 140 141 142 143 144 |
|
This class implements operations over \(\mathbb{Z}_p[X]\) polynomial ring, i.e. polynomials which coefficients are reduced modulo \(p\).
It's important to note that polynomials are represented as numpy's arrays with entries corresponding to coefficients in order of increasing powers.
Instance of ModIntPolyRing
class represents the polynomials ring.
It's methods implements operations in this rings, but polynomials in their inputs and outputs are still numpy's arrays.
Attributes:
Name | Type | Description |
---|---|---|
modulus |
int
|
modulus \(p\) of the \(\mathbb{Z}_p[X]\) polynomial ring. |
Source code in src/lbpqc/primitives/polynomial/modpoly.py
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 |
|
__init__(modulus)
Constructs the ring object for a given modulus.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
modulus
|
int
|
Ring modulus. |
required |
Source code in src/lbpqc/primitives/polynomial/modpoly.py
16 17 18 19 20 21 22 23 24 25 |
|
add(polynomial_a, polynomial_b)
Adds polynomial \(a\) to polynomial \(b\).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
polynomial_a
|
VectorInt
|
polynomial's \(a\) coefficients. |
required |
polynomial_b
|
VectorInt
|
polynomial's \(b\) coefficients. |
required |
Returns:
Type | Description |
---|---|
VectorModInt
|
Coefficients array of polynomial \(a + b\). |
Source code in src/lbpqc/primitives/polynomial/modpoly.py
71 72 73 74 75 76 77 78 79 80 81 82 83 |
|
coprime(polynomial_a, polynomial_b)
checks whether two polynomials \(a\) and \(b\) are coprime in a ring.
Checks whether \(\gcd(a,b)\) reduced to monic polynomial is equal to constant polynomial \(p \equiv 1\).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
polynomial_a
|
VectorInt
|
Coefficients array of polynomial \(a\). |
required |
polynomial_b
|
VectorInt
|
Coefficients array of polynomial \(b\). |
required |
Returns:
Type | Description |
---|---|
bool
|
|
Source code in src/lbpqc/primitives/polynomial/modpoly.py
201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
|
deg(polynomial)
Calculates degree of the given polynomial.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
polynomial
|
VectorInt
|
polynomial's coefficients |
required |
Returns:
Type | Description |
---|---|
int
|
degree of the polynomial in the ring. |
Source code in src/lbpqc/primitives/polynomial/modpoly.py
57 58 59 60 61 62 63 64 65 66 67 68 |
|
eea(polynomial_a, polynomial_b)
Extended Euclidean algorithm for polynomials, e.i algorithm that calculates coefficients for Bézout's identity.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
polynomial_a
|
VectorInt
|
Coefficients array of polynomial \(a\). |
required |
polynomial_b
|
VectorInt
|
Coefficients array of polynomial \(b\). |
required |
Returns:
Type | Description |
---|---|
Tuple[VectorModInt, VectorModInt, VectorModInt]
|
Tuple of polynomials \((d, s, t)\) that satisfies \(\gcd(a,b) = d\) and \(s \cdot a + t \cdot b = d\). |
Source code in src/lbpqc/primitives/polynomial/modpoly.py
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 |
|
euclidean_div(polynomial_a, polynomial_b)
Euclidean division (long division) for polynomials in the ring.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
polynomial_a
|
VectorInt
|
. |
required |
polynomial_b
|
VectorInt
|
. |
required |
Returns:
Type | Description |
---|---|
Tuple[VectorModInt, VectorModInt]
|
Quotient and Remainder polynomials. |
Source code in src/lbpqc/primitives/polynomial/modpoly.py
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 |
|
gcd(polynomial_a, polynomial_b)
calculates \(\gcd\) of polynomials \(a\) and \(b\) using euclidean algorithm.
It's worth noting that int the polynomial ring, \(\gcd\) is not unique.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
polynomial_a
|
VectorInt
|
Coefficients array of polynomial \(a\). |
required |
polynomial_b
|
VectorInt
|
Coefficients array of polynomial \(b\). |
required |
Returns:
Type | Description |
---|---|
VectorModInt
|
Polynomial with maximal degree that divides both \(a\) and \(b\). |
Source code in src/lbpqc/primitives/polynomial/modpoly.py
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
|
is_zero(polynomial)
Checks whether given polynomial is zero polynomial in the ring, so whether all it's coefficients are divisible by modulus.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
polynomial
|
VectorInt
|
Coefficients array of polynomial to be checked. |
required |
Returns:
Type | Description |
---|---|
bool
|
|
Source code in src/lbpqc/primitives/polynomial/modpoly.py
42 43 44 45 46 47 48 49 50 51 52 53 54 |
|
mul(polynomial_a, polynomial_b)
Multiplies polynomial \(a\) and polynomial \(b\).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
polynomial_a
|
VectorInt
|
polynomial's \(a\) coefficients. |
required |
polynomial_b
|
VectorInt
|
polynomial's \(b\) coefficients. |
required |
Returns:
Type | Description |
---|---|
VectorModInt
|
Coefficients array of polynomial \(a \cdot b\). |
Source code in src/lbpqc/primitives/polynomial/modpoly.py
101 102 103 104 105 106 107 108 109 110 111 112 113 |
|
reduce(polynomial)
Reduces the given polynomial to it's cannonical equivalence class in the ring, i.e. reduces polynomials' coefficients modulo modulus.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
polynomial
|
VectorInt
|
Polynomial's coefficients array. |
required |
Returns:
Type | Description |
---|---|
VectorModInt
|
Array of coefficients reduced modulo modulus. |
Source code in src/lbpqc/primitives/polynomial/modpoly.py
28 29 30 31 32 33 34 35 36 37 38 39 |
|
rem(polynomial_a, polynomial_b)
Computes only remainder of the euclidean divions of two ring's polynomials.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
polynomial_a
|
VectorInt
|
. |
required |
polynomial_b
|
VectorInt
|
. |
required |
Returns:
Type | Description |
---|---|
VectorModInt
|
Remainder of the euclidean division. |
Source code in src/lbpqc/primitives/polynomial/modpoly.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
|
sub(polynomial_a, polynomial_b)
Subtract polynomial \(b\) from polynomial \(a\).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
polynomial_a
|
VectorInt
|
polynomial's \(a\) coefficients. |
required |
polynomial_b
|
VectorInt
|
polynomial's \(b\) coefficients. |
required |
Returns:
Type | Description |
---|---|
VectorModInt
|
Coefficients array of polynomial \(a - b\). |
Source code in src/lbpqc/primitives/polynomial/modpoly.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
|
to_monic(polynomial)
Reduces given polynomial to monic polynomial by multiplying it by scalar equal to modular multiplicative inverse of the highest power coefficient.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
polynomial
|
VectorInt
|
. |
required |
Returns:
Type | Description |
---|---|
VectorModInt
|
Polynomial with leading coefficient equal to 1. |
Source code in src/lbpqc/primitives/polynomial/modpoly.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
|
This class implements operations over polynomial quotient ring $$ \frac{\mathbb{Z}_p[X]}{q(X)} $$ for given positive integer \(p\) and polynomial \(q\).
It's important to note that polynomials are represented as numpy's arrays with entries corresponding to coefficients in order of increasing powers.
Instance of PolyQuotientRing
class represents the polynomials ring.
It's methods implements operations in this rings, but polynomials in their inputs and outputs are still numpy's arrays.
Attributes:
Name | Type | Description |
---|---|---|
poly_modulus |
VectorInt
|
. |
int_modulus |
int
|
. |
Zm |
ModIntPolyRing
|
Object representing \(\mathbb{Z}_p\) ring. |
Source code in src/lbpqc/primitives/polynomial/polyqring.py
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 |
|
__init__(poly_modulus, int_modulus)
Constructs the ring object for a given polynomial modulus and integer modulus.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
poly_modulus
|
VectorInt
|
Coefficients array of polynomial modulus fot the ring. The \(q(X)\) in \(\frac{\mathbb{Z}_p[X]}{q(X)}\). |
required |
int_modulus
|
int
|
Integer modulus for the ring. The \(p\) in \(\frac{\mathbb{Z}_p[X]}{q(X)}\). |
required |
Source code in src/lbpqc/primitives/polynomial/polyqring.py
23 24 25 26 27 28 29 30 31 32 33 |
|
add(polynomial_a, polynomial_b)
Adds polynomial \(a\) to polynomial \(b\).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
polynomial_a
|
VectorInt
|
polynomial's \(a\) coefficients. |
required |
polynomial_b
|
VectorInt
|
polynomial's \(b\) coefficients. |
required |
Returns:
Type | Description |
---|---|
VectorModInt
|
Coefficients array of polynomial \(a + b\). |
Source code in src/lbpqc/primitives/polynomial/polyqring.py
56 57 58 59 60 61 62 63 64 65 66 67 68 |
|
inv(polynomial)
For a given polynomial \(v\), calculates it's multiplicative inverse in a ring.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
polynomial
|
VectorInt
|
Polynomial's coefficients array. |
required |
Returns:
Type | Description |
---|---|
VectorModInt
|
Coefficients array of polynomial \(u\), such that \(u \cdot v \equiv 1\). |
Raises:
Type | Description |
---|---|
ValueError
|
When given polynomial is not an unit in the ring (it's not coprime with ring's polynomial modulus). |
Source code in src/lbpqc/primitives/polynomial/polyqring.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
|
mul(polynomial_a, polynomial_b)
Multiplies polynomial \(a\) and polynomial \(b\).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
polynomial_a
|
VectorInt
|
polynomial's \(a\) coefficients. |
required |
polynomial_b
|
VectorInt
|
polynomial's \(b\) coefficients. |
required |
Returns:
Type | Description |
---|---|
VectorModInt
|
Coefficients array of polynomial \(a \cdot b\). |
Source code in src/lbpqc/primitives/polynomial/polyqring.py
85 86 87 88 89 90 91 92 93 94 95 96 97 |
|
reduce(polynomial)
Reduces the given polynomial \(u\) to it's cannonical equivalence class in the ring,
i.e. takes, the remainder of division \(\frac{u}{q}\), where \(q\) is polynomial modulus for the ring.
The division is performed in \(\mathbb{Z}_p[X]\) ring.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
polynomial
|
VectorInt
|
Polynomial's coefficients array. |
required |
Returns:
Type | Description |
---|---|
VectorModInt
|
Coefficients array of polynomial that is the remainder of the Euclidean division. |
Source code in src/lbpqc/primitives/polynomial/polyqring.py
41 42 43 44 45 46 47 48 49 50 51 52 53 |
|
sub(polynomial_a, polynomial_b)
Subtract polynomial \(b\) from polynomial \(a\).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
polynomial_a
|
VectorInt
|
polynomial's \(a\) coefficients. |
required |
polynomial_b
|
VectorInt
|
polynomial's \(b\) coefficients. |
required |
Returns:
Type | Description |
---|---|
VectorModInt
|
Coefficients array of polynomial \(a - b\). |
Source code in src/lbpqc/primitives/polynomial/polyqring.py
71 72 73 74 75 76 77 78 79 80 81 82 83 |
|
Function for constructing commonly used quotient rings.
Possible optinos:
- "-" || "X^N - 1": represents \(X^N - 1\) polynomial family.
- "+" || "X^N + 1": represents \(X^N + 1\) polynomial family.
- "prime" || "X^N - x - 1": represents \(X^N - X - 1\) polynomial family.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
p
|
str
|
string representation/symbol of ring's polynomial modulus. |
required |
N
|
int
|
Degree of the polynomial modulus of the ring. |
required |
q
|
int
|
Integer modulus of the ring. |
required |
Returns:
Type | Description |
---|---|
PolyQuotientRing | None
|
None if the parameters were invalid else ring object. |
Source code in src/lbpqc/primitives/polynomial/polyqring.py
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 |
|