sagemath / sage

Main repository of SageMath
https://www.sagemath.org
Other
1.33k stars 453 forks source link

ZeroDivisonError when creating polynomial system #31891

Open kliem opened 3 years ago

kliem commented 3 years ago
sage: set_random_seed(0)                                                                                                                                                            
sage: sr = mq.SR(1,1,1,4,gf2=True,polybori=True)                                                                                                                                    
sage: sr.polynomial_system()                                                                                                                                                        
(Polynomial Sequence with 36 Polynomials in 20 Variables,
 {k000: 1, k001: 0, k002: 1, k003: 1})
sage: sr.polynomial_system()                                                                                                                                                        
(Polynomial Sequence with 36 Polynomials in 20 Variables,
 {k000: 0, k001: 0, k002: 1, k003: 1})
sage: sr.polynomial_system()                                                                                                                                                        
(Polynomial Sequence with 36 Polynomials in 20 Variables,
 {k000: 0, k001: 0, k002: 0, k003: 1})
sage: sr.polynomial_system()                                                                                                                                                        
(Polynomial Sequence with 36 Polynomials in 20 Variables,
 {k000: 1, k001: 1, k002: 0, k003: 1})
sage: sr.polynomial_system()                                                                                                                                                        
(Polynomial Sequence with 36 Polynomials in 20 Variables,
 {k000: 1, k001: 0, k002: 0, k003: 1})
sage: sr.polynomial_system()                                                                                                                                                        
(Polynomial Sequence with 36 Polynomials in 20 Variables,
 {k000: 1, k001: 1, k002: 1, k003: 1})
sage: sr.polynomial_system()                                                                                                                                                        
(Polynomial Sequence with 36 Polynomials in 20 Variables,
 {k000: 0, k001: 1, k002: 0, k003: 1})
sage: sr.polynomial_system()                                                                                                                                                        
(Polynomial Sequence with 36 Polynomials in 20 Variables,
 {k000: 1, k001: 1, k002: 1, k003: 0})
sage: sr.polynomial_system()                                                                                                                                                        
(Polynomial Sequence with 36 Polynomials in 20 Variables,
 {k000: 1, k001: 1, k002: 1, k003: 1})
sage: sr.polynomial_system()                                                                                                                                                        
(Polynomial Sequence with 36 Polynomials in 20 Variables,
 {k000: 0, k001: 1, k002: 0, k003: 0})
sage: sr.polynomial_system()                                                                                                                                                        
(Polynomial Sequence with 36 Polynomials in 20 Variables,
 {k000: 0, k001: 1, k002: 0, k003: 1})
sage: sr.polynomial_system()                                                                                                                                                        
(Polynomial Sequence with 36 Polynomials in 20 Variables,
 {k000: 1, k001: 0, k002: 1, k003: 1})
sage: sr.polynomial_system()                                                                                                                                                        
(Polynomial Sequence with 36 Polynomials in 20 Variables,
 {k000: 0, k001: 0, k002: 1, k003: 1})
sage: sr.polynomial_system()                                                                                                                                                        
(Polynomial Sequence with 36 Polynomials in 20 Variables,
 {k000: 1, k001: 0, k002: 0, k003: 1})
sage: sr.polynomial_system()                                                                                                                                                        
(Polynomial Sequence with 36 Polynomials in 20 Variables,
 {k000: 0, k001: 1, k002: 1, k003: 0})
sage: sr.polynomial_system()                                                                                                                                                        
(Polynomial Sequence with 36 Polynomials in 20 Variables,
 {k000: 1, k001: 1, k002: 1, k003: 0})
sage: sr.polynomial_system()                                                                                                                                                        
(Polynomial Sequence with 36 Polynomials in 20 Variables,
 {k000: 0, k001: 1, k002: 0, k003: 0})
sage: sr.polynomial_system()                                                                                                                                                        
(Polynomial Sequence with 36 Polynomials in 20 Variables,
 {k000: 1, k001: 1, k002: 1, k003: 1})
sage: sr.polynomial_system()                                                                                                                                                        
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-25-ba9bfb97b584> in <module>
----> 1 sr.polynomial_system()

/srv/public/kliem/sage/local/lib/python3.7/site-packages/sage/crypto/mq/sr.py in polynomial_system(self, P, K, C)
   2137 
   2138         if ciphertext is None:
-> 2139             ciphertext = self(plaintext, key)
   2140         elif ciphertext is False:
   2141             raise TypeError("type %s of C not understood"%(type(ciphertext)))

/srv/public/kliem/sage/local/lib/python3.7/site-packages/sage/crypto/mq/sr.py in __call__(self, P, K)
   1386                 print("R[%02d].m_col   %s"%(self.n, self.hex_str_vector(P)))
   1387 
-> 1388         K = KeyExpansion(K, self._n)
   1389         if get_verbose() >= 2:
   1390             print("R[%02d].k_sch   %s"%(self.n, self.hex_str_vector(K)))

/srv/public/kliem/sage/local/lib/python3.7/site-packages/sage/crypto/mq/sr.py in key_schedule(self, kj, i)
   1179 
   1180         if r == 1:
-> 1181             s0 = SubByte(kj[0, c-1])
   1182 
   1183             if c > 1:

/srv/public/kliem/sage/local/lib/python3.7/site-packages/sage/crypto/mq/sr.py in sub_byte(self, b)
    726         if not b:
    727             if not self._allow_zero_inversions:
--> 728                 raise ZeroDivisionError("A zero inversion occurred during an encryption or key schedule.")
    729             else:
    730                 return self.sbox_constant()

ZeroDivisionError: A zero inversion occurred during an encryption or key schedule.

In #29981 we have to fix a doctest in src/doc/en/reference/sage/index.rst to account for this.

Component: symbolics

Issue created by migration from https://trac.sagemath.org/ticket/31891

kliem commented 3 years ago
comment:1

There are various places where we introduce a workaround in src/sage/rings/polynomial/multi_polynomial_sequence.py and src/sage/rings/pbori/pbori.pyx.

kliem commented 3 years ago
comment:2

And src/sage/crypto/mq/sr.py.