foxmath 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- foxmath-0.1.0/LICENSE +21 -0
- foxmath-0.1.0/PKG-INFO +78 -0
- foxmath-0.1.0/README.md +59 -0
- foxmath-0.1.0/pyproject.toml +30 -0
- foxmath-0.1.0/setup.cfg +4 -0
- foxmath-0.1.0/src/foxmath/__init__.py +0 -0
- foxmath-0.1.0/src/foxmath/foxmath.py +133 -0
- foxmath-0.1.0/src/foxmath.egg-info/PKG-INFO +78 -0
- foxmath-0.1.0/src/foxmath.egg-info/SOURCES.txt +12 -0
- foxmath-0.1.0/src/foxmath.egg-info/dependency_links.txt +1 -0
- foxmath-0.1.0/src/foxmath.egg-info/entry_points.txt +2 -0
- foxmath-0.1.0/src/foxmath.egg-info/top_level.txt +1 -0
- foxmath-0.1.0/tests/test_cli.py +94 -0
- foxmath-0.1.0/tests/test_foxmath.py +86 -0
foxmath-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Abhrankan Chakrabarti
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
foxmath-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: foxmath
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Minimalist CLI for math & cryptography exploration 🦊
|
|
5
|
+
Author-email: Abhrankan Chakrabarti <abhrankan@gmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/Abhrankan-Chakrabarti/foxmath
|
|
8
|
+
Project-URL: Repository, https://github.com/Abhrankan-Chakrabarti/foxmath
|
|
9
|
+
Keywords: math,cryptography,cli,number-theory,elliptic-curves
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
|
14
|
+
Classifier: Topic :: Security :: Cryptography
|
|
15
|
+
Requires-Python: >=3.8
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
License-File: LICENSE
|
|
18
|
+
Dynamic: license-file
|
|
19
|
+
|
|
20
|
+
# FoxMath 🦊
|
|
21
|
+
|
|
22
|
+
**Minimalist CLI for math & cryptography exploration.**
|
|
23
|
+
|
|
24
|
+
One binary. Clean output. Zero footguns. Educational by design.
|
|
25
|
+
|
|
26
|
+
Built with curiosity — for students, tinkerers, and crypto/math enthusiasts.
|
|
27
|
+
|
|
28
|
+
## Features
|
|
29
|
+
- Legendre symbol
|
|
30
|
+
- Catalan-inspired π approximation
|
|
31
|
+
- Elliptic curve point addition (over finite fields)
|
|
32
|
+
- Symlink-friendly (foxmath-legendre, foxmath-pi, foxmath-ecadd)
|
|
33
|
+
- JSON output mode
|
|
34
|
+
- Coming soon: CRT, continued fractions, challenge mode, more curves
|
|
35
|
+
|
|
36
|
+
## Installation
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
pip install foxmath
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Or from source:
|
|
43
|
+
```bash
|
|
44
|
+
git clone https://github.com/Abhrankan-Chakrabarti/foxmath.git
|
|
45
|
+
cd foxmath
|
|
46
|
+
pip install -e .
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Usage Examples
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
# Basic
|
|
53
|
+
foxmath pi --terms 200
|
|
54
|
+
foxmath legendre 5 17
|
|
55
|
+
|
|
56
|
+
# Symlinks (optional)
|
|
57
|
+
foxmath-ecadd --x1 1 --y1 2 --x2 3 --y2 4 --p 17
|
|
58
|
+
|
|
59
|
+
# JSON output
|
|
60
|
+
foxmath pi --terms 100 --json
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Example Output
|
|
64
|
+
```
|
|
65
|
+
π ≈ 3.1415926535897932384626433832795028841971693993751 (200 terms)
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Why FoxMath?
|
|
69
|
+
Because math and cryptography are more fun when you can play with them instantly in the terminal — with the same reliability as FoxPipe and fox-vault.
|
|
70
|
+
|
|
71
|
+
**Simple. Practical. Reliable.**
|
|
72
|
+
|
|
73
|
+
## License
|
|
74
|
+
MIT © Abhrankan Chakrabarti
|
|
75
|
+
|
|
76
|
+
---
|
|
77
|
+
|
|
78
|
+
**Star if you find it useful!** Issues and PRs welcome.
|
foxmath-0.1.0/README.md
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# FoxMath 🦊
|
|
2
|
+
|
|
3
|
+
**Minimalist CLI for math & cryptography exploration.**
|
|
4
|
+
|
|
5
|
+
One binary. Clean output. Zero footguns. Educational by design.
|
|
6
|
+
|
|
7
|
+
Built with curiosity — for students, tinkerers, and crypto/math enthusiasts.
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
- Legendre symbol
|
|
11
|
+
- Catalan-inspired π approximation
|
|
12
|
+
- Elliptic curve point addition (over finite fields)
|
|
13
|
+
- Symlink-friendly (foxmath-legendre, foxmath-pi, foxmath-ecadd)
|
|
14
|
+
- JSON output mode
|
|
15
|
+
- Coming soon: CRT, continued fractions, challenge mode, more curves
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
pip install foxmath
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Or from source:
|
|
24
|
+
```bash
|
|
25
|
+
git clone https://github.com/Abhrankan-Chakrabarti/foxmath.git
|
|
26
|
+
cd foxmath
|
|
27
|
+
pip install -e .
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Usage Examples
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
# Basic
|
|
34
|
+
foxmath pi --terms 200
|
|
35
|
+
foxmath legendre 5 17
|
|
36
|
+
|
|
37
|
+
# Symlinks (optional)
|
|
38
|
+
foxmath-ecadd --x1 1 --y1 2 --x2 3 --y2 4 --p 17
|
|
39
|
+
|
|
40
|
+
# JSON output
|
|
41
|
+
foxmath pi --terms 100 --json
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Example Output
|
|
45
|
+
```
|
|
46
|
+
π ≈ 3.1415926535897932384626433832795028841971693993751 (200 terms)
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Why FoxMath?
|
|
50
|
+
Because math and cryptography are more fun when you can play with them instantly in the terminal — with the same reliability as FoxPipe and fox-vault.
|
|
51
|
+
|
|
52
|
+
**Simple. Practical. Reliable.**
|
|
53
|
+
|
|
54
|
+
## License
|
|
55
|
+
MIT © Abhrankan Chakrabarti
|
|
56
|
+
|
|
57
|
+
---
|
|
58
|
+
|
|
59
|
+
**Star if you find it useful!** Issues and PRs welcome.
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "foxmath"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Minimalist CLI for math & cryptography exploration 🦊"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = {text = "MIT"}
|
|
11
|
+
authors = [
|
|
12
|
+
{name = "Abhrankan Chakrabarti", email = "abhrankan@gmail.com"}
|
|
13
|
+
]
|
|
14
|
+
keywords = ["math", "cryptography", "cli", "number-theory", "elliptic-curves"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Programming Language :: Python :: 3",
|
|
17
|
+
"License :: OSI Approved :: MIT License",
|
|
18
|
+
"Operating System :: OS Independent",
|
|
19
|
+
"Topic :: Scientific/Engineering :: Mathematics",
|
|
20
|
+
"Topic :: Security :: Cryptography",
|
|
21
|
+
]
|
|
22
|
+
requires-python = ">=3.8"
|
|
23
|
+
dependencies = [] # Pure stdlib for now
|
|
24
|
+
|
|
25
|
+
[project.urls]
|
|
26
|
+
"Homepage" = "https://github.com/Abhrankan-Chakrabarti/foxmath"
|
|
27
|
+
"Repository" = "https://github.com/Abhrankan-Chakrabarti/foxmath"
|
|
28
|
+
|
|
29
|
+
[project.scripts]
|
|
30
|
+
foxmath = "foxmath.foxmath:main"
|
foxmath-0.1.0/setup.cfg
ADDED
|
File without changes
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""
|
|
3
|
+
FoxMath — Minimalist math & crypto explorer 🦊
|
|
4
|
+
One binary. Clean. Educational. Safe.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import argparse
|
|
8
|
+
import sys
|
|
9
|
+
import json
|
|
10
|
+
from decimal import Decimal, getcontext
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
|
|
13
|
+
# ------------------- Math Functions -------------------
|
|
14
|
+
|
|
15
|
+
def legendre_symbol(a: int, p: int) -> int:
|
|
16
|
+
"""Compute Legendre symbol (a/p)"""
|
|
17
|
+
if p < 2 or p % 2 == 0:
|
|
18
|
+
raise ValueError("p must be an odd prime")
|
|
19
|
+
res = pow(a, (p - 1) // 2, p)
|
|
20
|
+
return 1 if res == 1 else (-1 if res == p - 1 else 0)
|
|
21
|
+
|
|
22
|
+
def _arctan_series(inv_n: Decimal, terms: int) -> Decimal:
|
|
23
|
+
"""arctan(1/n) via its Taylor series, evaluated to `terms` terms. `inv_n` is 1/n."""
|
|
24
|
+
total = Decimal(0)
|
|
25
|
+
for k in range(terms):
|
|
26
|
+
sign = Decimal(-1) ** k
|
|
27
|
+
total += sign * inv_n**(2 * k + 1) / (2 * k + 1)
|
|
28
|
+
return total
|
|
29
|
+
|
|
30
|
+
def catalan_pi_approx(terms: int = 100) -> Decimal:
|
|
31
|
+
"""
|
|
32
|
+
Fast-converging π approximation via the Euler/Hermann Machin-like
|
|
33
|
+
identity: pi/4 = arctan(1/2) + arctan(1/3).
|
|
34
|
+
"""
|
|
35
|
+
getcontext().prec = 50
|
|
36
|
+
s = _arctan_series(Decimal(1) / 2, terms) + _arctan_series(Decimal(1) / 3, terms)
|
|
37
|
+
return 4 * s
|
|
38
|
+
|
|
39
|
+
def ec_point_add(x1, y1, x2, y2, a, p):
|
|
40
|
+
"""Simple elliptic curve point addition over finite field y² = x³ + a x + b (mod p)"""
|
|
41
|
+
if x1 % p == x2 % p and (y1 + y2) % p == 0:
|
|
42
|
+
raise ValueError(
|
|
43
|
+
"Result is the point at infinity (P + (-P)); this simplified "
|
|
44
|
+
"implementation has no way to represent it."
|
|
45
|
+
)
|
|
46
|
+
if x1 == x2 and y1 == y2:
|
|
47
|
+
# Doubling
|
|
48
|
+
lam = (3 * x1**2 + a) * pow(2 * y1, -1, p) % p
|
|
49
|
+
else:
|
|
50
|
+
lam = (y2 - y1) * pow(x2 - x1, -1, p) % p
|
|
51
|
+
x3 = (lam**2 - x1 - x2) % p
|
|
52
|
+
y3 = (lam * (x1 - x3) - y1) % p
|
|
53
|
+
return x3, y3
|
|
54
|
+
|
|
55
|
+
# ------------------- CLI -------------------
|
|
56
|
+
|
|
57
|
+
def get_command():
|
|
58
|
+
"""Support symlinks: foxmath-legendre, foxmath-pi, etc."""
|
|
59
|
+
name = Path(sys.argv[0]).stem.lower()
|
|
60
|
+
if name.startswith("foxmath-"):
|
|
61
|
+
return name.split("-", 1)[1]
|
|
62
|
+
return None
|
|
63
|
+
|
|
64
|
+
def main():
|
|
65
|
+
cmd = get_command()
|
|
66
|
+
|
|
67
|
+
# Handle --json independently of argparse subparser scoping: a subparser's
|
|
68
|
+
# own default for a same-named flag silently overwrites whatever the
|
|
69
|
+
# parent parser already parsed for it, so --json only ever worked in one
|
|
70
|
+
# position when defined via argparse directly. Strip it out manually
|
|
71
|
+
# instead, so it works before or after the subcommand.
|
|
72
|
+
argv = sys.argv[1:]
|
|
73
|
+
json_output = "--json" in argv
|
|
74
|
+
argv = [a for a in argv if a != "--json"]
|
|
75
|
+
|
|
76
|
+
if cmd:
|
|
77
|
+
# Symlink invocation (e.g. `foxmath-legendre 2 7`): argparse's
|
|
78
|
+
# subparsers positional still expects a command-name token next,
|
|
79
|
+
# so without this it tries to match the first real argument
|
|
80
|
+
# (e.g. "2") against {legendre,pi,ecadd} and fails. Inject the
|
|
81
|
+
# detected command so it parses exactly as if typed explicitly.
|
|
82
|
+
argv = [cmd] + argv
|
|
83
|
+
|
|
84
|
+
parser = argparse.ArgumentParser(description="FoxMath — Math & Crypto Explorer 🦊")
|
|
85
|
+
subparsers = parser.add_subparsers(dest="command", required=not cmd)
|
|
86
|
+
|
|
87
|
+
# Legendre
|
|
88
|
+
leg = subparsers.add_parser("legendre", help="Legendre symbol (a/p)")
|
|
89
|
+
leg.add_argument("a", type=int, help="Integer a")
|
|
90
|
+
leg.add_argument("p", type=int, help="Odd prime p")
|
|
91
|
+
|
|
92
|
+
# Pi
|
|
93
|
+
pi_cmd = subparsers.add_parser("pi", help="Catalan-inspired π approximation")
|
|
94
|
+
pi_cmd.add_argument("--terms", type=int, default=100)
|
|
95
|
+
|
|
96
|
+
# Elliptic curve add
|
|
97
|
+
ec = subparsers.add_parser("ecadd", help="Elliptic curve point addition")
|
|
98
|
+
ec.add_argument("--x1", type=int, required=True)
|
|
99
|
+
ec.add_argument("--y1", type=int, required=True)
|
|
100
|
+
ec.add_argument("--x2", type=int, required=True)
|
|
101
|
+
ec.add_argument("--y2", type=int, required=True)
|
|
102
|
+
ec.add_argument("--a", type=int, default=-3, help="Curve parameter a")
|
|
103
|
+
ec.add_argument("--p", type=int, required=True, help="Prime modulus")
|
|
104
|
+
|
|
105
|
+
args = parser.parse_args(argv)
|
|
106
|
+
|
|
107
|
+
result = {"tool": "foxmath"}
|
|
108
|
+
|
|
109
|
+
try:
|
|
110
|
+
if cmd == "legendre" or args.command == "legendre":
|
|
111
|
+
ls = legendre_symbol(args.a, args.p)
|
|
112
|
+
result.update({"command": "legendre", "a": args.a, "p": args.p, "value": ls})
|
|
113
|
+
print(f"Legendre ({args.a}/{args.p}) = {ls}")
|
|
114
|
+
|
|
115
|
+
elif cmd == "pi" or args.command == "pi":
|
|
116
|
+
approx = catalan_pi_approx(args.terms)
|
|
117
|
+
result.update({"command": "pi", "terms": args.terms, "approx": str(approx)})
|
|
118
|
+
print(f"π ≈ {approx} ({args.terms} terms)")
|
|
119
|
+
|
|
120
|
+
elif cmd == "ecadd" or args.command == "ecadd":
|
|
121
|
+
x3, y3 = ec_point_add(args.x1, args.y1, args.x2, args.y2, args.a, args.p)
|
|
122
|
+
result.update({"command": "ecadd", "result": (x3, y3)})
|
|
123
|
+
print(f"Result point: ({x3}, {y3})")
|
|
124
|
+
|
|
125
|
+
except Exception as e:
|
|
126
|
+
print(f"Error: {e}", file=sys.stderr)
|
|
127
|
+
sys.exit(1)
|
|
128
|
+
|
|
129
|
+
if json_output:
|
|
130
|
+
print(json.dumps(result, indent=2))
|
|
131
|
+
|
|
132
|
+
if __name__ == "__main__":
|
|
133
|
+
main()
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: foxmath
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Minimalist CLI for math & cryptography exploration 🦊
|
|
5
|
+
Author-email: Abhrankan Chakrabarti <abhrankan@gmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/Abhrankan-Chakrabarti/foxmath
|
|
8
|
+
Project-URL: Repository, https://github.com/Abhrankan-Chakrabarti/foxmath
|
|
9
|
+
Keywords: math,cryptography,cli,number-theory,elliptic-curves
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
|
14
|
+
Classifier: Topic :: Security :: Cryptography
|
|
15
|
+
Requires-Python: >=3.8
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
License-File: LICENSE
|
|
18
|
+
Dynamic: license-file
|
|
19
|
+
|
|
20
|
+
# FoxMath 🦊
|
|
21
|
+
|
|
22
|
+
**Minimalist CLI for math & cryptography exploration.**
|
|
23
|
+
|
|
24
|
+
One binary. Clean output. Zero footguns. Educational by design.
|
|
25
|
+
|
|
26
|
+
Built with curiosity — for students, tinkerers, and crypto/math enthusiasts.
|
|
27
|
+
|
|
28
|
+
## Features
|
|
29
|
+
- Legendre symbol
|
|
30
|
+
- Catalan-inspired π approximation
|
|
31
|
+
- Elliptic curve point addition (over finite fields)
|
|
32
|
+
- Symlink-friendly (foxmath-legendre, foxmath-pi, foxmath-ecadd)
|
|
33
|
+
- JSON output mode
|
|
34
|
+
- Coming soon: CRT, continued fractions, challenge mode, more curves
|
|
35
|
+
|
|
36
|
+
## Installation
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
pip install foxmath
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Or from source:
|
|
43
|
+
```bash
|
|
44
|
+
git clone https://github.com/Abhrankan-Chakrabarti/foxmath.git
|
|
45
|
+
cd foxmath
|
|
46
|
+
pip install -e .
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Usage Examples
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
# Basic
|
|
53
|
+
foxmath pi --terms 200
|
|
54
|
+
foxmath legendre 5 17
|
|
55
|
+
|
|
56
|
+
# Symlinks (optional)
|
|
57
|
+
foxmath-ecadd --x1 1 --y1 2 --x2 3 --y2 4 --p 17
|
|
58
|
+
|
|
59
|
+
# JSON output
|
|
60
|
+
foxmath pi --terms 100 --json
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Example Output
|
|
64
|
+
```
|
|
65
|
+
π ≈ 3.1415926535897932384626433832795028841971693993751 (200 terms)
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Why FoxMath?
|
|
69
|
+
Because math and cryptography are more fun when you can play with them instantly in the terminal — with the same reliability as FoxPipe and fox-vault.
|
|
70
|
+
|
|
71
|
+
**Simple. Practical. Reliable.**
|
|
72
|
+
|
|
73
|
+
## License
|
|
74
|
+
MIT © Abhrankan Chakrabarti
|
|
75
|
+
|
|
76
|
+
---
|
|
77
|
+
|
|
78
|
+
**Star if you find it useful!** Issues and PRs welcome.
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
src/foxmath/__init__.py
|
|
5
|
+
src/foxmath/foxmath.py
|
|
6
|
+
src/foxmath.egg-info/PKG-INFO
|
|
7
|
+
src/foxmath.egg-info/SOURCES.txt
|
|
8
|
+
src/foxmath.egg-info/dependency_links.txt
|
|
9
|
+
src/foxmath.egg-info/entry_points.txt
|
|
10
|
+
src/foxmath.egg-info/top_level.txt
|
|
11
|
+
tests/test_cli.py
|
|
12
|
+
tests/test_foxmath.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
foxmath
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"""
|
|
2
|
+
End-to-end tests for foxmath's CLI, run as a subprocess to match real
|
|
3
|
+
usage (including simulated symlink invocation).
|
|
4
|
+
"""
|
|
5
|
+
import subprocess
|
|
6
|
+
import sys
|
|
7
|
+
import os
|
|
8
|
+
import shutil
|
|
9
|
+
import tempfile
|
|
10
|
+
import unittest
|
|
11
|
+
|
|
12
|
+
ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "src", "foxmath"))
|
|
13
|
+
SCRIPT = os.path.join(ROOT, "foxmath.py")
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def run(*args, script=SCRIPT):
|
|
17
|
+
result = subprocess.run(
|
|
18
|
+
[sys.executable, script, *args],
|
|
19
|
+
capture_output=True, text=True, timeout=15,
|
|
20
|
+
)
|
|
21
|
+
return result.returncode, result.stdout, result.stderr
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class TestJsonFlag(unittest.TestCase):
|
|
25
|
+
def test_json_after_subcommand(self):
|
|
26
|
+
# This is the exact usage shown in the README's own example.
|
|
27
|
+
code, out, err = run("legendre", "2", "7", "--json")
|
|
28
|
+
self.assertEqual(code, 0)
|
|
29
|
+
self.assertIn('"value": 1', out)
|
|
30
|
+
|
|
31
|
+
def test_json_before_subcommand(self):
|
|
32
|
+
code, out, err = run("--json", "legendre", "2", "7")
|
|
33
|
+
self.assertEqual(code, 0)
|
|
34
|
+
self.assertIn('"value": 1', out)
|
|
35
|
+
|
|
36
|
+
def test_no_json_flag_omits_json_block(self):
|
|
37
|
+
code, out, err = run("legendre", "2", "7")
|
|
38
|
+
self.assertEqual(code, 0)
|
|
39
|
+
self.assertNotIn("{", out)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class TestSymlinkInvocation(unittest.TestCase):
|
|
43
|
+
def setUp(self):
|
|
44
|
+
self.tmpdir = tempfile.mkdtemp()
|
|
45
|
+
|
|
46
|
+
def tearDown(self):
|
|
47
|
+
shutil.rmtree(self.tmpdir, ignore_errors=True)
|
|
48
|
+
|
|
49
|
+
def _make_symlink_script(self, name):
|
|
50
|
+
path = os.path.join(self.tmpdir, f"{name}.py")
|
|
51
|
+
shutil.copy(SCRIPT, path)
|
|
52
|
+
return path
|
|
53
|
+
|
|
54
|
+
def test_legendre_symlink(self):
|
|
55
|
+
script = self._make_symlink_script("foxmath-legendre")
|
|
56
|
+
code, out, err = run("2", "7", script=script)
|
|
57
|
+
self.assertEqual(code, 0)
|
|
58
|
+
self.assertIn("Legendre (2/7) = 1", out)
|
|
59
|
+
|
|
60
|
+
def test_pi_symlink(self):
|
|
61
|
+
script = self._make_symlink_script("foxmath-pi")
|
|
62
|
+
code, out, err = run("--terms", "10", script=script)
|
|
63
|
+
self.assertEqual(code, 0)
|
|
64
|
+
self.assertIn("π ≈", out)
|
|
65
|
+
|
|
66
|
+
def test_ecadd_symlink(self):
|
|
67
|
+
script = self._make_symlink_script("foxmath-ecadd")
|
|
68
|
+
code, out, err = run("--x1", "3", "--y1", "6", "--x2", "3", "--y2", "6",
|
|
69
|
+
"--a", "2", "--p", "97", script=script)
|
|
70
|
+
self.assertEqual(code, 0)
|
|
71
|
+
self.assertIn("Result point:", out)
|
|
72
|
+
|
|
73
|
+
def test_symlink_with_json(self):
|
|
74
|
+
script = self._make_symlink_script("foxmath-legendre")
|
|
75
|
+
code, out, err = run("2", "7", "--json", script=script)
|
|
76
|
+
self.assertEqual(code, 0)
|
|
77
|
+
self.assertIn('"value": 1', out)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
class TestNormalUsage(unittest.TestCase):
|
|
81
|
+
def test_pi(self):
|
|
82
|
+
code, out, err = run("pi", "--terms", "50")
|
|
83
|
+
self.assertEqual(code, 0)
|
|
84
|
+
self.assertIn("3.14159265358979", out)
|
|
85
|
+
|
|
86
|
+
def test_ecadd_point_at_infinity_errors_clearly(self):
|
|
87
|
+
code, out, err = run("ecadd", "--x1", "3", "--y1", "6",
|
|
88
|
+
"--x2", "3", "--y2", "91", "--a", "2", "--p", "97")
|
|
89
|
+
self.assertEqual(code, 1)
|
|
90
|
+
self.assertIn("point at infinity", err)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
if __name__ == "__main__":
|
|
94
|
+
unittest.main()
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Unit tests for foxmath's core functions, checked against independently
|
|
3
|
+
verifiable reference values (known Legendre symbols, math.pi, and
|
|
4
|
+
on-curve checks for elliptic curve arithmetic) rather than against the
|
|
5
|
+
module's own prior output.
|
|
6
|
+
"""
|
|
7
|
+
import sys
|
|
8
|
+
import os
|
|
9
|
+
import math
|
|
10
|
+
import unittest
|
|
11
|
+
from decimal import Decimal
|
|
12
|
+
|
|
13
|
+
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "src")))
|
|
14
|
+
|
|
15
|
+
from foxmath.foxmath import legendre_symbol, catalan_pi_approx, ec_point_add
|
|
16
|
+
|
|
17
|
+
# Reference pi digits (mpmath, 250 dps of working precision), "3" + fractional digits.
|
|
18
|
+
PI_REFERENCE = Decimal(
|
|
19
|
+
"3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803"
|
|
20
|
+
"4825342117067982148086513282306647093844609550582231725359408128481117450284102701938521"
|
|
21
|
+
"1055596446229489549303819644288"
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class TestLegendreSymbol(unittest.TestCase):
|
|
26
|
+
def test_known_quadratic_residues_mod_7(self):
|
|
27
|
+
# Squares mod 7: {1, 4, 2}
|
|
28
|
+
self.assertEqual(legendre_symbol(1, 7), 1)
|
|
29
|
+
self.assertEqual(legendre_symbol(2, 7), 1)
|
|
30
|
+
self.assertEqual(legendre_symbol(4, 7), 1)
|
|
31
|
+
|
|
32
|
+
def test_known_non_residues_mod_7(self):
|
|
33
|
+
self.assertEqual(legendre_symbol(3, 7), -1)
|
|
34
|
+
self.assertEqual(legendre_symbol(5, 7), -1)
|
|
35
|
+
self.assertEqual(legendre_symbol(6, 7), -1)
|
|
36
|
+
|
|
37
|
+
def test_multiple_of_p_is_zero(self):
|
|
38
|
+
self.assertEqual(legendre_symbol(7, 7), 0)
|
|
39
|
+
self.assertEqual(legendre_symbol(14, 7), 0)
|
|
40
|
+
|
|
41
|
+
def test_rejects_even_p(self):
|
|
42
|
+
with self.assertRaises(ValueError):
|
|
43
|
+
legendre_symbol(3, 8)
|
|
44
|
+
|
|
45
|
+
def test_rejects_p_less_than_2(self):
|
|
46
|
+
with self.assertRaises(ValueError):
|
|
47
|
+
legendre_symbol(3, 1)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class TestCatalanPiApprox(unittest.TestCase):
|
|
51
|
+
def test_converges_to_pi(self):
|
|
52
|
+
# Regression check: this used to converge to 16*arctan(1/2) =~ 7.418,
|
|
53
|
+
# not pi, due to a double-counted scaling factor.
|
|
54
|
+
approx = catalan_pi_approx(100)
|
|
55
|
+
self.assertLess(abs(approx - PI_REFERENCE), Decimal("1e-40"))
|
|
56
|
+
|
|
57
|
+
def test_more_terms_is_more_accurate(self):
|
|
58
|
+
err_10 = abs(catalan_pi_approx(10) - PI_REFERENCE)
|
|
59
|
+
err_50 = abs(catalan_pi_approx(50) - PI_REFERENCE)
|
|
60
|
+
self.assertLess(err_50, err_10)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
class TestECPointAdd(unittest.TestCase):
|
|
64
|
+
# Curve y^2 = x^3 + 2x + 3 (mod 97); (3, 6) is on this curve
|
|
65
|
+
# since 6^2=36 and 3^3+2*3+3=36.
|
|
66
|
+
A, P = 2, 97
|
|
67
|
+
|
|
68
|
+
def _on_curve(self, x, y):
|
|
69
|
+
return (y * y - (x ** 3 + self.A * x + 3)) % self.P == 0
|
|
70
|
+
|
|
71
|
+
def test_doubling_stays_on_curve(self):
|
|
72
|
+
x3, y3 = ec_point_add(3, 6, 3, 6, self.A, self.P)
|
|
73
|
+
self.assertTrue(self._on_curve(x3, y3))
|
|
74
|
+
|
|
75
|
+
def test_distinct_point_addition_stays_on_curve(self):
|
|
76
|
+
# Another point on the same curve: (80, 10) = 2*(3,6) from above.
|
|
77
|
+
x3, y3 = ec_point_add(3, 6, 80, 10, self.A, self.P)
|
|
78
|
+
self.assertTrue(self._on_curve(x3, y3))
|
|
79
|
+
|
|
80
|
+
def test_point_plus_negation_raises_clear_error(self):
|
|
81
|
+
with self.assertRaises(ValueError):
|
|
82
|
+
ec_point_add(3, 6, 3, self.P - 6, self.A, self.P)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
if __name__ == "__main__":
|
|
86
|
+
unittest.main()
|