foxmath 0.1.0__py3-none-any.whl

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/__init__.py ADDED
File without changes
foxmath/foxmath.py ADDED
@@ -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,8 @@
1
+ foxmath/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ foxmath/foxmath.py,sha256=jUHwCBJ7jKApJzzLwhmv6CBMXdb09cQjapNAtAQS8hs,4951
3
+ foxmath-0.1.0.dist-info/licenses/LICENSE,sha256=KuFxoMq6nEy9Q6D7oDpof2DmGPWg0ztuS3BrnLTwMPY,1078
4
+ foxmath-0.1.0.dist-info/METADATA,sha256=inTpRhGOxuFDBXv8NxIXH9z5zVcAXOpAJmMXkZjZPtA,2023
5
+ foxmath-0.1.0.dist-info/WHEEL,sha256=K260EYznzXsJYBQGqmI8VTxEdiZYNvDZwW9cBh9-_MA,91
6
+ foxmath-0.1.0.dist-info/entry_points.txt,sha256=-L4O5v1cnHMfGnxlxTyBBjBOMl4wVCNUFS7NqSJAEnY,49
7
+ foxmath-0.1.0.dist-info/top_level.txt,sha256=jFrfdE461tQcSDZtoodnhvICZLKw1gLb2DNvFkcuFRg,8
8
+ foxmath-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (83.0.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ foxmath = foxmath.foxmath:main
@@ -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.
@@ -0,0 +1 @@
1
+ foxmath