-
Notifications
You must be signed in to change notification settings - Fork 19.7k
/
Copy pathCompositeLFSR.java
69 lines (63 loc) · 2.26 KB
/
CompositeLFSR.java
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
package com.thealgorithms.ciphers.a5;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
/**
* The CompositeLFSR class represents a composite implementation of
* Linear Feedback Shift Registers (LFSRs) for cryptographic purposes.
*
* <p>
* This abstract class manages a collection of LFSR instances and
* provides a mechanism for irregular clocking based on the
* majority bit among the registers. It implements the BaseLFSR
* interface, requiring subclasses to define specific LFSR behaviors.
* </p>
*/
public abstract class CompositeLFSR implements BaseLFSR {
protected final List<LFSR> registers = new ArrayList<>();
/**
* Performs a clocking operation on the composite LFSR.
*
* <p>
* This method determines the majority bit across all registers and
* clocks each register based on its clock bit. If a register's
* clock bit matches the majority bit, it is clocked (shifted).
* The method also computes and returns the XOR of the last bits
* of all registers.
* </p>
*
* @return the XOR value of the last bits of all registers.
*/
@Override
public boolean clock() {
boolean majorityBit = getMajorityBit();
boolean result = false;
for (var register : registers) {
result ^= register.getLastBit();
if (register.getClockBit() == majorityBit) {
register.clock();
}
}
return result;
}
/**
* Calculates the majority bit among all registers.
*
* <p>
* This private method counts the number of true and false clock bits
* across all LFSR registers. It returns true if the count of true
* bits is greater than or equal to the count of false bits; otherwise,
* it returns false.
* </p>
*
* @return true if the majority clock bits are true; false otherwise.
*/
private boolean getMajorityBit() {
Map<Boolean, Integer> bitCount = new TreeMap<>();
bitCount.put(Boolean.FALSE, 0);
bitCount.put(Boolean.TRUE, 0);
registers.forEach(lfsr -> bitCount.put(lfsr.getClockBit(), bitCount.get(lfsr.getClockBit()) + 1));
return bitCount.get(Boolean.FALSE) <= bitCount.get(Boolean.TRUE);
}
}