UNPKG

gnablib

Version:

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

2 lines 4.47 kB
/*! Copyright 2025 the gnablib contributors MPL-1.1 */ var t,r=this&&this.__classPrivateFieldSet||function(t,r,o,e,s){if("m"===e)throw new TypeError("Private method is not writable");if("a"===e&&!s)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof r?t!==r||!s:!r.has(t))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===e?s.call(t,o):s?s.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,U64Mut as s,U64MutArray as a}from"../../primitive/number/U64.js";import{sLen as E}from"../../safe/safe.js";const q=t=>t.mut().lRotEq(2).xorEq(t.mut().lRotEq(1).andEq(t.lRot(8))),x=e.fromI32s(4294967292,4294967295),n=e.fromInt(1);class i{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);let s=0;const E=254&this.rounds;for(;s<E;)e.at(0).xorEq(q(e.at(1)).xorEq(o(this,t,"f").at(s++))),e.at(1).xorEq(q(e.at(0)).xorEq(o(this,t,"f").at(s++)));if(69==this.rounds){const r=e.at(1).mut();e.at(1).set(e.at(0).mut().xorEq(q(e.at(1)).xorEq(o(this,t,"f").at(s++)))),e.at(0).set(r)}}_decBlock(r){const e=a.fromBytes(r.buffer);let s=this.rounds;if(69==this.rounds){s--;const r=e.at(0).mut();e.at(0).set(e.at(1).mut().xorEq(q(e.at(0)).xorEq(o(this,t,"f").at(68)))),e.at(1).set(r)}for(let r=s-1;r>=0;)e.at(1).xorEq(q(e.at(0)).xorEq(o(this,t,"f").at(r--))),e.at(0).xorEq(q(e.at(1)).xorEq(o(this,t,"f").at(r--)))}encryptBlock(t,r=0){const o=8*r;E("block",t).atLeast(o+8).throwNot(),this._encBlock(t.subarray(o,o+8))}decryptBlock(t,r=0){const o=8*r;E("block",t).atLeast(o+8).throwNot(),this._decBlock(t.subarray(o,o+8))}}t=new WeakMap;export class Simon128_128 extends i{constructor(t){const r=a.fromBytes(t.buffer),o=a.fromLen(68),e=s.fromI32s(422317813,1936324741),E=r.at(0),q=r.at(1);let i=0;for(;i<64;)o.at(i++).set(E),E.xorEq(x.mut().xorEq(e.and(n)).xorEq(q.rRot(3)).xorEq(q.rRot(4))),e.rShiftEq(1),o.at(i++).set(q),q.xorEq(x.mut().xorEq(e.and(n)).xorEq(E.rRot(3)).xorEq(E.rRot(4))),e.rShiftEq(1);o.at(i++).set(E),E.xorEq(x.mut().xorEq(n).xorEq(q.rRot(3)).xorEq(q.rRot(4))),o.at(i++).set(q),q.xorEq(x.mut().xorEq(E.rRot(3)).xorEq(E.rRot(4))),o.at(i++).set(E),o.at(i++).set(q),super(68,o)}}export class Simon128_192 extends i{constructor(t){const r=a.fromBytes(t.buffer),o=a.fromLen(69),e=s.fromI32s(128333275,4230800658),E=r.at(0),q=r.at(1),i=r.at(2);let f=0;for(;f<63;)o.at(f++).set(E),E.xorEq(x.mut().xorEq(e.and(n)).xorEq(i.rRot(3)).xorEq(i.rRot(4))),e.rShiftEq(1),o.at(f++).set(q),q.xorEq(x.mut().xorEq(e.and(n)).xorEq(E.rRot(3)).xorEq(E.rRot(4))),e.rShiftEq(1),o.at(f++).set(i),i.xorEq(x.mut().xorEq(e.and(n)).xorEq(q.rRot(3)).xorEq(q.rRot(4))),e.rShiftEq(1);o.at(f++).set(E),E.xorEq(x.mut().xorEq(n).xorEq(i.rRot(3)).xorEq(i.rRot(4))),o.at(f++).set(q),q.xorEq(x.mut().xorEq(E.rRot(3)).xorEq(E.rRot(4))),o.at(f++).set(i),i.xorEq(x.mut().xorEq(n).xorEq(q.rRot(3)).xorEq(q.rRot(4))),o.at(f++).set(E),o.at(f++).set(q),o.at(f++).set(i),super(69,o)}}export class Simon128_256 extends i{constructor(t){const r=a.fromBytes(t.buffer),o=a.fromLen(72),e=s.fromI32s(74278795,4257827898),E=r.at(0),q=r.at(1),i=r.at(2),f=r.at(3);let c=0;for(;c<64;)o.at(c++).set(E),E.xorEq(x.mut().xorEq(e.and(n)).xorEq(f.rRot(3)).xorEq(f.rRot(4)).xorEq(q).xorEq(q.rRot(1))),e.rShiftEq(1),o.at(c++).set(q),q.xorEq(x.mut().xorEq(e.and(n)).xorEq(E.rRot(3)).xorEq(E.rRot(4)).xorEq(i).xorEq(i.rRot(1))),e.rShiftEq(1),o.at(c++).set(i),i.xorEq(x.mut().xorEq(e.and(n)).xorEq(q.rRot(3)).xorEq(q.rRot(4)).xorEq(f).xorEq(f.rRot(1))),e.rShiftEq(1),o.at(c++).set(f),f.xorEq(x.mut().xorEq(e.and(n)).xorEq(i.rRot(3)).xorEq(i.rRot(4)).xorEq(E).xorEq(E.rRot(1))),e.rShiftEq(1);o.at(c++).set(E),E.xorEq(x.mut().xorEq(f.rRot(3)).xorEq(f.rRot(4)).xorEq(q).xorEq(q.rRot(1))),o.at(c++).set(q),q.xorEq(x.mut().xorEq(n).xorEq(E.rRot(3)).xorEq(E.rRot(4)).xorEq(i).xorEq(i.rRot(1))),o.at(c++).set(i),i.xorEq(x.mut().xorEq(q.rRot(3)).xorEq(q.rRot(4)).xorEq(f).xorEq(f.rRot(1))),o.at(c++).set(f),f.xorEq(x.mut().xorEq(i.rRot(3)).xorEq(i.rRot(4)).xorEq(E).xorEq(E.rRot(1))),o.at(c++).set(E),o.at(c++).set(q),o.at(c++).set(i),o.at(c++).set(f),super(72,o)}}