FA-15836 / Numerics / Open access
Tonelli shanks square root: next subgroup generator · case 01
The exact tonelli shanks square root result violates the stated contract at next subgroup generator.
ROOT CAUSE
The next subgroup generator step uses b instead of b*b%p.
VERIFIED REPAIR
Use b*b%p at the next subgroup generator step.
Unsuccessful approach: The partial repair c still violates the next subgroup generator invariant.
Case contract
Input [a,p], odd prime p and nonzero quadratic residue a; return smaller of two square roots modulo p. Bounds: p<=97.
Why this case matters
Exact discrete arithmetic with observable algorithmic state; no floating point approximation is used.
1 / The failure
Exit 1"""Failure Map reference implementation. Python standard library only."""
import json
import math
import itertools
from fractions import Fraction
N = 1
observations = []
def solve(x):
a,p=x
q=p-1;s=0
while q%2==0:q//=2;s+=1
z=2
while pow(z,(p-1)//2,p)!=p-1:z+=1
c=pow(z,q,p);r=pow(a,(q+1)//2,p);t=pow(a,q,p);m=s
for _ in range(20):
if t==1:break
i=1;v=t*t%p
while i<m and v!=1:v=v*v%p;i+=1
if i>=m:return None
b=pow(c,1<<(m-i-1),p)
r=r*b%p;t=t*b*b%p;c=b;m=i
return min(r,p-r)
def check(label, actual, expected):
observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[([8, 17], 5), ([1, 5], 1), ([96, 97], 22), ([4, 5], 2), ([1, 7], 1), ([2, 7], 3), ([4, 7], 2), ([1, 11], 1)], [([9, 17], 3), ([15, 17], 7), ([1, 5], 1), ([96, 97], 22), ([2, 17], 6), ([4, 17], 2), ([8, 17], 5), ([13, 17], 8)], [([13, 17], 8), ([9, 41], 3), ([1, 5], 1), ([96, 97], 22), ([2, 23], 5), ([3, 23], 7), ([4, 23], 2), ([6, 23], 11)], [([15, 17], 7), ([25, 73], 5), ([1, 5], 1), ([96, 97], 22), ([16, 29], 4), ([20, 29], 7), ([22, 29], 14), ([23, 29], 9)], [([5, 41], 13), ([49, 73], 7), ([1, 5], 1), ([96, 97], 22), ([18, 31], 7), ([19, 31], 9), ([20, 31], 12), ([25, 31], 5)]]
for i, (args, expected) in enumerate(fixtures[N-1]):
check("explicit oracle %d" % i, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)
| Boundary fixture | Actual | Expected | Outcome |
|---|---|---|---|
| explicit oracle 0 | None | 5 | Failed |
| explicit oracle 1 | 1 | 1 | Passed |
| explicit oracle 2 | 22 | 22 | Passed |
| explicit oracle 3 | 2 | 2 | Passed |
| explicit oracle 4 | 1 | 1 | Passed |
| explicit oracle 5 | 3 | 3 | Passed |
| explicit oracle 6 | 2 | 2 | Passed |
| explicit oracle 7 | 1 | 1 | Passed |
SHA-256 / c4a0324c523f105764317e627f89327762490247d33cc382a9cb99c91e945759
2 / The unsuccessful fix
Exit 1"""Failure Map reference implementation. Python standard library only."""
import json
import math
import itertools
from fractions import Fraction
N = 1
observations = []
def solve(x):
a,p=x
q=p-1;s=0
while q%2==0:q//=2;s+=1
z=2
while pow(z,(p-1)//2,p)!=p-1:z+=1
c=pow(z,q,p);r=pow(a,(q+1)//2,p);t=pow(a,q,p);m=s
for _ in range(20):
if t==1:break
i=1;v=t*t%p
while i<m and v!=1:v=v*v%p;i+=1
if i>=m:return None
b=pow(c,1<<(m-i-1),p)
r=r*b%p;t=t*b*b%p;c=c;m=i
return min(r,p-r)
def check(label, actual, expected):
observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[([8, 17], 5), ([1, 5], 1), ([96, 97], 22), ([4, 5], 2), ([1, 7], 1), ([2, 7], 3), ([4, 7], 2), ([1, 11], 1)], [([9, 17], 3), ([15, 17], 7), ([1, 5], 1), ([96, 97], 22), ([2, 17], 6), ([4, 17], 2), ([8, 17], 5), ([13, 17], 8)], [([13, 17], 8), ([9, 41], 3), ([1, 5], 1), ([96, 97], 22), ([2, 23], 5), ([3, 23], 7), ([4, 23], 2), ([6, 23], 11)], [([15, 17], 7), ([25, 73], 5), ([1, 5], 1), ([96, 97], 22), ([16, 29], 4), ([20, 29], 7), ([22, 29], 14), ([23, 29], 9)], [([5, 41], 13), ([49, 73], 7), ([1, 5], 1), ([96, 97], 22), ([18, 31], 7), ([19, 31], 9), ([20, 31], 12), ([25, 31], 5)]]
for i, (args, expected) in enumerate(fixtures[N-1]):
check("explicit oracle %d" % i, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)
| Boundary fixture | Actual | Expected | Outcome |
|---|---|---|---|
| explicit oracle 0 | None | 5 | Failed |
| explicit oracle 1 | 1 | 1 | Passed |
| explicit oracle 2 | 22 | 22 | Passed |
| explicit oracle 3 | 2 | 2 | Passed |
| explicit oracle 4 | 1 | 1 | Passed |
| explicit oracle 5 | 3 | 3 | Passed |
| explicit oracle 6 | 2 | 2 | Passed |
| explicit oracle 7 | 1 | 1 | Passed |
SHA-256 / ca99ee70a90fa03b1b92743205285e9ef081c4e53fc063ebcc0038e910d610e9
3 / The verified repair
Exit 0"""Failure Map reference implementation. Python standard library only."""
import json
import math
import itertools
from fractions import Fraction
N = 1
observations = []
def solve(x):
a,p=x
q=p-1;s=0
while q%2==0:q//=2;s+=1
z=2
while pow(z,(p-1)//2,p)!=p-1:z+=1
c=pow(z,q,p);r=pow(a,(q+1)//2,p);t=pow(a,q,p);m=s
for _ in range(20):
if t==1:break
i=1;v=t*t%p
while i<m and v!=1:v=v*v%p;i+=1
if i>=m:return None
b=pow(c,1<<(m-i-1),p)
r=r*b%p;t=t*b*b%p;c=b*b%p;m=i
return min(r,p-r)
def check(label, actual, expected):
observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[([8, 17], 5), ([1, 5], 1), ([96, 97], 22), ([4, 5], 2), ([1, 7], 1), ([2, 7], 3), ([4, 7], 2), ([1, 11], 1)], [([9, 17], 3), ([15, 17], 7), ([1, 5], 1), ([96, 97], 22), ([2, 17], 6), ([4, 17], 2), ([8, 17], 5), ([13, 17], 8)], [([13, 17], 8), ([9, 41], 3), ([1, 5], 1), ([96, 97], 22), ([2, 23], 5), ([3, 23], 7), ([4, 23], 2), ([6, 23], 11)], [([15, 17], 7), ([25, 73], 5), ([1, 5], 1), ([96, 97], 22), ([16, 29], 4), ([20, 29], 7), ([22, 29], 14), ([23, 29], 9)], [([5, 41], 13), ([49, 73], 7), ([1, 5], 1), ([96, 97], 22), ([18, 31], 7), ([19, 31], 9), ([20, 31], 12), ([25, 31], 5)]]
for i, (args, expected) in enumerate(fixtures[N-1]):
check("explicit oracle %d" % i, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)
| Boundary fixture | Actual | Expected | Outcome |
|---|---|---|---|
| explicit oracle 0 | 5 | 5 | Passed |
| explicit oracle 1 | 1 | 1 | Passed |
| explicit oracle 2 | 22 | 22 | Passed |
| explicit oracle 3 | 2 | 2 | Passed |
| explicit oracle 4 | 1 | 1 | Passed |
| explicit oracle 5 | 3 | 3 | Passed |
| explicit oracle 6 | 2 | 2 | Passed |
| explicit oracle 7 | 1 | 1 | Passed |
SHA-256 / e381f65a9e89da54cef5e1fdb65d258573b84b7fe056b52c728a7a98d8615773
Verification & scope
A deterministic bounded teaching model. Inputs are restricted to the explicit contract; this is not a production algebra library. This reproducer isolates one failure mechanism. Results cover the supplied fixtures. Variants within a family share a test contract and should remain grouped when constructing evaluation splits. Related mechanisms with a shared evaluation_group must also remain together; these controlled models are not independent production incidents.
Observations recorded using Python 3.12.14 at 2026-09-29T14:39:30.780215+00:00.
Case digest / 3de9b2b427f8e0e19207ef3835cea5a53431cf8b4efcb726ad94892afa6a2090