โ† Back to EchoPrime
๐Ÿ“˜

Python SDK Documentation

Full reference for the EchoPrime Python SDK. Generate, verify, and trace deterministic safe primes.

Installation

๐Ÿ“ฆ PyPI: https://pypi.org/project/echoprime/

# Install from PyPI
pip install echoprime

# Or from source
git clone https://github.com/DarrenEdwards111/EchoPrime.git
cd EchoPrime
pip install -e .

Requirements: Python 3.8+, sympy โ‰ฅ 1.12 (installed automatically)

Quick Start

from echoprime.estimator import estimate_nth_safe_prime, find_safe_prime_near
from echoprime.verifier import verify_safe_prime
from echoprime.oracle import create_trace

# 1. Find the 100th safe prime
p = find_safe_prime_near(100)
print(f"Safe prime: {p}")

# 2. Verify it
result = verify_safe_prime(p)
print(f"Verified: {result['verified']}")
print(f"Score (p): {result['score_p']}")
print(f"Score (q): {result['score_q']}")

# 3. Create oracle trace for on-chain publication
trace = create_trace(
    index=100, p=result['p'], q=result['q'],
    score_p=result['score_p'], score_q=result['score_q'],
    verified=result['verified']
)
print(f"SHA-256 hash: {trace['hash']}")

Module: echoprime.estimator

Analytic projection engine. Predicts where safe primes are located using the Bateman-Horn heuristic.

Constants

A_CONSTANT = 2.8913

Empirically fitted constant from the Bateman-Horn heuristic. Used in the formula pโ‚โ‚™โ‚Ž โ‰ˆ ฮฑ ยท n ยท (ln n)ยฒ.

estimate_nth_safe_prime(n)

estimate_nth_safe_prime(n) โ†’ int

Estimate the n-th safe prime using analytic projection: pโ‚โ‚™โ‚Ž โ‰ˆ 2.8913 ยท n ยท (ln n)ยฒ. For n โ‰ค 5, returns known safe primes directly [5, 7, 11, 23, 47].

nintPrime index (must be โ‰ฅ 1)
returnsintEstimated value of the n-th safe prime
raisesValueErrorIf n < 1
from echoprime.estimator import estimate_nth_safe_prime

estimate_nth_safe_prime(1)      # โ†’ 5 (known)
estimate_nth_safe_prime(100)    # โ†’ 6139 (estimate near actual 6599)
estimate_nth_safe_prime(10000)  # โ†’ 2,449,261 (estimate)

get_candidate_from_index(k)

get_candidate_from_index(k) โ†’ int

Project index k to a candidate safe prime. Finds the next prime q after the estimate, then returns p = 2q + 1.

kintDeterministic index (must be โ‰ฅ 1)
returnsintCandidate safe prime p = 2q + 1

projector_index(epoch_n, offset=0)

projector_index(epoch_n, offset=0) โ†’ int

Map an epoch number to a starting lattice index. For large epochs, scales the search space appropriately.

epoch_nintEpoch number
offsetintOptional offset to shift the starting index (default: 0)
returnsintStarting lattice index (minimum 1)

find_safe_prime_near(n)

find_safe_prime_near(n) โ†’ dict

Find the nearest safe prime at or above the estimate for index n. Searches forward from the estimate, testing candidates until a verified safe prime is found.

nintTarget prime index
returnsdict{ p, q, index_approx, attempts }
raisesRuntimeErrorIf no safe prime found within 10,000 attempts
result = find_safe_prime_near(100)
# โ†’ { 'p': 6599, 'q': 3299, 'index_approx': 100, 'attempts': 24 }

Module: echoprime.verifier

Symbolic collapse score verifier. Confirms primes using Lucas' theorem (binomial coefficient divisibility) and Miller-Rabin.

Constants

DEFAULT_WINDOW = 128

Number of binomial coefficients to test. Higher = more thorough but slower.

DEFAULT_THRESHOLD = 0.95

Minimum collapse score to pass verification. Primes always score 1.0.

collapse_score(p, window=128)

collapse_score(p, window=128) โ†’ float

Compute symbolic collapse score. Tests C(p,k) mod p == 0 for k=1..window. By Lucas' theorem, primes always return 1.0. Composites return < 1.0.

pintCandidate number to test
windowintNumber of binomial coefficients to check (default: 128)
returnsfloatScore between 0.0 and 1.0 (primes = 1.0)
from echoprime.verifier import collapse_score

