signet-protocol 0.1.0 → 1.0.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "signet-protocol",
3
- "version": "0.1.0",
3
+ "version": "1.0.0",
4
4
  "description": "Decentralised identity verification protocol for Nostr",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -46,7 +46,7 @@
46
46
  },
47
47
  "repository": {
48
48
  "type": "git",
49
- "url": "https://github.com/forgesworn/signet-protocol.git"
49
+ "url": "https://github.com/forgesworn/signet.git"
50
50
  },
51
51
  "dependencies": {
52
52
  "@forgesworn/range-proof": "^2.0.0",
@@ -73,4 +73,4 @@
73
73
  "type": "lightning",
74
74
  "url": "lightning:thedonkey@strike.me"
75
75
  }
76
- }
76
+ }
@@ -1,43 +0,0 @@
1
- /** Encode a 32-byte private key as an nsec bech32 string (NIP-19) */
2
- export declare function encodeNsec(privateKey: string): string;
3
- /** Encode a 32-byte public key as an npub bech32 string (NIP-19) */
4
- export declare function encodeNpub(publicKey: string): string;
5
- /** Decode an nsec bech32 string to private + public key hex (NIP-19) */
6
- export declare function decodeNsec(nsec: string): {
7
- privateKey: string;
8
- publicKey: string;
9
- };
10
- /** NIP-06 derivation path: m/44'/1237'/0'/0/0 */
11
- export declare const NIP06_DERIVATION_PATH = "m/44'/1237'/0'/0/0";
12
- /** Parse a BIP-32 derivation path into numeric indices.
13
- * Retained for backwards compatibility — @scure/bip32 handles paths internally. */
14
- export declare function parsePath(path: string): number[];
15
- /** Derive a private key at the given BIP-32 path from a seed.
16
- * Uses @scure/bip32 (audited BIP-32 implementation by paulmillr). */
17
- export declare function deriveKeyFromSeed(seed: Uint8Array, path: string): Uint8Array;
18
- /** Derive a Nostr keypair from a BIP-39 mnemonic using NIP-06 path.
19
- *
20
- * SECURITY NOTE: Intermediate Uint8Arrays (seed, key bytes) are zeroed after use,
21
- * but the returned hex strings are JS primitives and cannot be wiped from memory.
22
- * This is a fundamental limitation of the JavaScript runtime. */
23
- export declare function deriveNostrKeyPair(mnemonic: string, passphrase?: string): {
24
- privateKey: string;
25
- publicKey: string;
26
- };
27
- /** Create a full Signet identity from a mnemonic.
28
- *
29
- * WARNING: The returned object contains the mnemonic alongside the private key.
30
- * Do not serialise, log, or persist this object without first removing the
31
- * mnemonic. The caller is responsible for discarding the mnemonic as quickly
32
- * as possible. Mnemonics must never be stored in plaintext (use crypto-store.ts). */
33
- export declare function createIdentityFromMnemonic(mnemonic: string, passphrase?: string): {
34
- mnemonic: string;
35
- privateKey: string;
36
- publicKey: string;
37
- };
38
- /** Derive a child account keypair (e.g., for a child's account) at a given account index */
39
- export declare function deriveChildAccount(mnemonic: string, accountIndex: number, passphrase?: string): {
40
- privateKey: string;
41
- publicKey: string;
42
- };
43
- //# sourceMappingURL=key-derivation.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"key-derivation.d.ts","sourceRoot":"","sources":["../src/key-derivation.ts"],"names":[],"mappings":"AAsGA,qEAAqE;AACrE,wBAAgB,UAAU,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,CAKrD;AAED,oEAAoE;AACpE,wBAAgB,UAAU,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CAKpD;AAED,wEAAwE;AACxE,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG;IAAE,UAAU,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,CAUlF;AAED,iDAAiD;AACjD,eAAO,MAAM,qBAAqB,uBAAuB,CAAC;AAQ1D;oFACoF;AACpF,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,CAiBhD;AAED;sEACsE;AACtE,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,GAAG,UAAU,CAU5E;AAED;;;;kEAIkE;AAClE,wBAAgB,kBAAkB,CAChC,QAAQ,EAAE,MAAM,EAChB,UAAU,CAAC,EAAE,MAAM,GAClB;IAAE,UAAU,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,CAa3C;AAED;;;;;qFAKqF;AACrF,wBAAgB,0BAA0B,CACxC,QAAQ,EAAE,MAAM,EAChB,UAAU,CAAC,EAAE,MAAM,GAClB;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,CAG7D;AAED,4FAA4F;AAC5F,wBAAgB,kBAAkB,CAChC,QAAQ,EAAE,MAAM,EAChB,YAAY,EAAE,MAAM,EACpB,UAAU,CAAC,EAAE,MAAM,GAClB;IAAE,UAAU,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,CAe3C"}
@@ -1,212 +0,0 @@
1
- // NIP-06 Nostr Key Derivation & NIP-19 nsec/npub encoding
2
- // Derives Nostr keypairs from BIP-39 mnemonics via @scure/bip32
3
- // Also provides nsec/npub bech32 encoding (NIP-19)
4
- import { secp256k1 } from '@noble/curves/secp256k1';
5
- import { bytesToHex, hexToBytes } from '@noble/hashes/utils';
6
- import { mnemonicToSeedSync } from '@scure/bip39';
7
- import { HDKey } from '@scure/bip32';
8
- import { zeroBytes } from './utils.js';
9
- import { SignetValidationError, SignetCryptoError } from './errors.js';
10
- // --- Bech32 encoding/decoding (BIP-173) for NIP-19 nsec/npub ---
11
- const BECH32_CHARSET = 'qpzry9x8gf2tvdw0s3jn54khce6mua7l';
12
- function bech32Polymod(values) {
13
- const GEN = [0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3];
14
- let chk = 1;
15
- for (const v of values) {
16
- const b = chk >> 25;
17
- chk = ((chk & 0x1ffffff) << 5) ^ v;
18
- for (let i = 0; i < 5; i++) {
19
- if ((b >> i) & 1)
20
- chk ^= GEN[i];
21
- }
22
- }
23
- return chk;
24
- }
25
- function bech32HrpExpand(hrp) {
26
- const ret = [];
27
- for (let i = 0; i < hrp.length; i++)
28
- ret.push(hrp.charCodeAt(i) >> 5);
29
- ret.push(0);
30
- for (let i = 0; i < hrp.length; i++)
31
- ret.push(hrp.charCodeAt(i) & 31);
32
- return ret;
33
- }
34
- function bech32CreateChecksum(hrp, data) {
35
- const values = bech32HrpExpand(hrp).concat(data).concat([0, 0, 0, 0, 0, 0]);
36
- const polymod = bech32Polymod(values) ^ 1;
37
- const ret = [];
38
- for (let i = 0; i < 6; i++)
39
- ret.push((polymod >> (5 * (5 - i))) & 31);
40
- return ret;
41
- }
42
- function bech32VerifyChecksum(hrp, data) {
43
- return bech32Polymod(bech32HrpExpand(hrp).concat(data)) === 1;
44
- }
45
- function bech32Encode(hrp, data) {
46
- const checksum = bech32CreateChecksum(hrp, data);
47
- const combined = data.concat(checksum);
48
- let ret = hrp + '1';
49
- for (const d of combined)
50
- ret += BECH32_CHARSET[d];
51
- return ret;
52
- }
53
- function bech32Decode(str) {
54
- const lower = str.toLowerCase();
55
- if (lower !== str && str.toUpperCase() !== str) {
56
- throw new SignetValidationError('Mixed-case bech32 string');
57
- }
58
- const s = lower;
59
- const pos = s.lastIndexOf('1');
60
- if (pos < 1 || pos + 7 > s.length || s.length > 90) {
61
- throw new SignetValidationError('Invalid bech32 string');
62
- }
63
- const hrp = s.slice(0, pos);
64
- const dataChars = s.slice(pos + 1);
65
- const data = [];
66
- for (const c of dataChars) {
67
- const idx = BECH32_CHARSET.indexOf(c);
68
- if (idx === -1)
69
- throw new SignetValidationError(`Invalid bech32 character: ${c}`);
70
- data.push(idx);
71
- }
72
- if (!bech32VerifyChecksum(hrp, data)) {
73
- throw new SignetValidationError('Invalid bech32 checksum');
74
- }
75
- return { hrp, data: data.slice(0, data.length - 6) };
76
- }
77
- function convertBits(data, fromBits, toBits, pad) {
78
- let acc = 0;
79
- let bits = 0;
80
- const ret = [];
81
- const maxv = (1 << toBits) - 1;
82
- for (const value of data) {
83
- if (value < 0 || value >> fromBits !== 0)
84
- throw new SignetValidationError('Invalid value for convertBits');
85
- acc = (acc << fromBits) | value;
86
- bits += fromBits;
87
- while (bits >= toBits) {
88
- bits -= toBits;
89
- ret.push((acc >> bits) & maxv);
90
- }
91
- }
92
- if (pad) {
93
- if (bits > 0)
94
- ret.push((acc << (toBits - bits)) & maxv);
95
- }
96
- else if (bits >= fromBits || ((acc << (toBits - bits)) & maxv) !== 0) {
97
- throw new SignetValidationError('Invalid padding in convertBits');
98
- }
99
- return ret;
100
- }
101
- /** Encode a 32-byte private key as an nsec bech32 string (NIP-19) */
102
- export function encodeNsec(privateKey) {
103
- const bytes = hexToBytes(privateKey);
104
- if (bytes.length !== 32)
105
- throw new SignetValidationError('Private key must be 32 bytes');
106
- const data5bit = convertBits(Array.from(bytes), 8, 5, true);
107
- return bech32Encode('nsec', data5bit);
108
- }
109
- /** Encode a 32-byte public key as an npub bech32 string (NIP-19) */
110
- export function encodeNpub(publicKey) {
111
- const bytes = hexToBytes(publicKey);
112
- if (bytes.length !== 32)
113
- throw new SignetValidationError('Public key must be 32 bytes');
114
- const data5bit = convertBits(Array.from(bytes), 8, 5, true);
115
- return bech32Encode('npub', data5bit);
116
- }
117
- /** Decode an nsec bech32 string to private + public key hex (NIP-19) */
118
- export function decodeNsec(nsec) {
119
- const { hrp, data } = bech32Decode(nsec);
120
- if (hrp !== 'nsec')
121
- throw new SignetValidationError(`Expected nsec prefix, got ${hrp}`);
122
- const bytes = convertBits(data, 5, 8, false);
123
- if (bytes.length !== 32)
124
- throw new SignetValidationError('Invalid nsec: decoded to wrong length');
125
- const privateKey = bytesToHex(new Uint8Array(bytes));
126
- // Derive x-only public key from private key
127
- const fullPubkey = secp256k1.getPublicKey(new Uint8Array(bytes), true);
128
- const publicKey = bytesToHex(fullPubkey.slice(1));
129
- return { privateKey, publicKey };
130
- }
131
- /** NIP-06 derivation path: m/44'/1237'/0'/0/0 */
132
- export const NIP06_DERIVATION_PATH = "m/44'/1237'/0'/0/0";
133
- /** Maximum derivation path depth — prevents CPU-bound DoS on untrusted paths */
134
- const MAX_PATH_DEPTH = 10;
135
- /** Hardened offset for BIP-32 */
136
- const HARDENED_OFFSET = 0x80000000;
137
- /** Parse a BIP-32 derivation path into numeric indices.
138
- * Retained for backwards compatibility — @scure/bip32 handles paths internally. */
139
- export function parsePath(path) {
140
- if (!path.startsWith('m/')) {
141
- throw new SignetValidationError('Derivation path must start with m/');
142
- }
143
- const segments = path.slice(2).split('/');
144
- if (segments.length > MAX_PATH_DEPTH) {
145
- throw new SignetValidationError(`Derivation path too deep: ${segments.length} levels (max ${MAX_PATH_DEPTH})`);
146
- }
147
- return segments
148
- .map((segment) => {
149
- const hardened = segment.endsWith("'") || segment.endsWith('h');
150
- const index = parseInt(hardened ? segment.slice(0, -1) : segment, 10);
151
- if (isNaN(index) || index < 0) {
152
- throw new SignetValidationError(`Invalid path segment: ${segment}`);
153
- }
154
- return hardened ? index + HARDENED_OFFSET : index;
155
- });
156
- }
157
- /** Derive a private key at the given BIP-32 path from a seed.
158
- * Uses @scure/bip32 (audited BIP-32 implementation by paulmillr). */
159
- export function deriveKeyFromSeed(seed, path) {
160
- // Validate path depth before delegating to @scure/bip32
161
- parsePath(path);
162
- const master = HDKey.fromMasterSeed(seed);
163
- const child = master.derive(path);
164
- if (!child.privateKey) {
165
- throw new SignetCryptoError('Key derivation produced no private key');
166
- }
167
- // Return a copy — HDKey owns its internal buffer
168
- return Uint8Array.from(child.privateKey);
169
- }
170
- /** Derive a Nostr keypair from a BIP-39 mnemonic using NIP-06 path.
171
- *
172
- * SECURITY NOTE: Intermediate Uint8Arrays (seed, key bytes) are zeroed after use,
173
- * but the returned hex strings are JS primitives and cannot be wiped from memory.
174
- * This is a fundamental limitation of the JavaScript runtime. */
175
- export function deriveNostrKeyPair(mnemonic, passphrase) {
176
- const seed = mnemonicToSeedSync(mnemonic, passphrase);
177
- const privateKeyBytes = deriveKeyFromSeed(seed, NIP06_DERIVATION_PATH);
178
- zeroBytes(seed);
179
- const privateKey = bytesToHex(privateKeyBytes);
180
- // x-only public key (BIP-340 / Nostr convention)
181
- const fullPubkey = secp256k1.getPublicKey(privateKeyBytes, true); // 33 bytes compressed
182
- // Drop the prefix byte to get x-only (32 bytes)
183
- const publicKey = bytesToHex(fullPubkey.slice(1));
184
- zeroBytes(privateKeyBytes);
185
- return { privateKey, publicKey };
186
- }
187
- /** Create a full Signet identity from a mnemonic.
188
- *
189
- * WARNING: The returned object contains the mnemonic alongside the private key.
190
- * Do not serialise, log, or persist this object without first removing the
191
- * mnemonic. The caller is responsible for discarding the mnemonic as quickly
192
- * as possible. Mnemonics must never be stored in plaintext (use crypto-store.ts). */
193
- export function createIdentityFromMnemonic(mnemonic, passphrase) {
194
- const { privateKey, publicKey } = deriveNostrKeyPair(mnemonic, passphrase);
195
- return { mnemonic, privateKey, publicKey };
196
- }
197
- /** Derive a child account keypair (e.g., for a child's account) at a given account index */
198
- export function deriveChildAccount(mnemonic, accountIndex, passphrase) {
199
- if (!Number.isSafeInteger(accountIndex) || accountIndex < 0 || accountIndex > 0x7fffffff) {
200
- throw new SignetValidationError('accountIndex must be an integer in [0, 2^31 - 1]');
201
- }
202
- const seed = mnemonicToSeedSync(mnemonic, passphrase);
203
- const path = `m/44'/1237'/${accountIndex}'/0/0`;
204
- const privateKeyBytes = deriveKeyFromSeed(seed, path);
205
- zeroBytes(seed);
206
- const privateKey = bytesToHex(privateKeyBytes);
207
- const fullPubkey = secp256k1.getPublicKey(privateKeyBytes, true);
208
- const publicKey = bytesToHex(fullPubkey.slice(1));
209
- zeroBytes(privateKeyBytes);
210
- return { privateKey, publicKey };
211
- }
212
- //# sourceMappingURL=key-derivation.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"key-derivation.js","sourceRoot":"","sources":["../src/key-derivation.ts"],"names":[],"mappings":"AAAA,0DAA0D;AAC1D,gEAAgE;AAChE,mDAAmD;AAEnD,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB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ENTIONAL DUPLICATION: Dominion Protocol has its own GF(256) Shamir
5
- * implementation. The low-level maths is identical but the protocols serve
6
- * different purposes:
7
- *
8
- * - Signet: splits identity keys across PEOPLE (guardian recovery).
9
- * Shares are encoded as BIP-39 words for human exchange.
10
- * - Dominion: splits content keys across MACHINES (warden relays).
11
- * Shares are raw bytes for relay distribution.
12
- *
13
- * Coupling the protocols via a shared dependency is worse than ~100 lines
14
- * of duplicated arithmetic. GF(256) is stable and will never change.
15
- *
16
- * See: trott-business/docs/plans/2026-03-12-fathom-alpha-live-design.md
17
- */
18
- export interface ShamirShare {
19
- id: number;
20
- data: Uint8Array;
21
- }
22
- /** Addition in GF(256) is XOR */
23
- export declare function gf256Add(a: number, b: number): number;
24
- /** Multiplication in GF(256) using log/exp tables */
25
- export declare function gf256Mul(a: number, b: number): number;
26
- /** Multiplicative inverse in GF(256) */
27
- export declare function gf256Inv(a: number): number;
28
- /**
29
- * Split a secret into shares using Shamir's Secret Sharing over GF(256).
30
- *
31
- * @param secret The secret bytes to split
32
- * @param threshold Minimum shares needed to reconstruct (>= 2)
33
- * @param shares Total number of shares to create (>= threshold, <= 255)
34
- * @returns Array of ShamirShare objects
35
- */
36
- export declare function splitSecret(secret: Uint8Array, threshold: number, shares: number): ShamirShare[];
37
- /**
38
- * Reconstruct a secret from shares using Lagrange interpolation over GF(256).
39
- *
40
- * @param shares Array of shares (at least `threshold` shares)
41
- * @param threshold The threshold used during splitting
42
- * @returns The reconstructed secret bytes
43
- */
44
- export declare function reconstructSecret(shares: ShamirShare[], threshold: number): Uint8Array;
45
- /**
46
- * Encode a share as BIP-39 words.
47
- * Prepends the share ID byte to the data, then converts to 11-bit groups.
48
- */
49
- export declare function shareToWords(share: ShamirShare): string[];
50
- /**
51
- * Decode BIP-39 words back to a share.
52
- * Reverses the encoding from shareToWords.
53
- */
54
- export declare function wordsToShare(words: string[]): ShamirShare;
55
- //# sourceMappingURL=shamir.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"shamir.d.ts","sourceRoot":"","sources":["../src/shamir.ts"],"names":[],"mappings":"AAQA;;;;;;;;;;;;;;;;GAgBG;AAMH,MAAM,WAAW,WAAW;IAC1B,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,UAAU,CAAC;CAClB;AAwCD,iCAAiC;AACjC,wBAAgB,QAAQ,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAErD;AAED,qDAAqD;AACrD,wBAAgB,QAAQ,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAGrD;AAED,wCAAwC;AACxC,wBAAgB,QAAQ,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAG1C;AAkBD;;;;;;;GAOG;AACH,wBAAgB,WAAW,CACzB,MAAM,EAAE,UAAU,EAClB,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,MAAM,GACb,WAAW,EAAE,CAwCf;AAED;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAC/B,MAAM,EAAE,WAAW,EAAE,EACrB,SAAS,EAAE,MAAM,GAChB,UAAU,CAuDZ;AAMD;;;GAGG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,WAAW,GAAG,MAAM,EAAE,CA+BzD;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,WAAW,CAkDzD"}
package/dist/shamir.js DELETED
@@ -1,253 +0,0 @@
1
- // Shamir's Secret Sharing over GF(256)
2
- // Split secrets into threshold-of-n shares using polynomial interpolation
3
- import { randomBytes } from '@noble/hashes/utils';
4
- import { wordlist as BIP39_WORDLIST } from '@scure/bip39/wordlists/english.js';
5
- import { zeroBytes } from './utils.js';
6
- import { SignetValidationError, SignetCryptoError } from './errors.js';
7
- // ---------------------------------------------------------------------------
8
- // GF(256) Arithmetic — irreducible polynomial 0x11b (same as AES)
9
- // ---------------------------------------------------------------------------
10
- const IRREDUCIBLE = 0x11b;
11
- const GENERATOR = 0x03;
12
- /** Log table: log_g(i) for i in [0..255]. LOG[0] is unused. */
13
- const LOG = new Uint8Array(256);
14
- /** Exp table: g^i for i in [0..255]. EXP[255] wraps to EXP[0]. */
15
- const EXP = new Uint8Array(256);
16
- /** Carryless multiplication used only during table construction */
17
- function gf256MulSlow(a, b) {
18
- let result = 0;
19
- let aa = a;
20
- let bb = b;
21
- while (bb > 0) {
22
- if (bb & 1)
23
- result ^= aa;
24
- aa <<= 1;
25
- if (aa & 0x100)
26
- aa ^= IRREDUCIBLE;
27
- bb >>= 1;
28
- }
29
- return result;
30
- }
31
- // Build log/exp tables at module load time using generator 0x03
32
- {
33
- let val = 1;
34
- for (let i = 0; i < 255; i++) {
35
- EXP[i] = val;
36
- LOG[val] = i;
37
- val = gf256MulSlow(val, GENERATOR);
38
- }
39
- // Wrap: makes modular indexing simpler
40
- EXP[255] = EXP[0];
41
- }
42
- /** Addition in GF(256) is XOR */
43
- export function gf256Add(a, b) {
44
- return a ^ b;
45
- }
46
- /** Multiplication in GF(256) using log/exp tables */
47
- export function gf256Mul(a, b) {
48
- if (a === 0 || b === 0)
49
- return 0;
50
- return EXP[(LOG[a] + LOG[b]) % 255];
51
- }
52
- /** Multiplicative inverse in GF(256) */
53
- export function gf256Inv(a) {
54
- if (a === 0)
55
- throw new SignetCryptoError('No inverse for zero in GF(256)');
56
- return EXP[(255 - LOG[a]) % 255];
57
- }
58
- // ---------------------------------------------------------------------------
59
- // Shamir's Secret Sharing
60
- // ---------------------------------------------------------------------------
61
- /**
62
- * Evaluate a polynomial at x in GF(256) using Horner's method.
63
- * coeffs[0] is the constant term (the secret byte).
64
- */
65
- function evalPoly(coeffs, x) {
66
- let result = 0;
67
- for (let i = coeffs.length - 1; i >= 0; i--) {
68
- result = gf256Add(gf256Mul(result, x), coeffs[i]);
69
- }
70
- return result;
71
- }
72
- /**
73
- * Split a secret into shares using Shamir's Secret Sharing over GF(256).
74
- *
75
- * @param secret The secret bytes to split
76
- * @param threshold Minimum shares needed to reconstruct (>= 2)
77
- * @param shares Total number of shares to create (>= threshold, <= 255)
78
- * @returns Array of ShamirShare objects
79
- */
80
- export function splitSecret(secret, threshold, shares) {
81
- if (threshold < 2) {
82
- throw new SignetValidationError('Threshold must be at least 2');
83
- }
84
- if (shares < threshold) {
85
- throw new SignetValidationError('Number of shares must be >= threshold');
86
- }
87
- if (shares > 255) {
88
- throw new SignetValidationError('Number of shares must be <= 255');
89
- }
90
- const secretLen = secret.length;
91
- const result = [];
92
- // Initialize share data arrays
93
- for (let i = 0; i < shares; i++) {
94
- result.push({ id: i + 1, data: new Uint8Array(secretLen) });
95
- }
96
- // For each byte of the secret, build a random polynomial and evaluate
97
- for (let byteIdx = 0; byteIdx < secretLen; byteIdx++) {
98
- // coeffs[0] = secret byte, coeffs[1..threshold-1] = random
99
- const coeffs = new Uint8Array(threshold);
100
- coeffs[0] = secret[byteIdx];
101
- const rand = randomBytes(threshold - 1);
102
- for (let j = 1; j < threshold; j++) {
103
- coeffs[j] = rand[j - 1];
104
- }
105
- // Evaluate at x = 1, 2, ..., shares
106
- for (let i = 0; i < shares; i++) {
107
- result[i].data[byteIdx] = evalPoly(coeffs, i + 1);
108
- }
109
- zeroBytes(coeffs);
110
- zeroBytes(rand);
111
- }
112
- return result;
113
- }
114
- /**
115
- * Reconstruct a secret from shares using Lagrange interpolation over GF(256).
116
- *
117
- * @param shares Array of shares (at least `threshold` shares)
118
- * @param threshold The threshold used during splitting
119
- * @returns The reconstructed secret bytes
120
- */
121
- export function reconstructSecret(shares, threshold) {
122
- if (shares.length < threshold) {
123
- throw new SignetValidationError(`Need at least ${threshold} shares, got ${shares.length}`);
124
- }
125
- // Use only the first `threshold` shares
126
- const used = shares.slice(0, threshold);
127
- // Validate no duplicate share IDs
128
- const ids = new Set(used.map(s => s.id));
129
- if (ids.size !== used.length) {
130
- throw new SignetValidationError('Duplicate share IDs detected — each share must have a unique ID');
131
- }
132
- // Reject invalid share IDs: x must be in [1, 255] for GF(256)
133
- for (const share of used) {
134
- if (share.id === 0) {
135
- throw new SignetValidationError('Invalid share ID: 0 is not a valid x-coordinate');
136
- }
137
- if (share.id > 255) {
138
- throw new SignetValidationError('Invalid share ID: must be in [1, 255] for GF(256)');
139
- }
140
- }
141
- const secretLen = used[0].data.length;
142
- for (const share of used) {
143
- if (share.data.length !== secretLen) {
144
- throw new SignetValidationError('Inconsistent share lengths — shares may be from different secrets');
145
- }
146
- }
147
- const result = new Uint8Array(secretLen);
148
- // Lagrange interpolation at x = 0 for each byte position
149
- for (let byteIdx = 0; byteIdx < secretLen; byteIdx++) {
150
- let value = 0;
151
- for (let i = 0; i < threshold; i++) {
152
- const xi = used[i].id;
153
- const yi = used[i].data[byteIdx];
154
- // Lagrange basis l_i(0) = product of x_j / (x_i ^ x_j) for j != i
155
- // In GF(256): subtraction = addition = XOR
156
- let basis = 1;
157
- for (let j = 0; j < threshold; j++) {
158
- if (i === j)
159
- continue;
160
- const xj = used[j].id;
161
- basis = gf256Mul(basis, gf256Mul(xj, gf256Inv(gf256Add(xi, xj))));
162
- }
163
- value = gf256Add(value, gf256Mul(yi, basis));
164
- }
165
- result[byteIdx] = value;
166
- }
167
- return result;
168
- }
169
- // ---------------------------------------------------------------------------
170
- // BIP-39 Word Encoding
171
- // ---------------------------------------------------------------------------
172
- /**
173
- * Encode a share as BIP-39 words.
174
- * Prepends the share ID byte to the data, then converts to 11-bit groups.
175
- */
176
- export function shareToWords(share) {
177
- // Prepend ID byte to data
178
- const bytes = new Uint8Array(1 + share.data.length);
179
- bytes[0] = share.id;
180
- bytes.set(share.data, 1);
181
- // Stream bytes into 11-bit word indices using Number (safe up to 53 bits)
182
- // We extract words as soon as we have 11 bits, keeping accumulator small
183
- const words = [];
184
- let bits = 0;
185
- let accumulator = 0;
186
- for (const byte of bytes) {
187
- // accumulator has at most 10 bits here (< 11), so (10 + 8 = 18) fits safely in 32-bit
188
- accumulator = ((accumulator << 8) | byte) >>> 0; // >>> 0 ensures unsigned 32-bit
189
- bits += 8;
190
- while (bits >= 11) {
191
- bits -= 11;
192
- const index = (accumulator >>> bits) & 0x7ff;
193
- words.push(BIP39_WORDLIST[index]);
194
- accumulator &= (1 << bits) - 1; // clear extracted bits to keep accumulator small
195
- }
196
- }
197
- // Pad remaining bits on the right to form a final 11-bit group
198
- if (bits > 0) {
199
- const index = ((accumulator << (11 - bits)) >>> 0) & 0x7ff;
200
- words.push(BIP39_WORDLIST[index]);
201
- }
202
- return words;
203
- }
204
- /**
205
- * Decode BIP-39 words back to a share.
206
- * Reverses the encoding from shareToWords.
207
- */
208
- export function wordsToShare(words) {
209
- if (words.length === 0)
210
- throw new SignetValidationError('Cannot decode empty word list');
211
- if (words.length > 256) {
212
- throw new SignetValidationError('Word count exceeds maximum (256)');
213
- }
214
- // Convert words to 11-bit indices
215
- const indices = [];
216
- for (const word of words) {
217
- const idx = BIP39_WORDLIST.indexOf(word.toLowerCase());
218
- if (idx === -1)
219
- throw new SignetValidationError(`Unknown BIP-39 word: "${word}"`);
220
- indices.push(idx);
221
- }
222
- // Total bits encoded, and how many full bytes that represents
223
- const totalBits = indices.length * 11;
224
- const totalBytes = Math.floor(totalBits / 8);
225
- // Stream 11-bit groups into bytes using safe unsigned arithmetic
226
- let bits = 0;
227
- let accumulator = 0;
228
- const byteList = [];
229
- for (const index of indices) {
230
- // accumulator has at most 7 bits here (< 8), so (7 + 11 = 18) fits safely in 32-bit
231
- accumulator = ((accumulator << 11) | index) >>> 0; // >>> 0 ensures unsigned 32-bit
232
- bits += 11;
233
- while (bits >= 8) {
234
- bits -= 8;
235
- byteList.push((accumulator >>> bits) & 0xff);
236
- accumulator &= (1 << bits) - 1; // clear extracted bits
237
- }
238
- }
239
- // Only take the expected number of full bytes (discard padding bits)
240
- const usable = byteList.slice(0, totalBytes);
241
- // Need at least 2 bytes: 1 ID + 1 data byte
242
- if (usable.length < 2) {
243
- throw new SignetValidationError('Word list too short — need at least 1 ID byte + 1 data byte');
244
- }
245
- // First byte is the share ID, rest is data
246
- const id = usable[0];
247
- if (id === 0) {
248
- throw new SignetValidationError('Invalid share ID: 0 is not a valid x-coordinate for GF(256)');
249
- }
250
- const data = new Uint8Array(usable.slice(1));
251
- return { id, data };
252
- }
253
- //# sourceMappingURL=shamir.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"shamir.js","sourceRoot":"","sources":["../src/shamir.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,0EAA0E;AAE1E,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAClD,OAAO,EAAE,QAAQ,IAAI,cAAc,EAAE,MAAM,mCAAmC,CAAC;AAC/E,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACvC,OAAO,EAAE,qBAAqB,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AA6BvE,8EAA8E;AAC9E,kEAAkE;AAClE,8EAA8E;AAE9E,MAAM,WAAW,GAAG,KAAK,CAAC;AAC1B,MAAM,SAAS,GAAG,IAAI,CAAC;AAEvB,+DAA+D;AAC/D,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,GAAG,CAAC,CAAC;AAChC,kEAAkE;AAClE,MAAM,GAAG,GAAG,IAAI,UAAU,CAAC,GAAG,CAAC,CAAC;AAEhC,mEAAmE;AACnE,SAAS,YAAY,CAAC,CAAS,EAAE,CAAS;IACxC,IAAI,MAAM,GAAG,CAAC,CAAC;IACf,IAAI,EAAE,GAAG,CAAC,CAAC;IACX,IAAI,EAAE,GAAG,CAAC,CAAC;IACX,OAAO,EAAE,GAAG,CAAC,EAAE,CAAC;QACd,IAAI,EAAE,GAAG,CAAC;YAAE,MAAM,IAAI,EAAE,CAAC;QACzB,EAAE,KAAK,CAAC,CAAC;QACT,IAAI,EAAE,GAAG,KAAK;YAAE,EAAE,IAAI,WAAW,CAAC;QAClC,EAAE,KAAK,CAAC,CAAC;IACX,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,gEAAgE;AAChE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