@audren-xyz/sdk 0.0.0-stage → 1.3.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/LICENSE +202 -0
- package/README.md +102 -2
- package/dist/constants.d.ts +229 -0
- package/dist/constants.js +301 -0
- package/dist/crypto.d.ts +85 -0
- package/dist/crypto.js +520 -0
- package/dist/idl.d.ts +4291 -0
- package/dist/idl.js +5608 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +13 -0
- package/dist/instructions.d.ts +398 -0
- package/dist/instructions.js +1139 -0
- package/dist/keys.d.ts +67 -0
- package/dist/keys.js +194 -0
- package/dist/layout.d.ts +153 -0
- package/dist/layout.js +535 -0
- package/dist/pda.d.ts +65 -0
- package/dist/pda.js +508 -0
- package/dist/poseidon-constants.d.ts +13 -0
- package/dist/poseidon-constants.js +1401 -0
- package/dist/poseidon.d.ts +46 -0
- package/dist/poseidon.js +214 -0
- package/dist/prover-entry.d.ts +1 -0
- package/dist/prover-entry.js +1 -0
- package/dist/prover.d.ts +100 -0
- package/dist/prover.js +515 -0
- package/dist/relayer.d.ts +336 -0
- package/dist/relayer.js +258 -0
- package/dist/scanner.d.ts +262 -0
- package/dist/scanner.js +3119 -0
- package/dist/store.d.ts +186 -0
- package/dist/store.js +1403 -0
- package/dist/swap.d.ts +105 -0
- package/dist/swap.js +156 -0
- package/dist/token2022.d.ts +29 -0
- package/dist/token2022.js +168 -0
- package/dist/tx-v1.d.ts +63 -0
- package/dist/tx-v1.js +249 -0
- package/idl/audren.anchor.json +1750 -0
- package/idl/audren.codama.json +3856 -0
- package/package.json +76 -4
- package/wasm/audren_circuits.wasm +0 -0
- package/wasm/audren_joinsplit.pk +0 -0
package/dist/crypto.js
ADDED
|
@@ -0,0 +1,520 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure TypeScript RFC 8439 ChaCha20-Poly1305 AEAD, X25519 low-order-safe versioned
|
|
3
|
+
* `106 B` SoA note memo encryption/decryption (with `34 B` AEAD AAD + `14 B` `paymentId`),
|
|
4
|
+
* and domain-separated `72 B` `AUD1DYN1` XOR-masked dynamic swap memo helpers matching
|
|
5
|
+
* `crates/client/src/keys.rs` byte-for-byte.
|
|
6
|
+
*/
|
|
7
|
+
import { x25519 } from '@noble/curves/ed25519.js';
|
|
8
|
+
import { sha256 } from '@noble/hashes/sha2.js';
|
|
9
|
+
import { DYN_MEMO_HEAD_LEN, DYN_MEMO_MAGIC, KDF_DOMAIN_BLIND, KDF_DOMAIN_DYN_BLIND, KDF_DOMAIN_DYN_MASK, KDF_DOMAIN_DYN_TWEAK, KDF_DOMAIN_MEMO_KEY, KDF_DOMAIN_MEMO_NONCE, KDF_DOMAIN_TWEAK, KDF_DOMAIN_VIEW_TAG, MEMO_CT_LEN, MEMO_ENTRY_WIRE_LEN, MEMO_PAYMENT_ID_LEN, MEMO_PLAINTEXT_LEN, MEMO_VERSION_V1, NUM_ESCROW_SHARDS, NUM_LANES, } from './constants.js';
|
|
10
|
+
import { bytes32ToFrMasked, computeNoteCommitment, deriveAssetId, deriveStealthOwner, frFromBeBytesStrict, frToBeBytes, } from './poseidon.js';
|
|
11
|
+
const utf8Encoder = new TextEncoder();
|
|
12
|
+
const DYN_MAGIC_BYTES = utf8Encoder.encode(DYN_MEMO_MAGIC);
|
|
13
|
+
const DYN_MASK_LABEL_BYTES = utf8Encoder.encode(KDF_DOMAIN_DYN_MASK);
|
|
14
|
+
const U64_MAX = 0xffffffffffffffffn;
|
|
15
|
+
// =============================================================================
|
|
16
|
+
// RFC 8439 ChaCha20-Poly1305 AEAD (zero extra dependencies)
|
|
17
|
+
// =============================================================================
|
|
18
|
+
function rotl32(v, n) {
|
|
19
|
+
return ((v << n) | (v >>> (32 - n))) >>> 0;
|
|
20
|
+
}
|
|
21
|
+
function quarterRound(s, a, b, c, d) {
|
|
22
|
+
s[a] = (s[a] + s[b]) >>> 0;
|
|
23
|
+
s[d] = rotl32(s[d] ^ s[a], 16);
|
|
24
|
+
s[c] = (s[c] + s[d]) >>> 0;
|
|
25
|
+
s[b] = rotl32(s[b] ^ s[c], 12);
|
|
26
|
+
s[a] = (s[a] + s[b]) >>> 0;
|
|
27
|
+
s[d] = rotl32(s[d] ^ s[a], 8);
|
|
28
|
+
s[c] = (s[c] + s[d]) >>> 0;
|
|
29
|
+
s[b] = rotl32(s[b] ^ s[c], 7);
|
|
30
|
+
}
|
|
31
|
+
function chacha20Block(key, counter, nonce) {
|
|
32
|
+
const kv = new DataView(key.buffer, key.byteOffset, key.byteLength);
|
|
33
|
+
const nv = new DataView(nonce.buffer, nonce.byteOffset, nonce.byteLength);
|
|
34
|
+
const state = new Uint32Array(16);
|
|
35
|
+
state[0] = 0x61707865;
|
|
36
|
+
state[1] = 0x3320646e;
|
|
37
|
+
state[2] = 0x79622d32;
|
|
38
|
+
state[3] = 0x6b206574;
|
|
39
|
+
for (let i = 0; i < 8; i++)
|
|
40
|
+
state[4 + i] = kv.getUint32(i * 4, true);
|
|
41
|
+
state[12] = counter >>> 0;
|
|
42
|
+
state[13] = nv.getUint32(0, true);
|
|
43
|
+
state[14] = nv.getUint32(4, true);
|
|
44
|
+
state[15] = nv.getUint32(8, true);
|
|
45
|
+
const w = new Uint32Array(state);
|
|
46
|
+
for (let i = 0; i < 10; i++) {
|
|
47
|
+
quarterRound(w, 0, 4, 8, 12);
|
|
48
|
+
quarterRound(w, 1, 5, 9, 13);
|
|
49
|
+
quarterRound(w, 2, 6, 10, 14);
|
|
50
|
+
quarterRound(w, 3, 7, 11, 15);
|
|
51
|
+
quarterRound(w, 0, 5, 10, 15);
|
|
52
|
+
quarterRound(w, 1, 6, 11, 12);
|
|
53
|
+
quarterRound(w, 2, 7, 8, 13);
|
|
54
|
+
quarterRound(w, 3, 4, 9, 14);
|
|
55
|
+
}
|
|
56
|
+
const out = new Uint8Array(64);
|
|
57
|
+
const ov = new DataView(out.buffer);
|
|
58
|
+
for (let i = 0; i < 16; i++) {
|
|
59
|
+
ov.setUint32(i * 4, (w[i] + state[i]) >>> 0, true);
|
|
60
|
+
}
|
|
61
|
+
return out;
|
|
62
|
+
}
|
|
63
|
+
function chacha20Xor(key, counter, nonce, input) {
|
|
64
|
+
const out = new Uint8Array(input.byteLength);
|
|
65
|
+
let blockCounter = counter;
|
|
66
|
+
for (let off = 0; off < input.byteLength; off += 64) {
|
|
67
|
+
const ks = chacha20Block(key, blockCounter++, nonce);
|
|
68
|
+
const n = Math.min(64, input.byteLength - off);
|
|
69
|
+
for (let i = 0; i < n; i++) {
|
|
70
|
+
out[off + i] = input[off + i] ^ ks[i];
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return out;
|
|
74
|
+
}
|
|
75
|
+
const POLY1305_P = (1n << 130n) - 5n;
|
|
76
|
+
function bytesToLeBigInt(bytes) {
|
|
77
|
+
let acc = 0n;
|
|
78
|
+
for (let i = bytes.byteLength - 1; i >= 0; i--) {
|
|
79
|
+
acc = (acc << 8n) | BigInt(bytes[i]);
|
|
80
|
+
}
|
|
81
|
+
return acc;
|
|
82
|
+
}
|
|
83
|
+
function poly1305Mac(msg, otk32) {
|
|
84
|
+
const rBytes = new Uint8Array(otk32.subarray(0, 16));
|
|
85
|
+
rBytes[3] = rBytes[3] & 15;
|
|
86
|
+
rBytes[7] = rBytes[7] & 15;
|
|
87
|
+
rBytes[11] = rBytes[11] & 15;
|
|
88
|
+
rBytes[15] = rBytes[15] & 15;
|
|
89
|
+
rBytes[4] = rBytes[4] & 252;
|
|
90
|
+
rBytes[8] = rBytes[8] & 252;
|
|
91
|
+
rBytes[12] = rBytes[12] & 252;
|
|
92
|
+
const r = bytesToLeBigInt(rBytes);
|
|
93
|
+
const s = bytesToLeBigInt(otk32.subarray(16, 32));
|
|
94
|
+
let acc = 0n;
|
|
95
|
+
for (let off = 0; off < msg.byteLength; off += 16) {
|
|
96
|
+
const chunk = msg.subarray(off, Math.min(off + 16, msg.byteLength));
|
|
97
|
+
const n = bytesToLeBigInt(chunk) | (1n << BigInt(chunk.byteLength * 8));
|
|
98
|
+
acc = ((acc + n) * r) % POLY1305_P;
|
|
99
|
+
}
|
|
100
|
+
const tagVal = (acc + s) & ((1n << 128n) - 1n);
|
|
101
|
+
const tag = new Uint8Array(16);
|
|
102
|
+
let t = tagVal;
|
|
103
|
+
for (let i = 0; i < 16; i++) {
|
|
104
|
+
tag[i] = Number(t & 0xffn);
|
|
105
|
+
t >>= 8n;
|
|
106
|
+
}
|
|
107
|
+
return tag;
|
|
108
|
+
}
|
|
109
|
+
function buildPoly1305Input(aad, ct) {
|
|
110
|
+
const padAad = (16 - (aad.byteLength % 16)) % 16;
|
|
111
|
+
const padCt = (16 - (ct.byteLength % 16)) % 16;
|
|
112
|
+
const buf = new Uint8Array(aad.byteLength + padAad + ct.byteLength + padCt + 16);
|
|
113
|
+
let off = 0;
|
|
114
|
+
buf.set(aad, off);
|
|
115
|
+
off += aad.byteLength + padAad;
|
|
116
|
+
buf.set(ct, off);
|
|
117
|
+
off += ct.byteLength + padCt;
|
|
118
|
+
const dv = new DataView(buf.buffer);
|
|
119
|
+
dv.setBigUint64(off, BigInt(aad.byteLength), true);
|
|
120
|
+
dv.setBigUint64(off + 8, BigInt(ct.byteLength), true);
|
|
121
|
+
return buf;
|
|
122
|
+
}
|
|
123
|
+
export function chacha20Poly1305Encrypt(key, nonce, plaintext, aad = new Uint8Array(0)) {
|
|
124
|
+
if (!(key instanceof Uint8Array) || key.byteLength !== 32) {
|
|
125
|
+
throw new RangeError(`ChaCha20-Poly1305 key must be 32-byte Uint8Array, got ${key instanceof Uint8Array ? key.byteLength : String(key)}`);
|
|
126
|
+
}
|
|
127
|
+
if (!(nonce instanceof Uint8Array) || nonce.byteLength !== 12) {
|
|
128
|
+
throw new RangeError(`ChaCha20-Poly1305 nonce must be 12-byte Uint8Array, got ${nonce instanceof Uint8Array ? nonce.byteLength : String(nonce)}`);
|
|
129
|
+
}
|
|
130
|
+
if (!(plaintext instanceof Uint8Array)) {
|
|
131
|
+
throw new RangeError('ChaCha20-Poly1305 plaintext must be a Uint8Array');
|
|
132
|
+
}
|
|
133
|
+
if (!(aad instanceof Uint8Array)) {
|
|
134
|
+
throw new RangeError('ChaCha20-Poly1305 aad must be a Uint8Array');
|
|
135
|
+
}
|
|
136
|
+
const otk = chacha20Block(key, 0, nonce).subarray(0, 32);
|
|
137
|
+
const ct = chacha20Xor(key, 1, nonce, plaintext);
|
|
138
|
+
const tag = poly1305Mac(buildPoly1305Input(aad, ct), otk);
|
|
139
|
+
const out = new Uint8Array(ct.byteLength + 16);
|
|
140
|
+
out.set(ct, 0);
|
|
141
|
+
out.set(tag, ct.byteLength);
|
|
142
|
+
return out;
|
|
143
|
+
}
|
|
144
|
+
export function chacha20Poly1305Decrypt(key, nonce, ciphertextWithTag, aad = new Uint8Array(0)) {
|
|
145
|
+
if (!(key instanceof Uint8Array) || key.byteLength !== 32) {
|
|
146
|
+
throw new RangeError(`ChaCha20-Poly1305 key must be 32-byte Uint8Array, got ${key instanceof Uint8Array ? key.byteLength : String(key)}`);
|
|
147
|
+
}
|
|
148
|
+
if (!(nonce instanceof Uint8Array) || nonce.byteLength !== 12) {
|
|
149
|
+
throw new RangeError(`ChaCha20-Poly1305 nonce must be 12-byte Uint8Array, got ${nonce instanceof Uint8Array ? nonce.byteLength : String(nonce)}`);
|
|
150
|
+
}
|
|
151
|
+
if (!(ciphertextWithTag instanceof Uint8Array) ||
|
|
152
|
+
ciphertextWithTag.byteLength < 16 ||
|
|
153
|
+
!(aad instanceof Uint8Array)) {
|
|
154
|
+
return null;
|
|
155
|
+
}
|
|
156
|
+
const ctLen = ciphertextWithTag.byteLength - 16;
|
|
157
|
+
const ct = ciphertextWithTag.subarray(0, ctLen);
|
|
158
|
+
const tag = ciphertextWithTag.subarray(ctLen);
|
|
159
|
+
const otk = chacha20Block(key, 0, nonce).subarray(0, 32);
|
|
160
|
+
const expected = poly1305Mac(buildPoly1305Input(aad, ct), otk);
|
|
161
|
+
let diff = 0;
|
|
162
|
+
for (let i = 0; i < 16; i++)
|
|
163
|
+
diff |= tag[i] ^ expected[i];
|
|
164
|
+
if (diff !== 0)
|
|
165
|
+
return null;
|
|
166
|
+
return chacha20Xor(key, 1, nonce, ct);
|
|
167
|
+
}
|
|
168
|
+
function isAllZero32(bytes) {
|
|
169
|
+
if (!(bytes instanceof Uint8Array) || bytes.byteLength !== 32)
|
|
170
|
+
return true;
|
|
171
|
+
let acc = 0;
|
|
172
|
+
for (let i = 0; i < 32; i++)
|
|
173
|
+
acc |= bytes[i];
|
|
174
|
+
return acc === 0;
|
|
175
|
+
}
|
|
176
|
+
function computeContributorySharedSecret(secret32, public32) {
|
|
177
|
+
if (!(secret32 instanceof Uint8Array) ||
|
|
178
|
+
secret32.byteLength !== 32 ||
|
|
179
|
+
!(public32 instanceof Uint8Array) ||
|
|
180
|
+
public32.byteLength !== 32) {
|
|
181
|
+
return null;
|
|
182
|
+
}
|
|
183
|
+
if (isAllZero32(public32)) {
|
|
184
|
+
return null;
|
|
185
|
+
}
|
|
186
|
+
let shared;
|
|
187
|
+
try {
|
|
188
|
+
shared = x25519.getSharedSecret(secret32, public32);
|
|
189
|
+
}
|
|
190
|
+
catch {
|
|
191
|
+
return null;
|
|
192
|
+
}
|
|
193
|
+
if (isAllZero32(shared)) {
|
|
194
|
+
return null;
|
|
195
|
+
}
|
|
196
|
+
return shared;
|
|
197
|
+
}
|
|
198
|
+
function sha256Labeled(label, data) {
|
|
199
|
+
if (!(data instanceof Uint8Array)) {
|
|
200
|
+
throw new RangeError(`sha256Labeled data must be a Uint8Array, got ${String(data)}`);
|
|
201
|
+
}
|
|
202
|
+
const lb = utf8Encoder.encode(label);
|
|
203
|
+
const buf = new Uint8Array(lb.byteLength + data.byteLength);
|
|
204
|
+
buf.set(lb, 0);
|
|
205
|
+
buf.set(data, lb.byteLength);
|
|
206
|
+
return sha256(buf);
|
|
207
|
+
}
|
|
208
|
+
function deriveDynMask72(shared) {
|
|
209
|
+
if (!(shared instanceof Uint8Array) || shared.byteLength !== 32) {
|
|
210
|
+
throw new RangeError('shared secret must be a 32-byte Uint8Array');
|
|
211
|
+
}
|
|
212
|
+
const buf = new Uint8Array(DYN_MASK_LABEL_BYTES.byteLength + 32 + 1);
|
|
213
|
+
buf.set(DYN_MASK_LABEL_BYTES, 0);
|
|
214
|
+
buf.set(shared, DYN_MASK_LABEL_BYTES.byteLength);
|
|
215
|
+
const out = new Uint8Array(MEMO_CT_LEN);
|
|
216
|
+
buf[DYN_MASK_LABEL_BYTES.byteLength + 32] = 0;
|
|
217
|
+
out.set(sha256(buf), 0);
|
|
218
|
+
buf[DYN_MASK_LABEL_BYTES.byteLength + 32] = 1;
|
|
219
|
+
out.set(sha256(buf), 32);
|
|
220
|
+
buf[DYN_MASK_LABEL_BYTES.byteLength + 32] = 2;
|
|
221
|
+
out.set(sha256(buf).subarray(0, 8), 64);
|
|
222
|
+
return out;
|
|
223
|
+
}
|
|
224
|
+
function deriveMemoSecrets(shared) {
|
|
225
|
+
const viewTag = sha256Labeled(KDF_DOMAIN_VIEW_TAG, shared).slice(0, 2);
|
|
226
|
+
const aeadKey = sha256Labeled(KDF_DOMAIN_MEMO_KEY, shared);
|
|
227
|
+
const nonce = sha256Labeled(KDF_DOMAIN_MEMO_NONCE, shared).slice(0, 12);
|
|
228
|
+
const tweak = bytes32ToFrMasked(sha256Labeled(KDF_DOMAIN_TWEAK, shared));
|
|
229
|
+
const blinding = bytes32ToFrMasked(sha256Labeled(KDF_DOMAIN_BLIND, shared));
|
|
230
|
+
const dynTweak = bytes32ToFrMasked(sha256Labeled(KDF_DOMAIN_DYN_TWEAK, shared));
|
|
231
|
+
const dynBlinding = bytes32ToFrMasked(sha256Labeled(KDF_DOMAIN_DYN_BLIND, shared));
|
|
232
|
+
const dynMask72 = deriveDynMask72(shared);
|
|
233
|
+
return { viewTag, aeadKey, nonce, tweak, blinding, dynTweak, dynBlinding, dynMask72 };
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Encrypt a versioned `106 B` SoA note memo (`view_tag[2] || epk[32] || ct[72]`) with
|
|
237
|
+
* `34 B` AEAD AAD (`view_tag[2] || epk[32]`) and `56 B` plaintext
|
|
238
|
+
* (`amount_le[8] || mint[32] || memo_ver[1](1) || memo_flags[1](0) || payment_id[14]`).
|
|
239
|
+
*
|
|
240
|
+
* Rejects low-order X25519 recipient public keys (non-contributory all-zero shared secret).
|
|
241
|
+
*/
|
|
242
|
+
export function encryptNoteMemo(recipientViewPk, ephSecret32, amount, mint32, paymentId) {
|
|
243
|
+
if (!(recipientViewPk instanceof Uint8Array) ||
|
|
244
|
+
recipientViewPk.byteLength !== 32 ||
|
|
245
|
+
!(ephSecret32 instanceof Uint8Array) ||
|
|
246
|
+
ephSecret32.byteLength !== 32 ||
|
|
247
|
+
!(mint32 instanceof Uint8Array) ||
|
|
248
|
+
mint32.byteLength !== 32) {
|
|
249
|
+
throw new RangeError('recipientViewPk, ephSecret32, and mint32 must be 32-byte Uint8Arrays');
|
|
250
|
+
}
|
|
251
|
+
if (typeof amount !== 'bigint' || amount < 0n || amount > U64_MAX) {
|
|
252
|
+
throw new RangeError(`amount must be a u64 bigint in 0..=2^64-1, got ${String(amount)}`);
|
|
253
|
+
}
|
|
254
|
+
if (paymentId !== undefined &&
|
|
255
|
+
(!(paymentId instanceof Uint8Array) || paymentId.byteLength !== MEMO_PAYMENT_ID_LEN)) {
|
|
256
|
+
throw new RangeError(`paymentId must be a ${MEMO_PAYMENT_ID_LEN}-byte Uint8Array, got ${paymentId instanceof Uint8Array ? paymentId.byteLength : String(paymentId)}`);
|
|
257
|
+
}
|
|
258
|
+
const ephPub = x25519.getPublicKey(ephSecret32);
|
|
259
|
+
const shared = computeContributorySharedSecret(ephSecret32, recipientViewPk);
|
|
260
|
+
if (!shared) {
|
|
261
|
+
throw new Error('Low-order X25519 recipientViewPk (non-contributory shared secret)');
|
|
262
|
+
}
|
|
263
|
+
const { viewTag, aeadKey, nonce, tweak, blinding } = deriveMemoSecrets(shared);
|
|
264
|
+
const aad = new Uint8Array(DYN_MEMO_HEAD_LEN);
|
|
265
|
+
aad.set(viewTag, 0);
|
|
266
|
+
aad.set(ephPub, 2);
|
|
267
|
+
const plain = new Uint8Array(MEMO_PLAINTEXT_LEN);
|
|
268
|
+
new DataView(plain.buffer).setBigUint64(0, amount, true);
|
|
269
|
+
plain.set(mint32, 8);
|
|
270
|
+
plain[40] = MEMO_VERSION_V1;
|
|
271
|
+
plain[41] = 0;
|
|
272
|
+
if (paymentId) {
|
|
273
|
+
plain.set(paymentId, 42);
|
|
274
|
+
}
|
|
275
|
+
const ct72 = chacha20Poly1305Encrypt(aeadKey, nonce, plain, aad);
|
|
276
|
+
const memo = new Uint8Array(MEMO_ENTRY_WIRE_LEN);
|
|
277
|
+
memo.set(aad, 0);
|
|
278
|
+
memo.set(ct72, DYN_MEMO_HEAD_LEN);
|
|
279
|
+
return { tweak, blinding, memo };
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Prepare domain-separated XOR-masked `72 B` `AUD1DYN1` dynamic swap memo parameters for
|
|
283
|
+
* `ExecViaEscrow`. Rejects low-order X25519 recipient public keys.
|
|
284
|
+
*/
|
|
285
|
+
export function prepareDynamicSwapMemo(recipientViewPk, ephSecret32) {
|
|
286
|
+
if (!(recipientViewPk instanceof Uint8Array) ||
|
|
287
|
+
recipientViewPk.byteLength !== 32 ||
|
|
288
|
+
!(ephSecret32 instanceof Uint8Array) ||
|
|
289
|
+
ephSecret32.byteLength !== 32) {
|
|
290
|
+
throw new RangeError('recipientViewPk and ephSecret32 must be 32-byte Uint8Arrays');
|
|
291
|
+
}
|
|
292
|
+
const ephPub = x25519.getPublicKey(ephSecret32);
|
|
293
|
+
const shared = computeContributorySharedSecret(ephSecret32, recipientViewPk);
|
|
294
|
+
if (!shared) {
|
|
295
|
+
throw new Error('Low-order X25519 recipientViewPk (non-contributory shared secret)');
|
|
296
|
+
}
|
|
297
|
+
const { viewTag, dynTweak, dynBlinding, dynMask72 } = deriveMemoSecrets(shared);
|
|
298
|
+
const dynHead34 = new Uint8Array(DYN_MEMO_HEAD_LEN);
|
|
299
|
+
dynHead34.set(viewTag, 0);
|
|
300
|
+
dynHead34.set(ephPub, 2);
|
|
301
|
+
return { tweak: dynTweak, blinding: dynBlinding, dynHead34, dynMask72 };
|
|
302
|
+
}
|
|
303
|
+
const KDF_DOMAIN_HIT_DEDUP = 'audren/v1/hit_dedup';
|
|
304
|
+
function decryptMemoPayloadInternal(viewSk, viewTag, epk, ct72) {
|
|
305
|
+
if (!(viewSk instanceof Uint8Array) ||
|
|
306
|
+
viewSk.byteLength !== 32 ||
|
|
307
|
+
!(viewTag instanceof Uint8Array) ||
|
|
308
|
+
viewTag.byteLength !== 2 ||
|
|
309
|
+
!(epk instanceof Uint8Array) ||
|
|
310
|
+
epk.byteLength !== 32 ||
|
|
311
|
+
!(ct72 instanceof Uint8Array) ||
|
|
312
|
+
ct72.byteLength !== MEMO_CT_LEN) {
|
|
313
|
+
return null;
|
|
314
|
+
}
|
|
315
|
+
const shared = computeContributorySharedSecret(viewSk, epk);
|
|
316
|
+
if (!shared) {
|
|
317
|
+
return null;
|
|
318
|
+
}
|
|
319
|
+
const { viewTag: expVt, aeadKey, nonce, tweak, blinding, dynTweak, dynBlinding, dynMask72, } = deriveMemoSecrets(shared);
|
|
320
|
+
if (expVt[0] !== viewTag[0] || expVt[1] !== viewTag[1]) {
|
|
321
|
+
shared.fill(0);
|
|
322
|
+
return null;
|
|
323
|
+
}
|
|
324
|
+
const aad = new Uint8Array(DYN_MEMO_HEAD_LEN);
|
|
325
|
+
aad.set(viewTag, 0);
|
|
326
|
+
aad.set(epk, 2);
|
|
327
|
+
let memo = null;
|
|
328
|
+
const plain = chacha20Poly1305Decrypt(aeadKey, nonce, ct72, aad);
|
|
329
|
+
if (plain !== null) {
|
|
330
|
+
if (plain.byteLength !== MEMO_PLAINTEXT_LEN || plain[40] !== MEMO_VERSION_V1) {
|
|
331
|
+
plain.fill(0);
|
|
332
|
+
shared.fill(0);
|
|
333
|
+
return null;
|
|
334
|
+
}
|
|
335
|
+
const dv = new DataView(plain.buffer, plain.byteOffset, plain.byteLength);
|
|
336
|
+
memo = {
|
|
337
|
+
tweak,
|
|
338
|
+
blinding,
|
|
339
|
+
amount: dv.getBigUint64(0, true),
|
|
340
|
+
mint: plain.slice(8, 40),
|
|
341
|
+
memoVersion: plain[40],
|
|
342
|
+
memoFlags: plain[41],
|
|
343
|
+
paymentId: plain.slice(42, 42 + MEMO_PAYMENT_ID_LEN),
|
|
344
|
+
isDynamic: false,
|
|
345
|
+
};
|
|
346
|
+
plain.fill(0);
|
|
347
|
+
}
|
|
348
|
+
else {
|
|
349
|
+
const unmasked = new Uint8Array(MEMO_CT_LEN);
|
|
350
|
+
for (let i = 0; i < MEMO_CT_LEN; i++) {
|
|
351
|
+
unmasked[i] = ct72[i] ^ dynMask72[i];
|
|
352
|
+
}
|
|
353
|
+
let magicOk = true;
|
|
354
|
+
for (let i = 0; i < 8; i++) {
|
|
355
|
+
if (unmasked[i] !== DYN_MAGIC_BYTES[i]) {
|
|
356
|
+
magicOk = false;
|
|
357
|
+
break;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
let paddingOk = magicOk;
|
|
361
|
+
if (paddingOk) {
|
|
362
|
+
let padOr = 0;
|
|
363
|
+
for (let i = 48; i < MEMO_CT_LEN; i++) {
|
|
364
|
+
padOr |= unmasked[i];
|
|
365
|
+
}
|
|
366
|
+
paddingOk = padOr === 0;
|
|
367
|
+
}
|
|
368
|
+
if (paddingOk) {
|
|
369
|
+
const dv = new DataView(unmasked.buffer);
|
|
370
|
+
memo = {
|
|
371
|
+
tweak: dynTweak,
|
|
372
|
+
blinding: dynBlinding,
|
|
373
|
+
amount: dv.getBigUint64(8, true),
|
|
374
|
+
mint: unmasked.slice(16, 48),
|
|
375
|
+
memoVersion: MEMO_VERSION_V1,
|
|
376
|
+
memoFlags: 0,
|
|
377
|
+
paymentId: new Uint8Array(MEMO_PAYMENT_ID_LEN),
|
|
378
|
+
isDynamic: true,
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
unmasked.fill(0);
|
|
382
|
+
}
|
|
383
|
+
if (!memo) {
|
|
384
|
+
shared.fill(0);
|
|
385
|
+
return null;
|
|
386
|
+
}
|
|
387
|
+
// Key cross-subtree Pass 1D hit deduplication on the X25519 shared secret (domain-separated)
|
|
388
|
+
// so all 16 Curve25519 torsion + bit-255 + non-canonical `epk` equivalents for `AUD1DYN1`
|
|
389
|
+
// dynamic swap memos collapse into a single deduplication entry.
|
|
390
|
+
const dedupTag = sha256Labeled(KDF_DOMAIN_HIT_DEDUP, shared);
|
|
391
|
+
shared.fill(0);
|
|
392
|
+
return { memo, dedupTag };
|
|
393
|
+
}
|
|
394
|
+
const PUBLIC_CANON_EPK_SCALAR = new Uint8Array(32);
|
|
395
|
+
PUBLIC_CANON_EPK_SCALAR[0] = 0x08;
|
|
396
|
+
PUBLIC_CANON_EPK_SCALAR[31] = 0x40;
|
|
397
|
+
/**
|
|
398
|
+
* Public, key-independent canonicalization of a Curve25519 ephemeral public key modulo cofactor 8
|
|
399
|
+
* and bit-255 / `u + p` non-canonicality (matches Rust `canonicalize_public_epk` bit-for-bit).
|
|
400
|
+
* Returns `null` for all-zero and all 12 low-order points (curve + twist).
|
|
401
|
+
*/
|
|
402
|
+
export function canonicalizePublicEpk(epk) {
|
|
403
|
+
return computeContributorySharedSecret(PUBLIC_CANON_EPK_SCALAR, epk);
|
|
404
|
+
}
|
|
405
|
+
/**
|
|
406
|
+
* AEAD / `AUD1DYN1` pre-filter for full `MemoPage` scans (`'strict'` mode): returns a domain-
|
|
407
|
+
* separated `32 B` deduplication tag `SHA256("audren/v1/hit_dedup" || shared)` (collapsing all 16
|
|
408
|
+
* Curve25519 torsion + bit-255 `epk` equivalents) iff the payload decrypts/unmasks cleanly, or
|
|
409
|
+
* `null` otherwise, without exposing any unverified note plaintext or raw shared secret.
|
|
410
|
+
*/
|
|
411
|
+
export function computeMemoScanDedupTag(viewSk, viewTag, epk, ct72) {
|
|
412
|
+
return decryptMemoPayloadInternal(viewSk, viewTag, epk, ct72)?.dedupTag ?? null;
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* Boolean-only AEAD / `AUD1DYN1` pre-filter for full `MemoPage` scans (`Strict` mode):
|
|
416
|
+
* rejects random `1/65536` `viewTag` false positives and `(viewTag, epk, junkCt)` probes before
|
|
417
|
+
* Pass 1D scheduling without exposing any unverified note plaintext.
|
|
418
|
+
*/
|
|
419
|
+
export function checkMemoPayloadForScan(viewSk, viewTag, epk, ct72) {
|
|
420
|
+
return computeMemoScanDedupTag(viewSk, viewTag, epk, ct72) !== null;
|
|
421
|
+
}
|
|
422
|
+
/**
|
|
423
|
+
* Decrypt or unmask a `106 B` `MemoPage` entry (`viewTag[2]`, `epk[32]`, `ct72[72]`) AND
|
|
424
|
+
* mandatorily verify `computeNoteCommitment(stealthOwner, deriveAssetId(mint), amount, blinding) == onchainCmBe`.
|
|
425
|
+
* Rejects low-order X25519 ephemeral keys, tampered AEAD AAD, non-v1 static memo versions,
|
|
426
|
+
* and non-zero trailing padding bytes on `AUD1DYN1` dynamic swap memos.
|
|
427
|
+
*/
|
|
428
|
+
export function tryDecryptAndVerifyMemoEntry(fvk, viewTag, epk, ct72, onchainCmBe) {
|
|
429
|
+
if (!fvk ||
|
|
430
|
+
typeof fvk !== 'object' ||
|
|
431
|
+
!(fvk.viewSk instanceof Uint8Array) ||
|
|
432
|
+
fvk.viewSk.byteLength !== 32 ||
|
|
433
|
+
!(fvk.spendPk instanceof Uint8Array) ||
|
|
434
|
+
fvk.spendPk.byteLength !== 32 ||
|
|
435
|
+
!(onchainCmBe instanceof Uint8Array) ||
|
|
436
|
+
onchainCmBe.byteLength !== 32) {
|
|
437
|
+
return null;
|
|
438
|
+
}
|
|
439
|
+
const decoded = decryptMemoPayloadInternal(fvk.viewSk, viewTag, epk, ct72);
|
|
440
|
+
if (!decoded)
|
|
441
|
+
return null;
|
|
442
|
+
const candidate = decoded.memo;
|
|
443
|
+
let spendPkFr;
|
|
444
|
+
try {
|
|
445
|
+
spendPkFr = frFromBeBytesStrict(fvk.spendPk);
|
|
446
|
+
}
|
|
447
|
+
catch {
|
|
448
|
+
return null;
|
|
449
|
+
}
|
|
450
|
+
const stealth = deriveStealthOwner(spendPkFr, candidate.tweak);
|
|
451
|
+
const assetId = deriveAssetId(candidate.mint);
|
|
452
|
+
const expectedCm = frToBeBytes(computeNoteCommitment(stealth, assetId, candidate.amount, candidate.blinding));
|
|
453
|
+
let cmDiff = 0;
|
|
454
|
+
for (let i = 0; i < 32; i++) {
|
|
455
|
+
cmDiff |= expectedCm[i] ^ onchainCmBe[i];
|
|
456
|
+
}
|
|
457
|
+
if (cmDiff !== 0)
|
|
458
|
+
return null;
|
|
459
|
+
return candidate;
|
|
460
|
+
}
|
|
461
|
+
/**
|
|
462
|
+
* Fast X25519 + 2-byte view-tag prefilter for SoA `MemoPage` scanning
|
|
463
|
+
* (`1 / 65,536` false-positive rate; rejects low-order X25519 ephemeral keys).
|
|
464
|
+
*/
|
|
465
|
+
export function checkMemoViewTag(viewSk, viewTag, epk) {
|
|
466
|
+
if (!(viewSk instanceof Uint8Array) ||
|
|
467
|
+
viewSk.byteLength !== 32 ||
|
|
468
|
+
!(viewTag instanceof Uint8Array) ||
|
|
469
|
+
viewTag.byteLength !== 2 ||
|
|
470
|
+
!(epk instanceof Uint8Array) ||
|
|
471
|
+
epk.byteLength !== 32) {
|
|
472
|
+
return false;
|
|
473
|
+
}
|
|
474
|
+
const shared = computeContributorySharedSecret(viewSk, epk);
|
|
475
|
+
if (!shared) {
|
|
476
|
+
return false;
|
|
477
|
+
}
|
|
478
|
+
const expVt = sha256Labeled(KDF_DOMAIN_VIEW_TAG, shared);
|
|
479
|
+
return expVt[0] === viewTag[0] && expVt[1] === viewTag[1];
|
|
480
|
+
}
|
|
481
|
+
function randomUniformInt(bound) {
|
|
482
|
+
if (!Number.isInteger(bound) || bound <= 0) {
|
|
483
|
+
throw new RangeError(`bound must be a positive integer, got ${bound}`);
|
|
484
|
+
}
|
|
485
|
+
if (bound === 1)
|
|
486
|
+
return 0;
|
|
487
|
+
// Unbiased rejection sampling over uint32 using CSPRNG (`crypto.getRandomValues`).
|
|
488
|
+
const maxUnbiased = Math.floor(0x1_0000_0000 / bound) * bound;
|
|
489
|
+
const buf = new Uint32Array(1);
|
|
490
|
+
for (;;) {
|
|
491
|
+
globalThis.crypto.getRandomValues(buf);
|
|
492
|
+
const v = buf[0];
|
|
493
|
+
if (v < maxUnbiased)
|
|
494
|
+
return v % bound;
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
/**
|
|
498
|
+
* Sample an unbiased uniform output lane ID in `0..activeLanes`, strictly independent of input notes.
|
|
499
|
+
*/
|
|
500
|
+
export function pickRandomLaneId(activeLanes) {
|
|
501
|
+
if (!Number.isInteger(activeLanes) || activeLanes < 1 || activeLanes > NUM_LANES) {
|
|
502
|
+
throw new RangeError(`activeLanes must be an integer in 1..=${NUM_LANES}, got ${activeLanes}`);
|
|
503
|
+
}
|
|
504
|
+
return randomUniformInt(activeLanes);
|
|
505
|
+
}
|
|
506
|
+
/**
|
|
507
|
+
* Sample an unbiased uniform `ExecViaEscrow` shard ID in `0..numShards`, strictly independent of input notes.
|
|
508
|
+
*/
|
|
509
|
+
export function pickRandomEscrowShard(numShards = NUM_ESCROW_SHARDS) {
|
|
510
|
+
if (!Number.isInteger(numShards) || numShards < 1 || numShards > NUM_ESCROW_SHARDS) {
|
|
511
|
+
throw new RangeError(`numShards must be an integer in 1..=${NUM_ESCROW_SHARDS}, got ${numShards}`);
|
|
512
|
+
}
|
|
513
|
+
return randomUniformInt(numShards);
|
|
514
|
+
}
|
|
515
|
+
/** Convenience wrapper accepting a single 106-byte wire memo slice. */
|
|
516
|
+
export function tryDecryptAndVerifyMemo(fvk, memo, onchainCmBe) {
|
|
517
|
+
if (!(memo instanceof Uint8Array) || memo.byteLength !== MEMO_ENTRY_WIRE_LEN)
|
|
518
|
+
return null;
|
|
519
|
+
return tryDecryptAndVerifyMemoEntry(fvk, memo.subarray(0, 2), memo.subarray(2, 34), memo.subarray(34, MEMO_ENTRY_WIRE_LEN), onchainCmBe);
|
|
520
|
+
}
|