tiny-zk-auth 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.
@@ -0,0 +1,74 @@
1
+ Metadata-Version: 2.4
2
+ Name: tiny-zk-auth
3
+ Version: 0.1.0
4
+ Summary: Tiny zero-dependency auth + commitment toolkit: Mersenne-31 cross-platform fingerprint, HMAC-SHA256 device challenge-response, and Merkle membership proofs.
5
+ License: MIT
6
+ Project-URL: Homepage, https://github.com/Amanbhaw/tiny-zk-auth
7
+ Keywords: mersenne31,merkle,commitment,hmac,iot,device-auth,fingerprint,embedded
8
+ Classifier: Development Status :: 4 - Beta
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Topic :: Security :: Cryptography
12
+ Requires-Python: >=3.8
13
+ Description-Content-Type: text/markdown
14
+
15
+ # tiny-zk-auth
16
+
17
+ **Tiny, zero-dependency authentication + commitment toolkit** that runs **byte-identically**
18
+ in the browser, Node.js, Python, and a **$1 microcontroller** (the field core is integer-only).
19
+
20
+ ```
21
+ npm i tiny-zk-auth # Node >= 12 (browser: field + fingerprint need no crypto)
22
+ pip install tiny-zk-auth # Python >= 3.8
23
+ ```
24
+
25
+ Three real pieces:
26
+
27
+ ### 1. Mersenne-31 fingerprint (fast, cross-platform integrity)
28
+ ```js
29
+ const { fingerprintStr } = require('tiny-zk-auth');
30
+ fingerprintStr('hello world'); // 1951127573 — same number in Python, Node, browser, MCU
31
+ ```
32
+ A polynomial (Horner) fingerprint over `p = 2³¹−1`. Deterministic everywhere, great for
33
+ integrity / dedup / cache keys on tiny devices. **Not** a cryptographic hash (≈2⁻³¹ collision) —
34
+ use it for integrity, not as a security boundary.
35
+
36
+ ### 2. HMAC-SHA256 device auth (the real security)
37
+ ```js
38
+ const ch = makeChallenge(); // server
39
+ const resp = respond(secret, ch); // device
40
+ verify(secret, ch, resp); // server -> true
41
+ ```
42
+ Symmetric shared-secret challenge-response, replay-resistant via random challenges.
43
+
44
+ ### 3. Merkle commitment (prove one, hide the rest)
45
+ ```js
46
+ const root = merkleRoot(['alice','bob','carol','dave']); // commit, reveals nothing
47
+ const proof = merkleProve(items, 1);
48
+ merkleVerify(root, 'bob', proof); // true — and the proof leaks no other member
49
+ ```
50
+
51
+ ## Honest scope (read this)
52
+ Despite the name, **this is not a zero-knowledge *proof system*.** A 31-bit field cannot give
53
+ sound ZK. What you get is: a fast cross-platform **fingerprint**, **symmetric (HMAC) auth**, and
54
+ **Merkle commitments** with selective disclosure. That combination is genuinely useful for IoT /
55
+ DePIN / Web3 device identity and tamper-evident sets — just don't market it as "proving statements
56
+ in zero knowledge." If you need real ZK, that's a separate, heavier lattice/elliptic construction.
57
+
58
+ ## Cross-platform guarantee
59
+ The JS and Python packages produce identical fingerprints, HMAC responses and Merkle roots —
60
+ verified by a cross-language test fixture in both test suites.
61
+
62
+ ## Open-core
63
+ - **Free (MIT):** everything above.
64
+ - **Pro:** commercial license + support tier (`proTier` flag). It gates **no cryptography** —
65
+ honesty: the crypto is all free. You sell support, SLAs, and signed release builds.
66
+
67
+ ## Tests
68
+ ```
69
+ cd npm && node test/test.js # 7 tests incl. cross-language fixture
70
+ cd pypi && python3 tests/test_auth.py # 7 tests
71
+ ```
72
+
73
+ ## License
74
+ MIT.
@@ -0,0 +1,60 @@
1
+ # tiny-zk-auth
2
+
3
+ **Tiny, zero-dependency authentication + commitment toolkit** that runs **byte-identically**
4
+ in the browser, Node.js, Python, and a **$1 microcontroller** (the field core is integer-only).
5
+
6
+ ```
7
+ npm i tiny-zk-auth # Node >= 12 (browser: field + fingerprint need no crypto)
8
+ pip install tiny-zk-auth # Python >= 3.8
9
+ ```
10
+
11
+ Three real pieces:
12
+
13
+ ### 1. Mersenne-31 fingerprint (fast, cross-platform integrity)
14
+ ```js
15
+ const { fingerprintStr } = require('tiny-zk-auth');
16
+ fingerprintStr('hello world'); // 1951127573 — same number in Python, Node, browser, MCU
17
+ ```
18
+ A polynomial (Horner) fingerprint over `p = 2³¹−1`. Deterministic everywhere, great for
19
+ integrity / dedup / cache keys on tiny devices. **Not** a cryptographic hash (≈2⁻³¹ collision) —
20
+ use it for integrity, not as a security boundary.
21
+
22
+ ### 2. HMAC-SHA256 device auth (the real security)
23
+ ```js
24
+ const ch = makeChallenge(); // server
25
+ const resp = respond(secret, ch); // device
26
+ verify(secret, ch, resp); // server -> true
27
+ ```
28
+ Symmetric shared-secret challenge-response, replay-resistant via random challenges.
29
+
30
+ ### 3. Merkle commitment (prove one, hide the rest)
31
+ ```js
32
+ const root = merkleRoot(['alice','bob','carol','dave']); // commit, reveals nothing
33
+ const proof = merkleProve(items, 1);
34
+ merkleVerify(root, 'bob', proof); // true — and the proof leaks no other member
35
+ ```
36
+
37
+ ## Honest scope (read this)
38
+ Despite the name, **this is not a zero-knowledge *proof system*.** A 31-bit field cannot give
39
+ sound ZK. What you get is: a fast cross-platform **fingerprint**, **symmetric (HMAC) auth**, and
40
+ **Merkle commitments** with selective disclosure. That combination is genuinely useful for IoT /
41
+ DePIN / Web3 device identity and tamper-evident sets — just don't market it as "proving statements
42
+ in zero knowledge." If you need real ZK, that's a separate, heavier lattice/elliptic construction.
43
+
44
+ ## Cross-platform guarantee
45
+ The JS and Python packages produce identical fingerprints, HMAC responses and Merkle roots —
46
+ verified by a cross-language test fixture in both test suites.
47
+
48
+ ## Open-core
49
+ - **Free (MIT):** everything above.
50
+ - **Pro:** commercial license + support tier (`proTier` flag). It gates **no cryptography** —
51
+ honesty: the crypto is all free. You sell support, SLAs, and signed release builds.
52
+
53
+ ## Tests
54
+ ```
55
+ cd npm && node test/test.js # 7 tests incl. cross-language fixture
56
+ cd pypi && python3 tests/test_auth.py # 7 tests
57
+ ```
58
+
59
+ ## License
60
+ MIT.
@@ -0,0 +1,24 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61.0"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "tiny-zk-auth"
7
+ version = "0.1.0"
8
+ description = "Tiny zero-dependency auth + commitment toolkit: Mersenne-31 cross-platform fingerprint, HMAC-SHA256 device challenge-response, and Merkle membership proofs."
9
+ readme = "README.md"
10
+ requires-python = ">=3.8"
11
+ license = { text = "MIT" }
12
+ keywords = ["mersenne31", "merkle", "commitment", "hmac", "iot", "device-auth", "fingerprint", "embedded"]
13
+ classifiers = [
14
+ "Development Status :: 4 - Beta",
15
+ "License :: OSI Approved :: MIT License",
16
+ "Programming Language :: Python :: 3",
17
+ "Topic :: Security :: Cryptography",
18
+ ]
19
+
20
+ [project.urls]
21
+ Homepage = "https://github.com/Amanbhaw/tiny-zk-auth"
22
+
23
+ [tool.setuptools.packages.find]
24
+ where = ["src"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,165 @@
1
+ """tiny-zk-auth — tiny, zero-dependency authentication + commitment toolkit.
2
+
3
+ Runs identically in the browser, Node.js, Python, and a $1 Cortex-M0 / ESP32 (the field
4
+ core is integer-only). Three real pieces:
5
+
6
+ 1. Mersenne-31 field + `fingerprint()` — a fast polynomial (Horner) fingerprint over
7
+ p = 2**31 - 1. Deterministic and byte-identical across all platforms. Great for
8
+ integrity / dedup / cache keys on tiny devices.
9
+ *Honest note:* a 31-bit fingerprint is NOT a cryptographic-strength hash
10
+ (~2**-31 collision). Use it for integrity, not as a security boundary.
11
+
12
+ 2. HMAC-SHA256 challenge-response device auth — the actual cryptographic security.
13
+ `make_challenge()`, `respond(secret, challenge)`, `verify(secret, challenge, resp)`.
14
+ Symmetric (shared-secret), replay-resistant via random challenges. This is a MAC,
15
+ not public-key and NOT a zero-knowledge proof.
16
+
17
+ 3. Merkle commitment — `merkle_root()`, `merkle_prove()`, `merkle_verify()`. Commit to a
18
+ set, later prove ONE member without revealing the others (selective disclosure).
19
+
20
+ HONEST SCOPE: despite the name, this package does not implement a zero-knowledge *proof
21
+ system* (a 31-bit field cannot give sound ZK). It gives commitments + symmetric auth. If you
22
+ need real ZK, that is a separate, heavier lattice/elliptic construction.
23
+
24
+ MIT core; a commercial license key gates nothing cryptographic — it is a support/commercial
25
+ tier flag only.
26
+ """
27
+
28
+ from __future__ import annotations
29
+
30
+ import hashlib
31
+ import hmac
32
+ import os
33
+ from typing import List, Optional, Sequence, Tuple
34
+
35
+ __version__ = "0.1.0"
36
+
37
+ P = (1 << 31) - 1 # Mersenne-31 prime, 2147483647
38
+
39
+
40
+ # ----------------------------- Mersenne-31 field -----------------------------
41
+
42
+ def m31_add(a: int, b: int) -> int:
43
+ s = (a + b) % P
44
+ return s
45
+
46
+
47
+ def m31_mul(a: int, b: int) -> int:
48
+ return (a * b) % P
49
+
50
+
51
+ def m31_pow(a: int, e: int) -> int:
52
+ return pow(a % P, e, P)
53
+
54
+
55
+ def fingerprint(data: bytes, base: int = 2654435761 % P) -> int:
56
+ """Polynomial (Horner) fingerprint over Mersenne-31. Byte-identical on every platform.
57
+
58
+ acc = ((acc * base) + byte) mod P, iterated over bytes, seeded with the length.
59
+ Deterministic integrity checksum (NOT cryptographically strong).
60
+ """
61
+ acc = (len(data) + 1) % P
62
+ base %= P
63
+ for byte in data:
64
+ acc = (acc * base + (byte + 1)) % P
65
+ return acc
66
+
67
+
68
+ def fingerprint_str(text: str, base: int = 2654435761 % P) -> int:
69
+ return fingerprint(text.encode("utf-8"), base)
70
+
71
+
72
+ # ----------------------------- HMAC challenge-response auth -----------------------------
73
+
74
+ def make_challenge(nbytes: int = 32) -> str:
75
+ """Random challenge (hex). The server stores it and expects a matching response."""
76
+ return os.urandom(nbytes).hex()
77
+
78
+
79
+ def respond(secret: str, challenge: str) -> str:
80
+ """Device side: HMAC-SHA256(secret, challenge) -> hex response."""
81
+ return hmac.new(secret.encode("utf-8"), challenge.encode("utf-8"), hashlib.sha256).hexdigest()
82
+
83
+
84
+ def verify(secret: str, challenge: str, response: str) -> bool:
85
+ """Server side: constant-time check of a device response."""
86
+ return hmac.compare_digest(respond(secret, challenge), response)
87
+
88
+
89
+ # ----------------------------- Merkle commitment -----------------------------
90
+
91
+ def _leaf(data: bytes) -> bytes:
92
+ return hashlib.sha256(b"\x00" + data).digest()
93
+
94
+
95
+ def _node(a: bytes, b: bytes) -> bytes:
96
+ return hashlib.sha256(b"\x01" + a + b).digest()
97
+
98
+
99
+ def _leaves(items: Sequence[bytes]) -> List[bytes]:
100
+ return [_leaf(x if isinstance(x, (bytes, bytearray)) else str(x).encode("utf-8")) for x in items]
101
+
102
+
103
+ def merkle_root(items: Sequence[bytes]) -> str:
104
+ """Commit to an ordered list; returns the root hex. Hides every item."""
105
+ if not items:
106
+ return hashlib.sha256(b"\x02empty").hexdigest()
107
+ level = _leaves(items)
108
+ while len(level) > 1:
109
+ nxt = []
110
+ for i in range(0, len(level), 2):
111
+ if i + 1 < len(level):
112
+ nxt.append(_node(level[i], level[i + 1]))
113
+ else:
114
+ nxt.append(_node(level[i], level[i])) # duplicate last (odd count)
115
+ level = nxt
116
+ return level[0].hex()
117
+
118
+
119
+ def merkle_prove(items: Sequence[bytes], index: int) -> List[Tuple[str, str]]:
120
+ """Membership proof for items[index]: list of (sibling_hex, side) where side in L/R."""
121
+ if index < 0 or index >= len(items):
122
+ raise IndexError("index out of range")
123
+ level = _leaves(items)
124
+ proof: List[Tuple[str, str]] = []
125
+ idx = index
126
+ while len(level) > 1:
127
+ nxt = []
128
+ for i in range(0, len(level), 2):
129
+ left = level[i]
130
+ right = level[i + 1] if i + 1 < len(level) else level[i]
131
+ nxt.append(_node(left, right))
132
+ sib_i = idx ^ 1
133
+ if sib_i >= len(level):
134
+ sib_i = idx # duplicated last node
135
+ if sib_i > idx:
136
+ proof.append((level[sib_i].hex(), "R"))
137
+ else:
138
+ proof.append((level[sib_i].hex(), "L"))
139
+ idx //= 2
140
+ level = nxt
141
+ return proof
142
+
143
+
144
+ def merkle_verify(root: str, item: bytes, proof: Sequence[Tuple[str, str]]) -> bool:
145
+ """Verify item is in the committed set, revealing nothing about the other items."""
146
+ h = _leaf(item if isinstance(item, (bytes, bytearray)) else str(item).encode("utf-8"))
147
+ for sib_hex, side in proof:
148
+ sib = bytes.fromhex(sib_hex)
149
+ if side == "R":
150
+ h = _node(h, sib)
151
+ else:
152
+ h = _node(sib, h)
153
+ return hmac.compare_digest(h.hex(), root)
154
+
155
+
156
+ def pro_tier(license_key: Optional[str]) -> bool:
157
+ """Commercial support/tier flag only. No cryptographic effect. Wire to your license API."""
158
+ return bool(license_key)
159
+
160
+
161
+ __all__ = [
162
+ "P", "m31_add", "m31_mul", "m31_pow", "fingerprint", "fingerprint_str",
163
+ "make_challenge", "respond", "verify",
164
+ "merkle_root", "merkle_prove", "merkle_verify", "pro_tier", "__version__",
165
+ ]
@@ -0,0 +1,74 @@
1
+ Metadata-Version: 2.4
2
+ Name: tiny-zk-auth
3
+ Version: 0.1.0
4
+ Summary: Tiny zero-dependency auth + commitment toolkit: Mersenne-31 cross-platform fingerprint, HMAC-SHA256 device challenge-response, and Merkle membership proofs.
5
+ License: MIT
6
+ Project-URL: Homepage, https://github.com/Amanbhaw/tiny-zk-auth
7
+ Keywords: mersenne31,merkle,commitment,hmac,iot,device-auth,fingerprint,embedded
8
+ Classifier: Development Status :: 4 - Beta
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Topic :: Security :: Cryptography
12
+ Requires-Python: >=3.8
13
+ Description-Content-Type: text/markdown
14
+
15
+ # tiny-zk-auth
16
+
17
+ **Tiny, zero-dependency authentication + commitment toolkit** that runs **byte-identically**
18
+ in the browser, Node.js, Python, and a **$1 microcontroller** (the field core is integer-only).
19
+
20
+ ```
21
+ npm i tiny-zk-auth # Node >= 12 (browser: field + fingerprint need no crypto)
22
+ pip install tiny-zk-auth # Python >= 3.8
23
+ ```
24
+
25
+ Three real pieces:
26
+
27
+ ### 1. Mersenne-31 fingerprint (fast, cross-platform integrity)
28
+ ```js
29
+ const { fingerprintStr } = require('tiny-zk-auth');
30
+ fingerprintStr('hello world'); // 1951127573 — same number in Python, Node, browser, MCU
31
+ ```
32
+ A polynomial (Horner) fingerprint over `p = 2³¹−1`. Deterministic everywhere, great for
33
+ integrity / dedup / cache keys on tiny devices. **Not** a cryptographic hash (≈2⁻³¹ collision) —
34
+ use it for integrity, not as a security boundary.
35
+
36
+ ### 2. HMAC-SHA256 device auth (the real security)
37
+ ```js
38
+ const ch = makeChallenge(); // server
39
+ const resp = respond(secret, ch); // device
40
+ verify(secret, ch, resp); // server -> true
41
+ ```
42
+ Symmetric shared-secret challenge-response, replay-resistant via random challenges.
43
+
44
+ ### 3. Merkle commitment (prove one, hide the rest)
45
+ ```js
46
+ const root = merkleRoot(['alice','bob','carol','dave']); // commit, reveals nothing
47
+ const proof = merkleProve(items, 1);
48
+ merkleVerify(root, 'bob', proof); // true — and the proof leaks no other member
49
+ ```
50
+
51
+ ## Honest scope (read this)
52
+ Despite the name, **this is not a zero-knowledge *proof system*.** A 31-bit field cannot give
53
+ sound ZK. What you get is: a fast cross-platform **fingerprint**, **symmetric (HMAC) auth**, and
54
+ **Merkle commitments** with selective disclosure. That combination is genuinely useful for IoT /
55
+ DePIN / Web3 device identity and tamper-evident sets — just don't market it as "proving statements
56
+ in zero knowledge." If you need real ZK, that's a separate, heavier lattice/elliptic construction.
57
+
58
+ ## Cross-platform guarantee
59
+ The JS and Python packages produce identical fingerprints, HMAC responses and Merkle roots —
60
+ verified by a cross-language test fixture in both test suites.
61
+
62
+ ## Open-core
63
+ - **Free (MIT):** everything above.
64
+ - **Pro:** commercial license + support tier (`proTier` flag). It gates **no cryptography** —
65
+ honesty: the crypto is all free. You sell support, SLAs, and signed release builds.
66
+
67
+ ## Tests
68
+ ```
69
+ cd npm && node test/test.js # 7 tests incl. cross-language fixture
70
+ cd pypi && python3 tests/test_auth.py # 7 tests
71
+ ```
72
+
73
+ ## License
74
+ MIT.
@@ -0,0 +1,8 @@
1
+ README.md
2
+ pyproject.toml
3
+ src/tiny_zk_auth/__init__.py
4
+ src/tiny_zk_auth.egg-info/PKG-INFO
5
+ src/tiny_zk_auth.egg-info/SOURCES.txt
6
+ src/tiny_zk_auth.egg-info/dependency_links.txt
7
+ src/tiny_zk_auth.egg-info/top_level.txt
8
+ tests/test_auth.py
@@ -0,0 +1 @@
1
+ tiny_zk_auth
@@ -0,0 +1,74 @@
1
+ import os
2
+ import sys
3
+
4
+ sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src"))
5
+
6
+ import tiny_zk_auth as t # noqa: E402
7
+
8
+
9
+ def test_field_basic():
10
+ assert t.m31_add(t.P - 1, 2) == 1
11
+ assert t.m31_mul(t.P - 1, t.P - 1) == 1 # (-1)*(-1)=1
12
+ assert t.m31_pow(2, 31) == 1 # 2^31 ≡ 1 (mod 2^31-1)
13
+ assert t.m31_pow(2, 32) == 2 # 2^32 ≡ 2 (mod 2^31-1)
14
+
15
+
16
+ def test_fingerprint_deterministic_and_sensitive():
17
+ a = t.fingerprint_str("hello world")
18
+ assert a == t.fingerprint_str("hello world") # deterministic
19
+ assert a != t.fingerprint_str("hello worle") # 1-char change -> different
20
+ assert 0 <= a < t.P
21
+ assert t.fingerprint_str("ab") != t.fingerprint_str("ba") # order matters
22
+
23
+
24
+ def test_auth_roundtrip():
25
+ secret = "device-shared-secret"
26
+ ch = t.make_challenge()
27
+ resp = t.respond(secret, ch)
28
+ assert t.verify(secret, ch, resp)
29
+ assert not t.verify("wrong-secret", ch, resp)
30
+ assert not t.verify(secret, t.make_challenge(), resp) # replay with new challenge fails
31
+
32
+
33
+ def test_merkle_membership():
34
+ items = [b"alice", b"bob", b"carol", b"dave", b"erin"]
35
+ root = t.merkle_root(items)
36
+ for i, it in enumerate(items):
37
+ proof = t.merkle_prove(items, i)
38
+ assert t.merkle_verify(root, it, proof)
39
+
40
+
41
+ def test_merkle_rejects_non_member():
42
+ items = [b"alice", b"bob", b"carol", b"dave"]
43
+ root = t.merkle_root(items)
44
+ proof = t.merkle_prove(items, 1)
45
+ assert t.merkle_verify(root, b"bob", proof)
46
+ assert not t.merkle_verify(root, b"mallory", proof) # wrong item
47
+ # tampered proof fails
48
+ if proof:
49
+ bad = [("00" * 32, proof[0][1])] + list(proof[1:])
50
+ assert not t.merkle_verify(root, b"bob", bad)
51
+
52
+
53
+ def test_merkle_hides_others():
54
+ # The proof for one item must not contain any other item's plaintext.
55
+ items = [b"SECRET-alice", b"SECRET-bob", b"SECRET-carol", b"SECRET-dave"]
56
+ proof = t.merkle_prove(items, 0)
57
+ blob = str(proof)
58
+ for other in items[1:]:
59
+ assert other.decode() not in blob
60
+
61
+
62
+ def test_odd_count():
63
+ items = [b"a", b"b", b"c"]
64
+ root = t.merkle_root(items)
65
+ for i, it in enumerate(items):
66
+ assert t.merkle_verify(root, it, t.merkle_prove(items, i))
67
+
68
+
69
+ if __name__ == "__main__":
70
+ fns = [v for k, v in sorted(globals().items()) if k.startswith("test_") and callable(v)]
71
+ for fn in fns:
72
+ fn()
73
+ print(f" ok {fn.__name__}")
74
+ print(f"\n{len(fns)} tests passed")