ed25519.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  1. /**
  2. * JavaScript implementation of Ed25519.
  3. *
  4. * Copyright (c) 2017-2018 Digital Bazaar, Inc.
  5. *
  6. * This implementation is based on the most excellent TweetNaCl which is
  7. * in the public domain. Many thanks to its contributors:
  8. *
  9. * https://github.com/dchest/tweetnacl-js
  10. */
  11. var forge = require('./forge');
  12. require('./jsbn');
  13. require('./random');
  14. require('./sha512');
  15. require('./util');
  16. if(typeof BigInteger === 'undefined') {
  17. var BigInteger = forge.jsbn.BigInteger;
  18. }
  19. var ByteBuffer = forge.util.ByteBuffer;
  20. var NativeBuffer = typeof Buffer === 'undefined' ? Uint8Array : Buffer;
  21. /*
  22. * Ed25519 algorithms, see RFC 8032:
  23. * https://tools.ietf.org/html/rfc8032
  24. */
  25. forge.pki = forge.pki || {};
  26. module.exports = forge.pki.ed25519 = forge.ed25519 = forge.ed25519 || {};
  27. var ed25519 = forge.ed25519;
  28. ed25519.constants = {};
  29. ed25519.constants.PUBLIC_KEY_BYTE_LENGTH = 32;
  30. ed25519.constants.PRIVATE_KEY_BYTE_LENGTH = 64;
  31. ed25519.constants.SEED_BYTE_LENGTH = 32;
  32. ed25519.constants.SIGN_BYTE_LENGTH = 64;
  33. ed25519.constants.HASH_BYTE_LENGTH = 64;
  34. ed25519.generateKeyPair = function(options) {
  35. options = options || {};
  36. var seed = options.seed;
  37. if(seed === undefined) {
  38. // generate seed
  39. seed = forge.random.getBytesSync(ed25519.constants.SEED_BYTE_LENGTH);
  40. } else if(typeof seed === 'string') {
  41. if(seed.length !== ed25519.constants.SEED_BYTE_LENGTH) {
  42. throw new TypeError(
  43. '"seed" must be ' + ed25519.constants.SEED_BYTE_LENGTH +
  44. ' bytes in length.');
  45. }
  46. } else if(!(seed instanceof Uint8Array)) {
  47. throw new TypeError(
  48. '"seed" must be a node.js Buffer, Uint8Array, or a binary string.');
  49. }
  50. seed = messageToNativeBuffer({message: seed, encoding: 'binary'});
  51. var pk = new NativeBuffer(ed25519.constants.PUBLIC_KEY_BYTE_LENGTH);
  52. var sk = new NativeBuffer(ed25519.constants.PRIVATE_KEY_BYTE_LENGTH);
  53. for(var i = 0; i < 32; ++i) {
  54. sk[i] = seed[i];
  55. }
  56. crypto_sign_keypair(pk, sk);
  57. return {publicKey: pk, privateKey: sk};
  58. };
  59. ed25519.publicKeyFromPrivateKey = function(options) {
  60. options = options || {};
  61. var privateKey = messageToNativeBuffer({
  62. message: options.privateKey, encoding: 'binary'
  63. });
  64. if(privateKey.length !== ed25519.constants.PRIVATE_KEY_BYTE_LENGTH) {
  65. throw new TypeError(
  66. '"options.privateKey" must have a byte length of ' +
  67. ed25519.constants.PRIVATE_KEY_BYTE_LENGTH);
  68. }
  69. var pk = new NativeBuffer(ed25519.constants.PUBLIC_KEY_BYTE_LENGTH);
  70. for(var i = 0; i < pk.length; ++i) {
  71. pk[i] = privateKey[32 + i];
  72. }
  73. return pk;
  74. };
  75. ed25519.sign = function(options) {
  76. options = options || {};
  77. var msg = messageToNativeBuffer(options);
  78. var privateKey = messageToNativeBuffer({
  79. message: options.privateKey,
  80. encoding: 'binary'
  81. });
  82. if(privateKey.length !== ed25519.constants.PRIVATE_KEY_BYTE_LENGTH) {
  83. throw new TypeError(
  84. '"options.privateKey" must have a byte length of ' +
  85. ed25519.constants.PRIVATE_KEY_BYTE_LENGTH);
  86. }
  87. var signedMsg = new NativeBuffer(
  88. ed25519.constants.SIGN_BYTE_LENGTH + msg.length);
  89. crypto_sign(signedMsg, msg, msg.length, privateKey);
  90. var sig = new NativeBuffer(ed25519.constants.SIGN_BYTE_LENGTH);
  91. for(var i = 0; i < sig.length; ++i) {
  92. sig[i] = signedMsg[i];
  93. }
  94. return sig;
  95. };
  96. ed25519.verify = function(options) {
  97. options = options || {};
  98. var msg = messageToNativeBuffer(options);
  99. if(options.signature === undefined) {
  100. throw new TypeError(
  101. '"options.signature" must be a node.js Buffer, a Uint8Array, a forge ' +
  102. 'ByteBuffer, or a binary string.');
  103. }
  104. var sig = messageToNativeBuffer({
  105. message: options.signature,
  106. encoding: 'binary'
  107. });
  108. if(sig.length !== ed25519.constants.SIGN_BYTE_LENGTH) {
  109. throw new TypeError(
  110. '"options.signature" must have a byte length of ' +
  111. ed25519.constants.SIGN_BYTE_LENGTH);
  112. }
  113. var publicKey = messageToNativeBuffer({
  114. message: options.publicKey,
  115. encoding: 'binary'
  116. });
  117. if(publicKey.length !== ed25519.constants.PUBLIC_KEY_BYTE_LENGTH) {
  118. throw new TypeError(
  119. '"options.publicKey" must have a byte length of ' +
  120. ed25519.constants.PUBLIC_KEY_BYTE_LENGTH);
  121. }
  122. var sm = new NativeBuffer(ed25519.constants.SIGN_BYTE_LENGTH + msg.length);
  123. var m = new NativeBuffer(ed25519.constants.SIGN_BYTE_LENGTH + msg.length);
  124. var i;
  125. for(i = 0; i < ed25519.constants.SIGN_BYTE_LENGTH; ++i) {
  126. sm[i] = sig[i];
  127. }
  128. for(i = 0; i < msg.length; ++i) {
  129. sm[i + ed25519.constants.SIGN_BYTE_LENGTH] = msg[i];
  130. }
  131. return (crypto_sign_open(m, sm, sm.length, publicKey) >= 0);
  132. };
  133. function messageToNativeBuffer(options) {
  134. var message = options.message;
  135. if(message instanceof Uint8Array) {
  136. return message;
  137. }
  138. var encoding = options.encoding;
  139. if(message === undefined) {
  140. if(options.md) {
  141. // TODO: more rigorous validation that `md` is a MessageDigest
  142. message = options.md.digest().getBytes();
  143. encoding = 'binary';
  144. } else {
  145. throw new TypeError('"options.message" or "options.md" not specified.');
  146. }
  147. }
  148. if(typeof message === 'string' && !encoding) {
  149. throw new TypeError('"options.encoding" must be "binary" or "utf8".');
  150. }
  151. if(typeof message === 'string') {
  152. if(typeof Buffer !== 'undefined') {
  153. return new Buffer(message, encoding);
  154. }
  155. message = new ByteBuffer(message, encoding);
  156. } else if(!(message instanceof ByteBuffer)) {
  157. throw new TypeError(
  158. '"options.message" must be a node.js Buffer, a Uint8Array, a forge ' +
  159. 'ByteBuffer, or a string with "options.encoding" specifying its ' +
  160. 'encoding.');
  161. }
  162. // convert to native buffer
  163. var buffer = new NativeBuffer(message.length());
  164. for(var i = 0; i < buffer.length; ++i) {
  165. buffer[i] = message.at(i);
  166. }
  167. return buffer;
  168. }
  169. var gf0 = gf();
  170. var gf1 = gf([1]);
  171. var D = gf([
  172. 0x78a3, 0x1359, 0x4dca, 0x75eb, 0xd8ab, 0x4141, 0x0a4d, 0x0070,
  173. 0xe898, 0x7779, 0x4079, 0x8cc7, 0xfe73, 0x2b6f, 0x6cee, 0x5203]);
  174. var D2 = gf([
  175. 0xf159, 0x26b2, 0x9b94, 0xebd6, 0xb156, 0x8283, 0x149a, 0x00e0,
  176. 0xd130, 0xeef3, 0x80f2, 0x198e, 0xfce7, 0x56df, 0xd9dc, 0x2406]);
  177. var X = gf([
  178. 0xd51a, 0x8f25, 0x2d60, 0xc956, 0xa7b2, 0x9525, 0xc760, 0x692c,
  179. 0xdc5c, 0xfdd6, 0xe231, 0xc0a4, 0x53fe, 0xcd6e, 0x36d3, 0x2169]);
  180. var Y = gf([
  181. 0x6658, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666,
  182. 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666]);
  183. var L = new Float64Array([
  184. 0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58,
  185. 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14,
  186. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x10]);
  187. var I = gf([
  188. 0xa0b0, 0x4a0e, 0x1b27, 0xc4ee, 0xe478, 0xad2f, 0x1806, 0x2f43,
  189. 0xd7a7, 0x3dfb, 0x0099, 0x2b4d, 0xdf0b, 0x4fc1, 0x2480, 0x2b83]);
  190. // TODO: update forge buffer implementation to use `Buffer` or `Uint8Array`,
  191. // whichever is available, to improve performance
  192. function sha512(msg, msgLen) {
  193. // Note: `out` and `msg` are NativeBuffer
  194. var md = forge.md.sha512.create();
  195. var buffer = new ByteBuffer(msg);
  196. md.update(buffer.getBytes(msgLen), 'binary');
  197. var hash = md.digest().getBytes();
  198. if(typeof Buffer !== 'undefined') {
  199. return new Buffer(hash, 'binary');
  200. }
  201. var out = new NativeBuffer(ed25519.constants.HASH_BYTE_LENGTH);
  202. for(var i = 0; i < 64; ++i) {
  203. out[i] = hash.charCodeAt(i);
  204. }
  205. return out;
  206. }
  207. function crypto_sign_keypair(pk, sk) {
  208. var p = [gf(), gf(), gf(), gf()];
  209. var i;
  210. var d = sha512(sk, 32);
  211. d[0] &= 248;
  212. d[31] &= 127;
  213. d[31] |= 64;
  214. scalarbase(p, d);
  215. pack(pk, p);
  216. for(i = 0; i < 32; ++i) {
  217. sk[i + 32] = pk[i];
  218. }
  219. return 0;
  220. }
  221. // Note: difference from C - smlen returned, not passed as argument.
  222. function crypto_sign(sm, m, n, sk) {
  223. var i, j, x = new Float64Array(64);
  224. var p = [gf(), gf(), gf(), gf()];
  225. var d = sha512(sk, 32);
  226. d[0] &= 248;
  227. d[31] &= 127;
  228. d[31] |= 64;
  229. var smlen = n + 64;
  230. for(i = 0; i < n; ++i) {
  231. sm[64 + i] = m[i];
  232. }
  233. for(i = 0; i < 32; ++i) {
  234. sm[32 + i] = d[32 + i];
  235. }
  236. var r = sha512(sm.subarray(32), n + 32);
  237. reduce(r);
  238. scalarbase(p, r);
  239. pack(sm, p);
  240. for(i = 32; i < 64; ++i) {
  241. sm[i] = sk[i];
  242. }
  243. var h = sha512(sm, n + 64);
  244. reduce(h);
  245. for(i = 32; i < 64; ++i) {
  246. x[i] = 0;
  247. }
  248. for(i = 0; i < 32; ++i) {
  249. x[i] = r[i];
  250. }
  251. for(i = 0; i < 32; ++i) {
  252. for(j = 0; j < 32; j++) {
  253. x[i + j] += h[i] * d[j];
  254. }
  255. }
  256. modL(sm.subarray(32), x);
  257. return smlen;
  258. }
  259. function crypto_sign_open(m, sm, n, pk) {
  260. var i, mlen;
  261. var t = new NativeBuffer(32);
  262. var p = [gf(), gf(), gf(), gf()],
  263. q = [gf(), gf(), gf(), gf()];
  264. mlen = -1;
  265. if(n < 64) {
  266. return -1;
  267. }
  268. if(unpackneg(q, pk)) {
  269. return -1;
  270. }
  271. for(i = 0; i < n; ++i) {
  272. m[i] = sm[i];
  273. }
  274. for(i = 0; i < 32; ++i) {
  275. m[i + 32] = pk[i];
  276. }
  277. var h = sha512(m, n);
  278. reduce(h);
  279. scalarmult(p, q, h);
  280. scalarbase(q, sm.subarray(32));
  281. add(p, q);
  282. pack(t, p);
  283. n -= 64;
  284. if(crypto_verify_32(sm, 0, t, 0)) {
  285. for(i = 0; i < n; ++i) {
  286. m[i] = 0;
  287. }
  288. return -1;
  289. }
  290. for(i = 0; i < n; ++i) {
  291. m[i] = sm[i + 64];
  292. }
  293. mlen = n;
  294. return mlen;
  295. }
  296. function modL(r, x) {
  297. var carry, i, j, k;
  298. for(i = 63; i >= 32; --i) {
  299. carry = 0;
  300. for(j = i - 32, k = i - 12; j < k; ++j) {
  301. x[j] += carry - 16 * x[i] * L[j - (i - 32)];
  302. carry = (x[j] + 128) >> 8;
  303. x[j] -= carry * 256;
  304. }
  305. x[j] += carry;
  306. x[i] = 0;
  307. }
  308. carry = 0;
  309. for(j = 0; j < 32; ++j) {
  310. x[j] += carry - (x[31] >> 4) * L[j];
  311. carry = x[j] >> 8;
  312. x[j] &= 255;
  313. }
  314. for(j = 0; j < 32; ++j) {
  315. x[j] -= carry * L[j];
  316. }
  317. for(i = 0; i < 32; ++i) {
  318. x[i + 1] += x[i] >> 8;
  319. r[i] = x[i] & 255;
  320. }
  321. }
  322. function reduce(r) {
  323. var x = new Float64Array(64);
  324. for(var i = 0; i < 64; ++i) {
  325. x[i] = r[i];
  326. r[i] = 0;
  327. }
  328. modL(r, x);
  329. }
  330. function add(p, q) {
  331. var a = gf(), b = gf(), c = gf(),
  332. d = gf(), e = gf(), f = gf(),
  333. g = gf(), h = gf(), t = gf();
  334. Z(a, p[1], p[0]);
  335. Z(t, q[1], q[0]);
  336. M(a, a, t);
  337. A(b, p[0], p[1]);
  338. A(t, q[0], q[1]);
  339. M(b, b, t);
  340. M(c, p[3], q[3]);
  341. M(c, c, D2);
  342. M(d, p[2], q[2]);
  343. A(d, d, d);
  344. Z(e, b, a);
  345. Z(f, d, c);
  346. A(g, d, c);
  347. A(h, b, a);
  348. M(p[0], e, f);
  349. M(p[1], h, g);
  350. M(p[2], g, f);
  351. M(p[3], e, h);
  352. }
  353. function cswap(p, q, b) {
  354. for(var i = 0; i < 4; ++i) {
  355. sel25519(p[i], q[i], b);
  356. }
  357. }
  358. function pack(r, p) {
  359. var tx = gf(), ty = gf(), zi = gf();
  360. inv25519(zi, p[2]);
  361. M(tx, p[0], zi);
  362. M(ty, p[1], zi);
  363. pack25519(r, ty);
  364. r[31] ^= par25519(tx) << 7;
  365. }
  366. function pack25519(o, n) {
  367. var i, j, b;
  368. var m = gf(), t = gf();
  369. for(i = 0; i < 16; ++i) {
  370. t[i] = n[i];
  371. }
  372. car25519(t);
  373. car25519(t);
  374. car25519(t);
  375. for(j = 0; j < 2; ++j) {
  376. m[0] = t[0] - 0xffed;
  377. for(i = 1; i < 15; ++i) {
  378. m[i] = t[i] - 0xffff - ((m[i - 1] >> 16) & 1);
  379. m[i-1] &= 0xffff;
  380. }
  381. m[15] = t[15] - 0x7fff - ((m[14] >> 16) & 1);
  382. b = (m[15] >> 16) & 1;
  383. m[14] &= 0xffff;
  384. sel25519(t, m, 1 - b);
  385. }
  386. for (i = 0; i < 16; i++) {
  387. o[2 * i] = t[i] & 0xff;
  388. o[2 * i + 1] = t[i] >> 8;
  389. }
  390. }
  391. function unpackneg(r, p) {
  392. var t = gf(), chk = gf(), num = gf(),
  393. den = gf(), den2 = gf(), den4 = gf(),
  394. den6 = gf();
  395. set25519(r[2], gf1);
  396. unpack25519(r[1], p);
  397. S(num, r[1]);
  398. M(den, num, D);
  399. Z(num, num, r[2]);
  400. A(den, r[2], den);
  401. S(den2, den);
  402. S(den4, den2);
  403. M(den6, den4, den2);
  404. M(t, den6, num);
  405. M(t, t, den);
  406. pow2523(t, t);
  407. M(t, t, num);
  408. M(t, t, den);
  409. M(t, t, den);
  410. M(r[0], t, den);
  411. S(chk, r[0]);
  412. M(chk, chk, den);
  413. if(neq25519(chk, num)) {
  414. M(r[0], r[0], I);
  415. }
  416. S(chk, r[0]);
  417. M(chk, chk, den);
  418. if(neq25519(chk, num)) {
  419. return -1;
  420. }
  421. if(par25519(r[0]) === (p[31] >> 7)) {
  422. Z(r[0], gf0, r[0]);
  423. }
  424. M(r[3], r[0], r[1]);
  425. return 0;
  426. }
  427. function unpack25519(o, n) {
  428. var i;
  429. for(i = 0; i < 16; ++i) {
  430. o[i] = n[2 * i] + (n[2 * i + 1] << 8);
  431. }
  432. o[15] &= 0x7fff;
  433. }
  434. function pow2523(o, i) {
  435. var c = gf();
  436. var a;
  437. for(a = 0; a < 16; ++a) {
  438. c[a] = i[a];
  439. }
  440. for(a = 250; a >= 0; --a) {
  441. S(c, c);
  442. if(a !== 1) {
  443. M(c, c, i);
  444. }
  445. }
  446. for(a = 0; a < 16; ++a) {
  447. o[a] = c[a];
  448. }
  449. }
  450. function neq25519(a, b) {
  451. var c = new NativeBuffer(32);
  452. var d = new NativeBuffer(32);
  453. pack25519(c, a);
  454. pack25519(d, b);
  455. return crypto_verify_32(c, 0, d, 0);
  456. }
  457. function crypto_verify_32(x, xi, y, yi) {
  458. return vn(x, xi, y, yi, 32);
  459. }
  460. function vn(x, xi, y, yi, n) {
  461. var i, d = 0;
  462. for(i = 0; i < n; ++i) {
  463. d |= x[xi + i] ^ y[yi + i];
  464. }
  465. return (1 & ((d - 1) >>> 8)) - 1;
  466. }
  467. function par25519(a) {
  468. var d = new NativeBuffer(32);
  469. pack25519(d, a);
  470. return d[0] & 1;
  471. }
  472. function scalarmult(p, q, s) {
  473. var b, i;
  474. set25519(p[0], gf0);
  475. set25519(p[1], gf1);
  476. set25519(p[2], gf1);
  477. set25519(p[3], gf0);
  478. for(i = 255; i >= 0; --i) {
  479. b = (s[(i / 8)|0] >> (i & 7)) & 1;
  480. cswap(p, q, b);
  481. add(q, p);
  482. add(p, p);
  483. cswap(p, q, b);
  484. }
  485. }
  486. function scalarbase(p, s) {
  487. var q = [gf(), gf(), gf(), gf()];
  488. set25519(q[0], X);
  489. set25519(q[1], Y);
  490. set25519(q[2], gf1);
  491. M(q[3], X, Y);
  492. scalarmult(p, q, s);
  493. }
  494. function set25519(r, a) {
  495. var i;
  496. for(i = 0; i < 16; i++) {
  497. r[i] = a[i] | 0;
  498. }
  499. }
  500. function inv25519(o, i) {
  501. var c = gf();
  502. var a;
  503. for(a = 0; a < 16; ++a) {
  504. c[a] = i[a];
  505. }
  506. for(a = 253; a >= 0; --a) {
  507. S(c, c);
  508. if(a !== 2 && a !== 4) {
  509. M(c, c, i);
  510. }
  511. }
  512. for(a = 0; a < 16; ++a) {
  513. o[a] = c[a];
  514. }
  515. }
  516. function car25519(o) {
  517. var i, v, c = 1;
  518. for(i = 0; i < 16; ++i) {
  519. v = o[i] + c + 65535;
  520. c = Math.floor(v / 65536);
  521. o[i] = v - c * 65536;
  522. }
  523. o[0] += c - 1 + 37 * (c - 1);
  524. }
  525. function sel25519(p, q, b) {
  526. var t, c = ~(b - 1);
  527. for(var i = 0; i < 16; ++i) {
  528. t = c & (p[i] ^ q[i]);
  529. p[i] ^= t;
  530. q[i] ^= t;
  531. }
  532. }
  533. function gf(init) {
  534. var i, r = new Float64Array(16);
  535. if(init) {
  536. for(i = 0; i < init.length; ++i) {
  537. r[i] = init[i];
  538. }
  539. }
  540. return r;
  541. }
  542. function A(o, a, b) {
  543. for(var i = 0; i < 16; ++i) {
  544. o[i] = a[i] + b[i];
  545. }
  546. }
  547. function Z(o, a, b) {
  548. for(var i = 0; i < 16; ++i) {
  549. o[i] = a[i] - b[i];
  550. }
  551. }
  552. function S(o, a) {
  553. M(o, a, a);
  554. }
  555. function M(o, a, b) {
  556. var v, c,
  557. t0 = 0, t1 = 0, t2 = 0, t3 = 0, t4 = 0, t5 = 0, t6 = 0, t7 = 0,
  558. t8 = 0, t9 = 0, t10 = 0, t11 = 0, t12 = 0, t13 = 0, t14 = 0, t15 = 0,
  559. t16 = 0, t17 = 0, t18 = 0, t19 = 0, t20 = 0, t21 = 0, t22 = 0, t23 = 0,
  560. t24 = 0, t25 = 0, t26 = 0, t27 = 0, t28 = 0, t29 = 0, t30 = 0,
  561. b0 = b[0],
  562. b1 = b[1],
  563. b2 = b[2],
  564. b3 = b[3],
  565. b4 = b[4],
  566. b5 = b[5],
  567. b6 = b[6],
  568. b7 = b[7],
  569. b8 = b[8],
  570. b9 = b[9],
  571. b10 = b[10],
  572. b11 = b[11],
  573. b12 = b[12],
  574. b13 = b[13],
  575. b14 = b[14],
  576. b15 = b[15];
  577. v = a[0];
  578. t0 += v * b0;
  579. t1 += v * b1;
  580. t2 += v * b2;
  581. t3 += v * b3;
  582. t4 += v * b4;
  583. t5 += v * b5;
  584. t6 += v * b6;
  585. t7 += v * b7;
  586. t8 += v * b8;
  587. t9 += v * b9;
  588. t10 += v * b10;
  589. t11 += v * b11;
  590. t12 += v * b12;
  591. t13 += v * b13;
  592. t14 += v * b14;
  593. t15 += v * b15;
  594. v = a[1];
  595. t1 += v * b0;
  596. t2 += v * b1;
  597. t3 += v * b2;
  598. t4 += v * b3;
  599. t5 += v * b4;
  600. t6 += v * b5;
  601. t7 += v * b6;
  602. t8 += v * b7;
  603. t9 += v * b8;
  604. t10 += v * b9;
  605. t11 += v * b10;
  606. t12 += v * b11;
  607. t13 += v * b12;
  608. t14 += v * b13;
  609. t15 += v * b14;
  610. t16 += v * b15;
  611. v = a[2];
  612. t2 += v * b0;
  613. t3 += v * b1;
  614. t4 += v * b2;
  615. t5 += v * b3;
  616. t6 += v * b4;
  617. t7 += v * b5;
  618. t8 += v * b6;
  619. t9 += v * b7;
  620. t10 += v * b8;
  621. t11 += v * b9;
  622. t12 += v * b10;
  623. t13 += v * b11;
  624. t14 += v * b12;
  625. t15 += v * b13;
  626. t16 += v * b14;
  627. t17 += v * b15;
  628. v = a[3];
  629. t3 += v * b0;
  630. t4 += v * b1;
  631. t5 += v * b2;
  632. t6 += v * b3;
  633. t7 += v * b4;
  634. t8 += v * b5;
  635. t9 += v * b6;
  636. t10 += v * b7;
  637. t11 += v * b8;
  638. t12 += v * b9;
  639. t13 += v * b10;
  640. t14 += v * b11;
  641. t15 += v * b12;
  642. t16 += v * b13;
  643. t17 += v * b14;
  644. t18 += v * b15;
  645. v = a[4];
  646. t4 += v * b0;
  647. t5 += v * b1;
  648. t6 += v * b2;
  649. t7 += v * b3;
  650. t8 += v * b4;
  651. t9 += v * b5;
  652. t10 += v * b6;
  653. t11 += v * b7;
  654. t12 += v * b8;
  655. t13 += v * b9;
  656. t14 += v * b10;
  657. t15 += v * b11;
  658. t16 += v * b12;
  659. t17 += v * b13;
  660. t18 += v * b14;
  661. t19 += v * b15;
  662. v = a[5];
  663. t5 += v * b0;
  664. t6 += v * b1;
  665. t7 += v * b2;
  666. t8 += v * b3;
  667. t9 += v * b4;
  668. t10 += v * b5;
  669. t11 += v * b6;
  670. t12 += v * b7;
  671. t13 += v * b8;
  672. t14 += v * b9;
  673. t15 += v * b10;
  674. t16 += v * b11;
  675. t17 += v * b12;
  676. t18 += v * b13;
  677. t19 += v * b14;
  678. t20 += v * b15;
  679. v = a[6];
  680. t6 += v * b0;
  681. t7 += v * b1;
  682. t8 += v * b2;
  683. t9 += v * b3;
  684. t10 += v * b4;
  685. t11 += v * b5;
  686. t12 += v * b6;
  687. t13 += v * b7;
  688. t14 += v * b8;
  689. t15 += v * b9;
  690. t16 += v * b10;
  691. t17 += v * b11;
  692. t18 += v * b12;
  693. t19 += v * b13;
  694. t20 += v * b14;
  695. t21 += v * b15;
  696. v = a[7];
  697. t7 += v * b0;
  698. t8 += v * b1;
  699. t9 += v * b2;
  700. t10 += v * b3;
  701. t11 += v * b4;
  702. t12 += v * b5;
  703. t13 += v * b6;
  704. t14 += v * b7;
  705. t15 += v * b8;
  706. t16 += v * b9;
  707. t17 += v * b10;
  708. t18 += v * b11;
  709. t19 += v * b12;
  710. t20 += v * b13;
  711. t21 += v * b14;
  712. t22 += v * b15;
  713. v = a[8];
  714. t8 += v * b0;
  715. t9 += v * b1;
  716. t10 += v * b2;
  717. t11 += v * b3;
  718. t12 += v * b4;
  719. t13 += v * b5;
  720. t14 += v * b6;
  721. t15 += v * b7;
  722. t16 += v * b8;
  723. t17 += v * b9;
  724. t18 += v * b10;
  725. t19 += v * b11;
  726. t20 += v * b12;
  727. t21 += v * b13;
  728. t22 += v * b14;
  729. t23 += v * b15;
  730. v = a[9];
  731. t9 += v * b0;
  732. t10 += v * b1;
  733. t11 += v * b2;
  734. t12 += v * b3;
  735. t13 += v * b4;
  736. t14 += v * b5;
  737. t15 += v * b6;
  738. t16 += v * b7;
  739. t17 += v * b8;
  740. t18 += v * b9;
  741. t19 += v * b10;
  742. t20 += v * b11;
  743. t21 += v * b12;
  744. t22 += v * b13;
  745. t23 += v * b14;
  746. t24 += v * b15;
  747. v = a[10];
  748. t10 += v * b0;
  749. t11 += v * b1;
  750. t12 += v * b2;
  751. t13 += v * b3;
  752. t14 += v * b4;
  753. t15 += v * b5;
  754. t16 += v * b6;
  755. t17 += v * b7;
  756. t18 += v * b8;
  757. t19 += v * b9;
  758. t20 += v * b10;
  759. t21 += v * b11;
  760. t22 += v * b12;
  761. t23 += v * b13;
  762. t24 += v * b14;
  763. t25 += v * b15;
  764. v = a[11];
  765. t11 += v * b0;
  766. t12 += v * b1;
  767. t13 += v * b2;
  768. t14 += v * b3;
  769. t15 += v * b4;
  770. t16 += v * b5;
  771. t17 += v * b6;
  772. t18 += v * b7;
  773. t19 += v * b8;
  774. t20 += v * b9;
  775. t21 += v * b10;
  776. t22 += v * b11;
  777. t23 += v * b12;
  778. t24 += v * b13;
  779. t25 += v * b14;
  780. t26 += v * b15;
  781. v = a[12];
  782. t12 += v * b0;
  783. t13 += v * b1;
  784. t14 += v * b2;
  785. t15 += v * b3;
  786. t16 += v * b4;
  787. t17 += v * b5;
  788. t18 += v * b6;
  789. t19 += v * b7;
  790. t20 += v * b8;
  791. t21 += v * b9;
  792. t22 += v * b10;
  793. t23 += v * b11;
  794. t24 += v * b12;
  795. t25 += v * b13;
  796. t26 += v * b14;
  797. t27 += v * b15;
  798. v = a[13];
  799. t13 += v * b0;
  800. t14 += v * b1;
  801. t15 += v * b2;
  802. t16 += v * b3;
  803. t17 += v * b4;
  804. t18 += v * b5;
  805. t19 += v * b6;
  806. t20 += v * b7;
  807. t21 += v * b8;
  808. t22 += v * b9;
  809. t23 += v * b10;
  810. t24 += v * b11;
  811. t25 += v * b12;
  812. t26 += v * b13;
  813. t27 += v * b14;
  814. t28 += v * b15;
  815. v = a[14];
  816. t14 += v * b0;
  817. t15 += v * b1;
  818. t16 += v * b2;
  819. t17 += v * b3;
  820. t18 += v * b4;
  821. t19 += v * b5;
  822. t20 += v * b6;
  823. t21 += v * b7;
  824. t22 += v * b8;
  825. t23 += v * b9;
  826. t24 += v * b10;
  827. t25 += v * b11;
  828. t26 += v * b12;
  829. t27 += v * b13;
  830. t28 += v * b14;
  831. t29 += v * b15;
  832. v = a[15];
  833. t15 += v * b0;
  834. t16 += v * b1;
  835. t17 += v * b2;
  836. t18 += v * b3;
  837. t19 += v * b4;
  838. t20 += v * b5;
  839. t21 += v * b6;
  840. t22 += v * b7;
  841. t23 += v * b8;
  842. t24 += v * b9;
  843. t25 += v * b10;
  844. t26 += v * b11;
  845. t27 += v * b12;
  846. t28 += v * b13;
  847. t29 += v * b14;
  848. t30 += v * b15;
  849. t0 += 38 * t16;
  850. t1 += 38 * t17;
  851. t2 += 38 * t18;
  852. t3 += 38 * t19;
  853. t4 += 38 * t20;
  854. t5 += 38 * t21;
  855. t6 += 38 * t22;
  856. t7 += 38 * t23;
  857. t8 += 38 * t24;
  858. t9 += 38 * t25;
  859. t10 += 38 * t26;
  860. t11 += 38 * t27;
  861. t12 += 38 * t28;
  862. t13 += 38 * t29;
  863. t14 += 38 * t30;
  864. // t15 left as is
  865. // first car
  866. c = 1;
  867. v = t0 + c + 65535; c = Math.floor(v / 65536); t0 = v - c * 65536;
  868. v = t1 + c + 65535; c = Math.floor(v / 65536); t1 = v - c * 65536;
  869. v = t2 + c + 65535; c = Math.floor(v / 65536); t2 = v - c * 65536;
  870. v = t3 + c + 65535; c = Math.floor(v / 65536); t3 = v - c * 65536;
  871. v = t4 + c + 65535; c = Math.floor(v / 65536); t4 = v - c * 65536;
  872. v = t5 + c + 65535; c = Math.floor(v / 65536); t5 = v - c * 65536;
  873. v = t6 + c + 65535; c = Math.floor(v / 65536); t6 = v - c * 65536;
  874. v = t7 + c + 65535; c = Math.floor(v / 65536); t7 = v - c * 65536;
  875. v = t8 + c + 65535; c = Math.floor(v / 65536); t8 = v - c * 65536;
  876. v = t9 + c + 65535; c = Math.floor(v / 65536); t9 = v - c * 65536;
  877. v = t10 + c + 65535; c = Math.floor(v / 65536); t10 = v - c * 65536;
  878. v = t11 + c + 65535; c = Math.floor(v / 65536); t11 = v - c * 65536;
  879. v = t12 + c + 65535; c = Math.floor(v / 65536); t12 = v - c * 65536;
  880. v = t13 + c + 65535; c = Math.floor(v / 65536); t13 = v - c * 65536;
  881. v = t14 + c + 65535; c = Math.floor(v / 65536); t14 = v - c * 65536;
  882. v = t15 + c + 65535; c = Math.floor(v / 65536); t15 = v - c * 65536;
  883. t0 += c-1 + 37 * (c-1);
  884. // second car
  885. c = 1;
  886. v = t0 + c + 65535; c = Math.floor(v / 65536); t0 = v - c * 65536;
  887. v = t1 + c + 65535; c = Math.floor(v / 65536); t1 = v - c * 65536;
  888. v = t2 + c + 65535; c = Math.floor(v / 65536); t2 = v - c * 65536;
  889. v = t3 + c + 65535; c = Math.floor(v / 65536); t3 = v - c * 65536;
  890. v = t4 + c + 65535; c = Math.floor(v / 65536); t4 = v - c * 65536;
  891. v = t5 + c + 65535; c = Math.floor(v / 65536); t5 = v - c * 65536;
  892. v = t6 + c + 65535; c = Math.floor(v / 65536); t6 = v - c * 65536;
  893. v = t7 + c + 65535; c = Math.floor(v / 65536); t7 = v - c * 65536;
  894. v = t8 + c + 65535; c = Math.floor(v / 65536); t8 = v - c * 65536;
  895. v = t9 + c + 65535; c = Math.floor(v / 65536); t9 = v - c * 65536;
  896. v = t10 + c + 65535; c = Math.floor(v / 65536); t10 = v - c * 65536;
  897. v = t11 + c + 65535; c = Math.floor(v / 65536); t11 = v - c * 65536;
  898. v = t12 + c + 65535; c = Math.floor(v / 65536); t12 = v - c * 65536;
  899. v = t13 + c + 65535; c = Math.floor(v / 65536); t13 = v - c * 65536;
  900. v = t14 + c + 65535; c = Math.floor(v / 65536); t14 = v - c * 65536;
  901. v = t15 + c + 65535; c = Math.floor(v / 65536); t15 = v - c * 65536;
  902. t0 += c-1 + 37 * (c-1);
  903. o[ 0] = t0;
  904. o[ 1] = t1;
  905. o[ 2] = t2;
  906. o[ 3] = t3;
  907. o[ 4] = t4;
  908. o[ 5] = t5;
  909. o[ 6] = t6;
  910. o[ 7] = t7;
  911. o[ 8] = t8;
  912. o[ 9] = t9;
  913. o[10] = t10;
  914. o[11] = t11;
  915. o[12] = t12;
  916. o[13] = t13;
  917. o[14] = t14;
  918. o[15] = t15;
  919. }