ecash-lib 4.13.0 → 4.14.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 (46) hide show
  1. package/README.md +1 -0
  2. package/dist/blindSchnorr.d.ts +27 -0
  3. package/dist/blindSchnorr.d.ts.map +1 -0
  4. package/dist/blindSchnorr.js +206 -0
  5. package/dist/blindSchnorr.js.map +1 -0
  6. package/dist/ecc.d.ts +15 -0
  7. package/dist/ecc.d.ts.map +1 -1
  8. package/dist/ecc.js +36 -1
  9. package/dist/ecc.js.map +1 -1
  10. package/dist/eccScalar.d.ts +12 -0
  11. package/dist/eccScalar.d.ts.map +1 -0
  12. package/dist/eccScalar.js +42 -0
  13. package/dist/eccScalar.js.map +1 -0
  14. package/dist/ffi/ecash_lib_wasm_bg_browser.js +21408 -22085
  15. package/dist/ffi/ecash_lib_wasm_bg_browser.wasm +0 -0
  16. package/dist/ffi/ecash_lib_wasm_bg_browser.wasm.d.ts +5 -0
  17. package/dist/ffi/ecash_lib_wasm_browser.d.ts +39 -0
  18. package/dist/ffi/ecash_lib_wasm_browser.js +135 -0
  19. package/dist/index.d.ts +4 -0
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js +4 -0
  22. package/dist/index.js.map +1 -1
  23. package/dist/io/array.d.ts +16 -0
  24. package/dist/io/array.d.ts.map +1 -0
  25. package/dist/io/array.js +71 -0
  26. package/dist/io/array.js.map +1 -0
  27. package/dist/payment/action.d.ts +7 -0
  28. package/dist/payment/action.d.ts.map +1 -1
  29. package/dist/payment/action.js.map +1 -1
  30. package/dist/pedersen.d.ts +62 -0
  31. package/dist/pedersen.d.ts.map +1 -0
  32. package/dist/pedersen.js +257 -0
  33. package/dist/pedersen.js.map +1 -0
  34. package/package.json +3 -3
  35. package/src/blindSchnorr.ts +241 -0
  36. package/src/ecc.ts +61 -1
  37. package/src/eccScalar.ts +41 -0
  38. package/src/ffi/ecash_lib_wasm_bg_browser.js +21408 -22085
  39. package/src/ffi/ecash_lib_wasm_bg_browser.wasm +0 -0
  40. package/src/ffi/ecash_lib_wasm_bg_browser.wasm.d.ts +5 -0
  41. package/src/ffi/ecash_lib_wasm_browser.d.ts +39 -0
  42. package/src/ffi/ecash_lib_wasm_browser.js +135 -0
  43. package/src/index.ts +4 -0
  44. package/src/io/array.ts +70 -0
  45. package/src/payment/action.ts +7 -0
  46. package/src/pedersen.ts +280 -0
