linear and differential bias change
This commit is contained in:
@@ -5,9 +5,15 @@ from random import Random
|
||||
|
||||
from .tea3 import Tea3
|
||||
|
||||
|
||||
def rand_key(rng: Random) -> list[int]:
|
||||
return [rng.getrandbits(8) for _ in range(10)]
|
||||
|
||||
|
||||
def rand_frame_number(rng: Random) -> int:
|
||||
return rng.getrandbits(32)
|
||||
|
||||
|
||||
@dataclass
|
||||
class BiasResult:
|
||||
samples: int
|
||||
@@ -16,9 +22,10 @@ class BiasResult:
|
||||
bias: float
|
||||
estimated_needed_samples: float
|
||||
|
||||
|
||||
def estimate_black_box_output_bias(
|
||||
*,
|
||||
frame_number: int,
|
||||
key: list[int],
|
||||
output_byte_index: int = 0,
|
||||
output_bit: int = 0,
|
||||
samples: int = 10000,
|
||||
@@ -28,12 +35,12 @@ def estimate_black_box_output_bias(
|
||||
matches = 0
|
||||
|
||||
for _ in range(samples):
|
||||
key = rand_key(rng)
|
||||
frame_number = rand_frame_number(rng)
|
||||
tea = Tea3(frame_number, key)
|
||||
|
||||
out_byte = 0
|
||||
for _ in range(output_byte_index + 1):
|
||||
tea.next_byte()
|
||||
|
||||
out_byte = tea.iv_view() >> 56
|
||||
|
||||
bit = (out_byte >> output_bit) & 1
|
||||
@@ -52,16 +59,22 @@ def estimate_black_box_output_bias(
|
||||
estimated_needed_samples=needed,
|
||||
)
|
||||
|
||||
|
||||
def main():
|
||||
frame = 0x12345678
|
||||
rng = Random(123)
|
||||
key = rand_key(rng)
|
||||
|
||||
res = estimate_black_box_output_bias(
|
||||
frame_number=frame,
|
||||
key=key,
|
||||
output_byte_index=0,
|
||||
output_bit=0,
|
||||
samples=20000,
|
||||
seed=123,
|
||||
)
|
||||
print("black-box output bias")
|
||||
print("expected non-biased p_match: 1/2") # 2 possibilities for 1 bit
|
||||
print("expected non-biased p_match: 1/2")
|
||||
print(res)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user