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
Empirically fitted constant from the Bateman-Horn heuristic. Used in the formula pโโโ โ ฮฑ ยท n ยท (ln n)ยฒ.
estimate_nth_safe_prime(n)
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].
| n | int | Prime index (must be โฅ 1) |
| returns | int | Estimated value of the n-th safe prime |
| raises | ValueError | If 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)
Project index k to a candidate safe prime. Finds the next prime q after the estimate, then returns p = 2q + 1.
| k | int | Deterministic index (must be โฅ 1) |
| returns | int | Candidate safe prime p = 2q + 1 |
projector_index(epoch_n, offset=0)
Map an epoch number to a starting lattice index. For large epochs, scales the search space appropriately.
| epoch_n | int | Epoch number |
| offset | int | Optional offset to shift the starting index (default: 0) |
| returns | int | Starting lattice index (minimum 1) |
find_safe_prime_near(n)
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.
| n | int | Target prime index |
| returns | dict | { p, q, index_approx, attempts } |
| raises | RuntimeError | If 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
Number of binomial coefficients to test. Higher = more thorough but slower.
Minimum collapse score to pass verification. Primes always score 1.0.
collapse_score(p, window=128)
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.
| p | int | Candidate number to test |
| window | int | Number of binomial coefficients to check (default: 128) |
| returns | float | Score 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)
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).
| p | int | Safe prime candidate |
| window | int | Collapse score window (default: 128) |
| threshold | float | Minimum score to pass (default: 0.95) |
| Returns dict: | ||
| p | int | The candidate |
| q | int | (p-1)/2 |
| is_safe_prime | bool | True if p is prime, q is prime, and p = 2q+1 |
| score_p | float | Collapse score for p |
| score_q | float | Collapse score for q |
| symbolic_pass | bool | Both scores above threshold |
| fallback_ok | bool | Miller-Rabin confirmation |
| verified | bool | True only if BOTH symbolic_pass AND fallback_ok |
| window | int | Window used |
| threshold | float | Threshold 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)
Verify a batch of candidates. Returns list of verification results.
| candidates | list[int] | List of numbers to verify |
| returns | list[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 an oracle trace record for on-chain publication. Includes SHA-256 hash for integrity verification.
| index | int | Prime index |
| p | int | Safe prime |
| q | int | Sophie Germain prime |
| score_p | float | Collapse score for p |
| score_q | float | Collapse score for q |
| verified | bool | Verification status |
| Returns dict: | ||
| version | str | "1.0.0" |
| index | int | Prime index |
| p, q | str | Prime values as strings |
| score_p, score_q | float | Collapse scores |
| verified | bool | Verification status |
| timestamp | str | ISO 8601 UTC timestamp |
| hash | str | SHA-256 hash of trace data |
format_for_contract(trace)
Format trace data for Ethereum smart contract submission. Converts scores to fixed-point (4 decimal places) and hash to bytes.
| trace | dict | Trace from create_trace() |
| returns | tuple | (index, p, q, score_pร10000, score_qร10000, verified, hash_bytes) |
export_traces(traces, filepath)
Export a list of traces to a JSON file.
| traces | list[dict] | List of trace dicts |
| filepath | str | Output 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
- PyPI: pypi.org/project/echoprime
- GitHub: github.com/DarrenEdwards111/EchoPrime
- Cloud API Docs: echoprime.xyz/echoprime-docs-api.html
- Whitepaper: Download PDF
- DOI: 10.5281/zenodo.18529723
- License: Apache 2.0