Ticket #7271: polybori_fixes.patch
File polybori_fixes.patch, 13.6 KB (added by , 11 years ago) 


c_lib/include/pb_wrap.h
# HG changeset patch # User Martin Albrecht <malb@informatik.unibremen.de> # Date 1255373013 3600 # Node ID e8436f9d468b13c91560af38f2839beeacfaa568 # Parent f872a92f5f9afff4b4971afc82bf9270faec269b implemented ring.var() and various functions required for PolyBoRi update diff r f872a92f5f9a r e8436f9d468b c_lib/include/pb_wrap.h
a b 3 3 #include "nf.h" 4 4 #include "fglm.h" 5 5 #include "interpolate.h" 6 #include "randomset.h" 6 7 #include "ccobject.h" 7 8 8 9 // M4RI … … 85 86 strat.addNonTrivialImplicationsDelayed(strat.generators[i]); 86 87 } 87 88 89 #define PBPolyVector_set(v,i,p) v[i] = p 
module_list.py
diff r f872a92f5f9a r e8436f9d468b module_list.py
a b 1245 1245 libraries=['m4ri', 'polybori','pboriCudd','groebner','gd'], 1246 1246 include_dirs = [SAGE_ROOT+'/local/include/cudd', 1247 1247 SAGE_ROOT+'/local/include/polybori', 1248 SAGE_ROOT+'/local/include/polybori/groebner'], 1248 SAGE_ROOT+'/local/include/polybori/groebner', 1249 "sage/libs/polybori"], 1249 1250 depends = [SAGE_ROOT + "/local/include/polybori/polybori.h"], 1250 1251 extra_compile_args = polybori_extra_compile_args, 1251 1252 language = 'c++'), 
sage/crypto/mq/mpolynomialsystem.py
diff r f872a92f5f9a r e8436f9d468b sage/crypto/mq/mpolynomialsystem.py
a b 1147 1147 ij = ij  len(r) 1148 1148 else: 1149 1149 return r[ij] 1150 raise IndexError("index out of range") 1150 1151 1151 1152 def __contains__(self, element): 1152 1153 """ 
sage/crypto/mq/mpolynomialsystemgenerator.py
diff r f872a92f5f9a r e8436f9d468b sage/crypto/mq/mpolynomialsystemgenerator.py
a b 13 13 """ 14 14 15 15 def __getattr__(self, attr): 16 """ 17 EXAMPLE:: 18 19 sage: from sage.crypto.mq.mpolynomialsystemgenerator import MPolynomialSystemGenerator 20 sage: msg = MPolynomialSystemGenerator() 21 sage: msg.R 22 Traceback (most recent call last): 23 ... 24 NotImplementedError 25 """ 16 26 if attr == "R": 17 27 self.R = self.ring() 18 28 return self.R 19 29 else: 20 raise AttributeError 30 raise AttributeError("'%s' object has no attribute '%s'"%(self.__class__,attr)) 21 31 22 32 def varformatstr(self, name): 23 33 """ … … 31 41 32 42 INPUT: 33 43 name  string 44 45 EXAMPLE:: 46 47 sage: from sage.crypto.mq.mpolynomialsystemgenerator import MPolynomialSystemGenerator 48 sage: msg = MPolynomialSystemGenerator() 49 sage: msg.varformatstr('K') 50 Traceback (most recent call last): 51 ... 52 NotImplementedError 34 53 """ 35 54 raise NotImplementedError 36 55 … … 44 63 INPUT: 45 64 name  string 46 65 round  integer index 66 67 EXAMPLE:: 68 69 sage: from sage.crypto.mq.mpolynomialsystemgenerator import MPolynomialSystemGenerator 70 sage: msg = MPolynomialSystemGenerator() 71 sage: msg.varstrs('K', i) 72 Traceback (most recent call last): 73 ... 74 NotImplementedError 47 75 """ 48 76 raise NotImplementedError 49 77 … … 55 83 INPUT: 56 84 name  string 57 85 round  integer index 86 87 EXAMPLE:: 88 89 sage: from sage.crypto.mq.mpolynomialsystemgenerator import MPolynomialSystemGenerator 90 sage: msg = MPolynomialSystemGenerator() 91 sage: msg.vars('K',0) 92 Traceback (most recent call last): 93 ... 94 NotImplementedError 58 95 """ 59 96 raise NotImplementedError 60 97 61 98 def ring(self): 62 99 """ 63 100 Return the ring in which the system is defined. 101 102 EXAMPLE:: 103 104 sage: from sage.crypto.mq.mpolynomialsystemgenerator import MPolynomialSystemGenerator 105 sage: msg = MPolynomialSystemGenerator() 106 sage: msg.ring() 107 Traceback (most recent call last): 108 ... 109 NotImplementedError 64 110 """ 65 111 raise NotImplementedError 66 112 … … 68 114 """ 69 115 Return a block term ordering for the equation systems 70 116 generated by self. 117 118 EXAMPLE:: 119 120 sage: from sage.crypto.mq.mpolynomialsystemgenerator import MPolynomialSystemGenerator 121 sage: msg = MPolynomialSystemGenerator() 122 sage: msg.block_order() 123 Traceback (most recent call last): 124 ... 125 NotImplementedError 71 126 """ 72 127 raise NotImplementedError 73 128 … … 78 133 INPUT: 79 134 P  plaintext (vector, list) 80 135 K  key (vector, list) 136 137 EXAMPLE:: 138 139 sage: from sage.crypto.mq.mpolynomialsystemgenerator import MPolynomialSystemGenerator 140 sage: msg = MPolynomialSystemGenerator() 141 sage: msg(None, None) 142 Traceback (most recent call last): 143 ... 144 NotImplementedError 81 145 """ 82 146 raise NotImplementedError 83 147 84 148 def sbox(self): 85 149 """ 86 150 Return SBox object for self. 151 152 EXAMPLE:: 153 154 sage: from sage.crypto.mq.mpolynomialsystemgenerator import MPolynomialSystemGenerator 155 sage: msg = MPolynomialSystemGenerator() 156 sage: msg.sbox() 157 Traceback (most recent call last): 158 ... 159 AttributeError: '<class 'sage.crypto.mq.mpolynomialsystemgenerator.MPolynomialSystemGenerator'>' object has no attribute '_sbox' 87 160 """ 88 161 return self._sbox 89 162 … … 96 169 INPUT: 97 170 P  plaintext (vector, list) 98 171 K  key (vector, list) 172 173 EXAMPLE:: 174 175 sage: from sage.crypto.mq.mpolynomialsystemgenerator import MPolynomialSystemGenerator 176 sage: msg = MPolynomialSystemGenerator() 177 sage: msg.polynomial_system() 178 Traceback (most recent call last): 179 ... 180 NotImplementedError 99 181 """ 100 182 raise NotImplementedError 101 183 … … 103 185 """ 104 186 Return random element. Usually this is a list of elements in 105 187 the base field of length 'blocksize'. 188 189 EXAMPLE:: 190 191 sage: from sage.crypto.mq.mpolynomialsystemgenerator import MPolynomialSystemGenerator 192 sage: msg = MPolynomialSystemGenerator() 193 sage: msg.random_element() 194 Traceback (most recent call last): 195 ... 196 NotImplementedError 106 197 """ 107 198 raise NotImplementedError 108 199 
sage/libs/polybori/decl.pxd
diff r f872a92f5f9a r e8436f9d468b sage/libs/polybori/decl.pxd
a b 431 431 432 432 PBPolyVector pb_gauss_on_polys "gauss_on_polys" (PBPolyVector inp) 433 433 434 PBPolyVector pb_easy_linear_factors "easy_linear_factors"(PBPoly p) 435 434 436 PBPoly pb_substitute_variables "substitute_variables<std::vector<BoolePolynomial>, BoolePolynomial>" (PBPolyVector vec, PBPoly poly) 435 437 436 438 void pb_append_block "BooleEnv::appendBlock" (int ind) … … 441 443 char * pb_get_variable_name "BooleEnv::getVariableName" \ 442 444 (int idx) 443 445 446 PBSet pb_random_set "random_set" (PBMonom variables, int length) 447 void pb_set_random_seed "set_random_seed" (unsigned int seed) 448 444 449 #M4RI initialization 445 450 void m4ri_build_all_codes() 446 451 void m4ri_destroy_all_codes() 452 453 void PBPolyVector_set(PBPolyVector v, int i, PBPoly p) 
sage/rings/polynomial/pbori.pyx
diff r f872a92f5f9a r e8436f9d468b sage/rings/polynomial/pbori.pyx
a b 1285 1285 # 1286 1286 ### 1287 1287 1288 def var(self, i=0): 1289 """ 1290 Returns the ith generator of this boolean polynomial ring. 1291 1292 INPUT: 1293 1294  ``i``  an integer or a boolean monomial in one variable 1295 1296 EXAMPLES:: 1297 1298 sage: P.<x,y,z> = BooleanPolynomialRing(3) 1299 sage: P.var() 1300 x 1301 sage: P.var(2) 1302 z 1303 sage: m = x.monomials()[0] 1304 sage: P.var(m) 1305 x 1306 1307 TESTS:: 1308 1309 sage: P.<x,y,z> = BooleanPolynomialRing(3, order='dp') 1310 sage: P.var(0) 1311 x 1312 """ 1313 if PY_TYPE_CHECK(i, BooleanMonomial): 1314 if len(i) == 1: 1315 i = i.index() 1316 else: 1317 raise TypeError, "Boolean monomials must be in one variable only." 1318 i = int(i) 1319 if i < 0 or i >= self._pbring.nVariables(): 1320 raise ValueError, "Generator not defined." 1321 return new_BP_from_DD(self, self._pbring.variable(self.pbind[i])) 1322 1288 1323 def _change_ordering(self, int order): 1289 1324 r""" 1290 1325 Change the ordering of this boolean polynomial ring. Do NOT call … … 4186 4221 to ``True`` linear algebra takes affect in this 4187 4222 block. (default: ``True``) 4188 4223 4224  "gauss_on_linear"  perform Gaussian elimination on linear 4225 polynomials (default: ``True``) 4226 4189 4227  ``selection_size``  maximum number of polynomials for 4190 4228 parallel reductions (default: ``1000``) 4191 4229 … … 4257 4295 if "redsb" not in kwds: 4258 4296 kwds["redsb"]=True 4259 4297 set_cring(self.ring()) 4260 #_sig_on4298 _sig_on 4261 4299 gb = groebner_basis(self.gens(), **kwds) 4262 #_sig_off4300 _sig_off 4263 4301 if kwds.get("deg_bound", False) is False: 4264 4302 g = GroebnerStrategy() 4265 4303 for p in gb: … … 5238 5276 raise IndexError 5239 5277 return new_BP_from_PBPoly(self._parent, self._vec.get(i)) 5240 5278 5279 5280 def __setitem__(self, ind, p): 5281 """ 5282 EXAMPLE:: 5283 5284 sage: B.<a,b,c,d,e,f> = BooleanPolynomialRing() 5285 sage: from polybori import BooleanPolynomialVector 5286 sage: l = [B.random_element() for _ in range(3)] 5287 sage: v = BooleanPolynomialVector(l) 5288 sage: len(v) 5289 3 5290 sage: v[0] = a; v[0] 5291 a 5292 sage: v[1] = b; v[1] 5293 b 5294 sage: v[3] = c 5295 Traceback (most recent call last): 5296 ... 5297 IndexError 5298 """ 5299 cdef long i = int(ind) 5300 while i < 0: 5301 i += self._vec.size() 5302 if i >= self._vec.size(): 5303 raise IndexError 5304 if not PY_TYPE_CHECK(p, BooleanPolynomialVector): 5305 p = self._parent(p) 5306 5307 PBPolyVector_set(self._vec, i, (<BooleanPolynomial>p)._pbpoly) 5308 5241 5309 def append(self, el): 5242 5310 """ 5243 5311 Append the element ``el`` to this vector. … … 5416 5484 sage: red.add_generator(y*z + z) 5417 5485 5418 5486 sage: red.head_normal_form(x + y*z) 5419 x + y*z5487 y + z + 1 5420 5488 5421 5489 sage; red.nf(x + y*z) 5422 5490 y + z + 1 … … 6983 7051 _vec.append(f) 6984 7052 return new_BP_from_PBPoly((<BooleanPolynomialRing>poly._parent), pb_substitute_variables(_vec._vec, poly._pbpoly)) 6985 7053 7054 7055 def set_random_seed(seed): 7056 """ 7057 The the PolyBoRi random seed to ``seed`` 7058 7059 EXAMPLE:: 7060 7061 sage: from polybori import random_set, set_random_seed 7062 sage: B.<a,b,c,d,e> = BooleanPolynomialRing() 7063 sage: (a*b*c*d).lm() 7064 a*b*c*d 7065 sage: set_random_seed(1337) 7066 sage: random_set((a*b*c*d).lm(),2) 7067 {{b,c}, {b}} 7068 sage: random_set((a*b*c*d).lm(),2) 7069 {{a,c}, {}} 7070 7071 sage: set_random_seed(1337) 7072 sage: random_set((a*b*c*d).lm(),2) 7073 {{b,c}, {b}} 7074 sage: random_set((a*b*c*d).lm(),2) 7075 {{a,c}, {}} 7076 """ 7077 pb_set_random_seed(seed) 7078 7079 def random_set(BooleanMonomial variables, length): 7080 """ 7081 Return a random set of monomials with ``length`` elements with 7082 each element in the variables ``variables``. 7083 7084 EXAMPLE:: 7085 7086 sage: from polybori import random_set, set_random_seed 7087 sage: B.<a,b,c,d,e> = BooleanPolynomialRing() 7088 sage: (a*b*c*d).lm() 7089 a*b*c*d 7090 sage: set_random_seed(1337) 7091 sage: random_set((a*b*c*d).lm(),10) 7092 {{a,b,c,d}, {a,b,d}, {a,c}, {a,d}, {a}, {b,c}, {b}, {c,d}, {c}, {}} 7093 """ 7094 cdef PBSet r 7095 r = pb_random_set(variables._pbmonom, length) 7096 return new_BS_from_PBSet(r, variables._ring) 7097 7098 def easy_linear_factors(BooleanPolynomial p): 7099 return new_BPV_from_PBPolyVector(p._parent, pb_easy_linear_factors(p._pbpoly)) 7100 6986 7101 def unpickle_BooleanPolynomial(ring, string): 6987 7102 """ 6988 7103 Unpickle boolean polynomials