@bounded-sh/client 0.0.18 → 0.0.20

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 (30) hide show
  1. package/dist/auth/providers/offchain-auth-provider.d.ts +2 -5
  2. package/dist/index.d.ts +0 -1
  3. package/dist/index.js +29 -2638
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.mjs +30 -2637
  6. package/dist/index.mjs.map +1 -1
  7. package/dist/{index.native-Dr3FOz16.js → index.native-BtM38BVP.js} +33 -2642
  8. package/dist/index.native-BtM38BVP.js.map +1 -0
  9. package/dist/{index.native-DahSrHrM.mjs → index.native-w12aoVNW.mjs} +34 -2641
  10. package/dist/index.native-w12aoVNW.mjs.map +1 -0
  11. package/dist/index.native.d.ts +0 -1
  12. package/dist/index.native.js +2 -4
  13. package/dist/index.native.js.map +1 -1
  14. package/dist/index.native.mjs +2 -2
  15. package/dist/{phantom-wallet-provider-CeLJEdcL.js → phantom-wallet-provider-BJWelaiH.js} +2 -2
  16. package/dist/{phantom-wallet-provider-CeLJEdcL.js.map → phantom-wallet-provider-BJWelaiH.js.map} +1 -1
  17. package/dist/{phantom-wallet-provider-l-CNnHPs.mjs → phantom-wallet-provider-BT2V4EU5.mjs} +2 -2
  18. package/dist/{phantom-wallet-provider-l-CNnHPs.mjs.map → phantom-wallet-provider-BT2V4EU5.mjs.map} +1 -1
  19. package/dist/{privy-wallet-provider-BSwx78fY.js → privy-wallet-provider-BSh0aZVL.js} +3 -3
  20. package/dist/{privy-wallet-provider-BSwx78fY.js.map → privy-wallet-provider-BSh0aZVL.js.map} +1 -1
  21. package/dist/{privy-wallet-provider-BWM8TNO9.mjs → privy-wallet-provider-LV1X7hZe.mjs} +3 -3
  22. package/dist/{privy-wallet-provider-BWM8TNO9.mjs.map → privy-wallet-provider-LV1X7hZe.mjs.map} +1 -1
  23. package/dist/{solana-mobile-wallet-provider-BvCrAgk8.js → solana-mobile-wallet-provider-CKsfiyDd.js} +2 -2
  24. package/dist/{solana-mobile-wallet-provider-BvCrAgk8.js.map → solana-mobile-wallet-provider-CKsfiyDd.js.map} +1 -1
  25. package/dist/{solana-mobile-wallet-provider-C78VWmM-.mjs → solana-mobile-wallet-provider-NLe9_mEn.mjs} +2 -2
  26. package/dist/{solana-mobile-wallet-provider-C78VWmM-.mjs.map → solana-mobile-wallet-provider-NLe9_mEn.mjs.map} +1 -1
  27. package/package.json +2 -2
  28. package/dist/auth/providers/mock-auth-provider.d.ts +0 -50
  29. package/dist/index.native-DahSrHrM.mjs.map +0 -1
  30. package/dist/index.native-Dr3FOz16.js.map +0 -1
package/dist/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
  'use strict';
2
2
 
3
3
  var core = require('@bounded-sh/core');
4
- var web3_js = require('@solana/web3.js');
5
4
  var React$1 = require('react');
5
+ var web3_js = require('@solana/web3.js');
6
6
  var anchor = require('@coral-xyz/anchor');
7
7
  var ReactDOM$1 = require('react-dom/client');
8
8
 
@@ -180,151 +180,11 @@ function detectAndroid() {
180
180
  return false;
181
181
  }
182
182
 
183
- // Default test wallet address (Solana devnet address)
184
- const DEFAULT_TEST_ADDRESS = 'HKbZbRR7jWWR5VRN8KFjvTCHEzJQgameYxKQxh2gPoof';
185
- // Session storage key to track if user explicitly called login()
186
- const EXPLICIT_LOGIN_KEY = 'mock-auth-explicit-login';
187
- /**
188
- * MockAuthProvider bypasses real wallet authentication for testing purposes.
189
- * Uses session storage to simulate logged-in user, with fallback to default test address.
190
- *
191
- * This is enabled by passing mockAuth: true to init()
192
- */
193
- class MockAuthProvider {
194
- /**
195
- * Mock login - reads user from session storage or uses default test address
196
- */
197
- async login() {
198
- let address = getPlatform().sessionStorage.getItem('test-user-address');
199
- if (!address) {
200
- address = DEFAULT_TEST_ADDRESS;
201
- // Store default address in session storage so it gets used in API calls
202
- getPlatform().sessionStorage.setItem('test-user-address', DEFAULT_TEST_ADDRESS);
203
- console.log('[MockAuth] Using default test address:', address);
204
- }
205
- // Mark that user explicitly called login() - allows restoreSession on refresh
206
- getPlatform().sessionStorage.setItem(EXPLICIT_LOGIN_KEY, 'true');
207
- console.log('[MockAuth] Mock login successful:', address);
208
- return {
209
- address,
210
- provider: this
211
- };
212
- }
213
- /**
214
- * Mock transaction - returns fake signature without actual blockchain interaction
215
- * The real transaction will be handled by the backend using test headers
216
- */
217
- async runTransaction(evmTransactionData, solTransactionData, options) {
218
- const address = getPlatform().sessionStorage.getItem('test-user-address') || DEFAULT_TEST_ADDRESS;
219
- // Generate a fake transaction signature
220
- const fakeSignature = `mock_tx_${Math.random().toString(36).substring(2, 15)}`;
221
- console.log('[MockAuth] Mock transaction executed:', {
222
- address,
223
- signature: fakeSignature,
224
- evmTx: evmTransactionData,
225
- solTx: solTransactionData
226
- });
227
- return {
228
- transactionSignature: fakeSignature,
229
- signedTransaction: fakeSignature,
230
- blockNumber: 0,
231
- gasUsed: '0'
232
- };
233
- }
234
- /**
235
- * Mock message signing
236
- */
237
- async signMessage(message) {
238
- const address = getPlatform().sessionStorage.getItem('test-user-address') || DEFAULT_TEST_ADDRESS;
239
- // Generate a fake signature
240
- const fakeSignature = `mock_sig_${Math.random().toString(36).substring(2, 15)}`;
241
- console.log('[MockAuth] Mock message signed:', {
242
- address,
243
- message,
244
- signature: fakeSignature
245
- });
246
- return fakeSignature;
247
- }
248
- async signMessageMock(message) {
249
- const address = getPlatform().sessionStorage.getItem('test-user-address') || DEFAULT_TEST_ADDRESS;
250
- // Offchain transactions are verified by hashing the exact message. Keep the
251
- // mock provider deterministic so mockAuth exercises the same integrity check.
252
- const signature = await this.generateOffchainSignature(message);
253
- console.log('[MockAuth] Mock message signed:', {
254
- address,
255
- message,
256
- signature
257
- });
258
- return signature;
259
- }
260
- async generateOffchainSignature(message) {
261
- const platform = getPlatform();
262
- const data = platform.textEncode(message);
263
- const hashBuffer = await platform.sha256(data);
264
- const hashArray = Array.from(new Uint8Array(hashBuffer));
265
- return platform.btoa(String.fromCharCode(...hashArray));
266
- }
267
- /**
268
- * Mock transaction signing - returns the transaction as-is (no actual signing)
269
- */
270
- async signTransaction(transaction) {
271
- console.log('[MockAuth] Mock transaction signing (no-op)');
272
- return transaction;
273
- }
274
- /**
275
- * Sign and submit transaction - not supported in mock environment.
276
- * See the real providers (PhantomWalletProvider, SolanaKeypairProvider)
277
- * for the full implementation with blockhash handling and feePayer support.
278
- */
279
- async signAndSubmitTransaction(_transaction, _feePayer) {
280
- throw new Error('signAndSubmitTransaction is not supported in the mock environment');
281
- }
282
- /**
283
- * Restore session - only restores if user previously called login() explicitly.
284
- * This prevents auto-login on first page load, but allows session persistence after login.
285
- */
286
- async restoreSession() {
287
- const explicitlyLoggedIn = getPlatform().sessionStorage.getItem(EXPLICIT_LOGIN_KEY);
288
- if (!explicitlyLoggedIn) {
289
- console.log('[MockAuth] No explicit login found. Call login() to authenticate.');
290
- return null;
291
- }
292
- const address = getPlatform().sessionStorage.getItem('test-user-address');
293
- if (!address) {
294
- console.log('[MockAuth] No previous session found');
295
- return null;
296
- }
297
- console.log('[MockAuth] Session restored:', address);
298
- return {
299
- address,
300
- provider: this
301
- };
302
- }
303
- /**
304
- * Mock logout - clears test user from session storage
305
- */
306
- async logout() {
307
- getPlatform().sessionStorage.removeItem('test-user-address');
308
- getPlatform().sessionStorage.removeItem('test-user-id');
309
- getPlatform().sessionStorage.removeItem(EXPLICIT_LOGIN_KEY);
310
- console.log('[MockAuth] Mock logout successful');
311
- }
312
- /**
313
- * Get native wallet methods (not needed for mock auth)
314
- */
315
- async getNativeMethods() {
316
- return {
317
- // Return empty object - no native methods needed for mock auth
318
- };
319
- }
320
- }
321
-
322
183
  /**
323
184
  * OffchainAuthProvider wraps a real auth provider (e.g., Phantom) for the poofnet environment.
324
185
  *
325
- * For signMessageMock, this shows a custom confirmation modal and generates a mock signature.
326
- * This is used for offchain transaction signing. For real wallet signing, use signMessage
327
- * which delegates to the wrapped provider.
186
+ * signMessage shows a custom confirmation modal and then delegates to the wrapped
187
+ * provider for a real detached signature over the exact offchain transaction message.
328
188
  */
