UNPKG

gnablib

Version:

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

2 lines 10.8 kB
/*! Copyright 2023-2025 the gnablib contributors MPL-1.1 */ var t,s,e,r,i,a,o,h,n=this&&this.__classPrivateFieldSet||function(t,s,e,r,i){if("m"===r)throw new TypeError("Private method is not writable");if("a"===r&&!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"===r?i.call(t,e):i?i.value=e:s.set(t,e),e},f=this&&this.__classPrivateFieldGet||function(t,s,e,r){if("a"===e&&!r)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof s?t!==s||!r:!s.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===e?r:"a"===e?r.call(t):r?r.value:s.get(t)};import{U32 as m}from"../../primitive/number/U32Static.js";import{U64 as p,U64Mut as l,U64MutArray as y}from"../../primitive/number/U64.js";import{LengthError as u}from"../../error/LengthError.js";import{sInt as w,sLen as c,sNum as B}from"../../safe/safe.js";import{asLE as E}from"../../endian/platform.js";import{ByteWriter as g}from"../../primitive/ByteWriter.js";const b=16,x=[1779033703,4089235720,3144134277,2227873595,1013904242,4271175723,2773480762,1595750129,1359893119,2917565137,2600822924,725511199,528734635,4215389547,1541459225,327033209],d=[[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15],[14,10,4,8,9,15,13,6,1,12,0,2,11,7,5,3],[11,8,12,0,5,2,15,13,10,14,3,6,7,1,9,4],[7,9,3,1,13,12,11,14,2,6,5,10,4,0,15,8],[9,0,5,7,2,4,10,15,14,1,11,12,6,8,3,13],[2,12,6,10,0,11,8,3,4,13,7,5,15,14,1,9],[12,5,1,15,14,13,4,10,0,7,6,3,9,2,8,11],[13,11,7,14,12,1,3,9,5,0,15,4,8,6,2,10],[6,15,14,9,11,3,0,8,12,2,13,7,1,4,10,5],[10,2,8,4,7,6,1,5,15,11,9,14,3,12,13,0]];class _{get size(){return this.params[0]}constructor(i,a){this.params=a,t.set(this,void 0),this.blockSize=64,s.set(this,new Uint32Array(8)),e.set(this,new Uint8Array(64)),r.set(this,new Uint32Array(f(this,e,"f").buffer)),this._ingestBytes=l.fromI32s(0,0),this._bPos=0,c("key",i).atMost(32).throwNot(),n(this,t,i,"f"),this.reset()}get keyLen(){return this.params[1]}get fanOut(){return this.params[2]}get maxDepth(){return this.params[3]}get leafLen(){return m.fromBytesLE(this.params,4)}get nodeOffset(){const t=new Uint8Array(8);return t.set(this.params.subarray(8,14)),p.fromBytesBE(t.reverse())}get nodeDepth(){return this.params[14]}get innerLen(){return this.params[15]}get salt(){return this.params.subarray(16,24)}get personalization(){return this.params.subarray(24)}mix(t,s,e,i,a,o){const h=3&t,n=t<<1,p=o[n],l=o[n+1],y=f(this,r,"f")[p],u=f(this,r,"f")[l];a[h]+=a[s]+y,a[i]=m.rRot(a[i]^a[h],16),a[e]+=a[i],a[s]=m.rRot(a[s]^a[e],12),a[h]+=a[s]+u,a[i]=m.rRot(a[i]^a[h],8),a[e]+=a[i],a[s]=m.rRot(a[s]^a[e],7)}hash(t=!1){const e=new Uint32Array(16);e.set(f(this,s,"f")),e[8]=x[0],e[9]=x[2],e[10]=x[4],e[11]=x[6],e[12]=x[8]^this._ingestBytes.low,e[13]=x[10]^this._ingestBytes.high,e[14]=x[12],e[15]=x[14],t&&(e[14]=~e[14]);for(let t=0;t<10;t++){const s=d[t%10];this.mix(0,4,8,12,e,s),this.mix(1,5,9,13,e,s),this.mix(2,6,10,14,e,s),this.mix(3,7,11,15,e,s),this.mix(4,5,10,15,e,s),this.mix(5,6,11,12,e,s),this.mix(6,7,8,13,e,s),this.mix(7,4,9,14,e,s)}f(this,s,"f")[0]^=e[0]^e[8],f(this,s,"f")[1]^=e[1]^e[9],f(this,s,"f")[2]^=e[2]^e[10],f(this,s,"f")[3]^=e[3]^e[11],f(this,s,"f")[4]^=e[4]^e[12],f(this,s,"f")[5]^=e[5]^e[13],f(this,s,"f")[6]^=e[6]^e[14],f(this,s,"f")[7]^=e[7]^e[15],this._bPos=0}write(t){let s=t.length,r=0,i=this.blockSize-this._bPos;for(;s>0;){if(i>s){const i=t.subarray(r);return f(this,e,"f").set(i,this._bPos),this._bPos+=s,void this._ingestBytes.addEq(p.fromInt(i.length))}f(this,e,"f").set(t.subarray(r,r+this.blockSize),this._bPos),this._ingestBytes.addEq(p.fromInt(this.blockSize)),this.hash(),r+=i,s-=i,i=this.blockSize}}sum(){return this.clone().sumIn()}sumIn(){f(this,e,"f").fill(0,this._bPos),this.hash(!0);const t=new Uint8Array(this.size),r=new Uint8Array(f(this,s,"f").buffer);return t.set(r.subarray(0,4*Math.floor(this.size/4))),E.i32(t,0,this.size/4),t}reset(){f(this,s,"f")[0]=x[0]^m.fromBytesLE(this.params,0),f(this,s,"f")[1]=x[2]^m.fromBytesLE(this.params,4),f(this,s,"f")[2]=x[4]^m.fromBytesLE(this.params,8),f(this,s,"f")[3]=x[6]^m.fromBytesLE(this.params,12),f(this,s,"f")[4]=x[8]^m.fromBytesLE(this.params,16),f(this,s,"f")[5]=x[10]^m.fromBytesLE(this.params,20),f(this,s,"f")[6]=x[12]^m.fromBytesLE(this.params,24),f(this,s,"f")[7]=x[14]^m.fromBytesLE(this.params,28),this._ingestBytes.zero(),this._bPos=0,f(this,t,"f").length>0&&this.write(f(this,t,"f"))}newEmpty(){return new _(f(this,t,"f"),this.params)}clone(){const r=new _(f(this,t,"f"),this.params);return f(r,s,"f").set(f(this,s,"f")),f(r,e,"f").set(f(this,e,"f")),r._ingestBytes=this._ingestBytes,r._bPos=this._bPos,r}}t=new WeakMap,s=new WeakMap,e=new WeakMap,r=new WeakMap;class k{get size(){return this.params[0]}constructor(t,s){this.params=s,i.set(this,void 0),this.blockSize=128,a.set(this,y.fromLen(8)),o.set(this,new Uint8Array(128)),h.set(this,y.fromBytes(f(this,o,"f").buffer)),this._ingestBytes=l.fromI32s(0,0),this._bPos=0,c("key",t).atMost(64).throwNot(),n(this,i,t,"f"),this.p32=new Uint32Array(s.buffer),this.reset()}get keyLen(){return this.params[1]}get fanOut(){return this.params[2]}get maxDepth(){return this.params[3]}get leafLen(){return m.fromBytesLE(this.params,4)}get nodeOffset(){return p.mount(this.p32,2)}get nodeDepth(){return this.params[16]}get innerLen(){return this.params[17]}get salt(){return this.params.subarray(32,48)}get personalization(){return this.params.subarray(48)}mix(t,s,e,r,i,a){const o=3&t,n=t<<1,m=a[n],p=a[n+1],l=f(this,h,"f").at(m),y=f(this,h,"f").at(p);i.at(o).addEq(i.at(s)).addEq(l),i.at(r).xorEq(i.at(o)).rRotEq(32),i.at(e).addEq(i.at(r)),i.at(s).xorEq(i.at(e)).rRotEq(24),i.at(o).addEq(i.at(s)).addEq(y),i.at(r).xorEq(i.at(o)).rRotEq(16),i.at(e).addEq(i.at(r)),i.at(s).xorEq(i.at(e)).rRotEq(63)}hash(t=!1){const s=y.fromLen(16);s.at(0).set(f(this,a,"f").at(0)),s.at(1).set(f(this,a,"f").at(1)),s.at(2).set(f(this,a,"f").at(2)),s.at(3).set(f(this,a,"f").at(3)),s.at(4).set(f(this,a,"f").at(4)),s.at(5).set(f(this,a,"f").at(5)),s.at(6).set(f(this,a,"f").at(6)),s.at(7).set(f(this,a,"f").at(7)),s.at(8).set(p.fromI32s(x[1],x[0])),s.at(9).set(p.fromI32s(x[3],x[2])),s.at(10).set(p.fromI32s(x[5],x[4])),s.at(11).set(p.fromI32s(x[7],x[6])),s.at(12).set(p.fromI32s(x[9],x[8]).xor(this._ingestBytes)),s.at(13).set(p.fromI32s(x[11],x[10])),s.at(14).set(p.fromI32s(x[13],x[12])),s.at(15).set(p.fromI32s(x[15],x[14])),t&&s.at(14).notEq();for(let t=0;t<12;t++){const e=d[t%10];this.mix(0,4,8,12,s,e),this.mix(1,5,9,13,s,e),this.mix(2,6,10,14,s,e),this.mix(3,7,11,15,s,e),this.mix(4,5,10,15,s,e),this.mix(5,6,11,12,s,e),this.mix(6,7,8,13,s,e),this.mix(7,4,9,14,s,e)}f(this,a,"f").at(0).xorEq(s.at(0)).xorEq(s.at(8)),f(this,a,"f").at(1).xorEq(s.at(1)).xorEq(s.at(9)),f(this,a,"f").at(2).xorEq(s.at(2)).xorEq(s.at(10)),f(this,a,"f").at(3).xorEq(s.at(3)).xorEq(s.at(11)),f(this,a,"f").at(4).xorEq(s.at(4)).xorEq(s.at(12)),f(this,a,"f").at(5).xorEq(s.at(5)).xorEq(s.at(13)),f(this,a,"f").at(6).xorEq(s.at(6)).xorEq(s.at(14)),f(this,a,"f").at(7).xorEq(s.at(7)).xorEq(s.at(15)),this._bPos=0}write(t){let s=t.length,e=0,r=this.blockSize-this._bPos;for(;s>0;){if(r>s){const r=t.subarray(e);return f(this,o,"f").set(r,this._bPos),this._bPos+=s,void this._ingestBytes.addEq(p.fromInt(r.length))}f(this,o,"f").set(t.subarray(e,e+this.blockSize),this._bPos),this._ingestBytes.addEq(p.fromInt(this.blockSize)),this.hash(),e+=r,s-=r,r=this.blockSize}}sum(){return this.clone().sumIn()}sumIn(){f(this,o,"f").fill(0,this._bPos),this.hash(!0);const t=new Uint8Array(this.size),s=f(this,a,"f").toBytesLE();return t.set(s.subarray(0,this.size)),t}reset(){f(this,a,"f").at(0).set(p.fromI32s(x[1]^m.fromBytesLE(this.params,0),x[0]^m.fromBytesLE(this.params,4))),f(this,a,"f").at(1).set(p.fromI32s(x[3]^m.fromBytesLE(this.params,8),x[2]^m.fromBytesLE(this.params,12))),f(this,a,"f").at(2).set(p.fromI32s(x[5]^m.fromBytesLE(this.params,16),x[4]^m.fromBytesLE(this.params,20))),f(this,a,"f").at(3).set(p.fromI32s(x[7]^m.fromBytesLE(this.params,24),x[6]^m.fromBytesLE(this.params,28))),f(this,a,"f").at(4).set(p.fromI32s(x[9]^m.fromBytesLE(this.params,32),x[8]^m.fromBytesLE(this.params,36))),f(this,a,"f").at(5).set(p.fromI32s(x[11]^m.fromBytesLE(this.params,40),x[10]^m.fromBytesLE(this.params,44))),f(this,a,"f").at(6).set(p.fromI32s(x[13]^m.fromBytesLE(this.params,48),x[12]^m.fromBytesLE(this.params,52))),f(this,a,"f").at(7).set(p.fromI32s(x[15]^m.fromBytesLE(this.params,56),x[14]^m.fromBytesLE(this.params,60))),this._ingestBytes.zero(),this._bPos=0,f(this,i,"f").length>0&&this.write(f(this,i,"f"))}newEmpty(){return new k(f(this,i,"f"),this.params)}clone(){const t=new k(f(this,i,"f"),this.params);for(let s=0;s<f(this,a,"f").length;s++)f(t,a,"f").at(s).set(f(this,a,"f").at(s));return f(t,o,"f").set(f(this,o,"f")),t._ingestBytes=this._ingestBytes,t._bPos=this._bPos,t}}i=new WeakMap,a=new WeakMap,o=new WeakMap,h=new WeakMap;export class Blake2s extends _{constructor(t,s,e,r,i,a,o,h,n,f){h=null!=h?h:new Uint8Array(0);const p=new Uint8Array(32),l=g.mount(p);w("digestSize",t).natural().atMost(32).throwNot(),w("fanOut",s).unsigned().atMost(255).throwNot(),l.writeByte(t),l.writeByte(h.length),l.writeByte(s),l.writeByte(e),m.intoBytesLE(r,l),l.skip(6),p.set(i.toBytesLE(),8),l.writeByte(a),l.writeByte(o),n&&0!=n.length?8!=n.length?c("salt",n).exactly(8).throwNot():l.write(n):l.skip(8),f&&0!=f.length?8!=f.length?c("personalization",f).exactly(8).throwNot():l.write(f):l.skip(8),super(h,p)}static Sequential(t,s,e,r){return new Blake2s(t,1,1,0,p.zero,0,0,s,e,r)}}export class Blake2b extends k{constructor(t,s,e,r,i,a,o,h,n,f){h=null!=h?h:new Uint8Array(0);const p=new Uint8Array(64),l=g.mount(p);if(B("digestSize",t).natural().atMost(64).throwNot(),B("fanOut",s).unsigned().atMost(255).throwNot(),l.writeByte(t),l.writeByte(h.length),l.writeByte(s),l.writeByte(e),m.intoBytesLE(r,l),l.skip(8),p.set(i.toBytesLE(),8),l.writeByte(a),l.writeByte(o),l.skip(14),n&&0!=n.length){if(n.length!=b)throw new u(b,"salt",n.length);l.write(n)}else l.skip(b);if(f&&0!=f.length){if(f.length!=b)throw new u(b,"personalization",f.length);l.write(f)}else l.skip(b);super(h,p)}static Sequential(t,s,e,r){return new Blake2b(t,1,1,0,p.zero,0,0,s,e,r)}}export class Blake2s_224 extends Blake2s{constructor(t,s,e){super(28,1,1,0,p.zero,0,0,t,s,e)}}export class Blake2s_256 extends Blake2s{constructor(t,s,e){super(32,1,1,0,p.zero,0,0,t,s,e)}}export class Blake2b_256 extends Blake2b{constructor(t,s,e){super(32,1,1,0,p.zero,0,0,t,s,e)}}export class Blake2b_384 extends Blake2b{constructor(t,s,e){super(48,1,1,0,p.zero,0,0,t,s,e)}}export class Blake2b_512 extends Blake2b{constructor(t,s,e){super(64,1,1,0,p.zero,0,0,t,s,e)}}