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