tiny-zk-auth 0.0.0-stage → 0.2.0

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.
Files changed (3) hide show
  1. package/package.json +10 -4
  2. package/src/index.js +131 -0
  3. package/README.md +0 -3
package/package.json CHANGED
@@ -1,6 +1,12 @@
1
1
  {
2
2
  "name": "tiny-zk-auth",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.2.0",
4
+ "description": "Tiny zero-dependency auth + commitment toolkit: Mersenne-31 cross-platform fingerprint, HMAC-SHA256 device challenge-response, and Merkle membership proofs. Runs identically in browser, Node, Python and a $1 MCU.",
5
+ "main": "src/index.js",
6
+ "files": ["src/"],
7
+ "scripts": { "test": "node test/test.js" },
8
+ "keywords": ["mersenne31", "merkle", "commitment", "hmac", "iot", "device-auth", "fingerprint", "depin", "zero-dependency", "embedded"],
9
+ "license": "AGPL-3.0-or-later",
10
+ "engines": { "node": ">=12" },
11
+ "repository": { "type": "git", "url": "https://github.com/Amanbhaw/tiny-zk-auth" }
12
+ }
package/src/index.js ADDED
@@ -0,0 +1,131 @@
1
+ 'use strict';
2
+ /**
3
+ * tiny-zk-auth — tiny, zero-dependency authentication + commitment toolkit.
4
+ *
5
+ * Byte-identical in the browser, Node.js, Python, and a $1 MCU (integer-only field core).
6
+ * 1. Mersenne-31 field + fingerprint() — fast cross-platform integrity checksum (NOT crypto-strength).
7
+ * 2. HMAC-SHA256 challenge-response — real symmetric device auth (make/respond/verify).
8
+ * 3. Merkle commitment — prove one member, hide the rest.
9
+ *
10
+ * HONEST: this is commitments + symmetric auth, NOT a zero-knowledge proof system
11
+ * (a 31-bit field cannot give sound ZK). See README.
12
+ *
13
+ * In Node this uses the built-in `crypto`. In a browser bundle, provide a SHA-256/HMAC shim
14
+ * or build only the field + fingerprint (which need no crypto). MIT.
15
+ */
16
+ let nodeCrypto = null;
17
+ try { nodeCrypto = require('crypto'); } catch (_) { /* browser: HMAC parts unavailable */ }
18
+
19
+ const VERSION = '0.1.0';
20
+ const P = 2 ** 31 - 1; // 2147483647 (NB: `1 << 31` is negative in JS — do not use it)
21
+
22
+ // ----------------------------- Mersenne-31 field (BigInt mul for exact 31-bit arithmetic) ---
23
+ const PB = BigInt(P);
24
+ function m31Add(a, b) { return ((a % P) + (b % P)) % P; }
25
+ function m31Mul(a, b) { return Number((BigInt(a) * BigInt(b)) % PB); }
26
+ function m31Pow(a, e) {
27
+ let base = ((a % P) + P) % P;
28
+ let r = 1;
29
+ let exp = e;
30
+ while (exp > 0) {
31
+ if (exp & 1) r = m31Mul(r, base);
32
+ base = m31Mul(base, base);
33
+ exp = Math.floor(exp / 2);
34
+ }
35
+ return r;
36
+ }
37
+
38
+ function fingerprintBytes(bytes, base) {
39
+ base = (base === undefined ? 2654435761 % P : base) % P;
40
+ let acc = (bytes.length + 1) % P;
41
+ const bb = BigInt(base);
42
+ let accB = BigInt(acc);
43
+ for (let i = 0; i < bytes.length; i++) {
44
+ accB = (accB * bb + BigInt((bytes[i] & 0xff) + 1)) % PB;
45
+ }
46
+ return Number(accB);
47
+ }
48
+
49
+ function fingerprintStr(text, base) {
50
+ return fingerprintBytes(Buffer.from(text, 'utf8'), base);
51
+ }
52
+
53
+ // ----------------------------- HMAC challenge-response -----------------------------
54
+ function requireCrypto() {
55
+ if (!nodeCrypto) throw new Error('tiny-zk-auth: HMAC auth needs Node crypto or a SHA-256 shim');
56
+ return nodeCrypto;
57
+ }
58
+ function makeChallenge(nbytes = 32) {
59
+ return requireCrypto().randomBytes(nbytes).toString('hex');
60
+ }
61
+ function respond(secret, challenge) {
62
+ return requireCrypto().createHmac('sha256', secret).update(challenge, 'utf8').digest('hex');
63
+ }
64
+ function verify(secret, challenge, response) {
65
+ const c = requireCrypto();
66
+ const expected = c.createHmac('sha256', secret).update(challenge, 'utf8').digest('hex');
67
+ if (typeof response !== 'string' || response.length !== expected.length) return false;
68
+ return c.timingSafeEqual(Buffer.from(expected), Buffer.from(response));
69
+ }
70
+
71
+ // ----------------------------- Merkle commitment -----------------------------
72
+ function sha256(buf) { return requireCrypto().createHash('sha256').update(buf).digest(); }
73
+ function toBuf(x) { return Buffer.isBuffer(x) ? x : Buffer.from(String(x), 'utf8'); }
74
+ function leaf(data) { return sha256(Buffer.concat([Buffer.from([0]), toBuf(data)])); }
75
+ function node(a, b) { return sha256(Buffer.concat([Buffer.from([1]), a, b])); }
76
+
77
+ function merkleRoot(items) {
78
+ if (!items.length) return sha256(Buffer.from('\x02empty', 'utf8')).toString('hex');
79
+ let level = items.map(leaf);
80
+ while (level.length > 1) {
81
+ const next = [];
82
+ for (let i = 0; i < level.length; i += 2) {
83
+ const left = level[i];
84
+ const right = i + 1 < level.length ? level[i + 1] : level[i];
85
+ next.push(node(left, right));
86
+ }
87
+ level = next;
88
+ }
89
+ return level[0].toString('hex');
90
+ }
91
+
92
+ function merkleProve(items, index) {
93
+ if (index < 0 || index >= items.length) throw new RangeError('index out of range');
94
+ let level = items.map(leaf);
95
+ let idx = index;
96
+ const proof = [];
97
+ while (level.length > 1) {
98
+ const next = [];
99
+ for (let i = 0; i < level.length; i += 2) {
100
+ const left = level[i];
101
+ const right = i + 1 < level.length ? level[i + 1] : level[i];
102
+ next.push(node(left, right));
103
+ }
104
+ let sib = idx ^ 1;
105
+ if (sib >= level.length) sib = idx;
106
+ proof.push([level[sib].toString('hex'), sib > idx ? 'R' : 'L']);
107
+ idx = Math.floor(idx / 2);
108
+ level = next;
109
+ }
110
+ return proof;
111
+ }
112
+
113
+ function merkleVerify(root, item, proof) {
114
+ let h = leaf(item);
115
+ for (const [sibHex, side] of proof) {
116
+ const sib = Buffer.from(sibHex, 'hex');
117
+ h = side === 'R' ? node(h, sib) : node(sib, h);
118
+ }
119
+ const hex = h.toString('hex');
120
+ if (hex.length !== root.length) return false;
121
+ return requireCrypto().timingSafeEqual(Buffer.from(hex), Buffer.from(root));
122
+ }
123
+
124
+ function proTier(licenseKey) { return !!licenseKey; } // commercial flag only; no crypto effect
125
+
126
+ module.exports = {
127
+ VERSION, P,
128
+ m31Add, m31Mul, m31Pow, fingerprintBytes, fingerprintStr,
129
+ makeChallenge, respond, verify,
130
+ merkleRoot, merkleProve, merkleVerify, proTier,
131
+ };
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.