Last active
May 18, 2026 07:39
-
-
Save MurageKibicho/b36af163a80c0acfbfbd8a194dd25bf8 to your computer and use it in GitHub Desktop.
LSB Bias for Bleichenbacher
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import random | |
| import cmath | |
| MOD = 1 << 20 | |
| samples = 10000 | |
| FIXED_LSBS = 3 | |
| # Generate nonces with FIXED_LSBS LSBs = 0 | |
| step = 1 << FIXED_LSBS | |
| max_val = MOD // step | |
| biased_nonces = [random.randint(0, max_val - 1) * step for _ in range(samples)] | |
| uniform_nonces = [random.randint(0, MOD-1) for _ in range(samples)] | |
| def bias(nonces, freq): | |
| total = sum(cmath.exp(2j * cmath.pi * freq * x / MOD) for x in nonces) | |
| return abs(total / len(nonces)) | |
| # Optimal frequency for LSB detection | |
| optimal_freq = MOD // step | |
| print(f"Fixed LSBs: {FIXED_LSBS} (numbers are multiples of {step})") | |
| print(f"Optimal frequency: MOD/{step} = {optimal_freq}") | |
| print() | |
| print(f"f=1 (standard):") | |
| print(f" Biased nonces: {bias(biased_nonces, 1):.6f}") | |
| print(f" Uniform: {bias(uniform_nonces, 1):.6f}") | |
| print() | |
| print(f"f=optimal (LSB-detecting):") | |
| print(f" Biased nonces: {bias(biased_nonces, optimal_freq):.6f}") | |
| print(f" Uniform: {bias(uniform_nonces, optimal_freq):.6f}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment