| | @@ -0,0 +1,182 @@ |
| 1 | +"""Exact constants and two finite counterexamples to eight-block stability.""" |
| 2 | + |
| 3 | +from collections import Counter |
| 4 | +from fractions import Fraction as Q |
| 5 | +from itertools import combinations_with_replacement, product |
| 6 | +from math import gcd, isqrt |
| 7 | + |
| 8 | + |
| 9 | +DENSITY = Q(19, 20) |
| 10 | +ROWS = ( |
| 11 | + # Target, upper endpoint of collision range, fraction removed, residual count rate. |
| 12 | + (Q(89, 100), Q(21, 50), Q(1, 75), Q(1, 5)), |
| 13 | + (Q(3, 4), Q(3, 10), Q(1, 110), Q(1, 6)), |
| 14 | +) |
| 15 | + |
| 16 | +# Coordinates of the two finite witnesses. The verifier only needs the sites. |
| 17 | +WITNESSES = ( |
| 18 | + (Q(89, 100), Q(19, 20), Q(1, 100), 3272, ( |
| 19 | + 0, 10, 173, 227, 256, 347, 359, 362, 415, 420, 499, 529, 549, |
| 20 | + 624, 704, 710, 800, 907, 916, 918, 941, 942, 958, 1035, 1182, |
| 21 | + 1186, 1227, 1245, 2712, 2857, 2912, 2979, 3091, 3110, 3148, 3240, |
| 22 | + )), |
| 23 | + (Q(3, 4), Q(499, 500), Q(1, 300), 15960, ( |
| 24 | + 0, 106, 279, 297, 469, 541, 834, 933, 1000, 1014, 1383, 1462, 1495, |
| 25 | + 1504, 1840, 2193, 2253, 3141, 3166, 3483, 3619, 4297, 4720, 4807, |
| 26 | + 5328, 5424, 5569, 5637, 5659, 5845, 6150, 6736, 6772, 7401, 7459, |
| 27 | + 7714, 8509, 8659, 8670, 8796, 9087, 9282, 9412, 10279, 10419, 11095, |
| 28 | + 11629, 11859, 11865, 12018, 12070, 12187, 12217, 12271, 13335, 13491, |
| 29 | + 13675, 13737, 14389, 14481, 14520, 14784, 14907, 15165, 15192, 15267, |
| 30 | + 15766, |
| 31 | + )), |
| 32 | +) |
| 33 | + |
| 34 | + |
| 35 | +def sqrt_bounds(value): |
| 36 | + """Enclose a nonnegative rational square root with rational endpoints.""" |
| 37 | + scale = 10**12 |
| 38 | + low = Q(isqrt(value.numerator * scale**2 // value.denominator), scale) |
| 39 | + high = low if low**2 == value else low + Q(1, scale) |
| 40 | + assert low**2 <= value <= high**2 |
| 41 | + return low, high |
| 42 | + |
| 43 | + |
| 44 | +def floor_thousandths(low, high): |
| 45 | + """Both endpoints must certify the same displayed decimal.""" |
| 46 | + mills = low * 1000 // 1 |
| 47 | + assert mills == high * 1000 // 1 |
| 48 | + return f"{mills // 1000}.{mills % 1000:03d}" |
| 49 | + |
| 50 | + |
| 51 | +def check_constants(): |
| 52 | + a = DENSITY |
| 53 | + excess = a * (1 - a**2) / (2*a**2 - 1) |
| 54 | + assert excess == Q(741, 6440) |
| 55 | + two_lo, two_hi = sqrt_bounds(Q(2)) |
| 56 | + assert excess * two_hi < Q(1, 6) |
| 57 | + assert (Q(33, 100) - Q(1, 6)) / 2 > Q(3, 40) > Q(1, 20) |
| 58 | + root_lo, root_hi = sqrt_bounds(1 - a**2) |
| 59 | + half_lo, half_hi = (a + root_lo)/2, (a + root_hi)/2 |
| 60 | + |
| 61 | + # log(x) > 2(x-1)/(x+1) for x>1, by the positive odd-power series. |
| 62 | + assert Q(33, 100) * Q(2, 3) > Q(1, 5) |
| 63 | + p_max = Q(18, 25) |
| 64 | + two_hit = 1 - (1 - p_max)**2 |
| 65 | + cap = two_hit * (1 + Q(96, 2000)) * Q(101, 100) |
| 66 | + assert two_hit == Q(576, 625) |
| 67 | + assert cap == Q(975495168, 10**9) |
| 68 | + deletion = [Q(d, 100) for d in (55, 62, 30, 53, 62, 64, 28, 46)] |
| 69 | + assert p_max == 1 - min(deletion) |
| 70 | + miss = min(deletion[i]*deletion[j] for j in range(4, 8) for i in range(j-3)) |
| 71 | + assert miss == deletion[2]*deletion[6] == Q(21, 250) |
| 72 | + local_cap = (1 - miss) * (1 + Q(35, 4*135)) * Q(201, 200) |
| 73 | + assert local_cap == Q(352889, 360000) |
| 74 | + for c, period_ratio, cover_fraction, count_rate in ROWS: |
| 75 | + beta_lo, beta_hi = sqrt_bounds(c**2 / 2) |
| 76 | + mobile_share = min(1, 2/c**2 - 2) |
| 77 | + switch_lo, switch_hi = beta_lo * mobile_share, beta_hi * mobile_share |
| 78 | + assert switch_lo > Q(33, 100) |
| 79 | + third_lo, third_hi = sqrt_bounds(c**2 / 3) |
| 80 | + small = (beta_lo - third_hi, beta_hi - third_lo) |
| 81 | + natural = (beta_lo - c*half_hi, beta_hi - c*half_lo) |
| 82 | + others = ((switch_lo - excess*two_hi)/2, (switch_hi - excess*two_lo)/2) |
| 83 | + assert min(small[0], natural[0], others[0]) > Q(1, 20) |
| 84 | + assert beta_lo - cover_fraction > a * sqrt_bounds(period_ratio)[1] |
| 85 | + assert cover_fraction < Q(1, 20) |
| 86 | + assert (1 - max(cap, local_cap)) * beta_lo > Q(1, 100) |
| 87 | + assert beta_lo * 2 * (1 - p_max)/(1 + p_max) > count_rate |
| 88 | + values = [(switch_lo, switch_hi), small, natural, others] |
| 89 | + print(f"c={c}: mobile labels, small periods, natural period, other periods =", |
| 90 | + ", ".join(floor_thousandths(*bounds) for bounds in values)) |
| 91 | + print(f"Replacement caps on mean hits divided by k: {cap} and {local_cap}.") |
| 92 | + |
| 93 | + |
| 94 | +def bitset(values): |
| 95 | + """Bits at the specified distinct nonnegative integers.""" |
| 96 | + return sum(1 << x for x in values) |
| 97 | + |
| 98 | + |
| 99 | +def elements(bits): |
| 100 | + while bits: |
| 101 | + lowest = bits & -bits |
| 102 | + yield lowest.bit_length() - 1 |
| 103 | + bits ^= lowest |
| 104 | + |
| 105 | + |
| 106 | +def residues(bits, v): |
| 107 | + """Reduce a set of nonnegative integers modulo v.""" |
| 108 | + mask = (1 << v) - 1 |
| 109 | + result = 0 |
| 110 | + while bits: |
| 111 | + result |= bits & mask |
| 112 | + bits >>= v |
| 113 | + return result |
| 114 | + |
| 115 | + |
| 116 | +def extension_bound(sites, v, sums, differences): |
| 117 | + """Upper bound on extra residues in a cyclic Sidon extension of these sites.""" |
| 118 | + mask = (1 << v) - 1 |
| 119 | + # Right-shifting two copies by j and keeping the low v bits translates by -j. |
| 120 | + sums_twice = sums | (sums << v) |
| 121 | + differences_twice = differences | (differences << v) |
| 122 | + possible = mask |
| 123 | + for r in sites: |
| 124 | + possible &= ~(sums_twice >> (r % v)) |
| 125 | + possible = bitset(x for x in elements(possible) if ((sums >> (2*x % v)) & 1) == 0) |
| 126 | + |
| 127 | + groups = 0 |
| 128 | + while possible: |
| 129 | + groups += 1 |
| 130 | + same_group = possible |
| 131 | + while same_group: |
| 132 | + first = same_group & -same_group |
| 133 | + x = first.bit_length() - 1 |
| 134 | + possible ^= first |
| 135 | + # Each remaining member of this group must conflict with every one selected. |
| 136 | + sum_conflicts = sums_twice >> x |
| 137 | + difference_conflicts = differences_twice >> ((-x) % v) |
| 138 | + same_group &= (sum_conflicts | difference_conflicts) & possible |
| 139 | + return groups |
| 140 | + |
| 141 | + |
| 142 | +def check_witness(c, density, edits, size, sites): |
| 143 | + k = len(sites) |
| 144 | + assert size % 2 == 0 and (k-1)**2 < c**2 * size/2 <= k**2 |
| 145 | + assert edits**2 * size < 1 # The template must admit the entire set as a section. |
| 146 | + assert tuple(sorted(set(sites))) == sites and 0 <= sites[0] <= sites[-1] < size |
| 147 | + pair_count = k*(k+1)//2 |
| 148 | + integer_sums = {x+y for x, y in combinations_with_replacement(sites, 2)} |
| 149 | + assert len(integer_sums) == pair_count |
| 150 | + assert gcd(*(x-sites[0] for x in sites)) == 1 |
| 151 | + triples = Counter(x+y-z for x, y, z in product(sites, repeat=3)) |
| 152 | + span = set(range(sites[0], sites[-1]+1)).difference(sites) |
| 153 | + poor = sum(triples[x]**4 < size for x in span) |
| 154 | + assert 25 * poor**2 > size |
| 155 | + |
| 156 | + sum_bits = bitset(integer_sums) |
| 157 | + # A nonnegative offset lets the same folding procedure handle signed differences. |
| 158 | + difference_bits = bitset({size + x-y for x in sites for y in sites if x != y}) |
| 159 | + shortfalls = [] |
| 160 | + for v in range(k*(k-1)+1, 2*size+1): # Smaller periods have too few differences. |
| 161 | + sums = residues(sum_bits, v) |
| 162 | + if sums.bit_count() < pair_count: |
| 163 | + continue |
| 164 | + shifted = residues(difference_bits, v) |
| 165 | + differences = ((shifted | (shifted << v)) >> (size % v)) & ((1 << v) - 1) |
| 166 | + assert differences.bit_count() == k*(k-1) and differences & 1 == 0 |
| 167 | + minimum = isqrt(density.numerator**2 * v // density.denominator**2) |
| 168 | + if (density.denominator * minimum)**2 < density.numerator**2 * v: |
| 169 | + minimum += 1 |
| 170 | + need = minimum - k |
| 171 | + bound = extension_bound(sites, v, sums, differences) |
| 172 | + assert bound < need, (c, v, bound, need) |
| 173 | + shortfalls.append(need - bound) |
| 174 | + print(f"c={c}: M={size}, k={k}, {poor} poorly represented sites in the span, " |
| 175 | + f"{len(shortfalls)} clean projections, smallest template shortfall {min(shortfalls)}, " |
| 176 | + f"all {2*size} periods excluded.") |
| 177 | + |
| 178 | + |
| 179 | +if __name__ == "__main__": |
| 180 | + check_constants() |
| 181 | + for witness in WITNESSES: |
| 182 | + check_witness(*witness) |