| 1 |
import sys |
| 2 |
|
| 3 |
if sys.argv[1] == "44": |
| 4 |
results = [7368698, 6238419, 9743544, 8252486, 0, 0, 0, 0, 93720, 79660, 154045, 131188, 1, 0, 0, 0] |
| 5 |
q = 8380417 |
| 6 |
gamma1 = 2 ** 17 |
| 7 |
gamma2 = (q - 1)/88 |
| 8 |
beta = 78 |
| 9 |
k, l = 4, 4 |
| 10 |
elif sys.argv[1] == "65": |
| 11 |
results = [4440848, 2731767, 9616285, 5919454, 0, 0, 0, 0, 14486, 9201, 47356, 29001, 0, 0, 0, 0] |
| 12 |
q = 8380417 |
| 13 |
gamma1 = 2 ** 19 |
| 14 |
gamma2 = (q - 1)/32 |
| 15 |
beta = 196 |
| 16 |
k, l = 6, 5 |
| 17 |
elif sys.argv[1] == "87": |
| 18 |
results = [4102322, 2088997, 6415247, 3270072, 0, 0, 0, 0, 29599, 15086, 61093, 30983, 0, 0, 0, 0] |
| 19 |
q = 8380417 |
| 20 |
gamma1 = 2 ** 19 |
| 21 |
gamma2 = (q - 1)/32 |
| 22 |
beta = 120 |
| 23 |
k, l = 8, 7 |
| 24 |
else: |
| 25 |
print("usage: stat.py [44|65|87]") |
| 26 |
sys.exit(1) |
| 27 |
|
| 28 |
checks = ["z", "r0", "ct0", "h"] |
| 29 |
|
| 30 |
total = sum(results) |
| 31 |
pass_prob = results[0] / total |
| 32 |
z = sum(results[j] for j in range(16) if (j & 0b0001)) |
| 33 |
z_prob = z / total |
| 34 |
r0 = sum(results[j] for j in range(16) if (j & 0b0010)) |
| 35 |
r0_prob = r0 / total |
| 36 |
ct0 = sum(results[j] for j in range(16) if (j & 0b0100)) |
| 37 |
ct0_prob = ct0 / total |
| 38 |
h = sum(results[j] for j in range(16) if (j & 0b1000)) |
| 39 |
h_prob = h / total |
| 40 |
z_and_r0 = sum(results[j] for j in range(16) if (j & 0b0001) and (j & 0b0010)) |
| 41 |
z_and_r0_prob = z_and_r0 / total |
| 42 |
z_and_h = sum(results[j] for j in range(16) if (j & 0b0001) and (j & 0b1000)) |
| 43 |
z_and_h_prob = z_and_h / total |
| 44 |
r0_and_h = sum(results[j] for j in range(16) if (j & 0b0010) and (j & 0b1000)) |
| 45 |
r0_and_h_prob = r0_and_h / total |
| 46 |
not_z_or_r0 = sum(results[j] for j in range(16) if not (j & 0b0011)) |
| 47 |
not_z_or_r0_prob = not_z_or_r0 / total |
| 48 |
z_not_r0 = sum(results[j] for j in range(16) if (j & 0b0001) and not (j & 0b0010)) |
| 49 |
z_not_r0_prob = z_not_r0 / total |
| 50 |
r0_not_z = sum(results[j] for j in range(16) if (j & 0b0010) and not (j & 0b0001)) |
| 51 |
r0_not_z_prob = r0_not_z / total |
| 52 |
only_h = results[0b1000] |
| 53 |
only_h_prob = only_h / total |
| 54 |
|
| 55 |
print(f"none: {pass_prob:.4f}") |
| 56 |
print(f"repetitions: {1 / pass_prob:.2f}") |
| 57 |
print(f"z: {z_prob:.4f}") |
| 58 |
print(f"r0: {r0_prob:.4f}") |
| 59 |
print(f"ct0: {ct0_prob:.4f}") |
| 60 |
print(f"h: {h_prob:.4f}") |
| 61 |
print(f"not(z | r0): {not_z_or_r0_prob:.4f}") |
| 62 |
print(f"only h: {only_h_prob:.4f}") |
| 63 |
|
| 64 |
6l |
| 65 |
expected_z = 1 - (1 - (beta / (gamma1 - 0.5))) ** (256 * l) |
| 66 |
print(f"expected z: {expected_z:.4f}") |
| 67 |
|
| 68 |
^ 256k |
| 69 |
expected_r0 = 1 - ((2 * (gamma2 - beta) - 1) / (2 * gamma2)) ** (256 * k) |
| 70 |
print(f"expected r0: {expected_r0:.4f}") |
| 71 |
|
| 72 |
print(f"expected not(z | r0): {(1 - expected_z) * (1 - expected_r0):.4f}") |
| 73 |
print(f"expected repetitions: {1 / ((1 - expected_z) * (1 - expected_r0)):.2f}") |
| 74 |
|
| 75 |
|
| 76 |
print("pairwise occurrences:") |
| 77 |
|
| 78 |
|
| 79 |
expected_z_and_r0 = z_prob * r0_prob |
| 80 |
print(f"z and r0: actual {z_and_r0 / total:.4f}, expected {expected_z_and_r0:.4f}, ratio {(z_and_r0 / total) / expected_z_and_r0:.2f}") |
| 81 |
|
| 82 |
|
| 83 |
expected_z_and_h = z_prob * h_prob |
| 84 |
print(f"z and h: actual {z_and_h / total:.4f}, expected {expected_z_and_h:.4f}, ratio {(z_and_h / total) / expected_z_and_h:.2f}") |
| 85 |
|
| 86 |
|
| 87 |
expected_r0_and_h = r0_prob * h_prob |
| 88 |
print(f"r0 and h: actual {r0_and_h / total:.4f}, expected {expected_r0_and_h:.4f}, ratio {(r0_and_h / total) / expected_r0_and_h:.2f}") |
| 89 |
|
| 90 |
passes_to_error = {n: abs(round(n / pass_prob) - (n / pass_prob)) for n in range(100, 200)} |
| 91 |
passes_with_lowest_error = min(passes_to_error.items(), key=lambda x: x[1])[0] |
| 92 |
trials = passes_with_lowest_error / pass_prob |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
passes = round(trials * pass_prob) |
| 97 |
fails_only_z = round(trials * z_not_r0_prob) |
| 98 |
fails_only_r0 = round(trials * r0_not_z_prob) |
| 99 |
fails_z_and_r0 = round(trials * z_and_r0_prob) |
| 100 |
fails_only_h = round(trials * only_h_prob) |
| 101 |
total_sum = passes + fails_only_z + fails_only_r0 + fails_z_and_r0 + fails_only_h |
| 102 |
|
| 103 |
print(f"\nExpected trials: {trials}") |
| 104 |
print(f"Total trials: {total_sum}") |
| 105 |
print(f" - Passes: {passes}") |
| 106 |
print(f" - Fails only z: {fails_only_z}") |
| 107 |
print(f" - Fails only r0: {fails_only_r0}") |
| 108 |
print(f" - Fails z and r0: {fails_z_and_r0}") |
| 109 |
print(f" - Fails only h: {fails_only_h}") |
| 110 |
|
| 111 |
floor = lambda x: int(x // 1) |
| 112 |
ceil = lambda x: int(-(-x // 1)) |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
def sample(): |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
import subprocess |
| 131 |
proc = subprocess.run( |
| 132 |
["./mldsa.test", "-test.run", f"TestSample/ML-DSA-{sys.argv[1]}", "-test.v", "-test.count", "1"], |
| 133 |
capture_output=True, |
| 134 |
text=True, |
| 135 |
) |
| 136 |
lines = proc.stdout.splitlines() |
| 137 |
seed_line = next(line for line in lines if line.startswith("seed: ")) |
| 138 |
seed = seed_line[len("seed: "):] |
| 139 |
rejections = [line[len("rejection: "):] for line in lines if line.startswith("rejection: ")] |
| 140 |
return (seed, rejections) |
| 141 |
|
| 142 |
remaining_only_z = fails_only_z |
| 143 |
remaining_only_r0 = fails_only_r0 |
| 144 |
remaining_z_and_r0 = fails_z_and_r0 |
| 145 |
remaining_only_h = fails_only_h |
| 146 |
samples = [] |
| 147 |
while len(samples) < passes: |
| 148 |
msg, rejections = sample() |
| 149 |
count_only_z = sum(1 for r in rejections if r == "z") |
| 150 |
count_only_r0 = sum(1 for r in rejections if r == "r0") |
| 151 |
count_z_and_r0 = sum(1 for r in rejections if r == "z/r0") |
| 152 |
count_only_h = sum(1 for r in rejections if r == "h") |
| 153 |
remaining_pass = passes - len(samples) |
| 154 |
|
| 155 |
|
| 156 |
if len(rejections) > 2 * ((total_sum - passes) / passes): |
| 157 |
continue |
| 158 |
|
| 159 |
if (count_only_h > remaining_only_h or |
| 160 |
count_only_z > remaining_only_z or |
| 161 |
count_only_r0 > remaining_only_r0 or |
| 162 |
count_z_and_r0 > remaining_z_and_r0): |
| 163 |
continue |
| 164 |
|
| 165 |
if ((remaining_only_r0 >= remaining_pass and count_only_r0 == 0) or |
| 166 |
(remaining_only_z >= remaining_pass and count_only_z == 0) or |
| 167 |
(remaining_z_and_r0 >= remaining_pass and count_z_and_r0 == 0) or |
| 168 |
(remaining_only_h >= remaining_pass and count_only_h == 0)): |
| 169 |
continue |
| 170 |
|
| 171 |
print(f"selected sample with rejections: {rejections}") |
| 172 |
print(f"remaining: z={remaining_only_z}, r0={remaining_only_r0}, z_and_r0={remaining_z_and_r0}, h={remaining_only_h} passes={remaining_pass}") |
| 173 |
samples.append(msg) |
| 174 |
remaining_only_z -= count_only_z |
| 175 |
remaining_only_r0 -= count_only_r0 |
| 176 |
remaining_z_and_r0 -= count_z_and_r0 |
| 177 |
remaining_only_h -= count_only_h |
| 178 |
from random import shuffle |
| 179 |
shuffle(samples) |
| 180 |
for sample in samples: |
| 181 |
print(f"\t\"{sample}\",") |