gnablib
Version:
A lean, zero dependency library to provide a useful base for your project.
1 lines • 4.86 kB
JavaScript
import{hex as s}from"../../codec/Hex.js";import{asBE as t,asLE as r}from"../../endian/platform.js";import{sInt as i,sLen as h}from"../../safe/safe.js";const _=Symbol.for("nodejs.util.inspect.custom");export class AInt{constructor(s,t,r,i){this._arr=s,this._pos=t,this.size32=r,this._name=i}_setValue(s){let t=0;do{this._arr[this._pos+t]=s._arr[s._pos+t]}while(++t<this.size32)}_setZero(){this._arr.fill(0,this._pos,this._pos+this.size32)}clone32(){return this._arr.slice(this._pos,this._pos+this.size32)}static _fromInt(s,t){i("i52",t).atLeast(Number.MIN_SAFE_INTEGER).atMost(Number.MAX_SAFE_INTEGER).throwNot();const r=new Uint32Array(s);return r[0]=t,r[1]=Math.floor(t/4294967296),r}static _fromSignedSet(s,t){h("ns",t).atMost(s).throwNot();const r=new Uint32Array(s);return r.set(t),t.length>0&&t[t.length-1]<0&&r.fill(4294967295,t.length),r}static _fromSet(s,t){h("ns",t).atMost(s).throwNot();const r=new Uint32Array(s);return r.set(t),r}static _fromBytesBE(s,r,i){const _=i+s;h("src",r).atLeast(_).throwNot();const e=r.slice(i,_);return t.bytes(e,s),new Uint32Array(e.buffer)}static _fromBytesLE(s,t,i){const _=i+s;h("src",t).atLeast(_).throwNot();const e=t.slice(i,_);return r.bytes(e,s),new Uint32Array(e.buffer)}_xorEq(s){let t=0;do{this._arr[this._pos+t]^=s._arr[s._pos+t]}while(++t<this.size32)}_orEq(s){let t=0;do{this._arr[this._pos+t]|=s._arr[s._pos+t]}while(++t<this.size32)}_andEq(s){let t=0;do{this._arr[this._pos+t]&=s._arr[s._pos+t]}while(++t<this.size32)}_notEq(){let s=0;do{this._arr[this._pos+s]=~this._arr[this._pos+s]}while(++s<this.size32)}_lShiftEq(s){const t=31&s,r=Math.min(s>>>5,this.size32),i=32-t,h=1-(i>>>5);let _=this.size32-1;for(;_>r;_--)this._arr[this._pos+_]=this._arr[this._pos+_-r]<<t|h*this._arr[this._pos+_-1-r]>>>i;r<this.size32&&(this._arr[this._pos+_]=this._arr[this._pos+_-r]<<t,_--),_>=0&&this._arr.fill(0,this._pos,this._pos+_+1)}_rShiftEq(s,t=0){const r=31&s,i=Math.min(s>>>5,this.size32),h=32-r,_=1-(h>>>5);let e=0;const o=this.size32-i-1;for(;e<o;e++)this._arr[this._pos+e]=this._arr[this._pos+e+i]>>>r|_*this._arr[this._pos+e+i+1]<<h;i<this.size32&&(this._arr[this._pos+e]=this._arr[this._pos+e+i]>>>r|_*t<<h,e++),this._arr.fill(t,this._pos+e,this._pos+this.size32)}_lRotEq(s){const t=31&s,r=(s>>>5)%this.size32,i=32-t,h=1-(i>>>5);this._arr.subarray(this._pos,this._pos+this.size32).reverse(),this._arr.subarray(this._pos,this._pos+r).reverse(),this._arr.subarray(this._pos+r,this._pos+this.size32).reverse();let _=this.size32-1;const e=this._arr[this._pos+_];do{this._arr[this._pos+_]=this._arr[this._pos+_]<<t|h*this._arr[this._pos+_-1]>>>i}while(--_>0);this._arr[this._pos]=this._arr[this._pos]<<t|h*e>>>i}_addEq(s){let t=0,r=0;do{const i=this._arr[this._pos+t]+s._arr[s._pos+t]+r;r=i>4294967295?1:0,this._arr[this._pos+t]=i}while(++t<this.size32)}_negEq(){this._notEq();let s=0;do{if(this._arr[this._pos+s]+=1,0!=this._arr[this._pos+s])break}while(++s<this.size32)}_subEq(s){let t=0,r=0;do{const i=this._arr[this._pos+t]-s._arr[s._pos+t]-r;r=i<0?1:0,this._arr[this._pos+t]=i}while(++t<this.size32)}_mul(s){const t=new Uint16Array(this._arr.buffer,4*this._pos,2*this.size32),r=new Uint16Array(s._arr.buffer,4*s._pos,2*this.size32),i=new Uint16Array(t.length);let h=0,_=0,e=0;do{let s=0;for(let i=2*_;i>=0;i--)for(let h=2*_-i;h>=0;h--)i+h<2*_||(s+=t[i]*r[h]);s+=h,i[e++]=s,h=s/65536|0,s=0;for(let i=2*_+1;i>=0;i--)for(let h=2*_+1-i;h>=0;h--)i+h<2*_+1||(s+=t[i]*r[h]);s+=h,i[e++]=s,h=s/65536|0}while(++_<this.size32);return new Uint32Array(i.buffer)}eq(s){let t=0;do{if(this._arr[this._pos+t]!=s._arr[s._pos+t])return!1}while(++t<this.size32);return!0}gt(s){let t=this.size32-1;do{if(this._arr[this._pos+t]!=s._arr[s._pos+t])return this._arr[this._pos+t]>s._arr[s._pos+t]}while(--t>0);return this._arr[this._pos]>s._arr[s._pos]}gte(s){let t=this.size32-1;do{if(this._arr[this._pos+t]!=s._arr[s._pos+t])return this._arr[this._pos+t]>s._arr[s._pos+t]}while(--t>0);return this._arr[this._pos]>=s._arr[s._pos]}lt(s){let t=this.size32-1;do{if(this._arr[this._pos+t]!=s._arr[s._pos+t])return this._arr[this._pos+t]<s._arr[s._pos+t]}while(--t>0);return this._arr[this._pos]<s._arr[s._pos]}lte(s){let t=this.size32-1;do{if(this._arr[this._pos+t]!=s._arr[s._pos+t])return this._arr[this._pos+t]<s._arr[s._pos+t]}while(--t>0);return this._arr[this._pos]<=s._arr[s._pos]}toBytesBE(){const s=new Uint8Array(this._arr.slice(this._pos,this._pos+this.size32).buffer);return r.i32(s,0,this.size32),t.bytes(s,4*this.size32),s}toBytesLE(){const s=new Uint8Array(this._arr.slice(this._pos,this._pos+this.size32).buffer);return r.i32(s,0,this.size32),s}getByte(s=0){i("byteIdx",s).unsigned().atMost(4*this.size32-1).throwNot();const t=8*(3&s),r=s>>>2;return this._arr[this._pos+r]>>>t&255}toString(){return s.fromBytes(this.toBytesBE())}get[Symbol.toStringTag](){return this._name}[_](){return`${this._name}(${this.toString()})`}}