1
- # sage.doctest: needs sage.rings.polynomial.pbori
1
+ # sage.doctest: optional - pycryptosat, needs sage.modules sage.rings.polynomial.pbori
2
2
"""
3
3
SAT Functions for Boolean Polynomials
4
4
@@ -72,8 +72,8 @@ def solve(F, converter=None, solver=None, n=1, target_variables=None, **kwds):
72
72
73
73
We construct a very small-scale AES system of equations::
74
74
75
- sage: sr = mq.SR(1, 1, 1, 4, gf2=True, polybori=True) # needs sage.modules sage.rings.finite_rings
76
- sage: while True: # workaround (see :trac:`31891`) # needs sage.modules sage.rings.finite_rings
75
+ sage: sr = mq.SR(1, 1, 1, 4, gf2=True, polybori=True)
76
+ sage: while True: # workaround (see :trac:`31891`)
77
77
....: try:
78
78
....: F, s = sr.polynomial_system()
79
79
....: break
@@ -83,57 +83,56 @@ def solve(F, converter=None, solver=None, n=1, target_variables=None, **kwds):
83
83
and pass it to a SAT solver::
84
84
85
85
sage: from sage.sat.boolean_polynomials import solve as solve_sat
86
- sage: s = solve_sat(F) # optional - pycryptosat # needs sage.modules sage.rings.finite_rings
87
- sage: F.subs(s[0]) # optional - pycryptosat # needs sage.modules sage.rings.finite_rings
86
+ sage: s = solve_sat(F)
87
+ sage: F.subs(s[0])
88
88
Polynomial Sequence with 36 Polynomials in 0 Variables
89
89
90
90
This time we pass a few options through to the converter and the solver::
91
91
92
- sage: s = solve_sat(F, c_max_vars_sparse=4, c_cutting_number=8) # optional - pycryptosat, needs sage.modules sage.rings.finite_rings
93
- sage: F.subs(s[0]) # optional - pycryptosat # needs sage.modules sage.rings.finite_rings
92
+ sage: s = solve_sat(F, c_max_vars_sparse=4, c_cutting_number=8)
93
+ sage: F.subs(s[0])
94
94
Polynomial Sequence with 36 Polynomials in 0 Variables
95
95
96
96
We construct a very simple system with three solutions
97
97
and ask for a specific number of solutions::
98
98
99
- sage: # optional - pycryptosat, needs sage.modules
100
99
sage: B.<a,b> = BooleanPolynomialRing()
101
100
sage: f = a*b
102
101
sage: l = solve_sat([f],n=1)
103
102
sage: len(l) == 1, f.subs(l[0])
104
103
(True, 0)
105
104
106
- sage: l = solve_sat([a*b],n=2) # optional - pycryptosat # needs sage.modules
107
- sage: len(l) == 2, f.subs(l[0]), f.subs(l[1]) # optional - pycryptosat # needs sage.modules
105
+ sage: l = solve_sat([a*b],n=2)
106
+ sage: len(l) == 2, f.subs(l[0]), f.subs(l[1])
108
107
(True, 0, 0)
109
108
110
- sage: sorted((d[a], d[b]) for d in solve_sat([a*b], n=3)) # optional - pycryptosat, needs sage.modules
109
+ sage: sorted((d[a], d[b]) for d in solve_sat([a*b], n=3))
111
110
[(0, 0), (0, 1), (1, 0)]
112
- sage: sorted((d[a], d[b]) for d in solve_sat([a*b], n=4)) # optional - pycryptosat, needs sage.modules
111
+ sage: sorted((d[a], d[b]) for d in solve_sat([a*b], n=4))
113
112
[(0, 0), (0, 1), (1, 0)]
114
- sage: sorted((d[a], d[b]) for d in solve_sat([a*b], n=infinity)) # optional - pycryptosat, needs sage.modules
113
+ sage: sorted((d[a], d[b]) for d in solve_sat([a*b], n=infinity))
115
114
[(0, 0), (0, 1), (1, 0)]
116
115
117
116
In the next example we see how the ``target_variables`` parameter works::
118
117
119
118
sage: from sage.sat.boolean_polynomials import solve as solve_sat
120
- sage: R.<a,b,c,d> = BooleanPolynomialRing() # optional - pycryptosat # needs sage.modules
121
- sage: F = [a + b, a + c + d] # optional - pycryptosat # needs sage.modules
119
+ sage: R.<a,b,c,d> = BooleanPolynomialRing()
120
+ sage: F = [a + b, a + c + d]
122
121
123
122
First the normal use case::
124
123
125
- sage: sorted((D[a], D[b], D[c], D[d]) # optional - pycryptosat # needs sage.modules
124
+ sage: sorted((D[a], D[b], D[c], D[d])
126
125
....: for D in solve_sat(F, n=infinity))
127
126
[(0, 0, 0, 0), (0, 0, 1, 1), (1, 1, 0, 1), (1, 1, 1, 0)]
128
127
129
128
Now we are only interested in the solutions of the variables a and b::
130
129
131
- sage: solve_sat(F, n=infinity, target_variables=[a,b]) # optional - pycryptosat, needs sage.modules
130
+ sage: solve_sat(F, n=infinity, target_variables=[a,b])
132
131
[{b: 0, a: 0}, {b: 1, a: 1}]
133
132
134
133
Here, we generate and solve the cubic equations of the AES SBox (see :trac:`26676`)::
135
134
136
- sage: # long time, optional - pycryptosat, needs sage.modules
135
+ sage: # long time
137
136
sage: from sage.rings.polynomial.multi_polynomial_sequence import PolynomialSequence
138
137
sage: from sage.sat.boolean_polynomials import solve as solve_sat
139
138
sage: sr = sage.crypto.mq.SR(1, 4, 4, 8,
@@ -145,7 +144,7 @@ def solve(F, converter=None, solver=None, n=1, target_variables=None, **kwds):
145
144
sage: variables = ",".join(variables)
146
145
sage: R = BooleanPolynomialRing(16, variables)
147
146
sage: eqs = [R(eq) for eq in eqs]
148
- sage: sls_aes = solve_sat(eqs, n = infinity)
147
+ sage: sls_aes = solve_sat(eqs, n= infinity)
149
148
sage: len(sls_aes)
150
149
256
151
150
@@ -168,7 +167,7 @@ def solve(F, converter=None, solver=None, n=1, target_variables=None, **kwds):
168
167
....: k9 + k28,
169
168
....: k11 + k20]
170
169
sage: from sage.sat.boolean_polynomials import solve as solve_sat
171
- sage: solve_sat(keqs, n=1, solver=SAT('cryptominisat')) # optional - pycryptosat
170
+ sage: solve_sat(keqs, n=1, solver=SAT('cryptominisat'))
172
171
[{k28: 0,
173
172
k26: 1,
174
173
k24: 0,
@@ -342,16 +341,16 @@ def learn(F, converter=None, solver=None, max_learnt_length=3, interreduction=Fa
342
341
343
342
EXAMPLES::
344
343
345
- sage: from sage.sat.boolean_polynomials import learn as learn_sat # optional - pycryptosat
344
+ sage: from sage.sat.boolean_polynomials import learn as learn_sat
346
345
347
346
We construct a simple system and solve it::
348
347
349
- sage: set_random_seed(2300)
350
- sage: sr = mq.SR(1, 2, 2, 4, gf2=True, polybori=True) # optional - pycryptosat, needs sage.modules sage.rings.finite_rings
351
- sage: F,s = sr.polynomial_system() # optional - pycryptosat # needs sage.modules sage.rings.finite_rings
352
- sage: H = learn_sat(F) # optional - pycryptosat # needs sage.modules sage.rings.finite_rings
353
- sage: H[-1] # optional - pycryptosat # needs sage.modules sage.rings.finite_rings
354
- k033 + 1
348
+ sage: set_random_seed(2300)
349
+ sage: sr = mq.SR(1, 2, 2, 4, gf2=True, polybori=True)
350
+ sage: F,s = sr.polynomial_system()
351
+ sage: H = learn_sat(F)
352
+ sage: H[-1]
353
+ k033 + 1
355
354
"""
356
355
try :
357
356
len (F )
0 commit comments