329
189
  class OffchainAuthProvider {
330
190
  constructor(wrappedProvider, config = {}) {
@@ -375,13 +235,7 @@ class OffchainAuthProvider {
375
235
  }
376
236
  // ============ signMessage delegates to wrapped provider ============
377
237
  async signMessage(message) {
378
- // Delegate to wrapped provider for real wallet signing
379
- return this.wrappedProvider.signMessage(message);
380
- }
381
- // ============ Mock signMessage with Custom Modal for Offchain Transactions ============
382
- async signMessageMock(message) {
383
238
  var _a, _b, _c, _d, _e, _f;
384
- // Callback: signing started
385
239
  (_b = (_a = this.config).onSigningStart) === null || _b === void 0 ? void 0 : _b.call(_a);
386
240
  try {
387
241
  // On RN (no DOM), auto-confirm — there's no modal to show.
@@ -392,14 +246,12 @@ class OffchainAuthProvider {
392
246
  throw new Error("Transaction rejected by user");
393
247
  }
394
248
  }
395
- // Generate mock signature (hash of message)
396
- const signature = await this.generateMockSignature(message);
249
+ const signature = await this.wrappedProvider.signMessage(message);
397
250
  // Callback: signing complete
398
251
  (_d = (_c = this.config).onSigningComplete) === null || _d === void 0 ? void 0 : _d.call(_c, signature);
399
252
  return signature;
400
253
  }
401
254
  catch (error) {
402
- // Callback: signing error
403
255
  (_f = (_e = this.config).onSigningError) === null || _f === void 0 ? void 0 : _f.call(_e, error);
404
256
  throw error;
405
257
  }
@@ -1002,26 +854,12 @@ class OffchainAuthProvider {
1002
854
  }
1003
855
  `;
1004
856
  }
1005
- async generateMockSignature(message) {
1006
- // Generate a deterministic signature using SHA-256 hash of message
1007
- // This can be verified on the backend by hashing the same message
1008
- const platform = getPlatform();
1009
- const data = platform.textEncode(message);
1010
- const hashBuffer = await platform.sha256(data);
1011
- const hashArray = Array.from(new Uint8Array(hashBuffer));
1012
- const base64 = platform.btoa(String.fromCharCode(...hashArray));
1013
- return base64;
1014
- }
1015
857
  }
1016
858
 
1017
859
  function getDefaultExportFromCjs (x) {
1018
860
  return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
1019
861
  }
1020
862
 
1021
- function getDefaultExportFromNamespaceIfNotNamed (n) {
1022
- return n && Object.prototype.hasOwnProperty.call(n, 'default') && Object.keys(n).length === 1 ? n['default'] : n;
1023
- }
1024
-
1025
863
  var safeBuffer = {exports: {}};
1026
864
 
1027
865
  var buffer = {};
@@ -3611,2444 +3449,24 @@ var index = /*#__PURE__*/Object.freeze({
3611
3449
  default: bs58
3612
3450
  });
3613
3451
 
3614
- function commonjsRequire(path) {
3615
- throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');
3616
- }
3617
-
3618
- var naclFast = {exports: {}};
3619
-
3620
- var _nodeResolve_empty = {};
3621
-
3622
- var _nodeResolve_empty$1 = /*#__PURE__*/Object.freeze({
3623
- __proto__: null,
3624
- default: _nodeResolve_empty
3625
- });
3626
-
3627
- var require$$0 = /*@__PURE__*/getDefaultExportFromNamespaceIfNotNamed(_nodeResolve_empty$1);
3628
-
3629
- var hasRequiredNaclFast;
3630
-
3631
- function requireNaclFast () {
3632
- if (hasRequiredNaclFast) return naclFast.exports;
3633
- hasRequiredNaclFast = 1;
3634
- (function (module) {
3635
- (function(nacl) {
3636
-
3637
- // Ported in 2014 by Dmitry Chestnykh and Devi Mandiri.
3638
- // Public domain.
3639
- //
3640
- // Implementation derived from TweetNaCl version 20140427.
3641
- // See for details: http://tweetnacl.cr.yp.to/
3642
-
3643
- var gf = function(init) {
3644
- var i, r = new Float64Array(16);
3645
- if (init) for (i = 0; i < init.length; i++) r[i] = init[i];
3646
- return r;
3647
- };
3648
-
3649
- // Pluggable, initialized in high-level API below.
3650
- var randombytes = function(/* x, n */) { throw new Error('no PRNG'); };
3651
-
3652
- var _0 = new Uint8Array(16);
3653
- var _9 = new Uint8Array(32); _9[0] = 9;
3654
-
3655
- var gf0 = gf(),
3656
- gf1 = gf([1]),
3657
- _121665 = gf([0xdb41, 1]),
3658
- D = gf([0x78a3, 0x1359, 0x4dca, 0x75eb, 0xd8ab, 0x4141, 0x0a4d, 0x0070, 0xe898, 0x7779, 0x4079, 0x8cc7, 0xfe73, 0x2b6f, 0x6cee, 0x5203]),
3659
- D2 = gf([0xf159, 0x26b2, 0x9b94, 0xebd6, 0xb156, 0x8283, 0x149a, 0x00e0, 0xd130, 0xeef3, 0x80f2, 0x198e, 0xfce7, 0x56df, 0xd9dc, 0x2406]),
3660
- X = gf([0xd51a, 0x8f25, 0x2d60, 0xc956, 0xa7b2, 0x9525, 0xc760, 0x692c, 0xdc5c, 0xfdd6, 0xe231, 0xc0a4, 0x53fe, 0xcd6e, 0x36d3, 0x2169]),
3661
- Y = gf([0x6658, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666]),
3662
- I = gf([0xa0b0, 0x4a0e, 0x1b27, 0xc4ee, 0xe478, 0xad2f, 0x1806, 0x2f43, 0xd7a7, 0x3dfb, 0x0099, 0x2b4d, 0xdf0b, 0x4fc1, 0x2480, 0x2b83]);
3663
-
3664
- function ts64(x, i, h, l) {
3665
- x[i] = (h >> 24) & 0xff;
3666
- x[i+1] = (h >> 16) & 0xff;
3667
- x[i+2] = (h >> 8) & 0xff;
3668
- x[i+3] = h & 0xff;
3669
- x[i+4] = (l >> 24) & 0xff;
3670
- x[i+5] = (l >> 16) & 0xff;
3671
- x[i+6] = (l >> 8) & 0xff;
3672
- x[i+7] = l & 0xff;
3673
- }
3674
-
3675
- function vn(x, xi, y, yi, n) {
3676
- var i,d = 0;
3677
- for (i = 0; i < n; i++) d |= x[xi+i]^y[yi+i];
3678
- return (1 & ((d - 1) >>> 8)) - 1;
3679
- }
3680
-
3681
- function crypto_verify_16(x, xi, y, yi) {
3682
- return vn(x,xi,y,yi,16);
3683
- }
3684
-
3685
- function crypto_verify_32(x, xi, y, yi) {
3686
- return vn(x,xi,y,yi,32);
3687
- }
3688
-
3689
- function core_salsa20(o, p, k, c) {
3690
- var j0 = c[ 0] & 0xff | (c[ 1] & 0xff)<<8 | (c[ 2] & 0xff)<<16 | (c[ 3] & 0xff)<<24,
3691
- j1 = k[ 0] & 0xff | (k[ 1] & 0xff)<<8 | (k[ 2] & 0xff)<<16 | (k[ 3] & 0xff)<<24,
3692
- j2 = k[ 4] & 0xff | (k[ 5] & 0xff)<<8 | (k[ 6] & 0xff)<<16 | (k[ 7] & 0xff)<<24,
3693
- j3 = k[ 8] & 0xff | (k[ 9] & 0xff)<<8 | (k[10] & 0xff)<<16 | (k[11] & 0xff)<<24,
3694
- j4 = k[12] & 0xff | (k[13] & 0xff)<<8 | (k[14] & 0xff)<<16 | (k[15] & 0xff)<<24,
3695
- j5 = c[ 4] & 0xff | (c[ 5] & 0xff)<<8 | (c[ 6] & 0xff)<<16 | (c[ 7] & 0xff)<<24,
3696
- j6 = p[ 0] & 0xff | (p[ 1] & 0xff)<<8 | (p[ 2] & 0xff)<<16 | (p[ 3] & 0xff)<<24,
3697
- j7 = p[ 4] & 0xff | (p[ 5] & 0xff)<<8 | (p[ 6] & 0xff)<<16 | (p[ 7] & 0xff)<<24,
3698
- j8 = p[ 8] & 0xff | (p[ 9] & 0xff)<<8 | (p[10] & 0xff)<<16 | (p[11] & 0xff)<<24,
3699
- j9 = p[12] & 0xff | (p[13] & 0xff)<<8 | (p[14] & 0xff)<<16 | (p[15] & 0xff)<<24,
3700
- j10 = c[ 8] & 0xff | (c[ 9] & 0xff)<<8 | (c[10] & 0xff)<<16 | (c[11] & 0xff)<<24,
3701
- j11 = k[16] & 0xff | (k[17] & 0xff)<<8 | (k[18] & 0xff)<<16 | (k[19] & 0xff)<<24,
3702
- j12 = k[20] & 0xff | (k[21] & 0xff)<<8 | (k[22] & 0xff)<<16 | (k[23] & 0xff)<<24,
3703
- j13 = k[24] & 0xff | (k[25] & 0xff)<<8 | (k[26] & 0xff)<<16 | (k[27] & 0xff)<<24,
3704
- j14 = k[28] & 0xff | (k[29] & 0xff)<<8 | (k[30] & 0xff)<<16 | (k[31] & 0xff)<<24,
3705
- j15 = c[12] & 0xff | (c[13] & 0xff)<<8 | (c[14] & 0xff)<<16 | (c[15] & 0xff)<<24;
3706
-
3707
- var x0 = j0, x1 = j1, x2 = j2, x3 = j3, x4 = j4, x5 = j5, x6 = j6, x7 = j7,
3708
- x8 = j8, x9 = j9, x10 = j10, x11 = j11, x12 = j12, x13 = j13, x14 = j14,
3709
- x15 = j15, u;
3710
-
3711
- for (var i = 0; i < 20; i += 2) {
3712
- u = x0 + x12 | 0;
3713
- x4 ^= u<<7 | u>>>(32-7);
3714
- u = x4 + x0 | 0;
3715
- x8 ^= u<<9 | u>>>(32-9);
3716
- u = x8 + x4 | 0;
3717
- x12 ^= u<<13 | u>>>(32-13);
3718
- u = x12 + x8 | 0;
3719
- x0 ^= u<<18 | u>>>(32-18);
3720
-
3721
- u = x5 + x1 | 0;
3722
- x9 ^= u<<7 | u>>>(32-7);
3723
- u = x9 + x5 | 0;
3724
- x13 ^= u<<9 | u>>>(32-9);
3725
- u = x13 + x9 | 0;
3726
- x1 ^= u<<13 | u>>>(32-13);
3727
- u = x1 + x13 | 0;
3728
- x5 ^= u<<18 | u>>>(32-18);
3729
-
3730
- u = x10 + x6 | 0;
3731
- x14 ^= u<<7 | u>>>(32-7);
3732
- u = x14 + x10 | 0;
3733
- x2 ^= u<<9 | u>>>(32-9);
3734
- u = x2 + x14 | 0;
3735
- x6 ^= u<<13 | u>>>(32-13);
3736
- u = x6 + x2 | 0;
3737
- x10 ^= u<<18 | u>>>(32-18);
3738
-
3739
- u = x15 + x11 | 0;
3740
- x3 ^= u<<7 | u>>>(32-7);
3741
- u = x3 + x15 | 0;
3742
- x7 ^= u<<9 | u>>>(32-9);
3743
- u = x7 + x3 | 0;
3744
- x11 ^= u<<13 | u>>>(32-13);
3745
- u = x11 + x7 | 0;
3746
- x15 ^= u<<18 | u>>>(32-18);
3747
-
3748
- u = x0 + x3 | 0;
3749
- x1 ^= u<<7 | u>>>(32-7);
3750
- u = x1 + x0 | 0;
3751
- x2 ^= u<<9 | u>>>(32-9);
3752
- u = x2 + x1 | 0;
3753
- x3 ^= u<<13 | u>>>(32-13);
3754
- u = x3 + x2 | 0;
3755
- x0 ^= u<<18 | u>>>(32-18);
3756
-
3757
- u = x5 + x4 | 0;
3758
- x6 ^= u<<7 | u>>>(32-7);
3759
- u = x6 + x5 | 0;
3760
- x7 ^= u<<9 | u>>>(32-9);
3761
- u = x7 + x6 | 0;
3762
- x4 ^= u<<13 | u>>>(32-13);
3763
- u = x4 + x7 | 0;
3764
- x5 ^= u<<18 | u>>>(32-18);
3765
-
3766
- u = x10 + x9 | 0;
3767
- x11 ^= u<<7 | u>>>(32-7);
3768
- u = x11 + x10 | 0;
3769
- x8 ^= u<<9 | u>>>(32-9);
3770
- u = x8 + x11 | 0;
3771
- x9 ^= u<<13 | u>>>(32-13);
3772
- u = x9 + x8 | 0;
3773
- x10 ^= u<<18 | u>>>(32-18);
3774
-
3775
- u = x15 + x14 | 0;
3776
- x12 ^= u<<7 | u>>>(32-7);
3777
- u = x12 + x15 | 0;
3778
- x13 ^= u<<9 | u>>>(32-9);
3779
- u = x13 + x12 | 0;
3780
- x14 ^= u<<13 | u>>>(32-13);
3781
- u = x14 + x13 | 0;
3782
- x15 ^= u<<18 | u>>>(32-18);
3783
- }
3784
- x0 = x0 + j0 | 0;
3785
- x1 = x1 + j1 | 0;
3786
- x2 = x2 + j2 | 0;
3787
- x3 = x3 + j3 | 0;
3788
- x4 = x4 + j4 | 0;
3789
- x5 = x5 + j5 | 0;
3790
- x6 = x6 + j6 | 0;
3791
- x7 = x7 + j7 | 0;
3792
- x8 = x8 + j8 | 0;
3793
- x9 = x9 + j9 | 0;
3794
- x10 = x10 + j10 | 0;
3795
- x11 = x11 + j11 | 0;
3796
- x12 = x12 + j12 | 0;
3797
- x13 = x13 + j13 | 0;
3798
- x14 = x14 + j14 | 0;
3799
- x15 = x15 + j15 | 0;
3800
-
3801
- o[ 0] = x0 >>> 0 & 0xff;
3802
- o[ 1] = x0 >>> 8 & 0xff;
3803
- o[ 2] = x0 >>> 16 & 0xff;
3804
- o[ 3] = x0 >>> 24 & 0xff;
3805
-
3806
- o[ 4] = x1 >>> 0 & 0xff;
3807
- o[ 5] = x1 >>> 8 & 0xff;
3808
- o[ 6] = x1 >>> 16 & 0xff;
3809
- o[ 7] = x1 >>> 24 & 0xff;
3810
-
3811
- o[ 8] = x2 >>> 0 & 0xff;
3812
- o[ 9] = x2 >>> 8 & 0xff;
3813
- o[10] = x2 >>> 16 & 0xff;
3814
- o[11] = x2 >>> 24 & 0xff;
3815
-
3816
- o[12] = x3 >>> 0 & 0xff;
3817
- o[13] = x3 >>> 8 & 0xff;
3818
- o[14] = x3 >>> 16 & 0xff;
3819
- o[15] = x3 >>> 24 & 0xff;
3820
-
3821
- o[16] = x4 >>> 0 & 0xff;
3822
- o[17] = x4 >>> 8 & 0xff;
3823
- o[18] = x4 >>> 16 & 0xff;
3824
- o[19] = x4 >>> 24 & 0xff;
3825
-
3826
- o[20] = x5 >>> 0 & 0xff;
3827
- o[21] = x5 >>> 8 & 0xff;
3828
- o[22] = x5 >>> 16 & 0xff;
3829
- o[23] = x5 >>> 24 & 0xff;
3830
-
3831
- o[24] = x6 >>> 0 & 0xff;
3832
- o[25] = x6 >>> 8 & 0xff;
3833
- o[26] = x6 >>> 16 & 0xff;
3834
- o[27] = x6 >>> 24 & 0xff;
3835
-
3836
- o[28] = x7 >>> 0 & 0xff;
3837
- o[29] = x7 >>> 8 & 0xff;
3838
- o[30] = x7 >>> 16 & 0xff;
3839
- o[31] = x7 >>> 24 & 0xff;
3840
-
3841
- o[32] = x8 >>> 0 & 0xff;
3842
- o[33] = x8 >>> 8 & 0xff;
3843
- o[34] = x8 >>> 16 & 0xff;
3844
- o[35] = x8 >>> 24 & 0xff;
3845
-
3846
- o[36] = x9 >>> 0 & 0xff;
3847
- o[37] = x9 >>> 8 & 0xff;
3848
- o[38] = x9 >>> 16 & 0xff;
3849
- o[39] = x9 >>> 24 & 0xff;
3850
-
3851
- o[40] = x10 >>> 0 & 0xff;
3852
- o[41] = x10 >>> 8 & 0xff;
3853
- o[42] = x10 >>> 16 & 0xff;
3854
- o[43] = x10 >>> 24 & 0xff;
3855
-
3856
- o[44] = x11 >>> 0 & 0xff;
3857
- o[45] = x11 >>> 8 & 0xff;
3858
- o[46] = x11 >>> 16 & 0xff;
3859
- o[47] = x11 >>> 24 & 0xff;
3860
-
3861
- o[48] = x12 >>> 0 & 0xff;
3862
- o[49] = x12 >>> 8 & 0xff;
3863
- o[50] = x12 >>> 16 & 0xff;
3864
- o[51] = x12 >>> 24 & 0xff;
3865
-
3866
- o[52] = x13 >>> 0 & 0xff;
3867
- o[53] = x13 >>> 8 & 0xff;
3868
- o[54] = x13 >>> 16 & 0xff;
3869
- o[55] = x13 >>> 24 & 0xff;
3870
-
3871
- o[56] = x14 >>> 0 & 0xff;
3872
- o[57] = x14 >>> 8 & 0xff;
3873
- o[58] = x14 >>> 16 & 0xff;
3874
- o[59] = x14 >>> 24 & 0xff;
3875
-
3876
- o[60] = x15 >>> 0 & 0xff;
3877
- o[61] = x15 >>> 8 & 0xff;
3878
- o[62] = x15 >>> 16 & 0xff;
3879
- o[63] = x15 >>> 24 & 0xff;
3880
- }
3881
-
3882
- function core_hsalsa20(o,p,k,c) {
3883
- var j0 = c[ 0] & 0xff | (c[ 1] & 0xff)<<8 | (c[ 2] & 0xff)<<16 | (c[ 3] & 0xff)<<24,
3884
- j1 = k[ 0] & 0xff | (k[ 1] & 0xff)<<8 | (k[ 2] & 0xff)<<16 | (k[ 3] & 0xff)<<24,
3885
- j2 = k[ 4] & 0xff | (k[ 5] & 0xff)<<8 | (k[ 6] & 0xff)<<16 | (k[ 7] & 0xff)<<24,
3886
- j3 = k[ 8] & 0xff | (k[ 9] & 0xff)<<8 | (k[10] & 0xff)<<16 | (k[11] & 0xff)<<24,
3887
- j4 = k[12] & 0xff | (k[13] & 0xff)<<8 | (k[14] & 0xff)<<16 | (k[15] & 0xff)<<24,
3888
- j5 = c[ 4] & 0xff | (c[ 5] & 0xff)<<8 | (c[ 6] & 0xff)<<16 | (c[ 7] & 0xff)<<24,
3889
- j6 = p[ 0] & 0xff | (p[ 1] & 0xff)<<8 | (p[ 2] & 0xff)<<16 | (p[ 3] & 0xff)<<24,
3890
- j7 = p[ 4] & 0xff | (p[ 5] & 0xff)<<8 | (p[ 6] & 0xff)<<16 | (p[ 7] & 0xff)<<24,
3891
- j8 = p[ 8] & 0xff | (p[ 9] & 0xff)<<8 | (p[10] & 0xff)<<16 | (p[11] & 0xff)<<24,
3892
- j9 = p[12] & 0xff | (p[13] & 0xff)<<8 | (p[14] & 0xff)<<16 | (p[15] & 0xff)<<24,
3893
- j10 = c[ 8] & 0xff | (c[ 9] & 0xff)<<8 | (c[10] & 0xff)<<16 | (c[11] & 0xff)<<24,
3894
- j11 = k[16] & 0xff | (k[17] & 0xff)<<8 | (k[18] & 0xff)<<16 | (k[19] & 0xff)<<24,
3895
- j12 = k[20] & 0xff | (k[21] & 0xff)<<8 | (k[22] & 0xff)<<16 | (k[23] & 0xff)<<24,
3896
- j13 = k[24] & 0xff | (k[25] & 0xff)<<8 | (k[26] & 0xff)<<16 | (k[27] & 0xff)<<24,
3897
- j14 = k[28] & 0xff | (k[29] & 0xff)<<8 | (k[30] & 0xff)<<16 | (k[31] & 0xff)<<24,
3898
- j15 = c[12] & 0xff | (c[13] & 0xff)<<8 | (c[14] & 0xff)<<16 | (c[15] & 0xff)<<24;
3899
-
3900
- var x0 = j0, x1 = j1, x2 = j2, x3 = j3, x4 = j4, x5 = j5, x6 = j6, x7 = j7,
3901
- x8 = j8, x9 = j9, x10 = j10, x11 = j11, x12 = j12, x13 = j13, x14 = j14,
3902
- x15 = j15, u;
3903
-
3904
- for (var i = 0; i < 20; i += 2) {
3905
- u = x0 + x12 | 0;
3906
- x4 ^= u<<7 | u>>>(32-7);
3907
- u = x4 + x0 | 0;
3908
- x8 ^= u<<9 | u>>>(32-9);
3909
- u = x8 + x4 | 0;
3910
- x12 ^= u<<13 | u>>>(32-13);
3911
- u = x12 + x8 | 0;
3912
- x0 ^= u<<18 | u>>>(32-18);
3913
-
3914
- u = x5 + x1 | 0;
3915
- x9 ^= u<<7 | u>>>(32-7);
3916
- u = x9 + x5 | 0;
3917
- x13 ^= u<<9 | u>>>(32-9);
3918
- u = x13 + x9 | 0;
3919
- x1 ^= u<<13 | u>>>(32-13);
3920
- u = x1 + x13 | 0;
3921
- x5 ^= u<<18 | u>>>(32-18);
3922
-
3923
- u = x10 + x6 | 0;
3924
- x14 ^= u<<7 | u>>>(32-7);
3925
- u = x14 + x10 | 0;
3926
- x2 ^= u<<9 | u>>>(32-9);
3927
- u = x2 + x14 | 0;
3928
- x6 ^= u<<13 | u>>>(32-13);
3929
- u = x6 + x2 | 0;
3930
- x10 ^= u<<18 | u>>>(32-18);
3931
-
3932
- u = x15 + x11 | 0;
3933
- x3 ^= u<<7 | u>>>(32-7);
3934
- u = x3 + x15 | 0;
3935
- x7 ^= u<<9 | u>>>(32-9);
3936
- u = x7 + x3 | 0;
3937
- x11 ^= u<<13 | u>>>(32-13);
3938
- u = x11 + x7 | 0;
3939
- x15 ^= u<<18 | u>>>(32-18);
3940
-
3941
- u = x0 + x3 | 0;
3942
- x1 ^= u<<7 | u>>>(32-7);
3943
- u = x1 + x0 | 0;
3944
- x2 ^= u<<9 | u>>>(32-9);
3945
- u = x2 + x1 | 0;
3946
- x3 ^= u<<13 | u>>>(32-13);
3947
- u = x3 + x2 | 0;
3948
- x0 ^= u<<18 | u>>>(32-18);
3949
-
3950
- u = x5 + x4 | 0;
3951
- x6 ^= u<<7 | u>>>(32-7);
3952
- u = x6 + x5 | 0;
3953
- x7 ^= u<<9 | u>>>(32-9);
3954
- u = x7 + x6 | 0;
3955
- x4 ^= u<<13 | u>>>(32-13);
3956
- u = x4 + x7 | 0;
3957
- x5 ^= u<<18 | u>>>(32-18);
3958
-
3959
- u = x10 + x9 | 0;
3960
- x11 ^= u<<7 | u>>>(32-7);
3961
- u = x11 + x10 | 0;
3962
- x8 ^= u<<9 | u>>>(32-9);
3963
- u = x8 + x11 | 0;
3964
- x9 ^= u<<13 | u>>>(32-13);
3965
- u = x9 + x8 | 0;
3966
- x10 ^= u<<18 | u>>>(32-18);
3967
-
3968
- u = x15 + x14 | 0;
3969
- x12 ^= u<<7 | u>>>(32-7);
3970
- u = x12 + x15 | 0;
3971
- x13 ^= u<<9 | u>>>(32-9);
3972
- u = x13 + x12 | 0;
3973
- x14 ^= u<<13 | u>>>(32-13);
3974
- u = x14 + x13 | 0;
3975
- x15 ^= u<<18 | u>>>(32-18);
3976
- }
3977
-
3978
- o[ 0] = x0 >>> 0 & 0xff;
3979
- o[ 1] = x0 >>> 8 & 0xff;
3980
- o[ 2] = x0 >>> 16 & 0xff;
3981
- o[ 3] = x0 >>> 24 & 0xff;
3982
-
3983
- o[ 4] = x5 >>> 0 & 0xff;
3984
- o[ 5] = x5 >>> 8 & 0xff;
3985
- o[ 6] = x5 >>> 16 & 0xff;
3986
- o[ 7] = x5 >>> 24 & 0xff;
3987
-
3988
- o[ 8] = x10 >>> 0 & 0xff;
3989
- o[ 9] = x10 >>> 8 & 0xff;
3990
- o[10] = x10 >>> 16 & 0xff;
3991
- o[11] = x10 >>> 24 & 0xff;
3992
-
3993
- o[12] = x15 >>> 0 & 0xff;
3994
- o[13] = x15 >>> 8 & 0xff;
3995
- o[14] = x15 >>> 16 & 0xff;
3996
- o[15] = x15 >>> 24 & 0xff;
3997
-
3998
- o[16] = x6 >>> 0 & 0xff;
3999
- o[17] = x6 >>> 8 & 0xff;
4000
- o[18] = x6 >>> 16 & 0xff;
4001
- o[19] = x6 >>> 24 & 0xff;
4002
-
4003
- o[20] = x7 >>> 0 & 0xff;
4004
- o[21] = x7 >>> 8 & 0xff;
4005
- o[22] = x7 >>> 16 & 0xff;
4006
- o[23] = x7 >>> 24 & 0xff;
4007
-
4008
- o[24] = x8 >>> 0 & 0xff;
4009
- o[25] = x8 >>> 8 & 0xff;
4010
- o[26] = x8 >>> 16 & 0xff;
4011
- o[27] = x8 >>> 24 & 0xff;
4012
-
4013
- o[28] = x9 >>> 0 & 0xff;
4014
- o[29] = x9 >>> 8 & 0xff;
4015
- o[30] = x9 >>> 16 & 0xff;
4016
- o[31] = x9 >>> 24 & 0xff;
4017
- }
4018
-
4019
- function crypto_core_salsa20(out,inp,k,c) {
4020
- core_salsa20(out,inp,k,c);
4021
- }
4022
-
4023
- function crypto_core_hsalsa20(out,inp,k,c) {
4024
- core_hsalsa20(out,inp,k,c);
4025
- }
4026
-
4027
- var sigma = new Uint8Array([101, 120, 112, 97, 110, 100, 32, 51, 50, 45, 98, 121, 116, 101, 32, 107]);
4028
- // "expand 32-byte k"
4029
-
4030
- function crypto_stream_salsa20_xor(c,cpos,m,mpos,b,n,k) {
4031
- var z = new Uint8Array(16), x = new Uint8Array(64);
4032
- var u, i;
4033
- for (i = 0; i < 16; i++) z[i] = 0;
4034
- for (i = 0; i < 8; i++) z[i] = n[i];
4035
- while (b >= 64) {
4036
- crypto_core_salsa20(x,z,k,sigma);
4037
- for (i = 0; i < 64; i++) c[cpos+i] = m[mpos+i] ^ x[i];
4038
- u = 1;
4039
- for (i = 8; i < 16; i++) {
4040
- u = u + (z[i] & 0xff) | 0;
4041
- z[i] = u & 0xff;
4042
- u >>>= 8;
4043
- }
4044
- b -= 64;
4045
- cpos += 64;
4046
- mpos += 64;
4047
- }
4048
- if (b > 0) {
4049
- crypto_core_salsa20(x,z,k,sigma);
4050
- for (i = 0; i < b; i++) c[cpos+i] = m[mpos+i] ^ x[i];
4051
- }
4052
- return 0;
4053
- }
4054
-
4055
- function crypto_stream_salsa20(c,cpos,b,n,k) {
4056
- var z = new Uint8Array(16), x = new Uint8Array(64);
4057
- var u, i;
4058
- for (i = 0; i < 16; i++) z[i] = 0;
4059
- for (i = 0; i < 8; i++) z[i] = n[i];
4060
- while (b >= 64) {
4061
- crypto_core_salsa20(x,z,k,sigma);
4062
- for (i = 0; i < 64; i++) c[cpos+i] = x[i];
4063
- u = 1;
4064
- for (i = 8; i < 16; i++) {
4065
- u = u + (z[i] & 0xff) | 0;
4066
- z[i] = u & 0xff;
4067
- u >>>= 8;
4068
- }
4069
- b -= 64;
4070
- cpos += 64;
4071
- }
4072
- if (b > 0) {
4073
- crypto_core_salsa20(x,z,k,sigma);
4074
- for (i = 0; i < b; i++) c[cpos+i] = x[i];
4075
- }
4076
- return 0;
4077
- }
4078
-
4079
- function crypto_stream(c,cpos,d,n,k) {
4080
- var s = new Uint8Array(32);
4081
- crypto_core_hsalsa20(s,n,k,sigma);
4082
- var sn = new Uint8Array(8);
4083
- for (var i = 0; i < 8; i++) sn[i] = n[i+16];
4084
- return crypto_stream_salsa20(c,cpos,d,sn,s);
4085
- }
4086
-
4087
- function crypto_stream_xor(c,cpos,m,mpos,d,n,k) {
4088
- var s = new Uint8Array(32);
4089
- crypto_core_hsalsa20(s,n,k,sigma);
4090
- var sn = new Uint8Array(8);
4091
- for (var i = 0; i < 8; i++) sn[i] = n[i+16];
4092
- return crypto_stream_salsa20_xor(c,cpos,m,mpos,d,sn,s);
4093
- }
4094
-
4095
- /*
4096
- * Port of Andrew Moon's Poly1305-donna-16. Public domain.
4097
- * https://github.com/floodyberry/poly1305-donna
4098
- */
4099
-
4100
- var poly1305 = function(key) {
4101
- this.buffer = new Uint8Array(16);
4102
- this.r = new Uint16Array(10);
4103
- this.h = new Uint16Array(10);
4104
- this.pad = new Uint16Array(8);
4105
- this.leftover = 0;
4106
- this.fin = 0;
4107
-
4108
- var t0, t1, t2, t3, t4, t5, t6, t7;
4109
-
4110
- t0 = key[ 0] & 0xff | (key[ 1] & 0xff) << 8; this.r[0] = ( t0 ) & 0x1fff;
4111
- t1 = key[ 2] & 0xff | (key[ 3] & 0xff) << 8; this.r[1] = ((t0 >>> 13) | (t1 << 3)) & 0x1fff;
4112
- t2 = key[ 4] & 0xff | (key[ 5] & 0xff) << 8; this.r[2] = ((t1 >>> 10) | (t2 << 6)) & 0x1f03;
4113
- t3 = key[ 6] & 0xff | (key[ 7] & 0xff) << 8; this.r[3] = ((t2 >>> 7) | (t3 << 9)) & 0x1fff;
4114
- t4 = key[ 8] & 0xff | (key[ 9] & 0xff) << 8; this.r[4] = ((t3 >>> 4) | (t4 << 12)) & 0x00ff;
4115
- this.r[5] = ((t4 >>> 1)) & 0x1ffe;
4116
- t5 = key[10] & 0xff | (key[11] & 0xff) << 8; this.r[6] = ((t4 >>> 14) | (t5 << 2)) & 0x1fff;
4117
- t6 = key[12] & 0xff | (key[13] & 0xff) << 8; this.r[7] = ((t5 >>> 11) | (t6 << 5)) & 0x1f81;
4118
- t7 = key[14] & 0xff | (key[15] & 0xff) << 8; this.r[8] = ((t6 >>> 8) | (t7 << 8)) & 0x1fff;
4119
- this.r[9] = ((t7 >>> 5)) & 0x007f;
4120
-
4121
- this.pad[0] = key[16] & 0xff | (key[17] & 0xff) << 8;
4122
- this.pad[1] = key[18] & 0xff | (key[19] & 0xff) << 8;
4123
- this.pad[2] = key[20] & 0xff | (key[21] & 0xff) << 8;
4124
- this.pad[3] = key[22] & 0xff | (key[23] & 0xff) << 8;
4125
- this.pad[4] = key[24] & 0xff | (key[25] & 0xff) << 8;
4126
- this.pad[5] = key[26] & 0xff | (key[27] & 0xff) << 8;
4127
- this.pad[6] = key[28] & 0xff | (key[29] & 0xff) << 8;
4128
- this.pad[7] = key[30] & 0xff | (key[31] & 0xff) << 8;
4129
- };
4130
-
4131
- poly1305.prototype.blocks = function(m, mpos, bytes) {
4132
- var hibit = this.fin ? 0 : (1 << 11);
4133
- var t0, t1, t2, t3, t4, t5, t6, t7, c;
4134
- var d0, d1, d2, d3, d4, d5, d6, d7, d8, d9;
4135
-
4136
- var h0 = this.h[0],
4137
- h1 = this.h[1],
4138
- h2 = this.h[2],
4139
- h3 = this.h[3],
4140
- h4 = this.h[4],
4141
- h5 = this.h[5],
4142
- h6 = this.h[6],
4143
- h7 = this.h[7],
4144
- h8 = this.h[8],
4145
- h9 = this.h[9];
4146
-
4147
- var r0 = this.r[0],
4148
- r1 = this.r[1],
4149
- r2 = this.r[2],
4150
- r3 = this.r[3],
4151
- r4 = this.r[4],
4152
- r5 = this.r[5],
4153
- r6 = this.r[6],
4154
- r7 = this.r[7],
4155
- r8 = this.r[8],
4156
- r9 = this.r[9];
4157
-
4158
- while (bytes >= 16) {
4159
- t0 = m[mpos+ 0] & 0xff | (m[mpos+ 1] & 0xff) << 8; h0 += ( t0 ) & 0x1fff;
4160
- t1 = m[mpos+ 2] & 0xff | (m[mpos+ 3] & 0xff) << 8; h1 += ((t0 >>> 13) | (t1 << 3)) & 0x1fff;
4161
- t2 = m[mpos+ 4] & 0xff | (m[mpos+ 5] & 0xff) << 8; h2 += ((t1 >>> 10) | (t2 << 6)) & 0x1fff;
4162
- t3 = m[mpos+ 6] & 0xff | (m[mpos+ 7] & 0xff) << 8; h3 += ((t2 >>> 7) | (t3 << 9)) & 0x1fff;
4163
- t4 = m[mpos+ 8] & 0xff | (m[mpos+ 9] & 0xff) << 8; h4 += ((t3 >>> 4) | (t4 << 12)) & 0x1fff;
4164
- h5 += ((t4 >>> 1)) & 0x1fff;
4165
- t5 = m[mpos+10] & 0xff | (m[mpos+11] & 0xff) << 8; h6 += ((t4 >>> 14) | (t5 << 2)) & 0x1fff;
4166
- t6 = m[mpos+12] & 0xff | (m[mpos+13] & 0xff) << 8; h7 += ((t5 >>> 11) | (t6 << 5)) & 0x1fff;
4167
- t7 = m[mpos+14] & 0xff | (m[mpos+15] & 0xff) << 8; h8 += ((t6 >>> 8) | (t7 << 8)) & 0x1fff;
4168
- h9 += ((t7 >>> 5)) | hibit;
4169
-
4170
- c = 0;
4171
-
4172
- d0 = c;
4173
- d0 += h0 * r0;
4174
- d0 += h1 * (5 * r9);
4175
- d0 += h2 * (5 * r8);
4176
- d0 += h3 * (5 * r7);
4177
- d0 += h4 * (5 * r6);
4178
- c = (d0 >>> 13); d0 &= 0x1fff;
4179
- d0 += h5 * (5 * r5);
4180
- d0 += h6 * (5 * r4);
4181
- d0 += h7 * (5 * r3);
4182
- d0 += h8 * (5 * r2);
4183
- d0 += h9 * (5 * r1);
4184
- c += (d0 >>> 13); d0 &= 0x1fff;
4185
-
4186
- d1 = c;
4187
- d1 += h0 * r1;
4188
- d1 += h1 * r0;
4189
- d1 += h2 * (5 * r9);
4190
- d1 += h3 * (5 * r8);
4191
- d1 += h4 * (5 * r7);
4192
- c = (d1 >>> 13); d1 &= 0x1fff;
4193
- d1 += h5 * (5 * r6);
4194
- d1 += h6 * (5 * r5);
4195
- d1 += h7 * (5 * r4);
4196
- d1 += h8 * (5 * r3);
4197
- d1 += h9 * (5 * r2);
4198
- c += (d1 >>> 13); d1 &= 0x1fff;
4199
-
4200
- d2 = c;
4201
- d2 += h0 * r2;
4202
- d2 += h1 * r1;
4203
- d2 += h2 * r0;
4204
- d2 += h3 * (5 * r9);
4205
- d2 += h4 * (5 * r8);
4206
- c = (d2 >>> 13); d2 &= 0x1fff;
4207
- d2 += h5 * (5 * r7);
4208
- d2 += h6 * (5 * r6);
4209
- d2 += h7 * (5 * r5);
4210
- d2 += h8 * (5 * r4);
4211
- d2 += h9 * (5 * r3);
4212
- c += (d2 >>> 13); d2 &= 0x1fff;
4213
-
4214
- d3 = c;
4215
- d3 += h0 * r3;
4216
- d3 += h1 * r2;
4217
- d3 += h2 * r1;
4218
- d3 += h3 * r0;
4219
- d3 += h4 * (5 * r9);
4220
- c = (d3 >>> 13); d3 &= 0x1fff;
4221
- d3 += h5 * (5 * r8);
4222
- d3 += h6 * (5 * r7);
4223
- d3 += h7 * (5 * r6);
4224
- d3 += h8 * (5 * r5);
4225
- d3 += h9 * (5 * r4);
4226
- c += (d3 >>> 13); d3 &= 0x1fff;
4227
-
4228
- d4 = c;
4229
- d4 += h0 * r4;
4230
- d4 += h1 * r3;
4231
- d4 += h2 * r2;
4232
- d4 += h3 * r1;
4233
- d4 += h4 * r0;
4234
- c = (d4 >>> 13); d4 &= 0x1fff;
4235
- d4 += h5 * (5 * r9);
4236
- d4 += h6 * (5 * r8);
4237
- d4 += h7 * (5 * r7);
4238
- d4 += h8 * (5 * r6);
4239
- d4 += h9 * (5 * r5);
4240
- c += (d4 >>> 13); d4 &= 0x1fff;
4241
-
4242
- d5 = c;
4243
- d5 += h0 * r5;
4244
- d5 += h1 * r4;
4245
- d5 += h2 * r3;
4246
- d5 += h3 * r2;
4247
- d5 += h4 * r1;
4248
- c = (d5 >>> 13); d5 &= 0x1fff;
4249
- d5 += h5 * r0;
4250
- d5 += h6 * (5 * r9);
4251
- d5 += h7 * (5 * r8);
4252
- d5 += h8 * (5 * r7);
4253
- d5 += h9 * (5 * r6);
4254
- c += (d5 >>> 13); d5 &= 0x1fff;
4255
-
4256
- d6 = c;
4257
- d6 += h0 * r6;
4258
- d6 += h1 * r5;
4259
- d6 += h2 * r4;
4260
- d6 += h3 * r3;
4261
- d6 += h4 * r2;
4262
- c = (d6 >>> 13); d6 &= 0x1fff;
4263
- d6 += h5 * r1;
4264
- d6 += h6 * r0;
4265
- d6 += h7 * (5 * r9);
4266
- d6 += h8 * (5 * r8);
4267
- d6 += h9 * (5 * r7);
4268
- c += (d6 >>> 13); d6 &= 0x1fff;
4269
-
4270
- d7 = c;
4271
- d7 += h0 * r7;
4272
- d7 += h1 * r6;
4273
- d7 += h2 * r5;
4274
- d7 += h3 * r4;
4275
- d7 += h4 * r3;
4276
- c = (d7 >>> 13); d7 &= 0x1fff;
4277
- d7 += h5 * r2;
4278
- d7 += h6 * r1;
4279
- d7 += h7 * r0;
4280
- d7 += h8 * (5 * r9);
4281
- d7 += h9 * (5 * r8);
4282
- c += (d7 >>> 13); d7 &= 0x1fff;
4283
-
4284
- d8 = c;
4285
- d8 += h0 * r8;
4286
- d8 += h1 * r7;
4287
- d8 += h2 * r6;
4288
- d8 += h3 * r5;
4289
- d8 += h4 * r4;
4290
- c = (d8 >>> 13); d8 &= 0x1fff;
4291
- d8 += h5 * r3;
4292
- d8 += h6 * r2;
4293
- d8 += h7 * r1;
4294
- d8 += h8 * r0;
4295
- d8 += h9 * (5 * r9);
4296
- c += (d8 >>> 13); d8 &= 0x1fff;
4297
-
4298
- d9 = c;
4299
- d9 += h0 * r9;
4300
- d9 += h1 * r8;
4301
- d9 += h2 * r7;
4302
- d9 += h3 * r6;
4303
- d9 += h4 * r5;
4304
- c = (d9 >>> 13); d9 &= 0x1fff;
4305
- d9 += h5 * r4;
4306
- d9 += h6 * r3;
4307
- d9 += h7 * r2;
4308
- d9 += h8 * r1;
4309
- d9 += h9 * r0;
4310
- c += (d9 >>> 13); d9 &= 0x1fff;
4311
-
4312
- c = (((c << 2) + c)) | 0;
4313
- c = (c + d0) | 0;
4314
- d0 = c & 0x1fff;
4315
- c = (c >>> 13);
4316
- d1 += c;
4317
-
4318
- h0 = d0;
4319
- h1 = d1;
4320
- h2 = d2;
4321
- h3 = d3;
4322
- h4 = d4;
4323
- h5 = d5;
4324
- h6 = d6;
4325
- h7 = d7;
4326
- h8 = d8;
4327
- h9 = d9;
4328
-
4329
- mpos += 16;
4330
- bytes -= 16;
4331
- }
4332
- this.h[0] = h0;
4333
- this.h[1] = h1;
4334
- this.h[2] = h2;
4335
- this.h[3] = h3;
4336
- this.h[4] = h4;
4337
- this.h[5] = h5;
4338
- this.h[6] = h6;
4339
- this.h[7] = h7;
4340
- this.h[8] = h8;
4341
- this.h[9] = h9;
4342
- };
4343
-
4344
- poly1305.prototype.finish = function(mac, macpos) {
4345
- var g = new Uint16Array(10);
4346
- var c, mask, f, i;
4347
-
4348
- if (this.leftover) {
4349
- i = this.leftover;
4350
- this.buffer[i++] = 1;
4351
- for (; i < 16; i++) this.buffer[i] = 0;
4352
- this.fin = 1;
4353
- this.blocks(this.buffer, 0, 16);
4354
- }
4355
-
4356
- c = this.h[1] >>> 13;
4357
- this.h[1] &= 0x1fff;
4358
- for (i = 2; i < 10; i++) {
4359
- this.h[i] += c;
4360
- c = this.h[i] >>> 13;
4361
- this.h[i] &= 0x1fff;
4362
- }
4363
- this.h[0] += (c * 5);
4364
- c = this.h[0] >>> 13;
4365
- this.h[0] &= 0x1fff;
4366
- this.h[1] += c;
4367
- c = this.h[1] >>> 13;
4368
- this.h[1] &= 0x1fff;
4369
- this.h[2] += c;
4370
-
4371
- g[0] = this.h[0] + 5;
4372
- c = g[0] >>> 13;
4373
- g[0] &= 0x1fff;
4374
- for (i = 1; i < 10; i++) {
4375
- g[i] = this.h[i] + c;
4376
- c = g[i] >>> 13;
4377
- g[i] &= 0x1fff;
4378
- }
4379
- g[9] -= (1 << 13);
4380
-
4381
- mask = (c ^ 1) - 1;
4382
- for (i = 0; i < 10; i++) g[i] &= mask;
4383
- mask = ~mask;
4384
- for (i = 0; i < 10; i++) this.h[i] = (this.h[i] & mask) | g[i];
4385
-
4386
- this.h[0] = ((this.h[0] ) | (this.h[1] << 13) ) & 0xffff;
4387
- this.h[1] = ((this.h[1] >>> 3) | (this.h[2] << 10) ) & 0xffff;
4388
- this.h[2] = ((this.h[2] >>> 6) | (this.h[3] << 7) ) & 0xffff;
4389
- this.h[3] = ((this.h[3] >>> 9) | (this.h[4] << 4) ) & 0xffff;
4390
- this.h[4] = ((this.h[4] >>> 12) | (this.h[5] << 1) | (this.h[6] << 14)) & 0xffff;
4391
- this.h[5] = ((this.h[6] >>> 2) | (this.h[7] << 11) ) & 0xffff;
4392
- this.h[6] = ((this.h[7] >>> 5) | (this.h[8] << 8) ) & 0xffff;
4393
- this.h[7] = ((this.h[8] >>> 8) | (this.h[9] << 5) ) & 0xffff;
4394
-
4395
- f = this.h[0] + this.pad[0];
4396
- this.h[0] = f & 0xffff;
4397
- for (i = 1; i < 8; i++) {
4398
- f = (((this.h[i] + this.pad[i]) | 0) + (f >>> 16)) | 0;
4399
- this.h[i] = f & 0xffff;
4400
- }
4401
-
4402
- mac[macpos+ 0] = (this.h[0] >>> 0) & 0xff;
4403
- mac[macpos+ 1] = (this.h[0] >>> 8) & 0xff;
4404
- mac[macpos+ 2] = (this.h[1] >>> 0) & 0xff;
4405
- mac[macpos+ 3] = (this.h[1] >>> 8) & 0xff;
4406
- mac[macpos+ 4] = (this.h[2] >>> 0) & 0xff;
4407
- mac[macpos+ 5] = (this.h[2] >>> 8) & 0xff;
4408
- mac[macpos+ 6] = (this.h[3] >>> 0) & 0xff;
4409
- mac[macpos+ 7] = (this.h[3] >>> 8) & 0xff;
4410
- mac[macpos+ 8] = (this.h[4] >>> 0) & 0xff;
4411
- mac[macpos+ 9] = (this.h[4] >>> 8) & 0xff;
4412
- mac[macpos+10] = (this.h[5] >>> 0) & 0xff;
4413
- mac[macpos+11] = (this.h[5] >>> 8) & 0xff;
4414
- mac[macpos+12] = (this.h[6] >>> 0) & 0xff;
4415
- mac[macpos+13] = (this.h[6] >>> 8) & 0xff;
4416
- mac[macpos+14] = (this.h[7] >>> 0) & 0xff;
4417
- mac[macpos+15] = (this.h[7] >>> 8) & 0xff;
4418
- };
4419
-
4420
- poly1305.prototype.update = function(m, mpos, bytes) {
4421
- var i, want;
4422
-
4423
- if (this.leftover) {
4424
- want = (16 - this.leftover);
4425
- if (want > bytes)
4426
- want = bytes;
4427
- for (i = 0; i < want; i++)
4428
- this.buffer[this.leftover + i] = m[mpos+i];
4429
- bytes -= want;
4430
- mpos += want;
4431
- this.leftover += want;
4432
- if (this.leftover < 16)
4433
- return;
4434
- this.blocks(this.buffer, 0, 16);
4435
- this.leftover = 0;
4436
- }
4437
-
4438
- if (bytes >= 16) {
4439
- want = bytes - (bytes % 16);
4440
- this.blocks(m, mpos, want);
4441
- mpos += want;
4442
- bytes -= want;
4443
- }
4444
-
4445
- if (bytes) {
4446
- for (i = 0; i < bytes; i++)
4447
- this.buffer[this.leftover + i] = m[mpos+i];
4448
- this.leftover += bytes;
4449
- }
4450
- };
4451
-
4452
- function crypto_onetimeauth(out, outpos, m, mpos, n, k) {
4453
- var s = new poly1305(k);
4454
- s.update(m, mpos, n);
4455
- s.finish(out, outpos);
4456
- return 0;
4457
- }
4458
-
4459
- function crypto_onetimeauth_verify(h, hpos, m, mpos, n, k) {
4460
- var x = new Uint8Array(16);
4461
- crypto_onetimeauth(x,0,m,mpos,n,k);
4462
- return crypto_verify_16(h,hpos,x,0);
4463
- }
4464
-
4465
- function crypto_secretbox(c,m,d,n,k) {
4466
- var i;
4467
- if (d < 32) return -1;
4468
- crypto_stream_xor(c,0,m,0,d,n,k);
4469
- crypto_onetimeauth(c, 16, c, 32, d - 32, c);
4470
- for (i = 0; i < 16; i++) c[i] = 0;
4471
- return 0;
4472
- }
4473
-
4474
- function crypto_secretbox_open(m,c,d,n,k) {
4475
- var i;
4476
- var x = new Uint8Array(32);
4477
- if (d < 32) return -1;
4478
- crypto_stream(x,0,32,n,k);
4479
- if (crypto_onetimeauth_verify(c, 16,c, 32,d - 32,x) !== 0) return -1;
4480
- crypto_stream_xor(m,0,c,0,d,n,k);
4481
- for (i = 0; i < 32; i++) m[i] = 0;
4482
- return 0;
4483
- }
4484
-
4485
- function set25519(r, a) {
4486
- var i;
4487
- for (i = 0; i < 16; i++) r[i] = a[i]|0;
4488
- }
4489
-
4490
- function car25519(o) {
4491
- var i, v, c = 1;
4492
- for (i = 0; i < 16; i++) {
4493
- v = o[i] + c + 65535;
4494
- c = Math.floor(v / 65536);
4495
- o[i] = v - c * 65536;
4496
- }
4497
- o[0] += c-1 + 37 * (c-1);
4498
- }
4499
-
4500
- function sel25519(p, q, b) {
4501
- var t, c = ~(b-1);
4502
- for (var i = 0; i < 16; i++) {
4503
- t = c & (p[i] ^ q[i]);
4504
- p[i] ^= t;
4505
- q[i] ^= t;
4506
- }
4507
- }
4508
-
4509
- function pack25519(o, n) {
4510
- var i, j, b;
4511
- var m = gf(), t = gf();
4512
- for (i = 0; i < 16; i++) t[i] = n[i];
4513
- car25519(t);
4514
- car25519(t);
4515
- car25519(t);
4516
- for (j = 0; j < 2; j++) {
4517
- m[0] = t[0] - 0xffed;
4518
- for (i = 1; i < 15; i++) {
4519
- m[i] = t[i] - 0xffff - ((m[i-1]>>16) & 1);
4520
- m[i-1] &= 0xffff;
4521
- }
4522
- m[15] = t[15] - 0x7fff - ((m[14]>>16) & 1);
4523
- b = (m[15]>>16) & 1;
4524
- m[14] &= 0xffff;
4525
- sel25519(t, m, 1-b);
4526
- }
4527
- for (i = 0; i < 16; i++) {
4528
- o[2*i] = t[i] & 0xff;
4529
- o[2*i+1] = t[i]>>8;
4530
- }
4531
- }
4532
-
4533
- function neq25519(a, b) {
4534
- var c = new Uint8Array(32), d = new Uint8Array(32);
4535
- pack25519(c, a);
4536
- pack25519(d, b);
4537
- return crypto_verify_32(c, 0, d, 0);
4538
- }
4539
-
4540
- function par25519(a) {
4541
- var d = new Uint8Array(32);
4542
- pack25519(d, a);
4543
- return d[0] & 1;
4544
- }
4545
-
4546
- function unpack25519(o, n) {
4547
- var i;
4548
- for (i = 0; i < 16; i++) o[i] = n[2*i] + (n[2*i+1] << 8);
4549
- o[15] &= 0x7fff;
4550
- }
4551
-
4552
- function A(o, a, b) {
4553
- for (var i = 0; i < 16; i++) o[i] = a[i] + b[i];
4554
- }
4555
-
4556
- function Z(o, a, b) {
4557
- for (var i = 0; i < 16; i++) o[i] = a[i] - b[i];
4558
- }
4559
-
4560
- function M(o, a, b) {
4561
- var v, c,
4562
- t0 = 0, t1 = 0, t2 = 0, t3 = 0, t4 = 0, t5 = 0, t6 = 0, t7 = 0,
4563
- t8 = 0, t9 = 0, t10 = 0, t11 = 0, t12 = 0, t13 = 0, t14 = 0, t15 = 0,
4564
- t16 = 0, t17 = 0, t18 = 0, t19 = 0, t20 = 0, t21 = 0, t22 = 0, t23 = 0,
4565
- t24 = 0, t25 = 0, t26 = 0, t27 = 0, t28 = 0, t29 = 0, t30 = 0,
4566
- b0 = b[0],
4567
- b1 = b[1],
4568
- b2 = b[2],
4569
- b3 = b[3],
4570
- b4 = b[4],
4571
- b5 = b[5],
4572
- b6 = b[6],
4573
- b7 = b[7],
4574
- b8 = b[8],
4575
- b9 = b[9],
4576
- b10 = b[10],
4577
- b11 = b[11],
4578
- b12 = b[12],
4579
- b13 = b[13],
4580
- b14 = b[14],
4581
- b15 = b[15];
4582
-
4583
- v = a[0];
4584
- t0 += v * b0;
4585
- t1 += v * b1;
4586
- t2 += v * b2;
4587
- t3 += v * b3;
4588
- t4 += v * b4;
4589
- t5 += v * b5;
4590
- t6 += v * b6;
4591
- t7 += v * b7;
4592
- t8 += v * b8;
4593
- t9 += v * b9;
4594
- t10 += v * b10;
4595
- t11 += v * b11;
4596
- t12 += v * b12;
4597
- t13 += v * b13;
4598
- t14 += v * b14;
4599
- t15 += v * b15;
4600
- v = a[1];
4601
- t1 += v * b0;
4602
- t2 += v * b1;
4603
- t3 += v * b2;
4604
- t4 += v * b3;
4605
- t5 += v * b4;
4606
- t6 += v * b5;
4607
- t7 += v * b6;
4608
- t8 += v * b7;
4609
- t9 += v * b8;
4610
- t10 += v * b9;
4611
- t11 += v * b10;
4612
- t12 += v * b11;
4613
- t13 += v * b12;
4614
- t14 += v * b13;
4615
- t15 += v * b14;
4616
- t16 += v * b15;
4617
- v = a[2];
4618
- t2 += v * b0;
4619
- t3 += v * b1;
4620
- t4 += v * b2;
4621
- t5 += v * b3;
4622
- t6 += v * b4;
4623
- t7 += v * b5;
4624
- t8 += v * b6;
4625
- t9 += v * b7;
4626
- t10 += v * b8;
4627
- t11 += v * b9;
4628
- t12 += v * b10;
4629
- t13 += v * b11;
4630
- t14 += v * b12;
4631
- t15 += v * b13;
4632
- t16 += v * b14;
4633
- t17 += v * b15;
4634
- v = a[3];
4635
- t3 += v * b0;
4636
- t4 += v * b1;
4637
- t5 += v * b2;
4638
- t6 += v * b3;
4639
- t7 += v * b4;
4640
- t8 += v * b5;
4641
- t9 += v * b6;
4642
- t10 += v * b7;
4643
- t11 += v * b8;
4644
- t12 += v * b9;
4645
- t13 += v * b10;
4646
- t14 += v * b11;
4647
- t15 += v * b12;
4648
- t16 += v * b13;
4649
- t17 += v * b14;
4650
- t18 += v * b15;
4651
- v = a[4];
4652
- t4 += v * b0;
4653
- t5 += v * b1;
4654
- t6 += v * b2;
4655
- t7 += v * b3;
4656
- t8 += v * b4;
4657
- t9 += v * b5;
4658
- t10 += v * b6;
4659
- t11 += v * b7;
4660
- t12 += v * b8;
4661
- t13 += v * b9;
4662
- t14 += v * b10;
4663
- t15 += v * b11;
4664
- t16 += v * b12;
4665
- t17 += v * b13;
4666
- t18 += v * b14;
4667
- t19 += v * b15;
4668
- v = a[5];
4669
- t5 += v * b0;
4670
- t6 += v * b1;
4671
- t7 += v * b2;
4672
- t8 += v * b3;
4673
- t9 += v * b4;
4674
- t10 += v * b5;
4675
- t11 += v * b6;
4676
- t12 += v * b7;
4677
- t13 += v * b8;
4678
- t14 += v * b9;
4679
- t15 += v * b10;
4680
- t16 += v * b11;
4681
- t17 += v * b12;
4682
- t18 += v * b13;
4683
- t19 += v * b14;
4684
- t20 += v * b15;
4685
- v = a[6];
4686
- t6 += v * b0;
4687
- t7 += v * b1;
4688
- t8 += v * b2;
4689
- t9 += v * b3;
4690
- t10 += v * b4;
4691
- t11 += v * b5;
4692
- t12 += v * b6;
4693
- t13 += v * b7;
4694
- t14 += v * b8;
4695
- t15 += v * b9;
4696
- t16 += v * b10;
4697
- t17 += v * b11;
4698
- t18 += v * b12;
4699
- t19 += v * b13;
4700
- t20 += v * b14;
4701
- t21 += v * b15;
4702
- v = a[7];
4703
- t7 += v * b0;
4704
- t8 += v * b1;
4705
- t9 += v * b2;
4706
- t10 += v * b3;
4707
- t11 += v * b4;
4708
- t12 += v * b5;
4709
- t13 += v * b6;
4710
- t14 += v * b7;
4711
- t15 += v * b8;
4712
- t16 += v * b9;
4713
- t17 += v * b10;
4714
- t18 += v * b11;
4715
- t19 += v * b12;
4716
- t20 += v * b13;
4717
- t21 += v * b14;
4718
- t22 += v * b15;
4719
- v = a[8];
4720
- t8 += v * b0;
4721
- t9 += v * b1;
4722
- t10 += v * b2;
4723
- t11 += v * b3;
4724
- t12 += v * b4;
4725
- t13 += v * b5;
4726
- t14 += v * b6;
4727
- t15 += v * b7;
4728
- t16 += v * b8;
4729
- t17 += v * b9;
4730
- t18 += v * b10;
4731
- t19 += v * b11;
4732
- t20 += v * b12;
4733
- t21 += v * b13;
4734
- t22 += v * b14;
4735
- t23 += v * b15;
4736
- v = a[9];
4737
- t9 += v * b0;
4738
- t10 += v * b1;
4739
- t11 += v * b2;
4740
- t12 += v * b3;
4741
- t13 += v * b4;
4742
- t14 += v * b5;
4743
- t15 += v * b6;
4744
- t16 += v * b7;
4745
- t17 += v * b8;
4746
- t18 += v * b9;
4747
- t19 += v * b10;
4748
- t20 += v * b11;
4749
- t21 += v * b12;
4750
- t22 += v * b13;
4751
- t23 += v * b14;
4752
- t24 += v * b15;
4753
- v = a[10];
4754
- t10 += v * b0;
4755
- t11 += v * b1;
4756
- t12 += v * b2;
4757
- t13 += v * b3;
4758
- t14 += v * b4;
4759
- t15 += v * b5;
4760
- t16 += v * b6;
4761
- t17 += v * b7;
4762
- t18 += v * b8;
4763
- t19 += v * b9;
4764
- t20 += v * b10;
4765
- t21 += v * b11;
4766
- t22 += v * b12;
4767
- t23 += v * b13;
4768
- t24 += v * b14;
4769
- t25 += v * b15;
4770
- v = a[11];
4771
- t11 += v * b0;
4772
- t12 += v * b1;
4773
- t13 += v * b2;
4774
- t14 += v * b3;
4775
- t15 += v * b4;
4776
- t16 += v * b5;
4777
- t17 += v * b6;
4778
- t18 += v * b7;
4779
- t19 += v * b8;
4780
- t20 += v * b9;
4781
- t21 += v * b10;
4782
- t22 += v * b11;
4783
- t23 += v * b12;
4784
- t24 += v * b13;
4785
- t25 += v * b14;
4786
- t26 += v * b15;
4787
- v = a[12];
4788
- t12 += v * b0;
4789
- t13 += v * b1;
4790
- t14 += v * b2;
4791
- t15 += v * b3;
4792
- t16 += v * b4;
4793
- t17 += v * b5;
4794
- t18 += v * b6;
4795
- t19 += v * b7;
4796
- t20 += v * b8;
4797
- t21 += v * b9;
4798
- t22 += v * b10;
4799
- t23 += v * b11;
4800
- t24 += v * b12;
4801
- t25 += v * b13;
4802
- t26 += v * b14;
4803
- t27 += v * b15;
4804
- v = a[13];
4805
- t13 += v * b0;
4806
- t14 += v * b1;
4807
- t15 += v * b2;
4808
- t16 += v * b3;
4809
- t17 += v * b4;
4810
- t18 += v * b5;
4811
- t19 += v * b6;
4812
- t20 += v * b7;
4813
- t21 += v * b8;
4814
- t22 += v * b9;
4815
- t23 += v * b10;
4816
- t24 += v * b11;
4817
- t25 += v * b12;
4818
- t26 += v * b13;
4819
- t27 += v * b14;
4820
- t28 += v * b15;
4821
- v = a[14];
4822
- t14 += v * b0;
4823
- t15 += v * b1;
4824
- t16 += v * b2;
4825
- t17 += v * b3;
4826
- t18 += v * b4;
4827
- t19 += v * b5;
4828
- t20 += v * b6;
4829
- t21 += v * b7;
4830
- t22 += v * b8;
4831
- t23 += v * b9;
4832
- t24 += v * b10;
4833
- t25 += v * b11;
4834
- t26 += v * b12;
4835
- t27 += v * b13;
4836
- t28 += v * b14;
4837
- t29 += v * b15;
4838
- v = a[15];
4839
- t15 += v * b0;
4840
- t16 += v * b1;
4841
- t17 += v * b2;
4842
- t18 += v * b3;
4843
- t19 += v * b4;
4844
- t20 += v * b5;
4845
- t21 += v * b6;
4846
- t22 += v * b7;
4847
- t23 += v * b8;
4848
- t24 += v * b9;
4849
- t25 += v * b10;
4850
- t26 += v * b11;
4851
- t27 += v * b12;
4852
- t28 += v * b13;
4853
- t29 += v * b14;
4854
- t30 += v * b15;
4855
-
4856
- t0 += 38 * t16;
4857
- t1 += 38 * t17;
4858
- t2 += 38 * t18;
4859
- t3 += 38 * t19;
4860
- t4 += 38 * t20;
4861
- t5 += 38 * t21;
4862
- t6 += 38 * t22;
4863
- t7 += 38 * t23;
4864
- t8 += 38 * t24;
4865
- t9 += 38 * t25;
4866
- t10 += 38 * t26;
4867
- t11 += 38 * t27;
4868
- t12 += 38 * t28;
4869
- t13 += 38 * t29;
4870
- t14 += 38 * t30;
4871
- // t15 left as is
4872
-
4873
- // first car
4874
- c = 1;
4875
- v = t0 + c + 65535; c = Math.floor(v / 65536); t0 = v - c * 65536;
4876
- v = t1 + c + 65535; c = Math.floor(v / 65536); t1 = v - c * 65536;
4877
- v = t2 + c + 65535; c = Math.floor(v / 65536); t2 = v - c * 65536;
4878
- v = t3 + c + 65535; c = Math.floor(v / 65536); t3 = v - c * 65536;
4879
- v = t4 + c + 65535; c = Math.floor(v / 65536); t4 = v - c * 65536;
4880
- v = t5 + c + 65535; c = Math.floor(v / 65536); t5 = v - c * 65536;
4881
- v = t6 + c + 65535; c = Math.floor(v / 65536); t6 = v - c * 65536;
4882
- v = t7 + c + 65535; c = Math.floor(v / 65536); t7 = v - c * 65536;
4883
- v = t8 + c + 65535; c = Math.floor(v / 65536); t8 = v - c * 65536;
4884
- v = t9 + c + 65535; c = Math.floor(v / 65536); t9 = v - c * 65536;
4885
- v = t10 + c + 65535; c = Math.floor(v / 65536); t10 = v - c * 65536;
4886
- v = t11 + c + 65535; c = Math.floor(v / 65536); t11 = v - c * 65536;
4887
- v = t12 + c + 65535; c = Math.floor(v / 65536); t12 = v - c * 65536;
4888
- v = t13 + c + 65535; c = Math.floor(v / 65536); t13 = v - c * 65536;
4889
- v = t14 + c + 65535; c = Math.floor(v / 65536); t14 = v - c * 65536;
4890
- v = t15 + c + 65535; c = Math.floor(v / 65536); t15 = v - c * 65536;
4891
- t0 += c-1 + 37 * (c-1);
4892
-
4893
- // second car
4894
- c = 1;
4895
- v = t0 + c + 65535; c = Math.floor(v / 65536); t0 = v - c * 65536;
4896
- v = t1 + c + 65535; c = Math.floor(v / 65536); t1 = v - c * 65536;
4897
- v = t2 + c + 65535; c = Math.floor(v / 65536); t2 = v - c * 65536;
4898
- v = t3 + c + 65535; c = Math.floor(v / 65536); t3 = v - c * 65536;
4899
- v = t4 + c + 65535; c = Math.floor(v / 65536); t4 = v - c * 65536;
4900
- v = t5 + c + 65535; c = Math.floor(v / 65536); t5 = v - c * 65536;
4901
- v = t6 + c + 65535; c = Math.floor(v / 65536); t6 = v - c * 65536;
4902
- v = t7 + c + 65535; c = Math.floor(v / 65536); t7 = v - c * 65536;
4903
- v = t8 + c + 65535; c = Math.floor(v / 65536); t8 = v - c * 65536;
4904
- v = t9 + c + 65535; c = Math.floor(v / 65536); t9 = v - c * 65536;
4905
- v = t10 + c + 65535; c = Math.floor(v / 65536); t10 = v - c * 65536;
4906
- v = t11 + c + 65535; c = Math.floor(v / 65536); t11 = v - c * 65536;
4907
- v = t12 + c + 65535; c = Math.floor(v / 65536); t12 = v - c * 65536;
4908
- v = t13 + c + 65535; c = Math.floor(v / 65536); t13 = v - c * 65536;
4909
- v = t14 + c + 65535; c = Math.floor(v / 65536); t14 = v - c * 65536;
4910
- v = t15 + c + 65535; c = Math.floor(v / 65536); t15 = v - c * 65536;
4911
- t0 += c-1 + 37 * (c-1);
4912
-
4913
- o[ 0] = t0;
4914
- o[ 1] = t1;
4915
- o[ 2] = t2;
4916
- o[ 3] = t3;
4917
- o[ 4] = t4;
4918
- o[ 5] = t5;
4919
- o[ 6] = t6;
4920
- o[ 7] = t7;
4921
- o[ 8] = t8;
4922
- o[ 9] = t9;
4923
- o[10] = t10;
4924
- o[11] = t11;
4925
- o[12] = t12;
4926
- o[13] = t13;
4927
- o[14] = t14;
4928
- o[15] = t15;
4929
- }
4930
-
4931
- function S(o, a) {
4932
- M(o, a, a);
4933
- }
4934
-
4935
- function inv25519(o, i) {
4936
- var c = gf();
4937
- var a;
4938
- for (a = 0; a < 16; a++) c[a] = i[a];
4939
- for (a = 253; a >= 0; a--) {
4940
- S(c, c);
4941
- if(a !== 2 && a !== 4) M(c, c, i);
4942
- }
4943
- for (a = 0; a < 16; a++) o[a] = c[a];
4944
- }
4945
-
4946
- function pow2523(o, i) {
4947
- var c = gf();
4948
- var a;
4949
- for (a = 0; a < 16; a++) c[a] = i[a];
4950
- for (a = 250; a >= 0; a--) {
4951
- S(c, c);
4952
- if(a !== 1) M(c, c, i);
4953
- }
4954
- for (a = 0; a < 16; a++) o[a] = c[a];
4955
- }
4956
-
4957
- function crypto_scalarmult(q, n, p) {
4958
- var z = new Uint8Array(32);
4959
- var x = new Float64Array(80), r, i;
4960
- var a = gf(), b = gf(), c = gf(),
4961
- d = gf(), e = gf(), f = gf();
4962
- for (i = 0; i < 31; i++) z[i] = n[i];
4963
- z[31]=(n[31]&127)|64;
4964
- z[0]&=248;
4965
- unpack25519(x,p);
4966
- for (i = 0; i < 16; i++) {
4967
- b[i]=x[i];
4968
- d[i]=a[i]=c[i]=0;
4969
- }
4970
- a[0]=d[0]=1;
4971
- for (i=254; i>=0; --i) {
4972
- r=(z[i>>>3]>>>(i&7))&1;
4973
- sel25519(a,b,r);
4974
- sel25519(c,d,r);
4975
- A(e,a,c);
4976
- Z(a,a,c);
4977
- A(c,b,d);
4978
- Z(b,b,d);
4979
- S(d,e);
4980
- S(f,a);
4981
- M(a,c,a);
4982
- M(c,b,e);
4983
- A(e,a,c);
4984
- Z(a,a,c);
4985
- S(b,a);
4986
- Z(c,d,f);
4987
- M(a,c,_121665);
4988
- A(a,a,d);
4989
- M(c,c,a);
4990
- M(a,d,f);
4991
- M(d,b,x);
4992
- S(b,e);
4993
- sel25519(a,b,r);
4994
- sel25519(c,d,r);
4995
- }
4996
- for (i = 0; i < 16; i++) {
4997
- x[i+16]=a[i];
4998
- x[i+32]=c[i];
4999
- x[i+48]=b[i];
5000
- x[i+64]=d[i];
5001
- }
5002
- var x32 = x.subarray(32);
5003
- var x16 = x.subarray(16);
5004
- inv25519(x32,x32);
5005
- M(x16,x16,x32);
5006
- pack25519(q,x16);
5007
- return 0;
5008
- }
5009
-
5010
- function crypto_scalarmult_base(q, n) {
5011
- return crypto_scalarmult(q, n, _9);
5012
- }
5013
-
5014
- function crypto_box_keypair(y, x) {
5015
- randombytes(x, 32);
5016
- return crypto_scalarmult_base(y, x);
5017
- }
5018
-
5019
- function crypto_box_beforenm(k, y, x) {
5020
- var s = new Uint8Array(32);
5021
- crypto_scalarmult(s, x, y);
5022
- return crypto_core_hsalsa20(k, _0, s, sigma);
5023
- }
5024
-
5025
- var crypto_box_afternm = crypto_secretbox;
5026
- var crypto_box_open_afternm = crypto_secretbox_open;
5027
-
5028
- function crypto_box(c, m, d, n, y, x) {
5029
- var k = new Uint8Array(32);
5030
- crypto_box_beforenm(k, y, x);
5031
- return crypto_box_afternm(c, m, d, n, k);
5032
- }
5033
-
5034
- function crypto_box_open(m, c, d, n, y, x) {
5035
- var k = new Uint8Array(32);
5036
- crypto_box_beforenm(k, y, x);
5037
- return crypto_box_open_afternm(m, c, d, n, k);
5038
- }
5039
-
5040
- var K = [
5041
- 0x428a2f98, 0xd728ae22, 0x71374491, 0x23ef65cd,
5042
- 0xb5c0fbcf, 0xec4d3b2f, 0xe9b5dba5, 0x8189dbbc,
5043
- 0x3956c25b, 0xf348b538, 0x59f111f1, 0xb605d019,
5044
- 0x923f82a4, 0xaf194f9b, 0xab1c5ed5, 0xda6d8118,
5045
- 0xd807aa98, 0xa3030242, 0x12835b01, 0x45706fbe,
5046
- 0x243185be, 0x4ee4b28c, 0x550c7dc3, 0xd5ffb4e2,
5047
- 0x72be5d74, 0xf27b896f, 0x80deb1fe, 0x3b1696b1,
5048
- 0x9bdc06a7, 0x25c71235, 0xc19bf174, 0xcf692694,
5049
- 0xe49b69c1, 0x9ef14ad2, 0xefbe4786, 0x384f25e3,
5050
- 0x0fc19dc6, 0x8b8cd5b5, 0x240ca1cc, 0x77ac9c65,
5051
- 0x2de92c6f, 0x592b0275, 0x4a7484aa, 0x6ea6e483,
5052
- 0x5cb0a9dc, 0xbd41fbd4, 0x76f988da, 0x831153b5,
5053
- 0x983e5152, 0xee66dfab, 0xa831c66d, 0x2db43210,
5054
- 0xb00327c8, 0x98fb213f, 0xbf597fc7, 0xbeef0ee4,
5055
- 0xc6e00bf3, 0x3da88fc2, 0xd5a79147, 0x930aa725,
5056
- 0x06ca6351, 0xe003826f, 0x14292967, 0x0a0e6e70,
5057
- 0x27b70a85, 0x46d22ffc, 0x2e1b2138, 0x5c26c926,
5058
- 0x4d2c6dfc, 0x5ac42aed, 0x53380d13, 0x9d95b3df,
5059
- 0x650a7354, 0x8baf63de, 0x766a0abb, 0x3c77b2a8,
5060
- 0x81c2c92e, 0x47edaee6, 0x92722c85, 0x1482353b,
5061
- 0xa2bfe8a1, 0x4cf10364, 0xa81a664b, 0xbc423001,
5062
- 0xc24b8b70, 0xd0f89791, 0xc76c51a3, 0x0654be30,
5063
- 0xd192e819, 0xd6ef5218, 0xd6990624, 0x5565a910,
5064
- 0xf40e3585, 0x5771202a, 0x106aa070, 0x32bbd1b8,
5065
- 0x19a4c116, 0xb8d2d0c8, 0x1e376c08, 0x5141ab53,
5066
- 0x2748774c, 0xdf8eeb99, 0x34b0bcb5, 0xe19b48a8,
5067
- 0x391c0cb3, 0xc5c95a63, 0x4ed8aa4a, 0xe3418acb,
5068
- 0x5b9cca4f, 0x7763e373, 0x682e6ff3, 0xd6b2b8a3,
5069
- 0x748f82ee, 0x5defb2fc, 0x78a5636f, 0x43172f60,
5070
- 0x84c87814, 0xa1f0ab72, 0x8cc70208, 0x1a6439ec,
5071
- 0x90befffa, 0x23631e28, 0xa4506ceb, 0xde82bde9,
5072
- 0xbef9a3f7, 0xb2c67915, 0xc67178f2, 0xe372532b,
5073
- 0xca273ece, 0xea26619c, 0xd186b8c7, 0x21c0c207,
5074
- 0xeada7dd6, 0xcde0eb1e, 0xf57d4f7f, 0xee6ed178,
5075
- 0x06f067aa, 0x72176fba, 0x0a637dc5, 0xa2c898a6,
5076
- 0x113f9804, 0xbef90dae, 0x1b710b35, 0x131c471b,
5077
- 0x28db77f5, 0x23047d84, 0x32caab7b, 0x40c72493,
5078
- 0x3c9ebe0a, 0x15c9bebc, 0x431d67c4, 0x9c100d4c,
5079
- 0x4cc5d4be, 0xcb3e42b6, 0x597f299c, 0xfc657e2a,
5080
- 0x5fcb6fab, 0x3ad6faec, 0x6c44198c, 0x4a475817
5081
- ];
5082
-
5083
- function crypto_hashblocks_hl(hh, hl, m, n) {
5084
- var wh = new Int32Array(16), wl = new Int32Array(16),
5085
- bh0, bh1, bh2, bh3, bh4, bh5, bh6, bh7,
5086
- bl0, bl1, bl2, bl3, bl4, bl5, bl6, bl7,
5087
- th, tl, i, j, h, l, a, b, c, d;
5088
-
5089
- var ah0 = hh[0],
5090
- ah1 = hh[1],
5091
- ah2 = hh[2],
5092
- ah3 = hh[3],
5093
- ah4 = hh[4],
5094
- ah5 = hh[5],
5095
- ah6 = hh[6],
5096
- ah7 = hh[7],
5097
-
5098
- al0 = hl[0],
5099
- al1 = hl[1],
5100
- al2 = hl[2],
5101
- al3 = hl[3],
5102
- al4 = hl[4],
5103
- al5 = hl[5],
5104
- al6 = hl[6],
5105
- al7 = hl[7];
5106
-
5107
- var pos = 0;
5108
- while (n >= 128) {
5109
- for (i = 0; i < 16; i++) {
5110
- j = 8 * i + pos;
5111
- wh[i] = (m[j+0] << 24) | (m[j+1] << 16) | (m[j+2] << 8) | m[j+3];
5112
- wl[i] = (m[j+4] << 24) | (m[j+5] << 16) | (m[j+6] << 8) | m[j+7];
5113
- }
5114
- for (i = 0; i < 80; i++) {
5115
- bh0 = ah0;
5116
- bh1 = ah1;
5117
- bh2 = ah2;
5118
- bh3 = ah3;
5119
- bh4 = ah4;
5120
- bh5 = ah5;
5121
- bh6 = ah6;
5122
- bh7 = ah7;
5123
-
5124
- bl0 = al0;
5125
- bl1 = al1;
5126
- bl2 = al2;
5127
- bl3 = al3;
5128
- bl4 = al4;
5129
- bl5 = al5;
5130
- bl6 = al6;
5131
- bl7 = al7;
5132
-
5133
- // add
5134
- h = ah7;
5135
- l = al7;
5136
-
5137
- a = l & 0xffff; b = l >>> 16;
5138
- c = h & 0xffff; d = h >>> 16;
5139
-
5140
- // Sigma1
5141
- h = ((ah4 >>> 14) | (al4 << (32-14))) ^ ((ah4 >>> 18) | (al4 << (32-18))) ^ ((al4 >>> (41-32)) | (ah4 << (32-(41-32))));
5142
- l = ((al4 >>> 14) | (ah4 << (32-14))) ^ ((al4 >>> 18) | (ah4 << (32-18))) ^ ((ah4 >>> (41-32)) | (al4 << (32-(41-32))));
5143
-
5144
- a += l & 0xffff; b += l >>> 16;
5145
- c += h & 0xffff; d += h >>> 16;
5146
-
5147
- // Ch
5148
- h = (ah4 & ah5) ^ (~ah4 & ah6);
5149
- l = (al4 & al5) ^ (~al4 & al6);
5150
-
5151
- a += l & 0xffff; b += l >>> 16;
5152
- c += h & 0xffff; d += h >>> 16;
5153
-
5154
- // K
5155
- h = K[i*2];
5156
- l = K[i*2+1];
5157
-
5158
- a += l & 0xffff; b += l >>> 16;
5159
- c += h & 0xffff; d += h >>> 16;
5160
-
5161
- // w
5162
- h = wh[i%16];
5163
- l = wl[i%16];
5164
-
5165
- a += l & 0xffff; b += l >>> 16;
5166
- c += h & 0xffff; d += h >>> 16;
5167
-
5168
- b += a >>> 16;
5169
- c += b >>> 16;
5170
- d += c >>> 16;
5171
-
5172
- th = c & 0xffff | d << 16;
5173
- tl = a & 0xffff | b << 16;
5174
-
5175
- // add
5176
- h = th;
5177
- l = tl;
5178
-
5179
- a = l & 0xffff; b = l >>> 16;
5180
- c = h & 0xffff; d = h >>> 16;
5181
-
5182
- // Sigma0
5183
- h = ((ah0 >>> 28) | (al0 << (32-28))) ^ ((al0 >>> (34-32)) | (ah0 << (32-(34-32)))) ^ ((al0 >>> (39-32)) | (ah0 << (32-(39-32))));
5184
- l = ((al0 >>> 28) | (ah0 << (32-28))) ^ ((ah0 >>> (34-32)) | (al0 << (32-(34-32)))) ^ ((ah0 >>> (39-32)) | (al0 << (32-(39-32))));
5185
-
5186
- a += l & 0xffff; b += l >>> 16;
5187
- c += h & 0xffff; d += h >>> 16;
5188
-
5189
- // Maj
5190
- h = (ah0 & ah1) ^ (ah0 & ah2) ^ (ah1 & ah2);
5191
- l = (al0 & al1) ^ (al0 & al2) ^ (al1 & al2);
5192
-
5193
- a += l & 0xffff; b += l >>> 16;
5194
- c += h & 0xffff; d += h >>> 16;
5195
-
5196
- b += a >>> 16;
5197
- c += b >>> 16;
5198
- d += c >>> 16;
5199
-
5200
- bh7 = (c & 0xffff) | (d << 16);
5201
- bl7 = (a & 0xffff) | (b << 16);
5202
-
5203
- // add
5204
- h = bh3;
5205
- l = bl3;
5206
-
5207
- a = l & 0xffff; b = l >>> 16;
5208
- c = h & 0xffff; d = h >>> 16;
5209
-
5210
- h = th;
5211
- l = tl;
5212
-
5213
- a += l & 0xffff; b += l >>> 16;
5214
- c += h & 0xffff; d += h >>> 16;
5215
-
5216
- b += a >>> 16;
5217
- c += b >>> 16;
5218
- d += c >>> 16;
5219
-
5220
- bh3 = (c & 0xffff) | (d << 16);
5221
- bl3 = (a & 0xffff) | (b << 16);
5222
-
5223
- ah1 = bh0;
5224
- ah2 = bh1;
5225
- ah3 = bh2;
5226
- ah4 = bh3;
5227
- ah5 = bh4;
5228
- ah6 = bh5;
5229
- ah7 = bh6;
5230
- ah0 = bh7;
5231
-
5232
- al1 = bl0;
5233
- al2 = bl1;
5234
- al3 = bl2;
5235
- al4 = bl3;
5236
- al5 = bl4;
5237
- al6 = bl5;
5238
- al7 = bl6;
5239
- al0 = bl7;
5240
-
5241
- if (i%16 === 15) {
5242
- for (j = 0; j < 16; j++) {
5243
- // add
5244
- h = wh[j];
5245
- l = wl[j];
5246
-
5247
- a = l & 0xffff; b = l >>> 16;
5248
- c = h & 0xffff; d = h >>> 16;
5249
-
5250
- h = wh[(j+9)%16];
5251
- l = wl[(j+9)%16];
5252
-
5253
- a += l & 0xffff; b += l >>> 16;
5254
- c += h & 0xffff; d += h >>> 16;
5255
-
5256
- // sigma0
5257
- th = wh[(j+1)%16];
5258
- tl = wl[(j+1)%16];
5259
- h = ((th >>> 1) | (tl << (32-1))) ^ ((th >>> 8) | (tl << (32-8))) ^ (th >>> 7);
5260
- l = ((tl >>> 1) | (th << (32-1))) ^ ((tl >>> 8) | (th << (32-8))) ^ ((tl >>> 7) | (th << (32-7)));
5261
-
5262
- a += l & 0xffff; b += l >>> 16;
5263
- c += h & 0xffff; d += h >>> 16;
5264
-
5265
- // sigma1
5266
- th = wh[(j+14)%16];
5267
- tl = wl[(j+14)%16];
5268
- h = ((th >>> 19) | (tl << (32-19))) ^ ((tl >>> (61-32)) | (th << (32-(61-32)))) ^ (th >>> 6);
5269
- l = ((tl >>> 19) | (th << (32-19))) ^ ((th >>> (61-32)) | (tl << (32-(61-32)))) ^ ((tl >>> 6) | (th << (32-6)));
5270
-
5271
- a += l & 0xffff; b += l >>> 16;
5272
- c += h & 0xffff; d += h >>> 16;
5273
-
5274
- b += a >>> 16;
5275
- c += b >>> 16;
5276
- d += c >>> 16;
5277
-
5278
- wh[j] = (c & 0xffff) | (d << 16);
5279
- wl[j] = (a & 0xffff) | (b << 16);
5280
- }
5281
- }
5282
- }
5283
-
5284
- // add
5285
- h = ah0;
5286
- l = al0;
5287
-
5288
- a = l & 0xffff; b = l >>> 16;
5289
- c = h & 0xffff; d = h >>> 16;
5290
-
5291
- h = hh[0];
5292
- l = hl[0];
5293
-
5294
- a += l & 0xffff; b += l >>> 16;
5295
- c += h & 0xffff; d += h >>> 16;
5296
-
5297
- b += a >>> 16;
5298
- c += b >>> 16;
5299
- d += c >>> 16;
5300
-
5301
- hh[0] = ah0 = (c & 0xffff) | (d << 16);
5302
- hl[0] = al0 = (a & 0xffff) | (b << 16);
5303
-
5304
- h = ah1;
5305
- l = al1;
5306
-
5307
- a = l & 0xffff; b = l >>> 16;
5308
- c = h & 0xffff; d = h >>> 16;
5309
-
5310
- h = hh[1];
5311
- l = hl[1];
5312
-
5313
- a += l & 0xffff; b += l >>> 16;
5314
- c += h & 0xffff; d += h >>> 16;
5315
-
5316
- b += a >>> 16;
5317
- c += b >>> 16;
5318
- d += c >>> 16;
5319
-
5320
- hh[1] = ah1 = (c & 0xffff) | (d << 16);
5321
- hl[1] = al1 = (a & 0xffff) | (b << 16);
5322
-
5323
- h = ah2;
5324
- l = al2;
5325
-
5326
- a = l & 0xffff; b = l >>> 16;
5327
- c = h & 0xffff; d = h >>> 16;
5328
-
5329
- h = hh[2];
5330
- l = hl[2];
5331
-
5332
- a += l & 0xffff; b += l >>> 16;
5333
- c += h & 0xffff; d += h >>> 16;
5334
-
5335
- b += a >>> 16;
5336
- c += b >>> 16;
5337
- d += c >>> 16;
5338
-
5339
- hh[2] = ah2 = (c & 0xffff) | (d << 16);
5340
- hl[2] = al2 = (a & 0xffff) | (b << 16);
5341
-
5342
- h = ah3;
5343
- l = al3;
5344
-
5345
- a = l & 0xffff; b = l >>> 16;
5346
- c = h & 0xffff; d = h >>> 16;
5347
-
5348
- h = hh[3];
5349
- l = hl[3];
5350
-
5351
- a += l & 0xffff; b += l >>> 16;
5352
- c += h & 0xffff; d += h >>> 16;
5353
-
5354
- b += a >>> 16;
5355
- c += b >>> 16;
5356
- d += c >>> 16;
5357
-
5358
- hh[3] = ah3 = (c & 0xffff) | (d << 16);
5359
- hl[3] = al3 = (a & 0xffff) | (b << 16);
5360
-
5361
- h = ah4;
5362
- l = al4;
5363
-
5364
- a = l & 0xffff; b = l >>> 16;
5365
- c = h & 0xffff; d = h >>> 16;
5366
-
5367
- h = hh[4];
5368
- l = hl[4];
5369
-
5370
- a += l & 0xffff; b += l >>> 16;
5371
- c += h & 0xffff; d += h >>> 16;
5372
-
5373
- b += a >>> 16;
5374
- c += b >>> 16;
5375
- d += c >>> 16;
5376
-
5377
- hh[4] = ah4 = (c & 0xffff) | (d << 16);
5378
- hl[4] = al4 = (a & 0xffff) | (b << 16);
5379
-
5380
- h = ah5;
5381
- l = al5;
5382
-
5383
- a = l & 0xffff; b = l >>> 16;
5384
- c = h & 0xffff; d = h >>> 16;
5385
-
5386
- h = hh[5];
5387
- l = hl[5];
5388
-
5389
- a += l & 0xffff; b += l >>> 16;
5390
- c += h & 0xffff; d += h >>> 16;
5391
-
5392
- b += a >>> 16;
5393
- c += b >>> 16;
5394
- d += c >>> 16;
5395
-
5396
- hh[5] = ah5 = (c & 0xffff) | (d << 16);
5397
- hl[5] = al5 = (a & 0xffff) | (b << 16);
5398
-
5399
- h = ah6;
5400
- l = al6;
5401
-
5402
- a = l & 0xffff; b = l >>> 16;
5403
- c = h & 0xffff; d = h >>> 16;
5404
-
5405
- h = hh[6];
5406
- l = hl[6];
5407
-
5408
- a += l & 0xffff; b += l >>> 16;
5409
- c += h & 0xffff; d += h >>> 16;
5410
-
5411
- b += a >>> 16;
5412
- c += b >>> 16;
5413
- d += c >>> 16;
5414
-
5415
- hh[6] = ah6 = (c & 0xffff) | (d << 16);
5416
- hl[6] = al6 = (a & 0xffff) | (b << 16);
5417
-
5418
- h = ah7;
5419
- l = al7;
5420
-
5421
- a = l & 0xffff; b = l >>> 16;
5422
- c = h & 0xffff; d = h >>> 16;
5423
-
5424
- h = hh[7];
5425
- l = hl[7];
5426
-
5427
- a += l & 0xffff; b += l >>> 16;
5428
- c += h & 0xffff; d += h >>> 16;
5429
-
5430
- b += a >>> 16;
5431
- c += b >>> 16;
5432
- d += c >>> 16;
5433
-
5434
- hh[7] = ah7 = (c & 0xffff) | (d << 16);
5435
- hl[7] = al7 = (a & 0xffff) | (b << 16);
5436
-
5437
- pos += 128;
5438
- n -= 128;
5439
- }
5440
-
5441
- return n;
5442
- }
5443
-
5444
- function crypto_hash(out, m, n) {
5445
- var hh = new Int32Array(8),
5446
- hl = new Int32Array(8),
5447
- x = new Uint8Array(256),
5448
- i, b = n;
5449
-
5450
- hh[0] = 0x6a09e667;
5451
- hh[1] = 0xbb67ae85;
5452
- hh[2] = 0x3c6ef372;
5453
- hh[3] = 0xa54ff53a;
5454
- hh[4] = 0x510e527f;
5455
- hh[5] = 0x9b05688c;
5456
- hh[6] = 0x1f83d9ab;
5457
- hh[7] = 0x5be0cd19;
5458
-
5459
- hl[0] = 0xf3bcc908;
5460
- hl[1] = 0x84caa73b;
5461
- hl[2] = 0xfe94f82b;
5462
- hl[3] = 0x5f1d36f1;
5463
- hl[4] = 0xade682d1;
5464
- hl[5] = 0x2b3e6c1f;
5465
- hl[6] = 0xfb41bd6b;
5466
- hl[7] = 0x137e2179;
5467
-
5468
- crypto_hashblocks_hl(hh, hl, m, n);
5469
- n %= 128;
5470
-
5471
- for (i = 0; i < n; i++) x[i] = m[b-n+i];
5472
- x[n] = 128;
5473
-
5474
- n = 256-128*(n<112?1:0);
5475
- x[n-9] = 0;
5476
- ts64(x, n-8, (b / 0x20000000) | 0, b << 3);
5477
- crypto_hashblocks_hl(hh, hl, x, n);
5478
-
5479
- for (i = 0; i < 8; i++) ts64(out, 8*i, hh[i], hl[i]);
5480
-
5481
- return 0;
5482
- }
5483
-
5484
- function add(p, q) {
5485
- var a = gf(), b = gf(), c = gf(),
5486
- d = gf(), e = gf(), f = gf(),
5487
- g = gf(), h = gf(), t = gf();
5488
-
5489
- Z(a, p[1], p[0]);
5490
- Z(t, q[1], q[0]);
5491
- M(a, a, t);
5492
- A(b, p[0], p[1]);
5493
- A(t, q[0], q[1]);
5494
- M(b, b, t);
5495
- M(c, p[3], q[3]);
5496
- M(c, c, D2);
5497
- M(d, p[2], q[2]);
5498
- A(d, d, d);
5499
- Z(e, b, a);
5500
- Z(f, d, c);
5501
- A(g, d, c);
5502
- A(h, b, a);
5503
-
5504
- M(p[0], e, f);
5505
- M(p[1], h, g);
5506
- M(p[2], g, f);
5507
- M(p[3], e, h);
5508
- }
5509
-
5510
- function cswap(p, q, b) {
5511
- var i;
5512
- for (i = 0; i < 4; i++) {
5513
- sel25519(p[i], q[i], b);
5514
- }
5515
- }
5516
-
5517
- function pack(r, p) {
5518
- var tx = gf(), ty = gf(), zi = gf();
5519
- inv25519(zi, p[2]);
5520
- M(tx, p[0], zi);
5521
- M(ty, p[1], zi);
5522
- pack25519(r, ty);
5523
- r[31] ^= par25519(tx) << 7;
5524
- }
5525
-
5526
- function scalarmult(p, q, s) {
5527
- var b, i;
5528
- set25519(p[0], gf0);
5529
- set25519(p[1], gf1);
5530
- set25519(p[2], gf1);
5531
- set25519(p[3], gf0);
5532
- for (i = 255; i >= 0; --i) {
5533
- b = (s[(i/8)|0] >> (i&7)) & 1;
5534
- cswap(p, q, b);
5535
- add(q, p);
5536
- add(p, p);
5537
- cswap(p, q, b);
5538
- }
5539
- }
5540
-
5541
- function scalarbase(p, s) {
5542
- var q = [gf(), gf(), gf(), gf()];
5543
- set25519(q[0], X);
5544
- set25519(q[1], Y);
5545
- set25519(q[2], gf1);
5546
- M(q[3], X, Y);
5547
- scalarmult(p, q, s);
5548
- }
5549
-
5550
- function crypto_sign_keypair(pk, sk, seeded) {
5551
- var d = new Uint8Array(64);
5552
- var p = [gf(), gf(), gf(), gf()];
5553
- var i;
5554
-
5555
- if (!seeded) randombytes(sk, 32);
5556
- crypto_hash(d, sk, 32);
5557
- d[0] &= 248;
5558
- d[31] &= 127;
5559
- d[31] |= 64;
5560
-
5561
- scalarbase(p, d);
5562
- pack(pk, p);
5563
-
5564
- for (i = 0; i < 32; i++) sk[i+32] = pk[i];
5565
- return 0;
5566
- }
5567
-
5568
- var L = new Float64Array([0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x10]);
5569
-
5570
- function modL(r, x) {
5571
- var carry, i, j, k;
5572
- for (i = 63; i >= 32; --i) {
5573
- carry = 0;
5574
- for (j = i - 32, k = i - 12; j < k; ++j) {
5575
- x[j] += carry - 16 * x[i] * L[j - (i - 32)];
5576
- carry = Math.floor((x[j] + 128) / 256);
5577
- x[j] -= carry * 256;
5578
- }
5579
- x[j] += carry;
5580
- x[i] = 0;
5581
- }
5582
- carry = 0;
5583
- for (j = 0; j < 32; j++) {
5584
- x[j] += carry - (x[31] >> 4) * L[j];
5585
- carry = x[j] >> 8;
5586
- x[j] &= 255;
5587
- }
5588
- for (j = 0; j < 32; j++) x[j] -= carry * L[j];
5589
- for (i = 0; i < 32; i++) {
5590
- x[i+1] += x[i] >> 8;
5591
- r[i] = x[i] & 255;
5592
- }
5593
- }
5594
-
5595
- function reduce(r) {
5596
- var x = new Float64Array(64), i;
5597
- for (i = 0; i < 64; i++) x[i] = r[i];
5598
- for (i = 0; i < 64; i++) r[i] = 0;
5599
- modL(r, x);
5600
- }
5601
-
5602
- // Note: difference from C - smlen returned, not passed as argument.
5603
- function crypto_sign(sm, m, n, sk) {
5604
- var d = new Uint8Array(64), h = new Uint8Array(64), r = new Uint8Array(64);
5605
- var i, j, x = new Float64Array(64);
5606
- var p = [gf(), gf(), gf(), gf()];
5607
-
5608
- crypto_hash(d, sk, 32);
5609
- d[0] &= 248;
5610
- d[31] &= 127;
5611
- d[31] |= 64;
5612
-
5613
- var smlen = n + 64;
5614
- for (i = 0; i < n; i++) sm[64 + i] = m[i];
5615
- for (i = 0; i < 32; i++) sm[32 + i] = d[32 + i];
5616
-
5617
- crypto_hash(r, sm.subarray(32), n+32);
5618
- reduce(r);
5619
- scalarbase(p, r);
5620
- pack(sm, p);
5621
-
5622
- for (i = 32; i < 64; i++) sm[i] = sk[i];
5623
- crypto_hash(h, sm, n + 64);
5624
- reduce(h);
5625
-
5626
- for (i = 0; i < 64; i++) x[i] = 0;
5627
- for (i = 0; i < 32; i++) x[i] = r[i];
5628
- for (i = 0; i < 32; i++) {
5629
- for (j = 0; j < 32; j++) {
5630
- x[i+j] += h[i] * d[j];
5631
- }
5632
- }
5633
-
5634
- modL(sm.subarray(32), x);
5635
- return smlen;
5636
- }
5637
-
5638
- function unpackneg(r, p) {
5639
- var t = gf(), chk = gf(), num = gf(),
5640
- den = gf(), den2 = gf(), den4 = gf(),
5641
- den6 = gf();
5642
-
5643
- set25519(r[2], gf1);
5644
- unpack25519(r[1], p);
5645
- S(num, r[1]);
5646
- M(den, num, D);
5647
- Z(num, num, r[2]);
5648
- A(den, r[2], den);
5649
-
5650
- S(den2, den);
5651
- S(den4, den2);
5652
- M(den6, den4, den2);
5653
- M(t, den6, num);
5654
- M(t, t, den);
5655
-
5656
- pow2523(t, t);
5657
- M(t, t, num);
5658
- M(t, t, den);
5659
- M(t, t, den);
5660
- M(r[0], t, den);
5661
-
5662
- S(chk, r[0]);
5663
- M(chk, chk, den);
5664
- if (neq25519(chk, num)) M(r[0], r[0], I);
5665
-
5666
- S(chk, r[0]);
5667
- M(chk, chk, den);
5668
- if (neq25519(chk, num)) return -1;
5669
-
5670
- if (par25519(r[0]) === (p[31]>>7)) Z(r[0], gf0, r[0]);
5671
-
5672
- M(r[3], r[0], r[1]);
5673
- return 0;
5674
- }
5675
-
5676
- function crypto_sign_open(m, sm, n, pk) {
5677
- var i;
5678
- var t = new Uint8Array(32), h = new Uint8Array(64);
5679
- var p = [gf(), gf(), gf(), gf()],
5680
- q = [gf(), gf(), gf(), gf()];
5681
-
5682
- if (n < 64) return -1;
5683
-
5684
- if (unpackneg(q, pk)) return -1;
5685
-
5686
- for (i = 0; i < n; i++) m[i] = sm[i];
5687
- for (i = 0; i < 32; i++) m[i+32] = pk[i];
5688
- crypto_hash(h, m, n);
5689
- reduce(h);
5690
- scalarmult(p, q, h);
5691
-
5692
- scalarbase(q, sm.subarray(32));
5693
- add(p, q);
5694
- pack(t, p);
5695
-
5696
- n -= 64;
5697
- if (crypto_verify_32(sm, 0, t, 0)) {
5698
- for (i = 0; i < n; i++) m[i] = 0;
5699
- return -1;
5700
- }
5701
-
5702
- for (i = 0; i < n; i++) m[i] = sm[i + 64];
5703
- return n;
5704
- }
5705
-
5706
- var crypto_secretbox_KEYBYTES = 32,
5707
- crypto_secretbox_NONCEBYTES = 24,
5708
- crypto_secretbox_ZEROBYTES = 32,
5709
- crypto_secretbox_BOXZEROBYTES = 16,
5710
- crypto_scalarmult_BYTES = 32,
5711
- crypto_scalarmult_SCALARBYTES = 32,
5712
- crypto_box_PUBLICKEYBYTES = 32,
5713
- crypto_box_SECRETKEYBYTES = 32,
5714
- crypto_box_BEFORENMBYTES = 32,
5715
- crypto_box_NONCEBYTES = crypto_secretbox_NONCEBYTES,
5716
- crypto_box_ZEROBYTES = crypto_secretbox_ZEROBYTES,
5717
- crypto_box_BOXZEROBYTES = crypto_secretbox_BOXZEROBYTES,
5718
- crypto_sign_BYTES = 64,
5719
- crypto_sign_PUBLICKEYBYTES = 32,
5720
- crypto_sign_SECRETKEYBYTES = 64,
5721
- crypto_sign_SEEDBYTES = 32,
5722
- crypto_hash_BYTES = 64;
5723
-
5724
- nacl.lowlevel = {
5725
- crypto_core_hsalsa20: crypto_core_hsalsa20,
5726
- crypto_stream_xor: crypto_stream_xor,
5727
- crypto_stream: crypto_stream,
5728
- crypto_stream_salsa20_xor: crypto_stream_salsa20_xor,
5729
- crypto_stream_salsa20: crypto_stream_salsa20,
5730
- crypto_onetimeauth: crypto_onetimeauth,
5731
- crypto_onetimeauth_verify: crypto_onetimeauth_verify,
5732
- crypto_verify_16: crypto_verify_16,
5733
- crypto_verify_32: crypto_verify_32,
5734
- crypto_secretbox: crypto_secretbox,
5735
- crypto_secretbox_open: crypto_secretbox_open,
5736
- crypto_scalarmult: crypto_scalarmult,
5737
- crypto_scalarmult_base: crypto_scalarmult_base,
5738
- crypto_box_beforenm: crypto_box_beforenm,
5739
- crypto_box_afternm: crypto_box_afternm,
5740
- crypto_box: crypto_box,
5741
- crypto_box_open: crypto_box_open,
5742
- crypto_box_keypair: crypto_box_keypair,
5743
- crypto_hash: crypto_hash,
5744
- crypto_sign: crypto_sign,
5745
- crypto_sign_keypair: crypto_sign_keypair,
5746
- crypto_sign_open: crypto_sign_open,
5747
-
5748
- crypto_secretbox_KEYBYTES: crypto_secretbox_KEYBYTES,
5749
- crypto_secretbox_NONCEBYTES: crypto_secretbox_NONCEBYTES,
5750
- crypto_secretbox_ZEROBYTES: crypto_secretbox_ZEROBYTES,
5751
- crypto_secretbox_BOXZEROBYTES: crypto_secretbox_BOXZEROBYTES,
5752
- crypto_scalarmult_BYTES: crypto_scalarmult_BYTES,
5753
- crypto_scalarmult_SCALARBYTES: crypto_scalarmult_SCALARBYTES,
5754
- crypto_box_PUBLICKEYBYTES: crypto_box_PUBLICKEYBYTES,
5755
- crypto_box_SECRETKEYBYTES: crypto_box_SECRETKEYBYTES,
5756
- crypto_box_BEFORENMBYTES: crypto_box_BEFORENMBYTES,
5757
- crypto_box_NONCEBYTES: crypto_box_NONCEBYTES,
5758
- crypto_box_ZEROBYTES: crypto_box_ZEROBYTES,
5759
- crypto_box_BOXZEROBYTES: crypto_box_BOXZEROBYTES,
5760
- crypto_sign_BYTES: crypto_sign_BYTES,
5761
- crypto_sign_PUBLICKEYBYTES: crypto_sign_PUBLICKEYBYTES,
5762
- crypto_sign_SECRETKEYBYTES: crypto_sign_SECRETKEYBYTES,
5763
- crypto_sign_SEEDBYTES: crypto_sign_SEEDBYTES,
5764
- crypto_hash_BYTES: crypto_hash_BYTES,
5765
-
5766
- gf: gf,
5767
- D: D,
5768
- L: L,
5769
- pack25519: pack25519,
5770
- unpack25519: unpack25519,
5771
- M: M,
5772
- A: A,
5773
- S: S,
5774
- Z: Z,
5775
- pow2523: pow2523,
5776
- add: add,
5777
- set25519: set25519,
5778
- modL: modL,
5779
- scalarmult: scalarmult,
5780
- scalarbase: scalarbase,
5781
- };
5782
-
5783
- /* High-level API */
5784
-
5785
- function checkLengths(k, n) {
5786
- if (k.length !== crypto_secretbox_KEYBYTES) throw new Error('bad key size');
5787
- if (n.length !== crypto_secretbox_NONCEBYTES) throw new Error('bad nonce size');
5788
- }
5789
-
5790
- function checkBoxLengths(pk, sk) {
5791
- if (pk.length !== crypto_box_PUBLICKEYBYTES) throw new Error('bad public key size');
5792
- if (sk.length !== crypto_box_SECRETKEYBYTES) throw new Error('bad secret key size');
5793
- }
5794
-
5795
- function checkArrayTypes() {
5796
- for (var i = 0; i < arguments.length; i++) {
5797
- if (!(arguments[i] instanceof Uint8Array))
5798
- throw new TypeError('unexpected type, use Uint8Array');
5799
- }
5800
- }
5801
-
5802
- function cleanup(arr) {
5803
- for (var i = 0; i < arr.length; i++) arr[i] = 0;
5804
- }
5805
-
5806
- nacl.randomBytes = function(n) {
5807
- var b = new Uint8Array(n);
5808
- randombytes(b, n);
5809
- return b;
5810
- };
5811
-
5812
- nacl.secretbox = function(msg, nonce, key) {
5813
- checkArrayTypes(msg, nonce, key);
5814
- checkLengths(key, nonce);
5815
- var m = new Uint8Array(crypto_secretbox_ZEROBYTES + msg.length);
5816
- var c = new Uint8Array(m.length);
5817
- for (var i = 0; i < msg.length; i++) m[i+crypto_secretbox_ZEROBYTES] = msg[i];
5818
- crypto_secretbox(c, m, m.length, nonce, key);
5819
- return c.subarray(crypto_secretbox_BOXZEROBYTES);
5820
- };
5821
-
5822
- nacl.secretbox.open = function(box, nonce, key) {
5823
- checkArrayTypes(box, nonce, key);
5824
- checkLengths(key, nonce);
5825
- var c = new Uint8Array(crypto_secretbox_BOXZEROBYTES + box.length);
5826
- var m = new Uint8Array(c.length);
5827
- for (var i = 0; i < box.length; i++) c[i+crypto_secretbox_BOXZEROBYTES] = box[i];
5828
- if (c.length < 32) return null;
5829
- if (crypto_secretbox_open(m, c, c.length, nonce, key) !== 0) return null;
5830
- return m.subarray(crypto_secretbox_ZEROBYTES);
5831
- };
5832
-
5833
- nacl.secretbox.keyLength = crypto_secretbox_KEYBYTES;
5834
- nacl.secretbox.nonceLength = crypto_secretbox_NONCEBYTES;
5835
- nacl.secretbox.overheadLength = crypto_secretbox_BOXZEROBYTES;
5836
-
5837
- nacl.scalarMult = function(n, p) {
5838
- checkArrayTypes(n, p);
5839
- if (n.length !== crypto_scalarmult_SCALARBYTES) throw new Error('bad n size');
5840
- if (p.length !== crypto_scalarmult_BYTES) throw new Error('bad p size');
5841
- var q = new Uint8Array(crypto_scalarmult_BYTES);
5842
- crypto_scalarmult(q, n, p);
5843
- return q;
5844
- };
5845
-
5846
- nacl.scalarMult.base = function(n) {
5847
- checkArrayTypes(n);
5848
- if (n.length !== crypto_scalarmult_SCALARBYTES) throw new Error('bad n size');
5849
- var q = new Uint8Array(crypto_scalarmult_BYTES);
5850
- crypto_scalarmult_base(q, n);
5851
- return q;
5852
- };
5853
-
5854
- nacl.scalarMult.scalarLength = crypto_scalarmult_SCALARBYTES;
5855
- nacl.scalarMult.groupElementLength = crypto_scalarmult_BYTES;
5856
-
5857
- nacl.box = function(msg, nonce, publicKey, secretKey) {
5858
- var k = nacl.box.before(publicKey, secretKey);
5859
- return nacl.secretbox(msg, nonce, k);
5860
- };
5861
-
5862
- nacl.box.before = function(publicKey, secretKey) {
5863
- checkArrayTypes(publicKey, secretKey);
5864
- checkBoxLengths(publicKey, secretKey);
5865
- var k = new Uint8Array(crypto_box_BEFORENMBYTES);
5866
- crypto_box_beforenm(k, publicKey, secretKey);
5867
- return k;
5868
- };
5869
-
5870
- nacl.box.after = nacl.secretbox;
5871
-
5872
- nacl.box.open = function(msg, nonce, publicKey, secretKey) {
5873
- var k = nacl.box.before(publicKey, secretKey);
5874
- return nacl.secretbox.open(msg, nonce, k);
5875
- };
5876
-
5877
- nacl.box.open.after = nacl.secretbox.open;
5878
-
5879
- nacl.box.keyPair = function() {
5880
- var pk = new Uint8Array(crypto_box_PUBLICKEYBYTES);
5881
- var sk = new Uint8Array(crypto_box_SECRETKEYBYTES);
5882
- crypto_box_keypair(pk, sk);
5883
- return {publicKey: pk, secretKey: sk};
5884
- };
5885
-
5886
- nacl.box.keyPair.fromSecretKey = function(secretKey) {
5887
- checkArrayTypes(secretKey);
5888
- if (secretKey.length !== crypto_box_SECRETKEYBYTES)
5889
- throw new Error('bad secret key size');
5890
- var pk = new Uint8Array(crypto_box_PUBLICKEYBYTES);
5891
- crypto_scalarmult_base(pk, secretKey);
5892
- return {publicKey: pk, secretKey: new Uint8Array(secretKey)};
5893
- };
5894
-
5895
- nacl.box.publicKeyLength = crypto_box_PUBLICKEYBYTES;
5896
- nacl.box.secretKeyLength = crypto_box_SECRETKEYBYTES;
5897
- nacl.box.sharedKeyLength = crypto_box_BEFORENMBYTES;
5898
- nacl.box.nonceLength = crypto_box_NONCEBYTES;
5899
- nacl.box.overheadLength = nacl.secretbox.overheadLength;
5900
-
5901
- nacl.sign = function(msg, secretKey) {
5902
- checkArrayTypes(msg, secretKey);
5903
- if (secretKey.length !== crypto_sign_SECRETKEYBYTES)
5904
- throw new Error('bad secret key size');
5905
- var signedMsg = new Uint8Array(crypto_sign_BYTES+msg.length);
5906
- crypto_sign(signedMsg, msg, msg.length, secretKey);
5907
- return signedMsg;
5908
- };
5909
-
5910
- nacl.sign.open = function(signedMsg, publicKey) {
5911
- checkArrayTypes(signedMsg, publicKey);
5912
- if (publicKey.length !== crypto_sign_PUBLICKEYBYTES)
5913
- throw new Error('bad public key size');
5914
- var tmp = new Uint8Array(signedMsg.length);
5915
- var mlen = crypto_sign_open(tmp, signedMsg, signedMsg.length, publicKey);
5916
- if (mlen < 0) return null;
5917
- var m = new Uint8Array(mlen);
5918
- for (var i = 0; i < m.length; i++) m[i] = tmp[i];
5919
- return m;
5920
- };
5921
-
5922
- nacl.sign.detached = function(msg, secretKey) {
5923
- var signedMsg = nacl.sign(msg, secretKey);
5924
- var sig = new Uint8Array(crypto_sign_BYTES);
5925
- for (var i = 0; i < sig.length; i++) sig[i] = signedMsg[i];
5926
- return sig;
5927
- };
5928
-
5929
- nacl.sign.detached.verify = function(msg, sig, publicKey) {
5930
- checkArrayTypes(msg, sig, publicKey);
5931
- if (sig.length !== crypto_sign_BYTES)
5932
- throw new Error('bad signature size');
5933
- if (publicKey.length !== crypto_sign_PUBLICKEYBYTES)
5934
- throw new Error('bad public key size');
5935
- var sm = new Uint8Array(crypto_sign_BYTES + msg.length);
5936
- var m = new Uint8Array(crypto_sign_BYTES + msg.length);
5937
- var i;
5938
- for (i = 0; i < crypto_sign_BYTES; i++) sm[i] = sig[i];
5939
- for (i = 0; i < msg.length; i++) sm[i+crypto_sign_BYTES] = msg[i];
5940
- return (crypto_sign_open(m, sm, sm.length, publicKey) >= 0);
5941
- };
5942
-
5943
- nacl.sign.keyPair = function() {
5944
- var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES);
5945
- var sk = new Uint8Array(crypto_sign_SECRETKEYBYTES);
5946
- crypto_sign_keypair(pk, sk);
5947
- return {publicKey: pk, secretKey: sk};
5948
- };
5949
-
5950
- nacl.sign.keyPair.fromSecretKey = function(secretKey) {
5951
- checkArrayTypes(secretKey);
5952
- if (secretKey.length !== crypto_sign_SECRETKEYBYTES)
5953
- throw new Error('bad secret key size');
5954
- var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES);
5955
- for (var i = 0; i < pk.length; i++) pk[i] = secretKey[32+i];
5956
- return {publicKey: pk, secretKey: new Uint8Array(secretKey)};
5957
- };
5958
-
5959
- nacl.sign.keyPair.fromSeed = function(seed) {
5960
- checkArrayTypes(seed);
5961
- if (seed.length !== crypto_sign_SEEDBYTES)
5962
- throw new Error('bad seed size');
5963
- var pk = new Uint8Array(crypto_sign_PUBLICKEYBYTES);
5964
- var sk = new Uint8Array(crypto_sign_SECRETKEYBYTES);
5965
- for (var i = 0; i < 32; i++) sk[i] = seed[i];
5966
- crypto_sign_keypair(pk, sk, true);
5967
- return {publicKey: pk, secretKey: sk};
5968
- };
5969
-
5970
- nacl.sign.publicKeyLength = crypto_sign_PUBLICKEYBYTES;
5971
- nacl.sign.secretKeyLength = crypto_sign_SECRETKEYBYTES;
5972
- nacl.sign.seedLength = crypto_sign_SEEDBYTES;
5973
- nacl.sign.signatureLength = crypto_sign_BYTES;
5974
-
5975
- nacl.hash = function(msg) {
5976
- checkArrayTypes(msg);
5977
- var h = new Uint8Array(crypto_hash_BYTES);
5978
- crypto_hash(h, msg, msg.length);
5979
- return h;
5980
- };
5981
-
5982
- nacl.hash.hashLength = crypto_hash_BYTES;
5983
-
5984
- nacl.verify = function(x, y) {
5985
- checkArrayTypes(x, y);
5986
- // Zero length arguments are considered not equal.
5987
- if (x.length === 0 || y.length === 0) return false;
5988
- if (x.length !== y.length) return false;
5989
- return (vn(x, 0, y, 0, x.length) === 0) ? true : false;
5990
- };
5991
-
5992
- nacl.setPRNG = function(fn) {
5993
- randombytes = fn;
5994
- };
5995
-
5996
- (function() {
5997
- // Initialize PRNG if environment provides CSPRNG.
5998
- // If not, methods calling randombytes will throw.
5999
- var crypto = typeof self !== 'undefined' ? (self.crypto || self.msCrypto) : null;
6000
- if (crypto && crypto.getRandomValues) {
6001
- // Browsers.
6002
- var QUOTA = 65536;
6003
- nacl.setPRNG(function(x, n) {
6004
- var i, v = new Uint8Array(n);
6005
- for (i = 0; i < n; i += QUOTA) {
6006
- crypto.getRandomValues(v.subarray(i, i + Math.min(n - i, QUOTA)));
6007
- }
6008
- for (i = 0; i < n; i++) x[i] = v[i];
6009
- cleanup(v);
6010
- });
6011
- } else if (typeof commonjsRequire !== 'undefined') {
6012
- // Node.js.
6013
- crypto = require$$0;
6014
- if (crypto && crypto.randomBytes) {
6015
- nacl.setPRNG(function(x, n) {
6016
- var i, v = crypto.randomBytes(n);
6017
- for (i = 0; i < n; i++) x[i] = v[i];
6018
- cleanup(v);
6019
- });
6020
- }
6021
- }
6022
- })();
6023
-
6024
- })(module.exports ? module.exports : (self.nacl = self.nacl || {}));
6025
- } (naclFast));
6026
- return naclFast.exports;
6027
- }
6028
-
6029
- var naclFastExports = requireNaclFast();
6030
- var nacl = /*@__PURE__*/getDefaultExportFromCjs(naclFastExports);
6031
-
6032
3452
  // guest-keystore.ts — durable per-device keypair for anonymous ("guest") auth.
6033
3453
  //
6034
3454
  // A guest is an ed25519 keypair generated on the device. Its public key is the
6035
3455
  // Bounded account address; it signs the auth challenge exactly like a wallet.
6036
3456
  //
6037
- // Two backends, chosen automatically:
6038
- // 1. WebCrypto Ed25519 + IndexedDB (browsers that support it) — the key is
6039
- // NON-EXTRACTABLE: an XSS can ask it to *sign* but can never read the
6040
- // private bytes. This is the secure default.
6041
- // 2. tweetnacl keypair in platform storage (React Native / older browsers) —
6042
- // the secret is stored as bytes. Simpler, but exfiltratable by XSS, so
6043
- // treat it as a low-stakes guest identity and tell users to upgrade
6044
- // (link an email) before they care about not losing it.
3457
+ // Guest auth has one backend: WebCrypto Ed25519 + IndexedDB. The key is
3458
+ // NON-EXTRACTABLE: an XSS can ask it to sign but can never read the private
3459
+ // bytes. If WebCrypto or IndexedDB is unavailable, guest auth fails closed
3460
+ // instead of silently creating an extractable/stored-secret credential.
6045
3461
  //
6046
- // Either way the public output is identical: a base58 address + a base64
3462
+ // The public output is a base58 address + a base64
6047
3463
  // detached signature, which is the format the session endpoint already accepts.
6048
- const FALLBACK_STORE_KEY = 'bounded_guest_key_v1';
3464
+ const LEGACY_STORED_SECRET_KEY = 'bounded_guest_key_v1';
6049
3465
  const IDB_NAME = 'bounded-guest';
6050
3466
  const IDB_STORE = 'keys';
6051
3467
  const IDB_KEY = 'ed25519';
3468
+ const GUEST_KEY_REQUIREMENT = 'Guest auth requires non-extractable WebCrypto Ed25519 keys persisted in IndexedDB; ' +
3469
+ 'stored-secret guest credentials are disabled.';
6052
3470
  function toBase64(bytes) {
6053
3471
  let bin = '';
6054
3472
  for (let i = 0; i < bytes.length; i++)
@@ -6083,10 +3501,17 @@ function idbPut(db, key, val) {
6083
3501
  r.onerror = () => reject(r.error);
6084
3502
  });
6085
3503
  }
6086
- async function tryWebCrypto() {
3504
+ function guestKeyUnavailable(cause) {
3505
+ const detail = cause instanceof Error && cause.message ? ` Cause: ${cause.message}` : '';
3506
+ return new Error(`${GUEST_KEY_REQUIREMENT}${detail}`);
3507
+ }
3508
+ async function webCryptoKey() {
6087
3509
  const s = subtle();
6088
- if (!s || typeof globalThis.indexedDB === 'undefined')
6089
- return null;
3510
+ if (!s)
3511
+ throw guestKeyUnavailable(new Error('WebCrypto subtle crypto is unavailable.'));
3512
+ if (typeof globalThis.indexedDB === 'undefined') {
3513
+ throw guestKeyUnavailable(new Error('IndexedDB is unavailable.'));
3514
+ }
6090
3515
  try {
6091
3516
  const db = await idb();
6092
3517
  let pair = (await idbGet(db, IDB_KEY));
@@ -6110,49 +3535,23 @@ async function tryWebCrypto() {
6110
3535
  },
6111
3536
  };
6112
3537
  }
6113
- catch (_a) {
6114
- return null; // engine lacks Ed25519 / IndexedDB blocked → fall back
3538
+ catch (error) {
3539
+ throw guestKeyUnavailable(error);
6115
3540
  }
6116
3541
  }
6117
- // ----- tweetnacl (stored-secret) fallback ------------------------------------
6118
- function fallbackKey() {
6119
- const store = getPlatform().storage;
6120
- let secret;
6121
- const saved = store.getItem(FALLBACK_STORE_KEY);
6122
- if (saved) {
6123
- secret = bs58.decode(saved);
6124
- }
6125
- else {
6126
- const kp = web3_js.Keypair.generate();
6127
- secret = kp.secretKey; // 64 bytes
6128
- store.setItem(FALLBACK_STORE_KEY, bs58.encode(secret));
6129
- }
6130
- const kp = web3_js.Keypair.fromSecretKey(secret);
6131
- const address = kp.publicKey.toBase58();
6132
- return {
6133
- address,
6134
- nonExtractable: false,
6135
- async sign(message) {
6136
- const data = new TextEncoder().encode(message);
6137
- const sig = nacl.sign.detached(data, secret);
6138
- return toBase64(sig);
6139
- },
6140
- };
6141
- }
6142
3542
  let cached = null;
6143
3543
  /** Get (or create) this device's durable guest key. */
6144
3544
  async function getGuestKey() {
6145
- var _a;
6146
3545
  if (cached)
6147
3546
  return cached;
6148
- cached = (_a = (await tryWebCrypto())) !== null && _a !== void 0 ? _a : fallbackKey();
3547
+ cached = await webCryptoKey();
6149
3548
  return cached;
6150
3549
  }
6151
3550
  /** Forget the guest key on this device (a fresh signInAnonymously mints a new one). */
6152
3551
  async function clearGuestKey() {
6153
3552
  cached = null;
6154
3553
  try {
6155
- getPlatform().storage.removeItem(FALLBACK_STORE_KEY);
3554
+ getPlatform().storage.removeItem(LEGACY_STORED_SECRET_KEY);
6156
3555
  }
6157
3556
  catch (_a) { }
6158
3557
  try {
@@ -7189,12 +4588,6 @@ async function getAuthProvider(config) {
7189
4588
  throw new Error("Config is required to initialize auth provider");
7190
4589
  }
7191
4590
  initConfig = config;
7192
- // If mockAuth is enabled, use MockAuthProvider regardless of authMethod
7193
- if (config.mockAuth) {
7194
- console.log("[MockAuth] Mock authentication enabled");
7195
- currentAuthProvider = new MockAuthProvider();
7196
- return currentAuthProvider;
7197
- }
7198
4591
  const authMethod = config.authMethod;
7199
4592
  if (!authMethod) {
7200
4593
  throw new Error("authMethod is required to initialize auth provider; configure the single intended auth method explicitly.");
@@ -7212,8 +4605,8 @@ async function getAuthProvider(config) {
7212
4605
  if (typeof window === 'undefined' && browserWalletMethods.includes(authMethod)) {
7213
4606
  throw new Error(`bounded-sh: '${authMethod}' auth requires a browser, but no \`window\` was found ` +
7214
4607
  `(you're likely running in Node). In server/Node code, import from 'bounded-sh/server' ` +
7215
- `instead of 'bounded-sh' — it signs with a keypair (pass it via createWalletClient({ keypair }), ` +
7216
- `or set BOUNDED_PRIVATE_KEY) and needs no browser.`);
4608
+ `instead of 'bounded-sh' — it signs with an explicit keypair passed to createWalletClient({ keypair }) ` +
4609
+ `and needs no browser.`);
7217
4610
  }