@@ -0,0 +1,257 @@
1
+ "use strict";
2
+ // Copyright (c) 2026 The Bitcoin developers
3
+ // Distributed under the MIT software license, see the accompanying
4
+ // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.PedersenSetup = exports.ResultAtInfinity = exports.InsecureHPoint = exports.NonceRangeError = void 0;
7
+ exports.addCommitmentPoints = addCommitmentPoints;
8
+ exports.verifyCommitmentSum = verifyCommitmentSum;
9
+ exports.addScalars = addScalars;
10
+ const ecc_js_1 = require("./ecc.js");
11
+ const eccScalar_js_1 = require("./eccScalar.js");
12
+ const array_js_1 = require("./io/array.js");
13
+ const hex_js_1 = require("./io/hex.js");
14
+ const ecc = new ecc_js_1.Ecc();
15
+ const SCALAR_ONE = (0, hex_js_1.fromHex)('0000000000000000000000000000000000000000000000000000000000000001');
16
+ class NonceRangeError extends Error {
17
+ constructor(message = 'Invalid Pedersen nonce') {
18
+ super(message);
19
+ this.name = 'NonceRangeError';
20
+ }
21
+ }
22
+ exports.NonceRangeError = NonceRangeError;
23
+ class InsecureHPoint extends Error {
24
+ constructor(message = 'InsecureHPoint') {
25
+ super(message);
26
+ this.name = 'InsecureHPoint';
27
+ }
28
+ }
29
+ exports.InsecureHPoint = InsecureHPoint;
30
+ /** Sum of commitment points is the point at infinity (cannot be serialized). */
31
+ class ResultAtInfinity extends Error {
32
+ constructor(message = 'ResultAtInfinity') {
33
+ super(message);
34
+ this.name = 'ResultAtInfinity';
35
+ }
36
+ }
37
+ exports.ResultAtInfinity = ResultAtInfinity;
38
+ /**
39
+ * Pedersen setup for commitments C = nonce*G + amount*H (same algebra as
40
+ * Electrum ABC).
41
+ *
42
+ * Rejects insecure H with a known discrete log vs G:
43
+ * - H = -G (HG at infinity), same as Electrum
44
+ * - H = G (then C = (nonce+amount)G and binding is lost)
45
+ *
46
+ * Callers supply a nothing-up-my-sleeve H (Electrum/ALP fixed domain strings).
47
+ * This API does not derive H itself.
48
+ *
49
+ * Implementation detail: we store H and H+G and evaluate the equivalent form
50
+ * C = (a - k)*H + k*(H + G)
51
+ * so the amount scalar multiply is blinded by the nonce (see Electrum
52
+ * `Commitment._calc_initial`). Expand to recover C = k*G + a*H.
53
+ *
54
+ * Fresh nonces from {@link commit} are in [1, n). A zero nonce is accepted when
55
+ * supplied explicitly (e.g. aggregate openings where nonces cancel): then
56
+ * C = amount*H. The (amount, nonce) = (0, 0) opening is the identity and cannot
57
+ * be serialized as a point — use {@link verifyCommitmentSum} for that case.
58
+ */
59
+ class PedersenSetup {
60
+ constructor(H) {
61
+ // Accept compressed (33) or uncompressed (65), same as Electrum ABC.
62
+ let compressed;
63
+ if (H.length === 33) {
64
+ try {
65
+ ecc.uncompressPk(H); // validate
66
+ }
67
+ catch {
68
+ throw new Error('H could not be parsed');
69
+ }
70
+ compressed = H;
71
+ }
72
+ else if (H.length === 65 && H[0] === 0x04) {
73
+ try {
74
+ compressed = ecc.compressPk(H);
75
+ }
76
+ catch {
77
+ throw new Error('H could not be parsed');
78
+ }
79
+ }
80
+ else {
81
+ throw new Error('H could not be parsed');
82
+ }
83
+ if ((0, array_js_1.equalBytes)(compressed, ecc.derivePubkey(SCALAR_ONE))) {
84
+ throw new InsecureHPoint('H = G');
85
+ }
86
+ try {
87
+ // HG = H + G. Fails if H = -G (result at infinity).
88
+ this.HG = ecc.pubkeyAdd(compressed, SCALAR_ONE);
89
+ }
90
+ catch {
91
+ throw new InsecureHPoint();
92
+ }
93
+ this.H = new Uint8Array(compressed);
94
+ }
95
+ commit(amount, nonce) {
96
+ const amountMod = (0, eccScalar_js_1.modCurveOrder)(amount);
97
+ const amountBytes = (0, eccScalar_js_1.scalarToBytes)(amountMod);
98
+ let k;
99
+ if (nonce === undefined) {
100
+ // Fresh nonce in [1, n) — never zero.
101
+ k = (0, eccScalar_js_1.randomScalarBytes)();
102
+ }
103
+ else {
104
+ if (nonce.length !== 32) {
105
+ throw new NonceRangeError('nonce must be 32 bytes');
106
+ }
107
+ const kn = (0, array_js_1.bytesToScalar)(nonce);
108
+ if (kn < 0n || kn >= eccScalar_js_1.CURVE_ORDER) {
109
+ throw new NonceRangeError();
110
+ }
111
+ k = new Uint8Array(nonce);
112
+ // Zero nonce: C = a*H (aggregate opening when nonces cancel).
113
+ // For independent random nonces this is ~2^-256 unlikely; it is
114
+ // expected when deliberately summing complementary nonces.
115
+ // libsecp rejects 0*P, so this path is explicit.
116
+ if (kn === 0n) {
117
+ if (amountMod === 0n) {
118
+ throw new ResultAtInfinity('commitment at infinity (zero amount and nonce)');
119
+ }
120
+ const point = ecc.pubkeyMul(this.H, amountBytes);
121
+ return {
122
+ amount,
123
+ nonce: k,
124
+ point,
125
+ pointUncompressed: ecc.uncompressPk(point),
126
+ };
127
+ }
128
+ }
129
+ // Electrum amount blinding: C = (a-k)*H + k*(H+G).
130
+ const kHG = ecc.pubkeyMul(this.HG, k);
131
+ let point = kHG;
132
+ const negK = ecc.seckeyNegate(k);
133
+ let aMinusK;
134
+ try {
135
+ // seckeyAdd = secp256k1 seckey tweak add: (a + b) mod n.
136
+ // Here: (a - k) mod n. Throws when the result is 0 (invalid seckey),
137
+ // i.e. when (a - k) ≡ 0 (mod n) — Electrum's a_k == 0 fast path.
138
+ // For uniform random k that is ~2^-256 unlikely (a ≡ k mod n).
139
+ aMinusK = ecc.seckeyAdd(negK, amountBytes);
140
+ }
141
+ catch {
142
+ // (a - k) ≡ 0 (mod n): C = k*(H+G) only (point already kHG).
143
+ }
144
+ finally {
145
+ (0, array_js_1.zeroBytes)(negK);
146
+ }
147
+ if (aMinusK !== undefined) {
148
+ try {
149
+ const akH = ecc.pubkeyMul(this.H, aMinusK);
150
+ try {
151
+ point = ecc.pubkeyCombine(kHG, akH);
152
+ }
153
+ catch {
154
+ // Point at infinity ⇒ discrete log of H is known.
155
+ throw new InsecureHPoint();
156
+ }
157
+ }
158
+ finally {
159
+ (0, array_js_1.zeroBytes)(aMinusK);
160
+ }
161
+ }
162
+ return {
163
+ amount,
164
+ nonce: k,
165
+ point,
166
+ pointUncompressed: ecc.uncompressPk(point),
167
+ };
168
+ }
169
+ }
170
+ exports.PedersenSetup = PedersenSetup;
171
+ /** Compress and validate a commitment point (33-byte compressed or 65-byte). */
172
+ function normalizeCommitmentPoint(p) {
173
+ if (p.length === 65) {
174
+ if (p[0] !== 0x04) {
175
+ throw new Error('Invalid uncompressed commitment point');
176
+ }
177
+ const compressed = ecc.compressPk(p);
178
+ // compressPk only packs bytes; require a valid curve point.
179
+ ecc.uncompressPk(compressed);
180
+ return compressed;
181
+ }
182
+ if (p.length === 33) {
183
+ if (p[0] !== 0x02 && p[0] !== 0x03) {
184
+ throw new Error('Invalid compressed commitment point');
185
+ }
186
+ ecc.uncompressPk(p);
187
+ return p;
188
+ }
189
+ throw new Error('Invalid commitment point length');
190
+ }
191
+ function addCommitmentPoints(points) {
192
+ if (points.length === 0) {
193
+ throw new Error('No points to add');
194
+ }
195
+ // Validate each point before combining so malformed input is not mistaken
196
+ // for the point at infinity.
197
+ const compressed = points.map(normalizeCommitmentPoint);
198
+ let sum;
199
+ try {
200
+ sum = compressed
201
+ .slice(1)
202
+ .reduce((acc, p) => ecc.pubkeyCombine(acc, p), compressed[0]);
203
+ }
204
+ catch {
205
+ throw new ResultAtInfinity();
206
+ }
207
+ return ecc.uncompressPk(sum);
208
+ }
209
+ /**
210
+ * Verify that commitments open to (totalAmount, totalNonce).
211
+ * Returns a boolean (does not throw for the identity aggregate): when the
212
+ * points sum to infinity, the only valid opening is amount ≡ 0 and nonce = 0.
213
+ * Ill-formed commitments or nonce always return false (never true).
214
+ */
215
+ function verifyCommitmentSum(setup, commitments, totalAmount, totalNonce) {
216
+ if (totalNonce.length !== 32) {
217
+ return false;
218
+ }
219
+ const amountMod = (0, eccScalar_js_1.modCurveOrder)(totalAmount);
220
+ const nonceIsZero = (0, array_js_1.bytesToScalar)(totalNonce) === 0n;
221
+ let sum;
222
+ try {
223
+ sum = addCommitmentPoints(commitments);
224
+ }
225
+ catch (e) {
226
+ // Genuine infinity only after points were validated.
227
+ if (e instanceof ResultAtInfinity) {
228
+ return nonceIsZero && amountMod === 0n;
229
+ }
230
+ return false;
231
+ }
232
+ // Claimed (0, 0) opening but the sum is a concrete point.
233
+ if (nonceIsZero && amountMod === 0n) {
234
+ return false;
235
+ }
236
+ try {
237
+ const expected = setup.commit(totalAmount, totalNonce);
238
+ return (0, array_js_1.equalBytes)(sum, expected.pointUncompressed);
239
+ }
240
+ catch {
241
+ return false;
242
+ }
243
+ }
244
+ /**
245
+ * Sum two 32-byte seckeys/nonces mod n (wasm seckeyAdd).
246
+ * When (a + b) ≡ 0 (mod n), seckeyAdd throws — return canonical zero.
247
+ */
248
+ function addScalars(a, b) {
249
+ try {
250
+ return ecc.seckeyAdd(a, b);
251
+ }
252
+ catch {
253
+ // (a + b) ≡ 0 (mod n)
254
+ return new Uint8Array(32);
255
+ }
256
+ }
257
+ //# sourceMappingURL=pedersen.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pedersen.js","sourceRoot":"","sources":["../src/pedersen.ts"],"names":[],"mappings":";AAAA,4CAA4C;AAC5C,mEAAmE;AACnE,sEAAsE;;;AA8MtE,kDAgBC;AAQD,kDAkCC;AAMD,gCAOC;AAnRD,qCAA+B;AAC/B,iDAKwB;AACxB,4CAAqE;AACrE,wCAAsC;AAEtC,MAAM,GAAG,GAAG,IAAI,YAAG,EAAE,CAAC;AACtB,MAAM,UAAU,GAAG,IAAA,gBAAO,EACtB,kEAAkE,CACrE,CAAC;AAEF,MAAa,eAAgB,SAAQ,KA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;IAAC,MAAM,CAAC;QACL,OAAO,KAAK,CAAC;IACjB,CAAC;AACL,CAAC;AAED;;;GAGG;AACH,SAAgB,UAAU,CAAC,CAAa,EAAE,CAAa;IACnD,IAAI,CAAC;QACD,OAAO,GAAG,CAAC,SAAS,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IAC/B,CAAC;IAAC,MAAM,CAAC;QACL,sBAAsB;QACtB,OAAO,IAAI,UAAU,CAAC,EAAE,CAAC,CAAC;IAC9B,CAAC;AACL,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ecash-lib",
3
- "version": "4.13.0",
3
+ "version": "4.14.0",
4
4
  "description": "Library for eCash transaction building",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -36,11 +36,11 @@
