@@ -71,8 +71,8 @@ def solve(F, converter=None, solver=None, n=1, target_variables=None, **kwds):
71
71
72
72
We construct a very small-scale AES system of equations::
73
73
74
- sage: sr = mq.SR(1,1,1,4,gf2=True,polybori=True)
75
- sage: while True: # workaround (see :trac:`31891`)
74
+ sage: sr = mq.SR(1,1,1,4,gf2=True,polybori=True) # optional - sage.modules
75
+ sage: while True: # workaround (see :trac:`31891`) # optional - sage.modules
76
76
....: try:
77
77
....: F, s = sr.polynomial_system()
78
78
....: break
@@ -81,74 +81,77 @@ def solve(F, converter=None, solver=None, n=1, target_variables=None, **kwds):
81
81
82
82
and pass it to a SAT solver::
83
83
84
- sage: from sage.sat.boolean_polynomials import solve as solve_sat # optional - pycryptosat
85
- sage: s = solve_sat(F) # optional - pycryptosat
86
- sage: F.subs(s[0]) # optional - pycryptosat
84
+ sage: from sage.sat.boolean_polynomials import solve as solve_sat
85
+ sage: s = solve_sat(F) # optional - pycryptosat sage.modules
86
+ sage: F.subs(s[0]) # optional - pycryptosat sage.modules
87
87
Polynomial Sequence with 36 Polynomials in 0 Variables
88
88
89
89
This time we pass a few options through to the converter and the solver::
90
90
91
- sage: s = solve_sat(F, c_max_vars_sparse=4, c_cutting_number=8) # optional - pycryptosat
92
- sage: F.subs(s[0]) # optional - pycryptosat
91
+ sage: s = solve_sat(F, c_max_vars_sparse=4, c_cutting_number=8) # optional - pycryptosat sage.modules
92
+ sage: F.subs(s[0]) # optional - pycryptosat sage.modules
93
93
Polynomial Sequence with 36 Polynomials in 0 Variables
94
94
95
- We construct a very simple system with three solutions and ask for a specific number of solutions::
95
+ We construct a very simple system with three solutions
96
+ and ask for a specific number of solutions::
96
97
97
- sage: B.<a,b> = BooleanPolynomialRing() # optional - pycryptosat
98
- sage: f = a*b # optional - pycryptosat
99
- sage: l = solve_sat([f],n=1) # optional - pycryptosat
100
- sage: len(l) == 1, f.subs(l[0]) # optional - pycryptosat
98
+ sage: B.<a,b> = BooleanPolynomialRing() # optional - pycryptosat sage.modules
99
+ sage: f = a*b # optional - pycryptosat sage.modules
100
+ sage: l = solve_sat([f],n=1) # optional - pycryptosat sage.modules
101
+ sage: len(l) == 1, f.subs(l[0]) # optional - pycryptosat sage.modules
101
102
(True, 0)
102
103
103
- sage: l = solve_sat([a*b],n=2) # optional - pycryptosat
104
- sage: len(l) == 2, f.subs(l[0]), f.subs(l[1]) # optional - pycryptosat
104
+ sage: l = solve_sat([a*b],n=2) # optional - pycryptosat sage.modules
105
+ sage: len(l) == 2, f.subs(l[0]), f.subs(l[1]) # optional - pycryptosat sage.modules
105
106
(True, 0, 0)
106
107
107
- sage: sorted((d[a], d[b]) for d in solve_sat([a*b],n=3)) # optional - pycryptosat
108
+ sage: sorted((d[a], d[b]) for d in solve_sat([a*b], n=3)) # optional - pycryptosat sage.modules
108
109
[(0, 0), (0, 1), (1, 0)]
109
- sage: sorted((d[a], d[b]) for d in solve_sat([a*b],n=4)) # optional - pycryptosat
110
+ sage: sorted((d[a], d[b]) for d in solve_sat([a*b], n=4)) # optional - pycryptosat sage.modules
110
111
[(0, 0), (0, 1), (1, 0)]
111
- sage: sorted((d[a], d[b]) for d in solve_sat([a*b],n=infinity)) # optional - pycryptosat
112
+ sage: sorted((d[a], d[b]) for d in solve_sat([a*b], n=infinity)) # optional - pycryptosat sage.modules
112
113
[(0, 0), (0, 1), (1, 0)]
113
114
114
115
In the next example we see how the ``target_variables`` parameter works::
115
116
116
- sage: from sage.sat.boolean_polynomials import solve as solve_sat # optional - pycryptosat
117
- sage: R.<a,b,c,d> = BooleanPolynomialRing() # optional - pycryptosat
118
- sage: F = [a+b,a+c+ d] # optional - pycryptosat
117
+ sage: from sage.sat.boolean_polynomials import solve as solve_sat
118
+ sage: R.<a,b,c,d> = BooleanPolynomialRing() # optional - pycryptosat sage.modules
119
+ sage: F = [a + b, a + c + d] # optional - pycryptosat sage.modules
119
120
120
121
First the normal use case::
121
122
122
- sage: sorted((D[a], D[b], D[c], D[d]) for D in solve_sat(F,n=infinity)) # optional - pycryptosat
123
+ sage: sorted((D[a], D[b], D[c], D[d]) # optional - pycryptosat sage.modules
124
+ ....: for D in solve_sat(F, n=infinity))
123
125
[(0, 0, 0, 0), (0, 0, 1, 1), (1, 1, 0, 1), (1, 1, 1, 0)]
124
126
125
127
Now we are only interested in the solutions of the variables a and b::
126
128
127
- sage: solve_sat(F,n=infinity,target_variables=[a,b]) # optional - pycryptosat
129
+ sage: solve_sat(F,n=infinity,target_variables=[a,b]) # optional - pycryptosat sage.modules
128
130
[{b: 0, a: 0}, {b: 1, a: 1}]
129
131
130
132
Here, we generate and solve the cubic equations of the AES SBox (see :trac:`26676`)::
131
133
132
- sage: from sage.rings.polynomial.multi_polynomial_sequence import PolynomialSequence # optional - pycryptosat, long time
133
- sage: from sage.sat.boolean_polynomials import solve as solve_sat # optional - pycryptosat, long time
134
- sage: sr = sage.crypto.mq.SR(1, 4, 4, 8, allow_zero_inversions = True) # optional - pycryptosat, long time
135
- sage: sb = sr.sbox() # optional - pycryptosat, long time
136
- sage: eqs = sb.polynomials(degree = 3) # optional - pycryptosat, long time
137
- sage: eqs = PolynomialSequence(eqs) # optional - pycryptosat, long time
138
- sage: variables = map(str, eqs.variables()) # optional - pycryptosat, long time
139
- sage: variables = ",".join(variables) # optional - pycryptosat, long time
140
- sage: R = BooleanPolynomialRing(16, variables) # optional - pycryptosat, long time
141
- sage: eqs = [R(eq) for eq in eqs] # optional - pycryptosat, long time
142
- sage: sls_aes = solve_sat(eqs, n = infinity) # optional - pycryptosat, long time
143
- sage: len(sls_aes) # optional - pycryptosat, long time
134
+ sage: from sage.rings.polynomial.multi_polynomial_sequence import PolynomialSequence
135
+ sage: from sage.sat.boolean_polynomials import solve as solve_sat
136
+ sage: sr = sage.crypto.mq.SR(1, 4, 4, 8, # long time, optional - pycryptosat sage.modules
137
+ ....: allow_zero_inversions=True)
138
+ sage: sb = sr.sbox() # long time, optional - pycryptosat sage.modules
139
+ sage: eqs = sb.polynomials(degree = 3) # long time, optional - pycryptosat sage.modules
140
+ sage: eqs = PolynomialSequence(eqs) # long time, optional - pycryptosat sage.modules
141
+ sage: variables = map(str, eqs.variables()) # long time, optional - pycryptosat sage.modules
142
+ sage: variables = ",".join(variables) # long time, optional - pycryptosat sage.modules
143
+ sage: R = BooleanPolynomialRing(16, variables) # long time, optional - pycryptosat sage.modules
144
+ sage: eqs = [R(eq) for eq in eqs] # long time, optional - pycryptosat sage.modules
145
+ sage: sls_aes = solve_sat(eqs, n = infinity) # long time, optional - pycryptosat sage.modules
146
+ sage: len(sls_aes) # long time, optional - pycryptosat sage.modules
144
147
256
145
148
146
149
TESTS:
147
150
148
151
Test that :trac:`26676` is fixed::
149
152
150
153
sage: varl = ['k{0}'.format(p) for p in range(29)]
151
- sage: B = BooleanPolynomialRing(names = varl)
154
+ sage: B = BooleanPolynomialRing(names= varl)
152
155
sage: B.inject_variables(verbose=False)
153
156
sage: keqs = [
154
157
....: k0 + k6 + 1,
@@ -162,7 +165,7 @@ def solve(F, converter=None, solver=None, n=1, target_variables=None, **kwds):
162
165
....: k9 + k28,
163
166
....: k11 + k20]
164
167
sage: from sage.sat.boolean_polynomials import solve as solve_sat
165
- sage: solve_sat(keqs, n=1, solver=SAT('cryptominisat')) # optional - pycryptosat
168
+ sage: solve_sat(keqs, n=1, solver=SAT('cryptominisat')) # optional - pycryptosat
166
169
[{k28: 0,
167
170
k26: 1,
168
171
k24: 0,
@@ -187,7 +190,7 @@ def solve(F, converter=None, solver=None, n=1, target_variables=None, **kwds):
187
190
k2: 0,
188
191
k1: 0,
189
192
k0: 0}]
190
- sage: solve_sat(keqs, n=1, solver=SAT('picosat')) # optional - pycosat
193
+ sage: solve_sat(keqs, n=1, solver=SAT('picosat')) # optional - pycosat
191
194
[{k28: 0,
192
195
k26: 1,
193
196
k24: 0,
0 commit comments