-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.py
More file actions
executable file
·777 lines (704 loc) · 22.4 KB
/
Copy pathdemo.py
File metadata and controls
executable file
·777 lines (704 loc) · 22.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
#!/usr/bin/env python
"""Demonstration of BitVector features.
This script demonstrates various ways to construct, manipulate, and
utilize BitVector objects.
"""
import base64
import io
from BitVector import BitVector
# Construct an EMPTY bit vector (a bit vector of size 0):
print("\nConstructing an EMPTY bit vector (a bit vector of size 0):")
bv1 = BitVector(size=0)
print(bv1) # no output
# Construct a bit vector of size 2:
print("\nConstructing a bit vector of size 2:")
bv2 = BitVector(size=2)
print(bv2) # 00
# Joining two bit vectors:
print("\nConcatenating two previously constructed bit vectors:")
result = bv1 + bv2
print(result) # 00
# Construct a bit vector with a tuple of bits:
print("\nConstructing a bit vector from a tuple of bits:")
bv = BitVector(bitlist=(1, 0, 0, 1))
print(bv) # 1001
# Construct a bit vector with a list of bits:
print("\nConstructing a bit vector from a list of bits:")
bv = BitVector(bitlist=[1, 1, 0, 1])
print(bv) # 1101
# Construct a bit vector from an integer
bv = BitVector.from_int(5678)
print("\nBit vector constructed from integer 5678:")
print(bv) # 1011000101110
print("\nBit vector constructed from integer 0:")
bv = BitVector.from_int(0)
print(bv) # 0
print("\nBit vector constructed from integer 2:")
bv = BitVector.from_int(2)
print(bv) # 10
print("\nBit vector constructed from integer 3:")
bv = BitVector.from_int(3)
print(bv) # 11
print("\nBit vector constructed from integer 123456:")
bv = BitVector.from_int(123456)
print(bv) # 11110001001000000
print("\nInt value of the previous bit vector as computed by int():")
print(int(bv)) # 123456
print("\nInt value of the previous bit vector as computed by int():")
print(int(bv)) # 123456
# Construct a bit vector from a very large integer:
x = 12345678901234567890123456789012345678901234567890123456789012345678901234567890
bv = BitVector.from_int(x)
print("\nHere is a bit vector constructed from a very large integer:")
print(bv)
print(f"The integer value of the above bit vector is:{int(bv)}")
# Construct a bit vector directly from a file-like object:
bit_str = "111100001111"
fp_read = io.StringIO(bit_str)
print("\nBit vector constructed directly from a file like object:")
print(bv) # 111100001111
# Construct a bit vector directly from a bit string:
bv = BitVector.from_bitstring("00110011")
print("\nBit Vector constructed directly from a bit string:")
print(bv) # 00110011
bv = BitVector.from_bitstring("")
print("\nBit Vector constructed directly from an empty bit string:")
print(bv) # nothing
print("\nInteger value of the previous bit vector:")
print(int(bv)) # 0
# Construct a bit vector from a text string:
print("\nConstructing a bit vector from the textstring 'hello':")
bv3 = BitVector.from_string("hello")
print(bv3) # 0110100001100101011011000110110001101111
mytext = bv3.get_bitvector_in_ascii()
print("Text recovered from the previous bitvector: ")
print(mytext) # hello
print("\nConstructing a bit vector from the textstring 'hello\\njello':")
bv3 = BitVector.from_string("hello\njello")
print(
bv3,
) # 0110100001100101011011000110110001101111000010100110101001100101011011000110110001101111
mytext = bv3.get_bitvector_in_ascii()
print("Text recovered from the previous bitvector:")
print(mytext) # hello
# jello
# Construct a bit vector from a hex string:
print("\nConstructing a bit vector from the hex string '68656c6c6f':")
bv4 = BitVector.from_hex("68656c6c6f")
print(bv4) # 0110100001100101011011000110110001101111
myhexstring = bv4.get_bitvector_in_hex()
print("Hex string recovered from the previous bitvector: ")
print(myhexstring) # 68656c6c6f
print("\nConstructing a bit vector from the uppercase hex string '68656C6C6F':")
bv4 = BitVector.from_hex("68656C6C6F")
print(bv4) # 0110100001100101011011000110110001101111
myhexstring = bv4.get_bitvector_in_hex()
print("Hex string recovered from the previous bitvector: ")
print(myhexstring) # 68656c6c6f
# Construct a bit vector from a string of raw bytes:
print(
"\nDemonstrating the raw bytes mode of constructing a bit vector (useful for reading public and private keys):",
)
mypubkey = "ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA5amriY96HQS8Y/nKc8zu3zOylvpOn3vzMmWwrtyDy+aBvns4UC1RXoaD9rDKqNNMCBAQwWDsYwCAFsrBzbxRQONHePX8lRWgM87MseWGlu6WPzWGiJMclTAO9CTknplG9wlNzLQBj3dP1M895iLF6jvJ7GR+V3CRU6UUbMmRvgPcsfv6ec9RRPm/B8ftUuQICL0jt4tKdPG45PBJUylHs71FuE9FJNp01hrj1EMFObNTcsy9zuis0YPyzArTYSOUsGglleExAQYi7iLh17pAa+y6fZrGLsptgqryuftN9Q4NqPuTiFjlqRowCDU7sSxKDgU7bzhshyVx3+pzXO4D2Q== kak@pixie"
keydata = base64.b64decode(bytes(mypubkey.split(None)[1], "utf-8"))
bv = BitVector.from_bytes(keydata)
print(bv)
# Test array-like indexing for a bit vector:
bv = BitVector.from_bitstring("110001")
print("\nPrints out bits individually from bitstring 110001:")
print(bv[0], bv[1], bv[2], bv[3], bv[4], bv[5]) # 1 1 0 0 0 1
print("\nSame as above but using negative array indexing:")
print(bv[-1], bv[-2], bv[-3], bv[-4], bv[-5], bv[-6]) # 1 0 0 0 1 1
# Test setting bit values with positive and negative accessors:
bv = BitVector.from_bitstring("1111")
print("\nBitstring for 1111:")
print(bv) # 1111
print("\nReset individual bits of above vector:")
bv[0] = 0
bv[1] = 0
bv[2] = 0
bv[3] = 0
print(bv) # 0000
print("\nDo the same as above with negative indices:")
bv[-1] = 1
bv[-2] = 1
bv[-4] = 1
print(bv) # 1011
print("\nCheck equality and inequality ops:")
bv1 = BitVector.from_bitstring("00110011")
bv2 = BitVector(bitlist=[0, 0, 1, 1, 0, 0, 1, 1])
print(bv1 == bv2) # True
print(bv1 != bv2) # False
print(bv1 < bv2) # False
print(bv1 <= bv2) # True
bv3 = BitVector.from_int(5678)
print(int(bv3)) # 5678
print(bv3) # 1011000101110
print(bv1 == bv3) # False
print(bv3 > bv1) # True
print(bv3 >= bv1) # True
# Write a bit vector to a file like object
fp_write = io.StringIO()
bv.write_bits_to_stream_object(fp_write)
print("\nGet bit vector written out to a file-like object:")
print(fp_write.getvalue()) # 1011
print("\nExperiments with bitwise logical operations:")
bv3 = bv1 | bv2
print(bv3) # 00110011
bv3 = bv1 & bv2
print(bv3) # 00110011
bv3 = bv1 + bv2
print(bv3) # 0011001100110011
bv4 = BitVector(size=3)
print(bv4) # 000
bv5 = bv3 + bv4
print(bv5) # 0011001100110011000
bv6 = ~bv5
print(bv6) # 1100110011001100111
bv7 = bv5 & bv6
print(bv7) # 0000000000000000000
bv7 = bv5 | bv6
print(bv7) # 1111111111111111111
print("\nTry logical operations on bit vectors of different sizes:")
print(BitVector.from_int(6) ^ BitVector.from_int(13)) # 1011
print(BitVector.from_int(6) & BitVector.from_int(13)) # 0100
print(BitVector.from_int(6) | BitVector.from_int(13)) # 1111
print(BitVector.from_int(1) ^ BitVector.from_int(13)) # 1100
print(BitVector.from_int(1) & BitVector.from_int(13)) # 0001
print(BitVector.from_int(1) | BitVector.from_int(13)) # 1101
print("\nExperiments with setbit() and len():")
bv7[7] = 0
print(bv7) # 1111111011111111111
print(len(bv7)) # 19
bv8 = (bv5 & bv6) ^ bv7
print(bv8) # 1111111011111111111
print("\nConstruct a bit vector from what is in the file testinput1.txt:")
# print bv # nothing to show
print("\nPrint out the first 64 bits read from the file:")
print(bv1)
# 0100000100100000011010000111010101101110011001110111001001111001
print("\nRead the next 64 bits from the same file:")
print(bv2)
# 0010000001100010011100100110111101110111011011100010000001100110
print("\nTake xor of the previous two bit vectors:")
bv3 = bv1 ^ bv2
print(bv3)
# 0110000101000010000110100001101000011001000010010101001000011111
print("\nExperiment with dividing an even-sized vector into two:")
bv4, bv5 = bv3.divide_into_two()
print(bv4) # 01100001010000100001101000011010
print(bv5) # 00011001000010010101001000011111
# Permute a bit vector:
print("\nWe will use this bit vector for experiments with permute()")
bv1 = BitVector(bitlist=[1, 0, 0, 1, 1, 0, 1])
print(bv1) # 1001101
bv2 = bv1.permute([6, 2, 0, 1])
print("\nPermuted and contracted form of the previous bit vector:")
print(bv2) # 1010
print(
"\nExperiment with writing an internally generated bit vector out to a disk file:",
)
# bv1 = BitVector( bitstring = '00001010' )
bv1 = BitVector.from_bitstring("11100111")
with open("test.txt", "wb") as FILEOUT:
bv1.write_to_file(FILEOUT)
print(
"\nDisplay bit vectors written out to file and read back from the file and their respective lengths:",
)
print(str(bv1) + " " + str(bv3))
print(str(len(bv1)) + " " + str(len(bv3)))
print("\nExperiments with reading a file from the beginning to end:")
print("\nHere are all the bits read from the file:")
print("\n")
print(
"\nExperiment with closing a file object and start extracting bit vectors from the file from the beginning again:",
)
print(
"\nHere are all the first 64 bits read from the file again after the file object was closed and opened again:",
)
print(bv1)
with open("testinput5.txt", "wb") as FILEOUT:
bv1.write_to_file(FILEOUT)
print(
"\nExperiment in 64-bit permutation and unpermutation of the previous 64-bit bitvector:",
)
print(
"The permutation array was generated separately by the Fisher-Yates shuffle algorithm:",
)
bv2 = bv1.permute(
[
22,
47,
33,
36,
18,
6,
32,
29,
54,
62,
4,
9,
42,
39,
45,
59,
8,
50,
35,
20,
25,
49,
15,
61,
55,
60,
0,
14,
38,
40,
23,
17,
41,
10,
57,
12,
30,
3,
52,
11,
26,
43,
21,
13,
58,
37,
48,
28,
1,
63,
2,
31,
53,
56,
44,
24,
51,
19,
7,
5,
34,
27,
16,
46,
],
)
print("Permuted bit vector:")
print(bv2)
bv3 = bv2.unpermute(
[
22,
47,
33,
36,
18,
6,
32,
29,
54,
62,
4,
9,
42,
39,
45,
59,
8,
50,
35,
20,
25,
49,
15,
61,
55,
60,
0,
14,
38,
40,
23,
17,
41,
10,
57,
12,
30,
3,
52,
11,
26,
43,
21,
13,
58,
37,
48,
28,
1,
63,
2,
31,
53,
56,
44,
24,
51,
19,
7,
5,
34,
27,
16,
46,
],
)
print("Unpurmute the bit vector:")
print(bv3)
print(
"\nTry circular shifts to the left and to the right for the following bit vector:",
)
print(bv3) # 0100000100100000011010000111010101101110011001110111001001111001
print("\nCircular shift to the left by 7 positions:")
bv3 << 7 # pylint: disable=pointless-statement
print(bv3) # 1001000000110100001110101011011100110011101110010011110010100000
print("\nCircular shift to the right by 7 positions:")
bv3 >> 7 # pylint: disable=pointless-statement
print(bv3) # 0100000100100000011010000111010101101110011001110111001001111001
print("Test len() on the above bit vector:")
print(len(bv3)) # 64
print("\nTest the iterator:")
for bit in bv4:
print(bit) # 0 0 1 0 0 1 0 0 0 0 0 0 1 1 0 1 0
print("\nDemonstrate padding a bit vector from left:")
bv = BitVector.from_bitstring("101010")
bv.pad_from_left(4)
print(bv) # 0000101010
print("\nDemonstrate padding a bit vector from right:")
bv.pad_from_right(4)
print(bv) # 00001010100000
print("\nTest the syntax 'if bit_vector_1 in bit_vector_2' syntax:")
try:
bv1 = BitVector.from_bitstring("0011001100")
bv2 = BitVector.from_bitstring("110011")
if bv2 in bv1:
print(f"{bv2} is in {bv1}")
else:
print(f"{bv2} is not in {bv1}")
except ValueError as arg:
print("Error Message: " + str(arg))
print(
"\nTest the size modifier when a bit vector is initialized with the intVal method:",
)
bv = BitVector.from_int(45, size=16)
print(bv) # 0000000000101101
bv = BitVector.from_int(0, size=8)
print(bv) # 00000000
bv = BitVector.from_int(1, size=8)
print(bv) # 00000001
print("\nTesting slice assignment:")
bv1 = BitVector(size=25)
print("bv1= " + str(bv1)) # 0000000000000000000000000
bv2 = BitVector.from_bitstring("1010001")
print("bv2= " + str(bv2)) # 1010001
bv1[6:9] = bv2[0:3]
print("bv1= " + str(bv1)) # 0000001010000000000000000
bv1[:5] = bv1[5:10]
print("bv1= " + str(bv1)) # 0101001010000000000000000
bv1[20:] = bv1[5:10]
print("bv1= " + str(bv1)) # 0101001010000000000001010
bv1[:] = bv1[:]
print("bv1= " + str(bv1)) # 0101001010000000000001010
bv3 = bv1[:]
print("bv3= " + str(bv3)) # 0101001010000000000001010
print("\nTesting slice assignment with negative limits in RHS slice:")
bv1 = BitVector(size=25)
print("bv1= " + str(bv1)) # 0000000000000000000000000
bv2 = BitVector.from_bitstring("1010111")
print("bv2= " + str(bv2)) # 1010001
bv1[2:5] = bv2[-7:-4]
print("bv1= " + str(bv1)) # 0010100000000000000000000
bv1[2:5] = bv2[-3:]
print("bv1= " + str(bv1)) # 0011100000000000000000000
bv1[6:9] = bv2[:-4]
print("bv1= " + str(bv1)) # 0011101010000000000000000
print("\nTesting slice assignment with negative limits in the LHS slice:")
bv1 = BitVector(size=25)
print("bv1= " + str(bv1)) # 0000000000000000000000000
bv2 = BitVector.from_bitstring("1111001")
print("bv2= " + str(bv2)) # 1111001
bv1[-5:-2] = bv2[0:3]
print("bv1= " + str(bv1)) # 0000000000000000000011100
bv1[-5:] = bv2[2:7]
print("bv1= " + str(bv1)) # 0000000000000000000011001
bv1[-3:] = bv2[:-4]
print("bv1= " + str(bv1)) # 0000000000000000000011111
bv1[6:9] = bv2[:3]
print("bv1= " + str(bv1)) # 0000001110000000000011111
bv1[:-20] = bv2[2:7]
print("bv1= " + str(bv1)) # 1100101110000000000011111
bv1[0:-20] = bv1[9:14]
print("bv1= " + str(bv1)) # 0000001110000000000011111
# bv1[-15:-20] = bv2[2:7] # error
# bv1[3:-18] = bv2[2:7] # error
print("\nTesting reset function:")
bv1.reset(1)
print("bv1= " + str(bv1)) # 1111111111111111111111111
print(bv1[3:9].reset(0)) # 000000
print(bv1[:].reset(0)) # 0000000000000000000000000
print("\nTesting bit_count():")
bv = BitVector.from_int(45, size=16)
y = bv.bit_count()
print(y) # 4
bv = BitVector.from_bitstring("100111")
print(bv.bit_count()) # 4
bv = BitVector.from_bitstring("00111000")
print(bv.bit_count()) # 3
bv = BitVector.from_bitstring("001")
print(bv.bit_count()) # 1
bv = BitVector.from_bitstring("00000000000000")
print(bv.bit_count()) # 0
print("\nTest set_value idea:")
bv = BitVector.from_int(7, size=16)
print(bv) # 0000000000000111
bv.set_value(bitlist=[1, 0, 1, 1, 0, 1])
print(bv) # 101101
print("\nTesting bit_count_sparse():")
bv = BitVector(size=2000000)
bv[345234] = 1
bv[233] = 1
bv[243] = 1
bv[18] = 1
bv[785] = 1
print("The number of bits set: " + str(bv.bit_count_sparse())) # 5
print("\nTesting Jaccard similarity and distance and Hamming distance:")
bv1 = BitVector.from_bitstring("11111111")
bv2 = BitVector.from_bitstring("00101011")
print("Jaccard similarity: " + str(bv1.jaccard_similarity(bv2))) # 0.5
print("Jaccard distance: " + str(bv1.jaccard_distance(bv2))) # 0.5
print("Hamming distance: " + str(bv1.hamming_distance(bv2))) # 4
print("\nTesting next_set_bit():")
bv = BitVector.from_bitstring("00000000000001")
print(bv.next_set_bit(5)) # 13
bv = BitVector.from_bitstring("000000000000001")
print(bv.next_set_bit(5)) # 14
bv = BitVector.from_bitstring("0000000000000001")
print(bv.next_set_bit(5)) # 15
bv = BitVector.from_bitstring("00000000000000001")
print(bv.next_set_bit(5)) # 16
bv = BitVector.from_bitstring("00000000000000000")
print(bv.next_set_bit(5)) # -1
print("\nTesting rank_of_bit_set_at_index():")
bv = BitVector.from_bitstring("01010101011100")
print(bv.rank_of_bit_set_at_index(10)) # 6
print("\nTesting is_power_of_2():")
bv = BitVector.from_bitstring("10000000001110")
print("int value: " + str(int(bv))) # 826
print(bv.is_power_of_2()) # False
print("\nTesting is_power_of_2_sparse():")
print(bv.is_power_of_2_sparse()) # False
print("\nTesting reverse():")
bv = BitVector.from_bitstring("0001100000000000001")
print("original bv: " + str(bv)) # 0001100000000000001
print("reversed bv: " + str(bv.reverse())) # 1000000000000011000
print("\nTesting Greatest Common Divisor (gcd):")
bv1 = BitVector.from_bitstring("01100110")
print("first arg bv: " + str(bv1) + " of int value: " + str(int(bv1))) # 102
bv2 = BitVector.from_bitstring("011010")
print("second arg bv: " + str(bv2) + " of int value: " + str(int(bv2))) # 26
bv = bv1.gcd(bv2)
print("gcd bitvec is: " + str(bv) + " of int value: " + str(int(bv))) # 2
print("\nTesting multiplicative_inverse:")
bv_modulus = BitVector.from_int(32)
print(
"modulus is bitvec: " + str(bv_modulus) + " of int value: " + str(int(bv_modulus)),
)
bv = BitVector.from_int(17)
print("bv: " + str(bv) + " of int value: " + str(int(bv)))
mi_result = bv.multiplicative_inverse(bv_modulus)
if mi_result is not None:
print("MI bitvec is: " + str(mi_result) + " of int value: " + str(int(mi_result)))
else:
print("No multiplicative inverse in this case")
# 17
print("\nTest multiplication in GF(2):")
# a = BitVector( bitstring='0110001' )
a = BitVector.from_bitstring("00000010")
# b = BitVector( bitstring='0110' )
b = BitVector.from_bitstring("000001111")
c = a.gf_multiply(b)
print("Product of a=" + str(a) + " b=" + str(b) + " is " + str(c))
print("\nTest division in GF(2^n):")
mod = BitVector.from_bitstring("100011011") # AES modulus
n = 8
a = BitVector.from_bitstring("11100010110001")
quotient, remainder = a.gf_divide_by_modulus(mod, n)
print(
"Dividing a="
+ str(a)
+ " by mod="
+ str(mod)
+ " in GF(2^8) returns the quotient "
+ str(quotient)
+ " and the remainder "
+ str(remainder),
)
print("\nTest modular multiplication in GF(2^n):")
modulus = BitVector.from_bitstring("100011011") # AES modulus
n = 8
a = BitVector.from_bitstring("0110001")
b = BitVector.from_bitstring("0110")
c = a.gf_multiply_modular(b, modulus, n)
print("Modular product of a=" + str(a) + " b=" + str(b) + " in GF(2^8) is " + str(c))
print(
"\nTest multiplicative inverses in GF(2^3) with modulus polynomial = x^3 + x + 1:",
)
print("Find multiplicative inverse of a single bit array")
modulus = BitVector.from_bitstring("100011011") # AES modulus
n = 8
a = BitVector.from_bitstring("00110011")
mi = a.gf_MI(modulus, n)
print("Multiplicative inverse of " + str(a) + " in GF(2^8) is " + str(mi))
print(
"\nIn the following three rows shown, the first row shows the "
"\nbinary code words, the second the multiplicative inverses,"
"\nand the third the product of a binary word with its"
"\nmultiplicative inverse:\n",
)
mod = BitVector.from_bitstring("1011")
n = 3
bitarrays = [BitVector.from_int(x, size=n) for x in range(1, 2**3)]
mi_list = [x.gf_MI(mod, n) for x in bitarrays]
mi_str_list = [str(x.gf_MI(mod, n)) for x in bitarrays]
print("bit arrays in GF(2^3): " + str([str(x) for x in bitarrays]))
print("multiplicati_inverses: " + str(mi_str_list))
products = [
str(bitarrays[i].gf_multiply_modular(mi_list[i], mod, n))
for i in range(len(bitarrays))
]
print("bit_array * multi_inv: " + str(products))
# UNCOMMENT THE FOLLOWING LINES FOR
# DISPLAYING ALL OF THE MULTIPLICATIVE
# INVERSES IN GF(2^8) WITH THE AES MODULUS:
# print("\nMultiplicative inverses in GF(2^8) with " + \
# "modulus polynomial x^8 + x^4 + x^3 + x + 1:")
# print("\n(This may take a few seconds)\n")
# mod = BitVector( bitstring = '100011011' )
# n = 8
# bitarrays = [BitVector.from_int(x, size=n) for x in range(1,2**8)]
# mi_list = [x.gf_MI(mod,n) for x in bitarrays]
# mi_str_list = [str(x.gf_MI(mod,n)) for x in bitarrays]
# print("\nMultiplicative Inverses:\n\n" + str(mi_str_list))
# products = [ str(bitarrays[i].gf_multiply_modular(mi_list[i], mod, n)) \
# for i in range(len(bitarrays)) ]
# print("\nShown below is the product of each binary code word " +\
# "in GF(2^3) and its multiplicative inverse:\n\n")
# print(products)
print("\nExperimenting with runs():")
bv = BitVector(bitlist=(1, 0, 0, 1))
print("For bit vector: " + str(bv))
print(" the runs are: " + str(bv.runs()))
bv = BitVector(bitlist=(1, 0))
print("For bit vector: " + str(bv))
print(" the runs are: " + str(bv.runs()))
bv = BitVector(bitlist=(0, 1))
print("For bit vector: " + str(bv))
print(" the runs are: " + str(bv.runs()))
bv = BitVector(bitlist=(0, 0, 0, 1))
print("For bit vector: " + str(bv))
print(" the runs are: " + str(bv.runs()))
bv = BitVector(bitlist=(0, 1, 1, 0))
print("For bit vector: " + str(bv))
print(" the runs are: " + str(bv.runs()))
print("\nExperiments with chained invocations of circular shifts:")
bv = BitVector(bitlist=(1, 1, 1, 0, 0, 1))
print(bv)
bv >> 1 # pylint: disable=pointless-statement
print(bv)
bv >> 1 >> 1 # pylint: disable=pointless-statement
print(bv)
bv = BitVector(bitlist=(1, 1, 1, 0, 0, 1))
print(bv)
bv << 1 # pylint: disable=pointless-statement
print(bv)
bv << 1 << 1 # pylint: disable=pointless-statement
print(bv)
print("\nExperiments with chained invocations of NON-circular shifts:")
bv = BitVector(bitlist=(1, 1, 1, 0, 0, 1))
print(bv)
bv.shift_right(1)
print(bv)
bv.shift_right(1).shift_right(1)
print(bv)
bv = BitVector(bitlist=(1, 1, 1, 0, 0, 1))
print(bv)
bv.shift_left(1)
print(bv)
bv.shift_left(1).shift_left(1)
print(bv)
# UNCOMMENT THE FOLLOWING LINES TO TEST THE
# PRIMALITY TESTING METHOD. IT SHOULD SHOW
# THAT ALL OF THE FOLLOWING NUMBERS ARE PRIME:
primes = [
179,
233,
283,
353,
419,
467,
547,
607,
661,
739,
811,
877,
947,
1019,
1087,
1153,
1229,
1297,
1381,
1453,
1523,
1597,
1663,
1741,
1823,
1901,
7001,
7109,
7211,
7307,
7417,
7507,
7573,
7649,
7727,
7841,
]
for p in primes:
bv = BitVector.from_int(p)
check = bv.test_for_primality()
print("The primality test for " + str(p) + ": " + str(check))
print("\nGenerate 32-bit wide candidate for primality testing:")
bv = BitVector.from_int(0)
bv = bv.gen_random_bits(32)
print(bv)
check = bv.test_for_primality()
print("The primality test for " + str(int(bv)) + ": " + str(check))
print("\nTest generating min-canonical form of a BitVector instance:")
for i in range(255, 10000, 1555):
bv = BitVector.from_int(i, size=14)
print(f"\nbv: {bv}")
print(f"min canonical: {bv.min_canonical()}")