Skip to content

Commit

Permalink
add support for python 3.9
Browse files Browse the repository at this point in the history
  • Loading branch information
GiacomoPope committed Jul 24, 2024
1 parent 83c5d1f commit 8e5d4db
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/kyber_py/polynomials/polynomials.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from ..utilities.utils import bit_count
from .polynomials_generic import PolynomialRing, Polynomial


Expand Down Expand Up @@ -67,8 +68,8 @@ def cbd(self, input_bytes, eta, is_ntt=False):
mask2 = (1 << 2 * eta) - 1
for i in range(256):
x = b_int & mask2
a = (x & mask).bit_count()
b = ((x >> eta) & mask).bit_count()
a = bit_count(x & mask)
b = bit_count((x >> eta) & mask)
b_int >>= 2 * eta
coefficients[i] = (a - b) % 3329
return self(coefficients, is_ntt=is_ntt)
Expand Down
14 changes: 14 additions & 0 deletions src/kyber_py/utilities/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
def bit_count(x):
"""
Count the number of bits in x
Method to support old python as `x.bit_count()`
was released in Python 3.10 and we currently
support Python 3.9
"""
try:
return x.bit_count()
except AttributeError:
return bin(x).count("1")


def xor_bytes(a, b):
"""
XOR two byte arrays, assume that they are
Expand Down

0 comments on commit 8e5d4db

Please sign in to comment.