gnablib
Version:
A lean, zero dependency library to provide a useful base for your project.
2 lines • 7.44 kB
JavaScript
/*! Copyright 2025 the gnablib contributors MPL-1.1 */
var t,s=this&&this.__classPrivateFieldSet||function(t,s,o,n,i){if("m"===n)throw new TypeError("Private method is not writable");if("a"===n&&!i)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof s?t!==s||!i:!s.has(t))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===n?i.call(t,o):i?i.value=o:s.set(t,o),o},o=this&&this.__classPrivateFieldGet||function(t,s,o,n){if("a"===o&&!n)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof s?t!==s||!n:!s.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===o?n:"a"===o?n.call(t):n?n.value:s.get(t)};import{U32 as n}from"../../primitive/number/U32Static.js";import{sLen as i}from"../../safe/safe.js";const c=2654435769;function f(t,s){const o=t[s]^t[s+3],n=t[s+2]^o,i=t[s+1]^n;t[s+3]=t[s]&t[s+3]^i;const c=t[s]^t[s+1]&o;t[s+2]=i^(t[s+2]|c);const f=t[s+3]&(n^c);t[s+1]=~n^f,t[s]=f^~c}function e(t,s){const o=t[s+1]^~t[s],n=t[s+2]^(t[s]|o);t[s+2]=t[s+3]^n;const i=t[s+1]^(t[s+3]|o),c=o^t[s+2];t[s+3]=c^n&i;const f=n^i;t[s+1]=t[s+3]^f,t[s]=n^c&f}function r(t,s){const o=t[s],n=t[s+3],i=~o,c=t[s+1]^n,f=t[s+2]&i;t[s]=c^f;const e=t[s+2]^i,r=t[s+2]^t[s],h=t[s+1]&r;t[s+3]=e^h,t[s+2]=o^(n|h)&(t[s]|e),t[s+1]=c^t[s+3]^t[s+2]^(n|i)}function h(t,s){const o=t[s+1],n=t[s+3],i=t[s]^t[s+1],c=t[s]&t[s+2],f=t[s]|t[s+3],e=t[s+2]^t[s+3],r=c|i&f;t[s+2]=e^r;const h=r^(t[s+1]^f),a=e&h;t[s]=i^a;const l=t[s+2]&t[s];t[s+1]=h^l,t[s+3]=e^l^(o|n)}function a(t,s){const o=t[s],n=o^t[s+3],i=t[s+3]&n,c=t[s+2]^i,f=t[s+1]|c;t[s+3]=n^f;const e=~t[s+1],r=n|e;t[s]=c^r;const h=o&t[s],a=n^e,l=f&a;t[s+2]=h^l,t[s+1]=o^c^a&t[s+2]}function l(t,s){const o=t[s+1],n=~t[s],i=t[s]^o,c=t[s]^t[s+3],f=t[s+2]^n,e=i|c;t[s]=f^e;const r=t[s+3]&t[s],h=i^t[s];t[s+1]=r^h;const a=i|r,l=c^(n|t[s]);t[s+2]=a^l,t[s+3]=o^r^t[s+1]&l}function u(t,s){const o=~t[s],n=t[s]^t[s+3],i=t[s+1]^n,c=o|n,f=t[s+2]^c;t[s+1]=t[s+1]^f;const e=n|t[s+1],r=t[s+3]^e,h=f&r;t[s+2]=i^h;const a=f^r;t[s]=t[s+2]^a,t[s+3]=~f^i&a}function w(t,s){const o=t[s+1]^t[s+2],n=t[s+2]&o,i=t[s+3]^n,c=t[s]^i,f=c&(t[s+3]|o);t[s+1]=t[s+1]^f;const e=i|t[s+1],r=t[s]&c;t[s+3]=o^r;const h=c^e,a=t[s+3]&h;t[s+2]=i^a,t[s]=~h^t[s+3]&t[s+2]}function y(t){const s=~t[0],o=t[0]^t[1],n=t[3]^(s|o),i=t[2]^n;t[2]=o^i;const c=s^t[3]&o;t[1]=n^t[2]&c,t[3]=t[0]&n^(i|t[1]),t[0]=t[3]^i^c}function p(t){const s=t[1]^t[3],o=t[0]^t[1]&s,n=s^o;t[3]=t[2]^n;const i=t[1]^s&o,c=t[3]|i;t[1]=o^c;const f=~t[1],e=t[3]^i;t[0]=f^e,t[2]=n^(f|e)}function d(t){const s=t[0],o=t[3],n=t[1]^o,i=~n,c=s^t[2],f=t[2]^n,e=t[1]&f;t[0]=c^e;const r=c|o^(s|i);t[3]=n^r;const h=~f,a=t[0]|t[3];t[1]=h^a,t[2]=o&h^c^a}function b(t){const s=t[0]|t[1],o=t[1]^t[2],n=t[1]&o,i=t[0]^n,c=t[2]^i,f=t[3]|i;t[0]=o^f;const e=o|f,r=t[3]^e;t[2]=c^r;const h=s^r,a=t[0]&h;t[3]=i^a,t[1]=t[3]^t[0]^h}function m(t){const s=t[3],o=t[2]|s,n=t[0]&o,i=t[1]^n,c=t[0]&i,f=t[2]^c;t[1]=s^f;const e=~t[0],r=f&t[1];t[3]=i^r;const h=s^(t[1]|e);t[0]=t[3]^h,t[2]=i&h^t[1]^e}function R(t){const s=t[0],o=t[1],n=t[3],i=~t[2],c=n^o&i,f=s&c,e=o^i;t[3]=f^e;const r=o|t[3],h=s&r;t[1]=c^h;const a=s|n,l=i^r;t[0]=a^l,t[2]=o&a^(f|s^t[2])}function k(t){const s=t[1],o=t[3],n=~t[0],i=t[0]^s,c=t[2]^i,f=o^(t[2]|n);t[1]=c^f;const e=i^c&f,r=s|e;t[3]=f^r;const h=s|t[3];t[0]=e^h,t[2]=o&n^c^h}function v(t){const s=t[0],o=t[3],n=t[2]|s&t[1],i=o&(s|t[1]);t[3]=n^i;const c=~o,f=t[1]^i,e=f|t[3]^c;t[1]=s^e,t[0]=t[2]^f^(o|t[1]),t[2]=n^t[1]^t[0]^s&t[3]}function _(t){t[0]=n.lRot(t[0],13),t[2]=n.lRot(t[2],3),t[1]=n.lRot(t[1]^t[0]^t[2],1),t[3]=n.lRot(t[3]^t[2]^t[0]<<3,7),t[0]=n.lRot(t[0]^t[1]^t[3],5),t[2]=n.lRot(t[2]^t[3]^t[1]<<7,22)}function x(t){const s=n.rRot(t[2],22)^t[3]^t[1]<<7,o=n.rRot(t[0],5)^t[1]^t[3];t[3]=n.rRot(t[3],7)^s^o<<3,t[1]=n.rRot(t[1],1)^o^s,t[2]=n.rRot(s,3),t[0]=n.rRot(o,13)}function g(t,s,o){t[0]^=s[o],t[1]^=s[o+1],t[2]^=s[o+2],t[3]^=s[o+3]}class B{constructor(o){this.blockSize=16,t.set(this,void 0),s(this,t,function(t){const s=new Uint32Array(132),o=new Uint32Array(t.buffer,t.byteOffset,t.length/4),i=new Uint32Array(16);i.set(o),o.length<8&&(i[o.length]=1);let y=0;for(;y<8;y++)i[y+8]=n.lRot(i[y]^i[y+3]^i[y+5]^i[y+7]^c^y,11);for(s.set(i.subarray(8));y<132;y++)s[y]=n.lRot(s[y-8]^s[y-5]^s[y-3]^s[y-1]^c^y,11);return h(s,0),r(s,4),e(s,8),f(s,12),w(s,16),u(s,20),l(s,24),a(s,28),h(s,32),r(s,36),e(s,40),f(s,44),w(s,48),u(s,52),l(s,56),a(s,60),h(s,64),r(s,68),e(s,72),f(s,76),w(s,80),u(s,84),l(s,88),a(s,92),h(s,96),r(s,100),e(s,104),f(s,108),w(s,112),u(s,116),l(s,120),a(s,124),h(s,128),s}(o),"f")}_encBlock(s){const n=new Uint32Array(s.buffer,s.byteOffset,s.length/4);g(n,o(this,t,"f"),0),f(n,0),_(n),g(n,o(this,t,"f"),4),e(n,0),_(n),g(n,o(this,t,"f"),8),r(n,0),_(n),g(n,o(this,t,"f"),12),h(n,0),_(n),g(n,o(this,t,"f"),16),a(n,0),_(n),g(n,o(this,t,"f"),20),l(n,0),_(n),g(n,o(this,t,"f"),24),u(n,0),_(n),g(n,o(this,t,"f"),28),w(n,0),_(n),g(n,o(this,t,"f"),32),f(n,0),_(n),g(n,o(this,t,"f"),36),e(n,0),_(n),g(n,o(this,t,"f"),40),r(n,0),_(n),g(n,o(this,t,"f"),44),h(n,0),_(n),g(n,o(this,t,"f"),48),a(n,0),_(n),g(n,o(this,t,"f"),52),l(n,0),_(n),g(n,o(this,t,"f"),56),u(n,0),_(n),g(n,o(this,t,"f"),60),w(n,0),_(n),g(n,o(this,t,"f"),64),f(n,0),_(n),g(n,o(this,t,"f"),68),e(n,0),_(n),g(n,o(this,t,"f"),72),r(n,0),_(n),g(n,o(this,t,"f"),76),h(n,0),_(n),g(n,o(this,t,"f"),80),a(n,0),_(n),g(n,o(this,t,"f"),84),l(n,0),_(n),g(n,o(this,t,"f"),88),u(n,0),_(n),g(n,o(this,t,"f"),92),w(n,0),_(n),g(n,o(this,t,"f"),96),f(n,0),_(n),g(n,o(this,t,"f"),100),e(n,0),_(n),g(n,o(this,t,"f"),104),r(n,0),_(n),g(n,o(this,t,"f"),108),h(n,0),_(n),g(n,o(this,t,"f"),112),a(n,0),_(n),g(n,o(this,t,"f"),116),l(n,0),_(n),g(n,o(this,t,"f"),120),u(n,0),_(n),g(n,o(this,t,"f"),124),w(n,0),g(n,o(this,t,"f"),128)}_decBlock(s){const n=new Uint32Array(s.buffer,s.byteOffset,s.length/4);g(n,o(this,t,"f"),128),v(n),g(n,o(this,t,"f"),124),x(n),k(n),g(n,o(this,t,"f"),120),x(n),R(n),g(n,o(this,t,"f"),116),x(n),m(n),g(n,o(this,t,"f"),112),x(n),b(n),g(n,o(this,t,"f"),108),x(n),d(n),g(n,o(this,t,"f"),104),x(n),p(n),g(n,o(this,t,"f"),100),x(n),y(n),g(n,o(this,t,"f"),96),x(n),v(n),g(n,o(this,t,"f"),92),x(n),k(n),g(n,o(this,t,"f"),88),x(n),R(n),g(n,o(this,t,"f"),84),x(n),m(n),g(n,o(this,t,"f"),80),x(n),b(n),g(n,o(this,t,"f"),76),x(n),d(n),g(n,o(this,t,"f"),72),x(n),p(n),g(n,o(this,t,"f"),68),x(n),y(n),g(n,o(this,t,"f"),64),x(n),v(n),g(n,o(this,t,"f"),60),x(n),k(n),g(n,o(this,t,"f"),56),x(n),R(n),g(n,o(this,t,"f"),52),x(n),m(n),g(n,o(this,t,"f"),48),x(n),b(n),g(n,o(this,t,"f"),44),x(n),d(n),g(n,o(this,t,"f"),40),x(n),p(n),g(n,o(this,t,"f"),36),x(n),y(n),g(n,o(this,t,"f"),32),x(n),v(n),g(n,o(this,t,"f"),28),x(n),k(n),g(n,o(this,t,"f"),24),x(n),R(n),g(n,o(this,t,"f"),20),x(n),m(n),g(n,o(this,t,"f"),16),x(n),b(n),g(n,o(this,t,"f"),12),x(n),d(n),g(n,o(this,t,"f"),8),x(n),p(n),g(n,o(this,t,"f"),4),x(n),y(n),g(n,o(this,t,"f"),0)}encryptBlock(t,s=0){const o=8*s;i("block",t).atLeast(o+16).throwNot(),this._encBlock(t.subarray(o,o+16))}decryptBlock(t,s=0){const o=8*s;i("block",t).atLeast(o+16).throwNot(),this._decBlock(t.subarray(o,o+16))}}t=new WeakMap;export class Serpent_128 extends B{constructor(t){i("key",t).exactly(16).throwNot(),super(t)}}export class Serpent_192 extends B{constructor(t){i("key",t).exactly(24).throwNot(),super(t)}}export class Serpent_256 extends B{constructor(t){i("key",t).exactly(32).throwNot(),super(t)}}