FAILURE MAP
← Case archive

FA-15816 / Numerics / Open access

Tonelli shanks square root: nonresidue subgroup generator · case 01

The exact tonelli shanks square root result violates the stated contract at nonresidue subgroup generator.

Verified by executionVariant 1 · 8 checks per implementationDownload source bundle ↓JSON ↗

ROOT CAUSE

The nonresidue subgroup generator step uses z%p instead of pow(z,q,p).

VERIFIED REPAIR

Use pow(z,q,p) at the nonresidue subgroup generator step.

Unsuccessful approach: The partial repair pow(z,s,p) still violates the nonresidue 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=z%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 = [[([4, 13], 2), ([4, 5], 2), ([1, 5], 1), ([96, 97], 22), ([1, 7], 1), ([2, 7], 3), ([4, 7], 2), ([1, 11], 1)], [([10, 13], 6), ([12, 13], 5), ([1, 5], 1), ([96, 97], 22), ([2, 17], 6), ([4, 17], 2), ([8, 17], 5), ([9, 17], 3)], [([12, 13], 5), ([8, 17], 5), ([1, 5], 1), ([96, 97], 22), ([2, 23], 5), ([3, 23], 7), ([4, 23], 2), ([6, 23], 11)], [([4, 29], 2), ([15, 17], 7), ([1, 5], 1), ([96, 97], 22), ([16, 29], 4), ([20, 29], 7), ([22, 29], 14), ([23, 29], 9)], [([5, 29], 11), ([1, 5], 1), ([96, 97], 22), ([18, 31], 7), ([19, 31], 9), ([20, 31], 12), ([25, 31], 5), ([28, 31], 11)]]
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 fixtureActualExpectedOutcome
explicit oracle 0None2Failed
explicit oracle 122Passed
explicit oracle 211Passed
explicit oracle 3None22Failed
explicit oracle 411Passed
explicit oracle 533Passed
explicit oracle 622Passed
explicit oracle 711Passed

SHA-256 / 4277f78f30c482517593e0b535d11952ab8fb2c6ee53ee9c47a95bb28fb88c9d

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,s,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 = [[([4, 13], 2), ([4, 5], 2), ([1, 5], 1), ([96, 97], 22), ([1, 7], 1), ([2, 7], 3), ([4, 7], 2), ([1, 11], 1)], [([10, 13], 6), ([12, 13], 5), ([1, 5], 1), ([96, 97], 22), ([2, 17], 6), ([4, 17], 2), ([8, 17], 5), ([9, 17], 3)], [([12, 13], 5), ([8, 17], 5), ([1, 5], 1), ([96, 97], 22), ([2, 23], 5), ([3, 23], 7), ([4, 23], 2), ([6, 23], 11)], [([4, 29], 2), ([15, 17], 7), ([1, 5], 1), ([96, 97], 22), ([16, 29], 4), ([20, 29], 7), ([22, 29], 14), ([23, 29], 9)], [([5, 29], 11), ([1, 5], 1), ([96, 97], 22), ([18, 31], 7), ([19, 31], 9), ([20, 31], 12), ([25, 31], 5), ([28, 31], 11)]]
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 fixtureActualExpectedOutcome
explicit oracle 0None2Failed
explicit oracle 1None2Failed
explicit oracle 211Passed
explicit oracle 3None22Failed
explicit oracle 411Passed
explicit oracle 533Passed
explicit oracle 622Passed
explicit oracle 711Passed

SHA-256 / 46e3085791024f4d59572a04c12bbc7643a8d662fd84a015b4d700108f06d46d

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 = [[([4, 13], 2), ([4, 5], 2), ([1, 5], 1), ([96, 97], 22), ([1, 7], 1), ([2, 7], 3), ([4, 7], 2), ([1, 11], 1)], [([10, 13], 6), ([12, 13], 5), ([1, 5], 1), ([96, 97], 22), ([2, 17], 6), ([4, 17], 2), ([8, 17], 5), ([9, 17], 3)], [([12, 13], 5), ([8, 17], 5), ([1, 5], 1), ([96, 97], 22), ([2, 23], 5), ([3, 23], 7), ([4, 23], 2), ([6, 23], 11)], [([4, 29], 2), ([15, 17], 7), ([1, 5], 1), ([96, 97], 22), ([16, 29], 4), ([20, 29], 7), ([22, 29], 14), ([23, 29], 9)], [([5, 29], 11), ([1, 5], 1), ([96, 97], 22), ([18, 31], 7), ([19, 31], 9), ([20, 31], 12), ([25, 31], 5), ([28, 31], 11)]]
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 fixtureActualExpectedOutcome
explicit oracle 022Passed
explicit oracle 122Passed
explicit oracle 211Passed
explicit oracle 32222Passed
explicit oracle 411Passed
explicit oracle 533Passed
explicit oracle 622Passed
explicit oracle 711Passed

SHA-256 / 382e58420955eb0267f01926bfab1583d621a031e3b65cf7fe0869916c11d250

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.492600+00:00.

Case digest / fff3d703272e9aa5c6be708aacd2a284497239ac852ba734ec957d04e7d37613