collapse_score(23)     # โ†’ 1.0 (prime)
collapse_score(6599)   # โ†’ 1.0 (prime)
collapse_score(100)    # โ†’ 0.43 (composite)
collapse_score(15)     # โ†’ 0.71 (composite)

verify_safe_prime(p, window=128, threshold=0.95)

verify_safe_prime(p, window=128, threshold=0.95) โ†’ dict

Full verification of a safe prime candidate. Checks both p and q=(p-1)/2 for primality using collapse scores AND Miller-Rabin (via sympy's Baillie-PSW).

pintSafe prime candidate
windowintCollapse score window (default: 128)
thresholdfloatMinimum score to pass (default: 0.95)
Returns dict:
pintThe candidate
qint(p-1)/2
is_safe_primeboolTrue if p is prime, q is prime, and p = 2q+1
score_pfloatCollapse score for p
score_qfloatCollapse score for q
symbolic_passboolBoth scores above threshold
fallback_okboolMiller-Rabin confirmation
verifiedboolTrue only if BOTH symbolic_pass AND fallback_ok
windowintWindow used
thresholdfloatThreshold used
from echoprime.verifier import verify_safe_prime

result = verify_safe_prime(23)
# โ†’ {
#   'p': 23, 'q': 11,
#   'is_safe_prime': True,
#   'score_p': 1.0, 'score_q': 1.0,
#   'symbolic_pass': True, 'fallback_ok': True,
#   'verified': True,
#   'window': 128, 'threshold': 0.95
# }

result = verify_safe_prime(100)
# โ†’ { 'verified': False, 'is_safe_prime': False, ... }

batch_verify(candidates, window=128, threshold=0.95)

batch_verify(candidates, window=128, threshold=0.95) โ†’ list[dict]

Verify a batch of candidates. Returns list of verification results.

candidateslist[int]List of numbers to verify
returnslist[dict]List of verify_safe_prime results
results = batch_verify([23, 47, 59, 83, 100])
safe_primes = [r for r in results if r['verified']]
print(f"Found {len(safe_primes)} safe primes")

Module: echoprime.oracle

Oracle interface for on-chain publication. Creates SHA-256 hashed trace records and formats them for Ethereum smart contract submission.

create_trace(index, p, q, score_p, score_q, verified)

create_trace(index, p, q, score_p, score_q, verified) โ†’ dict

Create an oracle trace record for on-chain publication. Includes SHA-256 hash for integrity verification.

indexintPrime index
pintSafe prime
qintSophie Germain prime
score_pfloatCollapse score for p
score_qfloatCollapse score for q
verifiedboolVerification status
Returns dict:
versionstr"1.0.0"
indexintPrime index
p, qstrPrime values as strings
score_p, score_qfloatCollapse scores
verifiedboolVerification status
timestampstrISO 8601 UTC timestamp
hashstrSHA-256 hash of trace data

format_for_contract(trace)

format_for_contract(trace) โ†’ tuple

Format trace data for Ethereum smart contract submission. Converts scores to fixed-point (4 decimal places) and hash to bytes.

tracedictTrace from create_trace()
returnstuple(index, p, q, score_pร—10000, score_qร—10000, verified, hash_bytes)

export_traces(traces, filepath)

export_traces(traces, filepath) โ†’ None

Export a list of traces to a JSON file.

traceslist[dict]List of trace dicts
filepathstrOutput file path
# Full pipeline: find โ†’ verify โ†’ trace โ†’ export
from echoprime.estimator import find_safe_prime_near
from echoprime.verifier import verify_safe_prime
from echoprime.oracle import create_trace, format_for_contract, export_traces

traces = []
for i in range(1, 101):
    result = find_safe_prime_near(i)
    v = verify_safe_prime(result['p'])
    trace = create_trace(i, v['p'], v['q'], v['score_p'], v['score_q'], v['verified'])
    traces.append(trace)

    # Format for smart contract
    contract_args = format_for_contract(trace)
    print(f"Index {i}: p={trace['p']}, hash={trace['hash'][:16]}...")

# Save all traces
export_traces(traces, "echoprime-traces.json")

Testing

The SDK includes 41 tests with 100% pass rate. Run them with:

# Run all tests
python -m pytest tests/ -v

# Run specific module tests
python -m pytest tests/test_estimator.py -v
python -m pytest tests/test_verifier.py -v
python -m pytest tests/test_oracle.py -v

Independent verification: 1,000 primes confirmed by sympy with 0 false positives and 100% accuracy.

Links