ecash-lib 4.13.0-rc.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.
- package/README.md +2 -1
- package/dist/blindSchnorr.d.ts +27 -0
- package/dist/blindSchnorr.d.ts.map +1 -0
- package/dist/blindSchnorr.js +206 -0
- package/dist/blindSchnorr.js.map +1 -0
- package/dist/ecc.d.ts +15 -0
- package/dist/ecc.d.ts.map +1 -1
- package/dist/ecc.js +36 -1
- package/dist/ecc.js.map +1 -1
- package/dist/eccScalar.d.ts +12 -0
- package/dist/eccScalar.d.ts.map +1 -0
- package/dist/eccScalar.js +42 -0
- package/dist/eccScalar.js.map +1 -0
- package/dist/ffi/ecash_lib_wasm_bg_browser.js +21400 -2628
- package/dist/ffi/ecash_lib_wasm_bg_browser.wasm +0 -0
- package/dist/ffi/ecash_lib_wasm_bg_browser.wasm.d.ts +8 -3
- package/dist/ffi/ecash_lib_wasm_browser.d.ts +59 -20
- package/dist/ffi/ecash_lib_wasm_browser.js +219 -83
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -1
- package/dist/initWasm.js +3 -4
- package/dist/initWasm.js.map +1 -1
- package/dist/io/array.d.ts +16 -0
- package/dist/io/array.d.ts.map +1 -0
- package/dist/io/array.js +71 -0
- package/dist/io/array.js.map +1 -0
- package/dist/payment/action.d.ts +7 -0
- package/dist/payment/action.d.ts.map +1 -1
- package/dist/payment/action.js.map +1 -1
- package/dist/pedersen.d.ts +62 -0
- package/dist/pedersen.d.ts.map +1 -0
- package/dist/pedersen.js +257 -0
- package/dist/pedersen.js.map +1 -0
- package/package.json +51 -62
- package/src/blindSchnorr.ts +241 -0
- package/src/ecc.ts +61 -1
- package/src/eccScalar.ts +41 -0
- package/src/ffi/ecash_lib_wasm_bg_browser.js +21400 -2628
- package/src/ffi/ecash_lib_wasm_bg_browser.wasm +0 -0
- package/src/ffi/ecash_lib_wasm_bg_browser.wasm.d.ts +8 -3
- package/src/ffi/ecash_lib_wasm_browser.d.ts +59 -20
- package/src/ffi/ecash_lib_wasm_browser.js +219 -83
- package/src/index.ts +6 -0
- package/src/initWasm.ts +3 -4
- package/src/io/array.ts +70 -0
- package/src/payment/action.ts +7 -0
- package/src/pedersen.ts +280 -0
- package/dist/ffi/ecash_lib_wasm_bg_nodejs.wasm +0 -0
- package/dist/ffi/ecash_lib_wasm_bg_nodejs.wasm.d.ts +0 -40
- package/dist/ffi/ecash_lib_wasm_nodejs.d.ts +0 -180
- package/dist/ffi/ecash_lib_wasm_nodejs.js +0 -726
- package/dist/indexBrowser.d.ts +0 -3
- package/dist/indexBrowser.d.ts.map +0 -1
- package/dist/indexBrowser.js +0 -22
- package/dist/indexBrowser.js.map +0 -1
- package/dist/indexNodeJs.d.ts +0 -3
- package/dist/indexNodeJs.d.ts.map +0 -1
- package/dist/indexNodeJs.js +0 -22
- package/dist/indexNodeJs.js.map +0 -1
- package/dist/initBrowser.d.ts +0 -2
- package/dist/initBrowser.d.ts.map +0 -1
- package/dist/initBrowser.js +0 -7
- package/dist/initBrowser.js.map +0 -1
- package/dist/initNodeJs.d.ts +0 -2
- package/dist/initNodeJs.d.ts.map +0 -1
- package/dist/initNodeJs.js +0 -7
- package/dist/initNodeJs.js.map +0 -1
- package/dist/payment/p2shInputData.d.ts +0 -18
- package/dist/payment/p2shInputData.d.ts.map +0 -1
- package/dist/payment/p2shInputData.js +0 -88
- package/dist/payment/p2shInputData.js.map +0 -1
- package/src/ffi/ecash_lib_wasm_bg_nodejs.wasm +0 -0
- package/src/ffi/ecash_lib_wasm_bg_nodejs.wasm.d.ts +0 -40
- package/src/ffi/ecash_lib_wasm_nodejs.d.ts +0 -180
- package/src/ffi/ecash_lib_wasm_nodejs.js +0 -726
- package/src/indexBrowser.ts +0 -6
- package/src/indexNodeJs.ts +0 -6
- package/src/initBrowser.ts +0 -5
- package/src/initNodeJs.ts +0 -5
|
@@ -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
|
-
|
|
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
|
}
|
package/src/eccScalar.ts
ADDED
|
@@ -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
|
+
}
|