ProtDos / pq-ntru

A quantum proof (post-quantum) implementation of the NTRU algorithm
Apache License 2.0
4 stars 0 forks source link

Example Usage not working #2

Closed som-cs closed 1 month ago

som-cs commented 3 months ago

Running example code

import pq_ntru

pq_ntru.generate_keys("key_filename", mode="moderate")
enc = pq_ntru.encrypt("key_filename", "message")
dec = pq_ntru.decrypt("key_filename", enc)

and getting error


UnificationFailed                         Traceback (most recent call last)
Cell In[6], line 1
----> 1 pq_ntru.generate_keys("key_filename", mode="moderate")

File [~/Projects/jupyter/venv/lib/python3.12/site-packages/pq_ntru/ntru.py:103](http://localhost:8888/lab/tree/venv/lib/python3.12/site-packages/pq_ntru/ntru.py#line=102), in generate_keys(name, mode, skip_check, debug)
     82 # print("ye")
     84 """
     85 N: Primzahl. Je höher die Primzahl, desto mehr Sicherheit wird garantiert.
     86 p: Ebenfalls eine Primzahl.
   (...)
    100 I: Die Einheitsmatrix der Größe N-1.
    101 """
--> 103 N1.genPubPriv(name)
    105 if skip_check:
    106     finished = True

File [~/Projects/jupyter/venv/lib/python3.12/site-packages/pq_ntru/NTRUdecrypt.py:242](http://localhost:8888/lab/tree/venv/lib/python3.12/site-packages/pq_ntru/NTRUdecrypt.py#line=241), in NTRUdecrypt.genPubPriv(self, keyfileName)
    237 def genPubPriv(self, keyfileName="key"):
    238     """
    239     Generate the public and private keys from class N, p and q values.
    240     Also write output files for the public and private keys.
    241     """
--> 242     self.genfg()
    243     self.genh()
    244     self.writePub(keyfileName)

File [~/Projects/jupyter/venv/lib/python3.12/site-packages/pq_ntru/NTRUdecrypt.py:172](http://localhost:8888/lab/tree/venv/lib/python3.12/site-packages/pq_ntru/NTRUdecrypt.py#line=171), in NTRUdecrypt.genfg(self)
    170 self.f = genRand10(self.N, self.df, self.df - 1)
    171 # print(self.f)
--> 172 invStat = self.invf()
    173 if invStat:
    174     break

File [~/Projects/jupyter/venv/lib/python3.12/site-packages/pq_ntru/NTRUdecrypt.py:146](http://localhost:8888/lab/tree/venv/lib/python3.12/site-packages/pq_ntru/NTRUdecrypt.py#line=145), in NTRUdecrypt.invf(self)
    140 """
    141 Invert the f polynomial with respect to input p and q values (class values).
    142 Return True if inverses w.r.t. p and q exists (after setting self.fp and self.fq)
    143 Return False if inverse w.r.t. either[/or](http://localhost:8888/or) p[/q](http://localhost:8888/q) does nto exist
    144 """
    145 fp_tmp = poly_inv(self.f, self.I, self.p)
--> 146 fq_tmp = poly_inv(self.f, self.I, self.q)
    147 if len(fp_tmp) > 0 and len(fq_tmp) > 0:
    148     self.fp = np.array(fp_tmp)

File [~/Projects/jupyter/venv/lib/python3.12/site-packages/pq_ntru/NTRUutil.py:102](http://localhost:8888/lab/tree/venv/lib/python3.12/site-packages/pq_ntru/NTRUutil.py#line=101), in poly_inv(poly_in, poly_I, poly_mod)
     99     return np.array([])
    101 # If we have got this far we have calculated an inverse, double check the inverse via poly mult
--> 102 tmpCheck = np.array(Poly((Poly(inv, x) * Poly(poly_in, x)) % Ppoly_I,
    103                          domain=GF(poly_mod, symmetric=False)).all_coeffs(), dtype=int)
    104 if len(tmpCheck) > 1 or tmpCheck[0] != 1:
    105     sys.exit("ERROR : Error in calculation of polynomial inverse")

File [~/Projects/jupyter/venv/lib/python3.12/site-packages/sympy/polys/polytools.py:73](http://localhost:8888/lab/tree/venv/lib/python3.12/site-packages/sympy/polys/polytools.py#line=72), in _polifyit.<locals>.wrapper(f, g)
     71 g = _sympify(g)
     72 if isinstance(g, Poly):
---> 73     return func(f, g)
     74 elif isinstance(g, Integer):
     75     g = f.from_expr(g, *f.gens, domain=f.domain)

File [~/Projects/jupyter/venv/lib/python3.12/site-packages/sympy/polys/polytools.py:4310](http://localhost:8888/lab/tree/venv/lib/python3.12/site-packages/sympy/polys/polytools.py#line=4309), in Poly.__mul__(f, g)
   4308 @_polifyit
   4309 def __mul__(f, g):
-> 4310     return f.mul(g)

File [~/Projects/jupyter/venv/lib/python3.12/site-packages/sympy/polys/polytools.py:1515](http://localhost:8888/lab/tree/venv/lib/python3.12/site-packages/sympy/polys/polytools.py#line=1514), in Poly.mul(f, g)
   1512 if not g.is_Poly:
   1513     return f.mul_ground(g)
-> 1515 _, per, F, G = f._unify(g)
   1517 if hasattr(f.rep, 'mul'):
   1518     result = F.mul(G)

File [~/Projects/jupyter/venv/lib/python3.12/site-packages/sympy/polys/polytools.py:489](http://localhost:8888/lab/tree/venv/lib/python3.12/site-packages/sympy/polys/polytools.py#line=488), in Poly._unify(f, g)
    486 if isinstance(f.rep, DMP) and isinstance(g.rep, DMP):
    487     gens = _unify_gens(f.gens, g.gens)
--> 489     dom, lev = f.rep.dom.unify(g.rep.dom, gens), len(gens) - 1
    491     if f.gens != gens:
    492         f_monoms, f_coeffs = _dict_reorder(
    493             f.rep.to_dict(), f.gens, gens)

File [~/Projects/jupyter/venv/lib/python3.12/site-packages/sympy/polys/domains/domain.py:727](http://localhost:8888/lab/tree/venv/lib/python3.12/site-packages/sympy/polys/domains/domain.py#line=726), in Domain.unify(K0, K1, symbols)
    710 """
    711 Construct a minimal domain that contains elements of ``K0`` and ``K1``.
    712 
   (...)
    724 
    725 """
    726 if symbols is not None:
--> 727     return K0.unify_with_symbols(K1, symbols)
    729 if K0 == K1:
    730     return K0

File [~/Projects/jupyter/venv/lib/python3.12/site-packages/sympy/polys/domains/domain.py:674](http://localhost:8888/lab/tree/venv/lib/python3.12/site-packages/sympy/polys/domains/domain.py#line=673), in Domain.unify_with_symbols(K0, K1, symbols)
    671 if (K0.is_Composite and (set(K0.symbols) & set(symbols))) or (K1.is_Composite and (set(K1.symbols) & set(symbols))):
    672     raise UnificationFailed("Cannot unify %s with %s, given %s generators" % (K0, K1, tuple(symbols)))
--> 674 return K0.unify(K1)

File [~/Projects/jupyter/venv/lib/python3.12/site-packages/sympy/polys/domains/domain.py:735](http://localhost:8888/lab/tree/venv/lib/python3.12/site-packages/sympy/polys/domains/domain.py#line=734), in Domain.unify(K0, K1, symbols)
    732 if not (K0.has_CharacteristicZero and K1.has_CharacteristicZero):
    733     # Reject unification of domains with different characteristics.
    734     if K0.characteristic() != K1.characteristic():
--> 735         raise UnificationFailed("Cannot unify %s with %s" % (K0, K1))
    737     # We do not get here if K0 == K1. The two domains have the same
    738     # characteristic but are unequal so at least one is composite and
    739     # we are unifying something like GF(3).unify(GF(3)[x]).
    740     return K0.unify_composite(K1)

UnificationFailed: Cannot unify GF(2048) with ZZ```

Running on linux manjaro kernel 6.16 on python 3.12
saviornt commented 1 month ago

Same, UnificationFailed error using Python 3.12 and pq_ntru 0.1.3

ProtDos commented 1 month ago

@som-cs @saviornt

I have updated the repository. Please check it down below.