7218
4611
  switch (authMethod) {
7219
4612
  case "email":
@@ -7290,7 +4683,7 @@ function assertBrowserAuthEnv(method) {
7290
4683
  throw new Error(`bounded-sh: '${method}' is a browser/React-Native auth flow — it needs \`window\`/\`localStorage\` ` +
7291
4684
  `to persist the session, which Node doesn't have (the session would be silently dropped, then every ` +
7292
4685
  `request 403s). In Node/server code import from 'bounded-sh/server' and sign with a keypair ` +
7293
- `(createWalletClient({ keypair }) or BOUNDED_PRIVATE_KEY).`);
4686
+ `by using createWalletClient({ keypair }).`);
7294
4687
  }
7295
4688
  }
7296
4689
  /**
@@ -12157,8 +9550,6 @@ Object.defineProperty(exports, "subscribe", {
12157
9550
  enumerable: true,
12158
9551
  get: function () { return core.subscribe; }
12159
9552
  });
12160
- exports.DEFAULT_TEST_ADDRESS = DEFAULT_TEST_ADDRESS;
12161
- exports.MockAuthProvider = MockAuthProvider;
12162
9553
  exports.OffchainAuthProvider = OffchainAuthProvider;
12163
9554
  exports.PhantomWalletProvider = PhantomWalletProvider;
12164
9555
  exports.PrivyExpoProvider = PrivyExpoProvider;