Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 19204
1
r"""
2
A boolean function class that represents a General Linear Equivalence Class
3
============================================================================
4
5
The ``boolean_function_general_linear_class`` module defines
6
the ``BooleanFunctionGeneralLinearClass`` class,
7
which is a subclass of BooleanFunctionImproved that represents
8
a general linear equivalence class of boolean functions.
9
10
11
AUTHORS:
12
13
- Paul Leopardi (2023-02-05): initial version
14
15
EXAMPLES:
16
17
::
18
19
sage: from boolean_cayley_graphs.boolean_function_general_linear_class import (
20
....: BooleanFunctionGeneralLinearClass)
21
sage: bf = BooleanFunctionGeneralLinearClass([0,0,0,1])
22
sage: type(bf)
23
<class 'boolean_cayley_graphs.boolean_function_general_linear_class.BooleanFunctionGeneralLinearClass'>
24
sage: bf.truth_table(format='int')
25
(0, 0, 0, 1)
26
"""
27
28
#*****************************************************************************
29
# Copyright (C) 2016-2017 Paul Leopardi [email protected]
30
#
31
# Distributed under the terms of the GNU General Public License (GPL)
32
# as published by the Free Software Foundation; either version 2 of
33
# the License, or (at your option) any later version.
34
# http://www.gnu.org/licenses/
35
#*****************************************************************************
36
37
import binascii
38
import csv
39
40
from datetime import datetime
41
from sage.crypto.boolean_function import BooleanFunction
42
from sage.matrix.constructor import Matrix
43
from sage.rings.finite_rings.finite_field_constructor import FiniteField as GF
44
from sage.rings.integer import Integer
45
from sage.rings.integer_ring import ZZ
46
from sys import stdout
47
48
from boolean_cayley_graphs.boolean_function_improved import BooleanFunctionImproved
49
from boolean_cayley_graphs.saveable import Saveable
50
51
import boolean_cayley_graphs.cayley_graph_controls as controls
52
53
encoding = "UTF-8"
54
55
56
class BooleanFunctionGeneralLinearClass(BooleanFunctionImproved, Saveable):
57
r"""
58
A subclass of BooleanFunctionImproved that represents
59
a general linear equivalence class of boolean functions.
60
61
The class inherits from BooleanFunctionImproved and is initialized in the same way.
62
The class inherits from Saveable to obtain load_mangled and save_mangled methods.
63
64
EXAMPLES:
65
66
::
67
68
sage: from boolean_cayley_graphs.boolean_function_general_linear_class import (
69
....: BooleanFunctionGeneralLinearClass)
70
sage: bf1 = BooleanFunctionGeneralLinearClass([0,1,0,0])
71
sage: type(bf1)
72
<class 'boolean_cayley_graphs.boolean_function_general_linear_class.BooleanFunctionGeneralLinearClass'>
73
sage: bf1.algebraic_normal_form()
74
x0*x1 + x0
75
sage: bf1.truth_table()
76
(False, True, False, False)
77
78
TESTS:
79
80
::
81
82
sage: from boolean_cayley_graphs.boolean_function_general_linear_class import (
83
....: BooleanFunctionGeneralLinearClass)
84
sage: bf = BooleanFunctionGeneralLinearClass([0,1,0,0])
85
sage: print(bf)
86
Boolean function with 2 variables
87
88
sage: from boolean_cayley_graphs.boolean_function_general_linear_class import (
89
....: BooleanFunctionGeneralLinearClass)
90
sage: bf = BooleanFunctionGeneralLinearClass([0,1,0,0])
91
sage: latex(bf)
92
\text{\texttt{Boolean{ }function{ }with{ }2{ }variables}}
93
"""
94
95
96
@classmethod
97
def from_tt_buffer(
98
cls,
99
dim,
100
tt_buffer):
101
r"""
102
Constructor from the buffer tt_buffer.
103
104
The buffer tt_buffer is assumed to be the result of method tt_buffer(),
105
which returns a result of type buffer representing a truth table in hex.
106
107
INPUT:
108
109
- ``cls`` -- the class object.
110
- ``dim`` -- integer: the dimension of the Boolean function.
111
- ``tt_buffer`` -- buffer: the result of the method tt_buffer()
112
for the Boolean function.
113
114
EXAMPLES:
115
116
::
117
118
sage: from boolean_cayley_graphs.boolean_function_general_linear_class import (
119
....: BooleanFunctionGeneralLinearClass)
120
sage: bf2 = BooleanFunctionGeneralLinearClass([0,1,0,0])
121
sage: bf2_tt_buffer = bf2.tt_buffer()
122
sage: bf2_test = BooleanFunctionGeneralLinearClass.from_tt_buffer(2, bf2_tt_buffer)
123
sage: bf2_test.algebraic_normal_form()
124
x0*x1 + x0
125
sage: bf2 == bf2_test
126
True
127
sage: bf3 = BooleanFunctionGeneralLinearClass([0,1,0,0]*2)
128
sage: bf3.nvariables()
129
3
130
sage: bf3_tt_buffer = bf3.tt_buffer()
131
sage: bf3_test = BooleanFunctionGeneralLinearClass.from_tt_buffer(3, bf3_tt_buffer)
132
sage: bf3 == bf3_test
133
True
134
"""
135
return cls(BooleanFunctionImproved.from_tt_buffer(dim, tt_buffer))
136
137
138
@classmethod
139
def from_tt_hex(
140
cls,
141
dim,
142
tt_hex):
143
r"""
144
Constructor from the dimension dim, and the string tt_hex.
145
146
The string tt_hex is assumed to be the result of method tt_hex(), which returns
147
a string representing a truth table in hex.
148
149
INPUT:
150
151
- ``cls`` -- the class object.
152
- ``dim`` -- integer: the dimension of the Boolean function.
153
- ``tt_hex`` -- string: the result of the method tt_hex() for the Boolean function.
154
155
EXAMPLES:
156
157
::
158
159
sage: from boolean_cayley_graphs.boolean_function_general_linear_class import (
160
....: BooleanFunctionGeneralLinearClass)
161
sage: bf2 = BooleanFunctionGeneralLinearClass([0,1,0,0])
162
sage: bf2_tt_hex = bf2.tt_hex()
163
sage: bf2_test = BooleanFunctionGeneralLinearClass.from_tt_hex(2, bf2_tt_hex)
164
sage: bf2_test.algebraic_normal_form()
165
x0*x1 + x0
166
sage: bf2 == bf2_test
167
True
168
169
TESTS:
170
171
::
172
173
sage: from boolean_cayley_graphs.boolean_function_general_linear_class import (
174
....: BooleanFunctionGeneralLinearClass)
175
sage: bf1 = BooleanFunctionGeneralLinearClass([0,1])
176
sage: bf1_tt_hex = bf1.tt_hex()
177
sage: bf1_test = BooleanFunctionGeneralLinearClass.from_tt_hex(1, bf1_tt_hex)
178
sage: bf1_test.algebraic_normal_form()
179
x
180
sage: bf1 == bf1_test
181
True
182
sage: bf3 = BooleanFunctionGeneralLinearClass([0,1,0,0]*2)
183
sage: bf3.nvariables()
184
3
185
sage: bf3_tt_hex = bf3.tt_hex()
186
sage: bf3_test = BooleanFunctionGeneralLinearClass.from_tt_hex(3, bf3_tt_hex)
187
sage: bf3 == bf3_test
188
True
189
"""
190
return cls(BooleanFunctionImproved.from_tt_hex(dim, tt_hex))
191
192
193
@classmethod
194
def from_csv(
195
cls,
196
csv_file_name):
197
"""
198
Constructor from a csv file.
199
200
The csv file is assumed to be produced by the method save_as_csv().
201
202
INPUT:
203
204
- ``cls`` -- the class object.
205
- ``csv_file_name`` -- string: the name of the csv file to read from.
206
207
EXAMPLES:
208
209
::
210
211
sage: import csv
212
sage: import os
213
sage: from boolean_cayley_graphs.boolean_function_general_linear_class import (
214
....: BooleanFunctionGeneralLinearClass)
215
sage: bf2 = BooleanFunctionGeneralLinearClass([1,0,1,1])
216
sage: bf2_csv_name = tmp_filename(ext='.csv')
217
sage: bf2.save_as_csv(bf2_csv_name)
218
sage: bf2_test = BooleanFunctionGeneralLinearClass.from_csv(bf2_csv_name)
219
sage: bf2 == bf2_test
220
True
221
sage: os.remove(bf2_csv_name)
222
sage: bf3 = BooleanFunctionGeneralLinearClass([0,1,0,0]*2)
223
sage: bf3_csv_name = tmp_filename(ext='.csv')
224
sage: bf3.save_as_csv(bf3_csv_name)
225
sage: bf3_test = BooleanFunctionGeneralLinearClass.from_csv(bf3_csv_name)
226
sage: bf3 == bf3_test
227
True
228
"""
229
return cls(BooleanFunctionImproved.from_csv(csv_file_name))
230
231
232
def __eq__(self, other):
233
"""
234
Test for equality between extended translation equivalence classes.
235
236
WARNING:
237
238
This test is for mathematical equivalence rather than strict equality.
239
240
INPUT:
241
242
- ``other`` - BooleanFunctionExtendedTranslateClassification: another equivalence class.
243
244
OUTPUT:
245
246
A Boolean value indicating whether ``self`` is equivalent to ``other``.
247
248
EXAMPLES:
249
250
::
251
252
sage: from boolean_cayley_graphs.boolean_function_improved import BooleanFunctionImproved
253
sage: from boolean_cayley_graphs.boolean_function_extended_translate_classification import (
254
....: BooleanFunctionExtendedTranslateClassification as BooleanFunctionETC)
255
sage: R2.<x0,x1> = BooleanPolynomialRing(2)
256
sage: p = x0*x1
257
sage: f1 =BooleanFunctionImproved(p)
258
sage: c1 = BooleanFunctionETC.from_function(f1)
259
sage: f2 =BooleanFunctionImproved([0,0,0,1])
260
sage: c2 = BooleanFunctionETC.from_function(f2)
261
sage: print(c2.algebraic_normal_form)
262
x0*x1
263
sage: print(c1 == c2)
264
True
265
"""
266
return self.is_linear_equivalent(other)
267
268
269
def __invert__(self):
270
"""
271
Return the complement Boolean function of `self`.
272
273
INPUT:
274
275
- ``self`` -- the current object.
276
277
EXAMPLES:
278
279
::
280
281
sage: from boolean_cayley_graphs.boolean_function_general_linear_class import BooleanFunctionGeneralLinearClass
282
sage: bf0 = BooleanFunctionGeneralLinearClass([1,0,1,1])
283
sage: bf1 = ~bf0
284
sage: type(bf1)
285
<class 'boolean_cayley_graphs.boolean_function_general_linear_class.BooleanFunctionGeneralLinearClass'>
286
sage: bf1.algebraic_normal_form()
287
x0*x1 + x0
288
sage: bf1.truth_table()
289
(False, True, False, False)
290
"""
291
bf_self = BooleanFunctionImproved(self)
292
return type(self)(~bf_self)
293
294
295
def __add__(self, other):
296
"""
297
Return the elementwise sum of `self`and `other` which must have the same number of variables.
298
299
INPUT:
300
301
- ``self`` -- the current object.
302
- ``other`` -- another Boolean function.
303
304
OUTPUT:
305
306
The elementwise sum of `self`and `other`
307
308
EXAMPLES:
309
310
::
311
312
sage: from boolean_cayley_graphs.boolean_function_general_linear_class import BooleanFunctionGeneralLinearClass
313
sage: bf0 = BooleanFunctionGeneralLinearClass([1,0,1,0])
314
sage: bf1 = BooleanFunctionGeneralLinearClass([1,1,0,0])
315
sage: (bf0+bf1).truth_table(format='int')
316
(0, 1, 1, 0)
317
sage: S = bf0.algebraic_normal_form() + bf1.algebraic_normal_form()
318
sage: (bf0+bf1).algebraic_normal_form() == S
319
True
320
321
TESTS:
322
323
::
324
325
sage: bf0+BooleanFunctionGeneralLinearClass([0,1])
326
Traceback (most recent call last):
327
...
328
ValueError: the two Boolean functions must have the same number of variables
329
"""
330
bf_self = BooleanFunctionImproved(self)
331
return type(self)(bf_self + other)
332
333
334
def __mul__(self, other):
335
"""
336
Return the elementwise product of `self`and `other` which must have the same number of variables.
337
338
INPUT:
339
340
- ``self`` -- the current object.
341
- ``other`` -- another Boolean function.
342
343
OUTPUT:
344
345
The elementwise product of `self`and `other`
346
347
EXAMPLES:
348
349
::
350
351
sage: from boolean_cayley_graphs.boolean_function_general_linear_class import BooleanFunctionGeneralLinearClass
352
sage: bf0 = BooleanFunctionGeneralLinearClass([1,0,1,0])
353
sage: bf1 = BooleanFunctionGeneralLinearClass([1,1,0,0])
354
sage: (bf0*bf1).truth_table(format='int')
355
(1, 0, 0, 0)
356
sage: P = bf0.algebraic_normal_form() * bf1.algebraic_normal_form()
357
sage: (bf0*bf1).algebraic_normal_form() == P
358
True
359
360
TESTS:
361
362
::
363
364
sage: bf0*BooleanFunctionGeneralLinearClass([0,1])
365
Traceback (most recent call last):
366
...
367
ValueError: the two Boolean functions must have the same number of variables
368
"""
369
bf_self = BooleanFunctionImproved(self)
370
return type(self)(bf_self * other)
371
372
373
def __or__(self, other):
374
"""
375
Return the concatenation of `self` and `other` which must have the same number of variables.
376
377
INPUT:
378
379
- ``self`` -- the current object.
380
- ``other`` -- another Boolean function.
381
382
OUTPUT:
383
384
The concatenation of `self`and `other`
385
386
EXAMPLES:
387
388
::
389
390
sage: from boolean_cayley_graphs.boolean_function_general_linear_class import BooleanFunctionGeneralLinearClass
391
sage: bf0 = BooleanFunctionGeneralLinearClass([1,0,1,0])
392
sage: bf1 = BooleanFunctionGeneralLinearClass([1,1,0,0])
393
sage: (bf0|bf1).truth_table(format='int')
394
(1, 0, 1, 0, 1, 1, 0, 0)
395
sage: C = bf0.truth_table() + bf1.truth_table()
396
sage: (bf0|bf1).truth_table(format='int') == C
397
True
398
399
TESTS:
400
401
::
402
403
sage: bf0|BooleanFunctionGeneralLinearClass([0,1])
404
Traceback (most recent call last):
405
...
406
ValueError: the two Boolean functions must have the same number of variables
407
"""
408
bf_self = BooleanFunctionImproved(self)
409
return type(self)(bf_self | other)
410
411