@@ -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) # optional - sage.modules sage.rings.finite_rings
76
- sage: while True: # workaround (see :trac:`31891`) # optional - sage.modules sage.rings.finite_rings
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
77
77
....: try:
78
78
....: F, s = sr.polynomial_system()
79
79
....: break
@@ -83,68 +83,70 @@ 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 sage.modules sage.rings.finite_rings
87
- sage: F.subs(s[0]) # optional - pycryptosat sage.modules sage.rings.finite_rings
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
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 sage.modules sage.rings.finite_rings
93
- sage: F.subs(s[0]) # optional - pycryptosat sage.modules sage.rings.finite_rings
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
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: B.<a,b> = BooleanPolynomialRing() # optional - pycryptosat sage.modules
100
- sage: f = a*b # optional - pycryptosat sage.modules
101
- sage: l = solve_sat([f],n=1) # optional - pycryptosat sage.modules
102
- sage: len(l) == 1, f.subs(l[0]) # optional - pycryptosat sage.modules
99
+ sage: # optional - pycryptosat, needs sage.modules
100
+ sage: B.<a,b> = BooleanPolynomialRing()
101
+ sage: f = a*b
102
+ sage: l = solve_sat([f],n=1)
103
+ sage: len(l) == 1, f.subs(l[0])
103
104
(True, 0)
104
105
105
- sage: l = solve_sat([a*b],n=2) # optional - pycryptosat sage.modules
106
- sage: len(l) == 2, f.subs(l[0]), f.subs(l[1]) # optional - pycryptosat sage.modules
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
107
108
(True, 0, 0)
108
109
109
- sage: sorted((d[a], d[b]) for d in solve_sat([a*b], n=3)) # optional - pycryptosat sage.modules
110
+ sage: sorted((d[a], d[b]) for d in solve_sat([a*b], n=3)) # optional - pycryptosat, needs 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=4)) # optional - pycryptosat sage.modules
112
+ sage: sorted((d[a], d[b]) for d in solve_sat([a*b], n=4)) # optional - pycryptosat, needs sage.modules
112
113
[(0, 0), (0, 1), (1, 0)]
113
- sage: sorted((d[a], d[b]) for d in solve_sat([a*b], n=infinity)) # optional - pycryptosat sage.modules
114
+ sage: sorted((d[a], d[b]) for d in solve_sat([a*b], n=infinity)) # optional - pycryptosat, needs sage.modules
114
115
[(0, 0), (0, 1), (1, 0)]
115
116
116
117
In the next example we see how the ``target_variables`` parameter works::
117
118
118
119
sage: from sage.sat.boolean_polynomials import solve as solve_sat
119
- sage: R.<a,b,c,d> = BooleanPolynomialRing() # optional - pycryptosat sage.modules
120
- sage: F = [a + b, a + c + d] # optional - pycryptosat sage.modules
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
121
122
122
123
First the normal use case::
123
124
124
- sage: sorted((D[a], D[b], D[c], D[d]) # optional - pycryptosat sage.modules
125
+ sage: sorted((D[a], D[b], D[c], D[d]) # optional - pycryptosat # needs sage.modules
125
126
....: for D in solve_sat(F, n=infinity))
126
127
[(0, 0, 0, 0), (0, 0, 1, 1), (1, 1, 0, 1), (1, 1, 1, 0)]
127
128
128
129
Now we are only interested in the solutions of the variables a and b::
129
130
130
- sage: solve_sat(F, n=infinity, target_variables=[a,b]) # optional - pycryptosat sage.modules
131
+ sage: solve_sat(F, n=infinity, target_variables=[a,b]) # optional - pycryptosat, needs sage.modules
131
132
[{b: 0, a: 0}, {b: 1, a: 1}]
132
133
133
134
Here, we generate and solve the cubic equations of the AES SBox (see :trac:`26676`)::
134
135
136
+ sage: # long time, optional - pycryptosat, needs sage.modules
135
137
sage: from sage.rings.polynomial.multi_polynomial_sequence import PolynomialSequence
136
138
sage: from sage.sat.boolean_polynomials import solve as solve_sat
137
- sage: sr = sage.crypto.mq.SR(1, 4, 4, 8, # long time, optional - pycryptosat sage.modules
139
+ sage: sr = sage.crypto.mq.SR(1, 4, 4, 8,
138
140
....: allow_zero_inversions=True)
139
- sage: sb = sr.sbox() # long time, optional - pycryptosat sage.modules
140
- sage: eqs = sb.polynomials(degree=3) # long time, optional - pycryptosat sage.modules
141
- sage: eqs = PolynomialSequence(eqs) # long time, optional - pycryptosat sage.modules
142
- sage: variables = map(str, eqs.variables()) # long time, optional - pycryptosat sage.modules
143
- sage: variables = ",".join(variables) # long time, optional - pycryptosat sage.modules
144
- sage: R = BooleanPolynomialRing(16, variables) # long time, optional - pycryptosat sage.modules
145
- sage: eqs = [R(eq) for eq in eqs] # long time, optional - pycryptosat sage.modules
146
- sage: sls_aes = solve_sat(eqs, n = infinity) # long time, optional - pycryptosat sage.modules
147
- sage: len(sls_aes) # long time, optional - pycryptosat sage.modules
141
+ sage: sb = sr.sbox()
142
+ sage: eqs = sb.polynomials(degree=3)
143
+ sage: eqs = PolynomialSequence(eqs)
144
+ sage: variables = map(str, eqs.variables())
145
+ sage: variables = ",".join(variables)
146
+ sage: R = BooleanPolynomialRing(16, variables)
147
+ sage: eqs = [R(eq) for eq in eqs]
148
+ sage: sls_aes = solve_sat(eqs, n = infinity)
149
+ sage: len(sls_aes)
148
150
256
149
151
150
152
TESTS:
@@ -345,10 +347,10 @@ def learn(F, converter=None, solver=None, max_learnt_length=3, interreduction=Fa
345
347
We construct a simple system and solve it::
346
348
347
349
sage: set_random_seed(2300)
348
- sage: sr = mq.SR(1, 2, 2, 4, gf2=True, polybori=True) # optional - pycryptosat sage.modules sage.rings.finite_rings
349
- sage: F,s = sr.polynomial_system() # optional - pycryptosat sage.modules sage.rings.finite_rings
350
- sage: H = learn_sat(F) # optional - pycryptosat sage.modules sage.rings.finite_rings
351
- sage: H[-1] # optional - pycryptosat sage.modules sage.rings.finite_rings
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
352
354
k033 + 1
353
355
"""
354
356
try :
0 commit comments