UNPKG

gnablib

Version:

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

2 lines 4.53 kB
/*! Copyright 2023-2025 the gnablib contributors MPL-1.1 */ var t,s,e,o=this&&this.__classPrivateFieldGet||function(t,s,e,o){if("a"===e&&!o)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof s?t!==s||!o:!s.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===e?o:"a"===e?o.call(t):o?o.value:s.get(t)};import{asBE as r}from"../../endian/platform.js";import{U64Mut as i,U64MutArray as a}from"../../primitive/number/U64.js";const h=a.fromLen(2048),n=a.fromLen(10),f=new Uint8Array([24,35,198,232,135,184,1,79,54,166,210,245,121,111,145,82,96,188,155,142,163,12,123,53,29,224,215,194,46,75,254,87,21,119,55,229,159,240,74,218,88,201,41,10,177,160,107,133,189,93,16,244,203,62,5,103,228,39,65,139,167,125,149,216,251,238,124,102,221,23,71,158,202,45,191,7,173,90,131,51,99,2,170,113,200,25,73,217,242,227,91,136,154,38,50,176,233,15,213,128,190,205,52,72,255,122,144,95,32,104,26,174,180,84,147,34,100,241,115,18,64,8,195,236,219,161,141,61,151,0,207,43,118,130,214,27,181,175,106,80,69,243,48,239,63,85,162,234,101,186,47,192,222,28,253,77,146,117,6,138,178,230,14,31,98,212,168,150,249,197,37,89,132,114,57,76,94,120,56,140,209,165,226,97,179,33,156,30,67,199,252,4,81,153,109,13,250,223,126,36,59,171,206,17,143,78,183,235,60,129,148,247,185,19,44,211,231,110,196,3,86,68,127,169,42,187,193,83,220,11,157,108,49,116,246,70,172,137,20,225,22,58,105,9,112,182,208,237,204,66,152,164,40,92,248,134]);!function(){for(let t=0;t<256;t++){const s=f[t];let e=s<<1;e>=256&&(e^=285);let o=e<<1;o>=256&&(o^=285);const r=o^s;let a=o<<1;a>=256&&(a^=285);const n=a^s;h.at(t).set(i.fromI32s(a<<24|r<<16|e<<8|n,s<<24|s<<16|o<<8|s));for(let s=1;s<8;s++)h.at(s<<8|t).set(h.at(s-1<<8|t).rRot(8))}for(let t=0;t<10;t++){let s=t<<3;n.at(t).set(i.fromI32s(0,4278190080).andEq(h.at(s++)).xorEq(i.fromI32s(0,16711680).andEq(h.at(256|s++))).xorEq(i.fromI32s(0,65280).andEq(h.at(512|s++))).xorEq(i.fromI32s(0,255).andEq(h.at(768|s++))).xorEq(i.fromI32s(4278190080,0).andEq(h.at(1024|s++))).xorEq(i.fromI32s(16711680,0).andEq(h.at(1280|s++))).xorEq(i.fromI32s(65280,0).andEq(h.at(1536|s++))).xorEq(i.fromI32s(255,0).andEq(h.at(1792|s++))))}}();export class Whirlpool{constructor(){this.size=64,this.blockSize=64,t.set(this,a.fromLen(8)),s.set(this,new Uint8Array(64)),e.set(this,a.fromBytes(o(this,s,"f").buffer)),this._ingestBytes=i.fromI32s(0,0),this._bPos=0,this.reset()}hash(){const i=o(this,t,"f").clone(),f=o(this,t,"f").clone(),l=a.fromLen(8);r.i64(o(this,s,"f"),0,8);for(let t=0;t<8;t++)i.at(t).xorEq(o(this,e,"f").at(t));for(let t=0;t<10;t++){for(let t=0;t<8;t++)l.at(t).set(h.at(f.at(t).getByte(7)).mut().xorEq(h.at(256|f.at(t-1&7).getByte(6))).xorEq(h.at(512|f.at(t-2&7).getByte(5))).xorEq(h.at(768|f.at(t-3&7).getByte(4))).xorEq(h.at(1024|f.at(t-4&7).getByte(3))).xorEq(h.at(1280|f.at(t-5&7).getByte(2))).xorEq(h.at(1536|f.at(t-6&7).getByte(1))).xorEq(h.at(1792|f.at(t-7&7).getByte())));l.at(0).xorEq(n.at(t)),f.set(l);for(let t=0;t<8;t++)l.at(t).xorEq(h.at(i.at(t).getByte(7))).xorEq(h.at(256|i.at(t-1&7).getByte(6))).xorEq(h.at(512|i.at(t-2&7).getByte(5))).xorEq(h.at(768|i.at(t-3&7).getByte(4))).xorEq(h.at(1024|i.at(t-4&7).getByte(3))).xorEq(h.at(1280|i.at(t-5&7).getByte(2))).xorEq(h.at(1536|i.at(t-6&7).getByte(1))).xorEq(h.at(1792|i.at(t-7&7).getByte()));i.set(l)}for(let s=0;s<8;s++)o(this,t,"f").at(s).xorEq(i.at(s)).xorEq(o(this,e,"f").at(s));this._bPos=0}write(t){this._ingestBytes.addEq(i.fromInt(t.length));let e=t.length,r=0,a=this.blockSize-this._bPos;for(;e>0;){if(a>e)return o(this,s,"f").set(t.subarray(r),this._bPos),void(this._bPos+=e);o(this,s,"f").set(t.subarray(r,r+this.blockSize),this._bPos),this._bPos+=a,this.hash(),r+=a,e-=a,a=this.blockSize}}sum(){return this.clone().sumIn()}sumIn(){o(this,s,"f")[this._bPos]=128,this._bPos++;const i=this.blockSize-32;this._bPos>i&&(o(this,s,"f").fill(0,this._bPos),this.hash()),o(this,s,"f").fill(0,this._bPos);const a=i>>3;return o(this,e,"f").at(a+2).set(this._ingestBytes.rShift(61)),o(this,e,"f").at(a+3).set(this._ingestBytes.lShift(3)),r.i64(o(this,s,"f"),i+16),r.i64(o(this,s,"f"),i+24),this.hash(),o(this,t,"f").toBytesBE().subarray(0,this.size)}reset(){for(let s=0;s<8;s++)o(this,t,"f").at(s).zero();this._ingestBytes.zero(),this._bPos=0}newEmpty(){return new Whirlpool}clone(){const e=new Whirlpool;return o(e,t,"f").set(o(this,t,"f")),o(e,s,"f").set(o(this,s,"f")),e._ingestBytes.set(this._ingestBytes),e._bPos=this._bPos,e}}t=new WeakMap,s=new WeakMap,e=new WeakMap;