UNPKG

gnablib

Version:

A lean, zero dependency library to provide a useful base for your project.

2 lines 2.54 kB
/*! Copyright 2025 the gnablib contributors MPL-1.1 */ var t,r=this&&this.__classPrivateFieldSet||function(t,r,o,e,a){if("m"===e)throw new TypeError("Private method is not writable");if("a"===e&&!a)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof r?t!==r||!a:!r.has(t))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===e?a.call(t,o):a?a.value=o:r.set(t,o),o},o=this&&this.__classPrivateFieldGet||function(t,r,o,e){if("a"===o&&!e)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof r?t!==r||!e:!r.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===o?e:"a"===o?e.call(t):e?e.value:r.get(t)};import{U64 as e,U64MutArray as a}from"../../primitive/number/U64.js";import{sLen as s}from"../../safe/safe.js";class n{constructor(o,e){this.blockSize=16,t.set(this,void 0),this.rounds=o,r(this,t,e,"f")}_encBlock(r){const e=a.fromBytes(r.buffer);for(let r=0;r<this.rounds;)e.at(1).rRotEq(8).addEq(e.at(0)).xorEq(o(this,t,"f").at(r++)),e.at(0).lRotEq(3).xorEq(e.at(1))}_decBlock(r){const e=a.fromBytes(r.buffer);for(let r=this.rounds-1;r>=0;)e.at(0).xorEq(e.at(1)).rRotEq(3),e.at(1).xorEq(o(this,t,"f").at(r--)).subEq(e.at(0)).lRotEq(8)}encryptBlock(t,r=0){const o=8*r;s("block",t).atLeast(o+8).throwNot(),this._encBlock(t.subarray(o,o+8))}decryptBlock(t,r=0){const o=8*r;s("block",t).atLeast(o+8).throwNot(),this._decBlock(t.subarray(o,o+8))}}t=new WeakMap;export class Speck128_128 extends n{constructor(t){const r=a.fromBytes(t.buffer),o=a.fromLen(32),s=r.at(0),n=r.at(1);for(let t=0;t<31;)o.at(t).set(s),n.rRotEq(8).addEq(s).xorEq(e.fromInt(t++)),s.lRotEq(3).xorEq(n);o.at(31).set(s),super(32,o)}}export class Speck128_192 extends n{constructor(t){const r=a.fromBytes(t.buffer),o=a.fromLen(33),s=r.at(0),n=r.at(1),c=r.at(2);for(let t=0;t<32;)o.at(t).set(s),n.rRotEq(8).addEq(s).xorEq(e.fromInt(t++)),s.lRotEq(3).xorEq(n),o.at(t).set(s),c.rRotEq(8).addEq(s).xorEq(e.fromInt(t++)),s.lRotEq(3).xorEq(c);o.at(32).set(s),super(33,o)}}export class Speck128_256 extends n{constructor(t){const r=a.fromBytes(t.buffer),o=a.fromLen(34),s=r.at(0),n=r.at(1),c=r.at(2),f=r.at(3);for(let t=0;t<33;)o.at(t).set(s),n.rRotEq(8).addEq(s).xorEq(e.fromInt(t++)),s.lRotEq(3).xorEq(n),o.at(t).set(s),c.rRotEq(8).addEq(s).xorEq(e.fromInt(t++)),s.lRotEq(3).xorEq(c),o.at(t).set(s),f.rRotEq(8).addEq(s).xorEq(e.fromInt(t++)),s.lRotEq(3).xorEq(f);o.at(33).set(s),super(34,o)}}