36
36
  "ts-node": "^10.9.2",
37
37
  "tsx": "^4.7.2",
38
38
  "typescript": "^5.9.3",
39
- "chronik-client": "^4.3.0"
39
+ "chronik-client": "^4.3.1"
40
40
  },
41
41
  "dependencies": {
42
42
  "b58-ts": "^0.1.0",
43
- "ecashaddrjs": "^2.0.0"
43
+ "ecashaddrjs": "^2.0.1"
44
44
  },
45
45
  "scripts": {
46
46
  "build": "tsc && tsc -p ./tsconfig.build.json && rm -rf ./dist/ffi && cp -r ./src/ffi ./dist",
@@ -0,0 +1,241 @@
1
+ // Copyright (c) 2026 The Bitcoin developers
2
+ // Distributed under the MIT software license, see the accompanying
3
+ // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
+
5
+ /**
6
+ * Schnorr blind signatures (CashFusion / Electrum ABC).
7
+ * Port of electrumabc/schnorr.py BlindSigner + BlindSignatureRequest.
8
+ *
9
+ * Secret scalars are Uint8Array values combined with wasm ECC
10
+ * (`seckeyAdd` / `seckeyMul` / `seckeyNegate`), which reduce mod n.
11
+ */
12
+ import { Ecc } from './ecc.js';
13
+ import {
14
+ modCurveOrder,
15
+ randomScalarBytes,
16
+ scalarToBytes,
17
+ } from './eccScalar.js';
18
+ import { sha256 } from './hash.js';
19
+ import { bytesToScalar, concatBytes, zeroBytes } from './io/array.js';
20
+
21
+ const ecc = new Ecc();
22
+
23
+ const FIELD_SIZE =
24
+ 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2fn;
25
+
26
+ /** Jacobi symbol — port of electrumabc/schnorr.py jacobi(). Public inputs only. */
27
+ function jacobi(a: bigint, n: bigint): number {
28
+ let x = ((a % n) + n) % n;
29
+ if (n < 3n || (n & 1n) === 0n) {
30
+ throw new Error('jacobi: bad n');
31
+ }
32
+ let s = 1;
33
+ while (x > 1n) {
34
+ let a1 = x;
35
+ let e = 0;
36
+ while ((a1 & 1n) === 0n) {
37
+ a1 >>= 1n;
38
+ e++;
39
+ }
40
+ if (e % 2 === 1 && n % 8n !== 1n && n % 8n !== 7n) {
41
+ s = -s;
42
+ }
43
+ if (a1 === 1n) {
44
+ return s;
45
+ }
46
+ if (n % 4n === 3n && a1 % 4n === 3n) {
47
+ s = -s;
48
+ }
49
+ x = n % a1;
50
+ n = a1;
51
+ }
52
+ if (x === 0n) {
53
+ return 0;
54
+ }
55
+ if (x === 1n) {
56
+ return s;
57
+ }
58
+ return 0;
59
+ }
60
+
61
+ function assertCompressedPoint(name: string, pk: Uint8Array): void {
62
+ if (pk.length !== 33 || (pk[0] !== 0x02 && pk[0] !== 0x03)) {
63
+ throw new Error(`${name} must be a 33-byte compressed secp256k1 point`);
64
+ }
65
+ try {
66
+ ecc.uncompressPk(pk);
67
+ } catch {
68
+ throw new Error(`${name} is not a valid secp256k1 point`);
69
+ }
70
+ }
71
+
72
+ /** Server-side: one nonce R per component slot; signs once. */
73
+ export class BlindSigner {
74
+ private k: Uint8Array | undefined;
75
+ private readonly rPoint: Uint8Array;
76
+
77
+ constructor() {
78
+ this.k = randomScalarBytes();
79
+ this.rPoint = ecc.derivePubkey(this.k);
80
+ }
81
+
82
+ /** Compressed nonce point R = k*G (defensive copy). */
83
+ getR(): Uint8Array {
84
+ return new Uint8Array(this.rPoint);
85
+ }
86
+
87
+ sign(roundSeckey: Uint8Array, ebytes: Uint8Array): Uint8Array {
88
+ if (ebytes.length !== 32 || roundSeckey.length !== 32) {
89
+ throw new Error('BlindSigner: expected 32-byte key and challenge');
90
+ }
91
+ const k = this.k;
92
+ if (k === undefined) {
93
+ throw new Error('BlindSigner: attempted to sign twice');
94
+ }
95
+ this.k = undefined;
96
+ try {
97
+ // s = (k + e*x) mod n
98
+ const ex = ecc.seckeyMul(roundSeckey, ebytes);
99
+ try {
100
+ return ecc.seckeyAdd(k, ex);
101
+ } finally {
102
+ zeroBytes(ex);
103
+ }
104
+ } finally {
105
+ zeroBytes(k);
106
+ }
107
+ }
108
+ }
109
+
110
+ /** Client-side: blinds component hash; unblinds server scalar to Schnorr sig. */
111
+ export class BlindSignatureRequest {
112
+ private a: Uint8Array | undefined;
113
+ private readonly c: number;
114
+ private readonly Rxnew: Uint8Array;
115
+ private readonly pubkeyCompressed: Uint8Array;
116
+ private readonly messageHash: Uint8Array;
117
+ private readonly e: Uint8Array;
118
+
119
+ constructor(pubkey: Uint8Array, R: Uint8Array, messageHash: Uint8Array) {
120
+ if (messageHash.length !== 32) {
121
+ throw new Error('message_hash must be 32 bytes');
122
+ }
123
+ // Fail fast on bad inputs; the retry loop only covers rare EC degeneracy
124
+ // / jacobi-0 (inputs already validated).
125
+ assertCompressedPoint('pubkey', pubkey);
126
+ assertCompressedPoint('R', R);
127
+ this.messageHash = new Uint8Array(messageHash);
128
+ this.pubkeyCompressed = new Uint8Array(pubkey);
129
+
130
+ // R' = R + a*G + b*P; c = jacobi(R'.y) ∈ {±1}.
131
+ for (let attempt = 0; attempt < 32; attempt++) {
132
+ const a = randomScalarBytes();
133
+ const b = randomScalarBytes();
134
+ try {
135
+ const bP = ecc.pubkeyMul(pubkey, b);
136
+ const RplusBP = ecc.pubkeyCombine(R, bP);
137
+ const combined = ecc.pubkeyAdd(RplusBP, a);
138
+ const uncompressed = ecc.uncompressPk(combined);
139
+ const Rxnew = uncompressed.subarray(1, 33);
140
+ const y = bytesToScalar(uncompressed.subarray(33));
141
+ const c = jacobi(y, FIELD_SIZE);
142
+ if (c !== 1 && c !== -1) {
143
+ zeroBytes(a);
144
+ zeroBytes(b);
145
+ continue;
146
+ }
147
+ // ehash is public; reduce with bigint then combine with secret b via wasm.
148
+ const ehash = sha256(
149
+ concatBytes(Rxnew, this.pubkeyCompressed, this.messageHash),
150
+ );
151
+ const ehashMod = modCurveOrder(bytesToScalar(ehash));
152
+ const ehashTweak = scalarToBytes(
153
+ c === 1 ? ehashMod : modCurveOrder(-ehashMod),
154
+ );
155
+ // e = (b + c*ehash) mod n
156
+ const e = ecc.seckeyAdd(b, ehashTweak);
157
+ this.a = new Uint8Array(a);
158
+ this.c = c;
159
+ this.Rxnew = new Uint8Array(Rxnew);
160
+ this.e = e;
161
+ zeroBytes(a);
162
+ zeroBytes(b);
163
+ return;
164
+ } catch {
165
+ // Point at infinity or other rare EC failure (~2^-256) — retry.
166
+ zeroBytes(a);
167
+ zeroBytes(b);
168
+ }
169
+ }
170
+ throw new Error(
171
+ "BlindSignatureRequest: failed to find valid R' after retries",
172
+ );
173
+ }
174
+
175
+ getRequest(): Uint8Array {
176
+ return new Uint8Array(this.e);
177
+ }
178
+
179
+ finalize(sbytes: Uint8Array, check = true): Uint8Array {
180
+ if (sbytes.length !== 32) {
181
+ throw new Error('s must be 32 bytes');
182
+ }
183
+ const a = this.a;
184
+ if (a === undefined) {
185
+ throw new Error('BlindSignatureRequest: already finalized');
186
+ }
187
+ this.a = undefined;
188
+ try {
189
+ // s' = c*(s + a) mod n for c ∈ {±1}:
190
+ // always compute (s + a) mod n, then negate when c == -1.
191
+ const sPlusA = ecc.seckeyAdd(sbytes, a);
192
+ let sFinal: Uint8Array;
193
+ if (this.c === -1) {
194
+ sFinal = ecc.seckeyNegate(sPlusA);
195
+ // Intermediate still mixes secret a; s' in the sig is public.
196
+ zeroBytes(sPlusA);
197
+ } else {
198
+ sFinal = sPlusA;
199
+ }
200
+ const sig = concatBytes(this.Rxnew, sFinal);
201
+ if (check) {
202
+ ecc.schnorrVerify(sig, this.messageHash, this.pubkeyCompressed);
203
+ }
204
+ return sig;
205
+ } finally {
206
+ zeroBytes(a);
207
+ }
208
+ }
209
+ }
210
+
211
+ export function buildBlindSigRequests(
212
+ roundPubkey: Uint8Array,
213
+ blindNoncePoints: Uint8Array[],
214
+ components: Uint8Array[],
215
+ ): { requests: BlindSignatureRequest[]; eValues: Uint8Array[] } {
216
+ if (components.length > blindNoncePoints.length) {
217
+ throw new Error('not enough blind nonce points');
218
+ }
219
+ const requests = components.map(
220
+ (comp, i) =>
221
+ new BlindSignatureRequest(
222
+ roundPubkey,
223
+ blindNoncePoints[i],
224
+ sha256(comp),
225
+ ),
226
+ );
227
+ return {
228
+ requests,
229
+ eValues: requests.map(r => r.getRequest()),
230
+ };
231
+ }
232
+
233
+ export function finalizeBlindSigs(
234
+ requests: BlindSignatureRequest[],
235
+ scalars: Uint8Array[],
236
+ ): Uint8Array[] {
237
+ if (requests.length !== scalars.length) {
238
+ throw new Error('blind scalar count mismatch');
239
+ }
240
+ return requests.map((r, i) => r.finalize(scalars[i]));
241
+ }
package/src/ecc.ts CHANGED
@@ -35,9 +35,24 @@ export interface Ecc {
35
35
  /** Add a scalar to a secret key */
36
36
  seckeyAdd(a: Uint8Array, b: Uint8Array): Uint8Array;
37
37
 
38
+ /** Multiply a secret key by a scalar (a * b) */
39
+ seckeyMul(a: Uint8Array, b: Uint8Array): Uint8Array;
40
+
41
+ /** Negate a secret key (−a mod n) */
42
+ seckeyNegate(a: Uint8Array): Uint8Array;
43
+
38
44
  /** Add a scalar to a public key (adding G*b) */
39
45
  pubkeyAdd(a: Uint8Array, b: Uint8Array): Uint8Array;
40
46
 
47
+ /** Multiply a public key by a scalar (P * b) */
48
+ pubkeyMul(a: Uint8Array, b: Uint8Array): Uint8Array;
49
+
50
+ /** Add two public keys (P + Q). Errors if the sum is infinity. */
51
+ pubkeyCombine(a: Uint8Array, b: Uint8Array): Uint8Array;
52
+
53
+ /** Decompress a compressed public key to 65-byte uncompressed (0x04||x||y) */
54
+ uncompressPk(pk: Uint8Array): Uint8Array;
55
+
41
56
  /** Sign a ECDSA recoverable signature, includes the recovery ID */
42
57
  signRecoverable(seckey: Uint8Array, msg: Uint8Array): Uint8Array;
43
58
 
@@ -74,10 +89,30 @@ export class EccDummy implements Ecc {
74
89
  return new Uint8Array(32);
75
90
  }
76
91
 
77
- pubkeyAdd(_a: Uint8Array, _b: Uint8Array): Uint8Array {
92
+ seckeyMul(_a: Uint8Array, _b: Uint8Array): Uint8Array {
93
+ return new Uint8Array(32);
94
+ }
95
+
96
+ seckeyNegate(_a: Uint8Array): Uint8Array {
78
97
  return new Uint8Array(32);
79
98
  }
80
99
 
100
+ pubkeyAdd(_a: Uint8Array, _b: Uint8Array): Uint8Array {
101
+ return new Uint8Array(33);
102
+ }
103
+
104
+ pubkeyMul(_a: Uint8Array, _b: Uint8Array): Uint8Array {
105
+ return new Uint8Array(33);
106
+ }
107
+
108
+ pubkeyCombine(_a: Uint8Array, _b: Uint8Array): Uint8Array {
109
+ return new Uint8Array(33);
110
+ }
111
+
112
+ uncompressPk(_pk: Uint8Array): Uint8Array {
113
+ return new Uint8Array(65);
114
+ }
115
+
81
116
  signRecoverable(_seckey: Uint8Array, _msg: Uint8Array): Uint8Array {
82
117
  return new Uint8Array(65);
83
118
  }
@@ -144,11 +179,36 @@ export class Ecc implements Ecc {
144
179
  return ECC.ecc!.seckeyAdd(a, b);
145
180
  }
146
181
 
182
+ /** Multiply a secret key by a scalar (a * b) */
183
+ seckeyMul(a: Uint8Array, b: Uint8Array): Uint8Array {
184
+ return ECC.ecc!.seckeyMul(a, b);
185
+ }
186
+
187
+ /** Negate a secret key (−a mod n) */
188
+ seckeyNegate(a: Uint8Array): Uint8Array {
189
+ return ECC.ecc!.seckeyNegate(a);
190
+ }
191
+
147
192
  /** Add a scalar to a public key (adding G*b) */
148
193
  pubkeyAdd(a: Uint8Array, b: Uint8Array): Uint8Array {
149
194
  return ECC.ecc!.pubkeyAdd(a, b);
150
195
  }
151
196
 
197
+ /** Multiply a public key by a scalar (P * b) */
198
+ pubkeyMul(a: Uint8Array, b: Uint8Array): Uint8Array {
199
+ return ECC.ecc!.pubkeyMul(a, b);
200
+ }
201
+
202
+ /** Add two public keys (P + Q). Errors if the sum is infinity. */
203
+ pubkeyCombine(a: Uint8Array, b: Uint8Array): Uint8Array {
204
+ return ECC.ecc!.pubkeyCombine(a, b);
205
+ }
206
+
207
+ /** Decompress a compressed public key to 65-byte uncompressed (0x04||x||y) */
208
+ uncompressPk(pk: Uint8Array): Uint8Array {
209
+ return ECC.ecc!.uncompressPk(pk);
210
+ }
211
+
152
212
  signRecoverable(seckey: Uint8Array, msg: Uint8Array): Uint8Array {
153
213
  return ECC.ecc!.signRecoverable(seckey, msg);
154
214
  }
@@ -0,0 +1,41 @@
1
+ // Copyright (c) 2026 The Bitcoin developers
2
+ // Distributed under the MIT software license, see the accompanying
3
+ // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
+
5
+ import { bytesToScalar } from './io/array.js';
6
+
7
+ /** secp256k1 group order n. */
8
+ export const CURVE_ORDER =
9
+ 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141n;
10
+
11
+ /** Reduce `n` into [0, CURVE_ORDER). */
12
+ export function modCurveOrder(n: bigint): bigint {
13
+ const r = n % CURVE_ORDER;
14
+ return r < 0n ? r + CURVE_ORDER : r;
15
+ }
16
+
17
+ /** Big-endian 32-byte encoding of a scalar reduced into [0, n). */
18
+ export function scalarToBytes(n: bigint): Uint8Array {
19
+ let x = modCurveOrder(n);
20
+ const out = new Uint8Array(32);
21
+ for (let i = 31; i >= 0; i--) {
22
+ out[i] = Number(x & 0xffn);
23
+ x >>= 8n;
24
+ }
25
+ return out;
26
+ }
27
+
28
+ /**
29
+ * Cryptographically random 32-byte scalar in [1, n).
30
+ * Rejection sampling — no modular reduction of the RNG output (avoids bias).
31
+ */
32
+ export function randomScalarBytes(): Uint8Array {
33
+ const buf = new Uint8Array(32);
34
+ for (;;) {
35
+ crypto.getRandomValues(buf);
36
+ const n = bytesToScalar(buf);
37
+ if (n > 0n && n < CURVE_ORDER) {
38
+ return buf;
39
+ }
40
+ }
41
+ }