jscofhe
Version:
Implements js library for CoFHE
1 lines • 69.4 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).jscofhe={})}(this,(function(t){"use strict";var e=/^-?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/i,n=Math.ceil,r=Math.floor,i="[BigNumber Error] ",s=i+"Number primitive has more than 15 significant digits: ",o=1e14,a=14,_=9007199254740991,c=[1,10,100,1e3,1e4,1e5,1e6,1e7,1e8,1e9,1e10,1e11,1e12,1e13],u=1e7,l=1e9;function h(t){var e=0|t;return t>0||t===e?e:e-1}function m(t){for(var e,n,r=1,i=t.length,s=t[0]+"";r<i;){for(e=t[r++]+"",n=a-e.length;n--;e="0"+e);s+=e}for(i=s.length;48===s.charCodeAt(--i););return s.slice(0,i+1||1)}function p(t,e){var n,r,i=t.c,s=e.c,o=t.s,a=e.s,_=t.e,c=e.e;if(!o||!a)return null;if(n=i&&!i[0],r=s&&!s[0],n||r)return n?r?0:-a:o;if(o!=a)return o;if(n=o<0,r=_==c,!i||!s)return r?0:!i^n?1:-1;if(!r)return _>c^n?1:-1;for(a=(_=i.length)<(c=s.length)?_:c,o=0;o<a;o++)if(i[o]!=s[o])return i[o]>s[o]^n?1:-1;return _==c?0:_>c^n?1:-1}function f(t,e,n,s){if(t<e||t>n||t!==r(t))throw Error(i+(s||"Argument")+("number"==typeof t?t<e||t>n?" out of range: ":" not an integer: ":" not a primitive number: ")+String(t))}function d(t){var e=t.c.length-1;return h(t.e/a)==e&&t.c[e]%2!=0}function g(t,e){return(t.length>1?t.charAt(0)+"."+t.slice(1):t)+(e<0?"e":"e+")+e}function w(t,e,n){var r,i;if(e<0){for(i=n+".";++e;i+=n);t=i+t}else if(++e>(r=t.length)){for(i=n,e-=r;--e;i+=n);t+=i}else e<r&&(t=t.slice(0,e)+"."+t.slice(e));return t}var y=function t(y){var E,b,I,B,v,N,k,R,S,T,A=Q.prototype={constructor:Q,toString:null,valueOf:null},x=new Q(1),O=20,C=4,D=-7,z=21,U=-1e7,P=1e7,M=!1,q=1,L=0,H={prefix:"",groupSize:3,secondaryGroupSize:0,groupSeparator:",",decimalSeparator:".",fractionGroupSize:0,fractionGroupSeparator:" ",suffix:""},F="0123456789abcdefghijklmnopqrstuvwxyz",V=!0;function Q(t,n){var i,o,c,u,l,h,m,p,d=this;if(!(d instanceof Q))return new Q(t,n);if(null==n){if(t&&!0===t._isBigNumber)return d.s=t.s,void(!t.c||t.e>P?d.c=d.e=null:t.e<U?d.c=[d.e=0]:(d.e=t.e,d.c=t.c.slice()));if((h="number"==typeof t)&&0*t==0){if(d.s=1/t<0?(t=-t,-1):1,t===~~t){for(u=0,l=t;l>=10;l/=10,u++);return void(u>P?d.c=d.e=null:(d.e=u,d.c=[t]))}p=String(t)}else{if(!e.test(p=String(t)))return I(d,p,h);d.s=45==p.charCodeAt(0)?(p=p.slice(1),-1):1}(u=p.indexOf("."))>-1&&(p=p.replace(".","")),(l=p.search(/e/i))>0?(u<0&&(u=l),u+=+p.slice(l+1),p=p.substring(0,l)):u<0&&(u=p.length)}else{if(f(n,2,F.length,"Base"),10==n&&V)return K(d=new Q(t),O+d.e+1,C);if(p=String(t),h="number"==typeof t){if(0*t!=0)return I(d,p,h,n);if(d.s=1/t<0?(p=p.slice(1),-1):1,Q.DEBUG&&p.replace(/^0\.0*|\./,"").length>15)throw Error(s+t)}else d.s=45===p.charCodeAt(0)?(p=p.slice(1),-1):1;for(i=F.slice(0,n),u=l=0,m=p.length;l<m;l++)if(i.indexOf(o=p.charAt(l))<0){if("."==o){if(l>u){u=m;continue}}else if(!c&&(p==p.toUpperCase()&&(p=p.toLowerCase())||p==p.toLowerCase()&&(p=p.toUpperCase()))){c=!0,l=-1,u=0;continue}return I(d,String(t),h,n)}h=!1,(u=(p=b(p,n,10,d.s)).indexOf("."))>-1?p=p.replace(".",""):u=p.length}for(l=0;48===p.charCodeAt(l);l++);for(m=p.length;48===p.charCodeAt(--m););if(p=p.slice(l,++m)){if(m-=l,h&&Q.DEBUG&&m>15&&(t>_||t!==r(t)))throw Error(s+d.s*t);if((u=u-l-1)>P)d.c=d.e=null;else if(u<U)d.c=[d.e=0];else{if(d.e=u,d.c=[],l=(u+1)%a,u<0&&(l+=a),l<m){for(l&&d.c.push(+p.slice(0,l)),m-=a;l<m;)d.c.push(+p.slice(l,l+=a));l=a-(p=p.slice(l)).length}else l-=m;for(;l--;p+="0");d.c.push(+p)}}else d.c=[d.e=0]}function G(t,e,n,r){var i,s,o,a,_;if(null==n?n=C:f(n,0,8),!t.c)return t.toString();if(i=t.c[0],o=t.e,null==e)_=m(t.c),_=1==r||2==r&&(o<=D||o>=z)?g(_,o):w(_,o,"0");else if(s=(t=K(new Q(t),e,n)).e,a=(_=m(t.c)).length,1==r||2==r&&(e<=s||s<=D)){for(;a<e;_+="0",a++);_=g(_,s)}else if(e-=o,_=w(_,s,"0"),s+1>a){if(--e>0)for(_+=".";e--;_+="0");}else if((e+=s-a)>0)for(s+1==a&&(_+=".");e--;_+="0");return t.s<0&&i?"-"+_:_}function j(t,e){for(var n,r,i=1,s=new Q(t[0]);i<t.length;i++)(!(r=new Q(t[i])).s||(n=p(s,r))===e||0===n&&s.s===e)&&(s=r);return s}function $(t,e,n){for(var r=1,i=e.length;!e[--i];e.pop());for(i=e[0];i>=10;i/=10,r++);return(n=r+n*a-1)>P?t.c=t.e=null:n<U?t.c=[t.e=0]:(t.e=n,t.c=e),t}function K(t,e,i,s){var _,u,l,h,m,p,f,d=t.c,g=c;if(d){t:{for(_=1,h=d[0];h>=10;h/=10,_++);if((u=e-_)<0)u+=a,l=e,m=d[p=0],f=r(m/g[_-l-1]%10);else if((p=n((u+1)/a))>=d.length){if(!s)break t;for(;d.length<=p;d.push(0));m=f=0,_=1,l=(u%=a)-a+1}else{for(m=h=d[p],_=1;h>=10;h/=10,_++);f=(l=(u%=a)-a+_)<0?0:r(m/g[_-l-1]%10)}if(s=s||e<0||null!=d[p+1]||(l<0?m:m%g[_-l-1]),s=i<4?(f||s)&&(0==i||i==(t.s<0?3:2)):f>5||5==f&&(4==i||s||6==i&&(u>0?l>0?m/g[_-l]:0:d[p-1])%10&1||i==(t.s<0?8:7)),e<1||!d[0])return d.length=0,s?(e-=t.e+1,d[0]=g[(a-e%a)%a],t.e=-e||0):d[0]=t.e=0,t;if(0==u?(d.length=p,h=1,p--):(d.length=p+1,h=g[a-u],d[p]=l>0?r(m/g[_-l]%g[l])*h:0),s)for(;;){if(0==p){for(u=1,l=d[0];l>=10;l/=10,u++);for(l=d[0]+=h,h=1;l>=10;l/=10,h++);u!=h&&(t.e++,d[0]==o&&(d[0]=1));break}if(d[p]+=h,d[p]!=o)break;d[p--]=0,h=1}for(u=d.length;0===d[--u];d.pop());}t.e>P?t.c=t.e=null:t.e<U&&(t.c=[t.e=0])}return t}function Y(t){var e,n=t.e;return null===n?t.toString():(e=m(t.c),e=n<=D||n>=z?g(e,n):w(e,n,"0"),t.s<0?"-"+e:e)}return Q.clone=t,Q.ROUND_UP=0,Q.ROUND_DOWN=1,Q.ROUND_CEIL=2,Q.ROUND_FLOOR=3,Q.ROUND_HALF_UP=4,Q.ROUND_HALF_DOWN=5,Q.ROUND_HALF_EVEN=6,Q.ROUND_HALF_CEIL=7,Q.ROUND_HALF_FLOOR=8,Q.EUCLID=9,Q.config=Q.set=function(t){var e,n;if(null!=t){if("object"!=typeof t)throw Error(i+"Object expected: "+t);if(t.hasOwnProperty(e="DECIMAL_PLACES")&&(f(n=t[e],0,l,e),O=n),t.hasOwnProperty(e="ROUNDING_MODE")&&(f(n=t[e],0,8,e),C=n),t.hasOwnProperty(e="EXPONENTIAL_AT")&&((n=t[e])&&n.pop?(f(n[0],-1e9,0,e),f(n[1],0,l,e),D=n[0],z=n[1]):(f(n,-1e9,l,e),D=-(z=n<0?-n:n))),t.hasOwnProperty(e="RANGE"))if((n=t[e])&&n.pop)f(n[0],-1e9,-1,e),f(n[1],1,l,e),U=n[0],P=n[1];else{if(f(n,-1e9,l,e),!n)throw Error(i+e+" cannot be zero: "+n);U=-(P=n<0?-n:n)}if(t.hasOwnProperty(e="CRYPTO")){if((n=t[e])!==!!n)throw Error(i+e+" not true or false: "+n);if(n){if("undefined"==typeof crypto||!crypto||!crypto.getRandomValues&&!crypto.randomBytes)throw M=!n,Error(i+"crypto unavailable");M=n}else M=n}if(t.hasOwnProperty(e="MODULO_MODE")&&(f(n=t[e],0,9,e),q=n),t.hasOwnProperty(e="POW_PRECISION")&&(f(n=t[e],0,l,e),L=n),t.hasOwnProperty(e="FORMAT")){if("object"!=typeof(n=t[e]))throw Error(i+e+" not an object: "+n);H=n}if(t.hasOwnProperty(e="ALPHABET")){if("string"!=typeof(n=t[e])||/^.?$|[+\-.\s]|(.).*\1/.test(n))throw Error(i+e+" invalid: "+n);V="0123456789"==n.slice(0,10),F=n}}return{DECIMAL_PLACES:O,ROUNDING_MODE:C,EXPONENTIAL_AT:[D,z],RANGE:[U,P],CRYPTO:M,MODULO_MODE:q,POW_PRECISION:L,FORMAT:H,ALPHABET:F}},Q.isBigNumber=function(t){if(!t||!0!==t._isBigNumber)return!1;if(!Q.DEBUG)return!0;var e,n,s=t.c,_=t.e,c=t.s;t:if("[object Array]"=={}.toString.call(s)){if((1===c||-1===c)&&_>=-1e9&&_<=l&&_===r(_)){if(0===s[0]){if(0===_&&1===s.length)return!0;break t}if((e=(_+1)%a)<1&&(e+=a),String(s[0]).length==e){for(e=0;e<s.length;e++)if((n=s[e])<0||n>=o||n!==r(n))break t;if(0!==n)return!0}}}else if(null===s&&null===_&&(null===c||1===c||-1===c))return!0;throw Error(i+"Invalid BigNumber: "+t)},Q.maximum=Q.max=function(){return j(arguments,-1)},Q.minimum=Q.min=function(){return j(arguments,1)},Q.random=(B=9007199254740992,v=Math.random()*B&2097151?function(){return r(Math.random()*B)}:function(){return 8388608*(1073741824*Math.random()|0)+(8388608*Math.random()|0)},function(t){var e,s,o,_,u,h=0,m=[],p=new Q(x);if(null==t?t=O:f(t,0,l),_=n(t/a),M)if(crypto.getRandomValues){for(e=crypto.getRandomValues(new Uint32Array(_*=2));h<_;)(u=131072*e[h]+(e[h+1]>>>11))>=9e15?(s=crypto.getRandomValues(new Uint32Array(2)),e[h]=s[0],e[h+1]=s[1]):(m.push(u%1e14),h+=2);h=_/2}else{if(!crypto.randomBytes)throw M=!1,Error(i+"crypto unavailable");for(e=crypto.randomBytes(_*=7);h<_;)(u=281474976710656*(31&e[h])+1099511627776*e[h+1]+4294967296*e[h+2]+16777216*e[h+3]+(e[h+4]<<16)+(e[h+5]<<8)+e[h+6])>=9e15?crypto.randomBytes(7).copy(e,h):(m.push(u%1e14),h+=7);h=_/7}if(!M)for(;h<_;)(u=v())<9e15&&(m[h++]=u%1e14);for(_=m[--h],t%=a,_&&t&&(u=c[a-t],m[h]=r(_/u)*u);0===m[h];m.pop(),h--);if(h<0)m=[o=0];else{for(o=-1;0===m[0];m.splice(0,1),o-=a);for(h=1,u=m[0];u>=10;u/=10,h++);h<a&&(o-=a-h)}return p.e=o,p.c=m,p}),Q.sum=function(){for(var t=1,e=arguments,n=new Q(e[0]);t<e.length;)n=n.plus(e[t++]);return n},b=function(){var t="0123456789";function e(t,e,n,r){for(var i,s,o=[0],a=0,_=t.length;a<_;){for(s=o.length;s--;o[s]*=e);for(o[0]+=r.indexOf(t.charAt(a++)),i=0;i<o.length;i++)o[i]>n-1&&(null==o[i+1]&&(o[i+1]=0),o[i+1]+=o[i]/n|0,o[i]%=n)}return o.reverse()}return function(n,r,i,s,o){var a,_,c,u,l,h,p,f,d=n.indexOf("."),g=O,y=C;for(d>=0&&(u=L,L=0,n=n.replace(".",""),h=(f=new Q(r)).pow(n.length-d),L=u,f.c=e(w(m(h.c),h.e,"0"),10,i,t),f.e=f.c.length),c=u=(p=e(n,r,i,o?(a=F,t):(a=t,F))).length;0==p[--u];p.pop());if(!p[0])return a.charAt(0);if(d<0?--c:(h.c=p,h.e=c,h.s=s,p=(h=E(h,f,g,y,i)).c,l=h.r,c=h.e),d=p[_=c+g+1],u=i/2,l=l||_<0||null!=p[_+1],l=y<4?(null!=d||l)&&(0==y||y==(h.s<0?3:2)):d>u||d==u&&(4==y||l||6==y&&1&p[_-1]||y==(h.s<0?8:7)),_<1||!p[0])n=l?w(a.charAt(1),-g,a.charAt(0)):a.charAt(0);else{if(p.length=_,l)for(--i;++p[--_]>i;)p[_]=0,_||(++c,p=[1].concat(p));for(u=p.length;!p[--u];);for(d=0,n="";d<=u;n+=a.charAt(p[d++]));n=w(n,c,a.charAt(0))}return n}}(),E=function(){function t(t,e,n){var r,i,s,o,a=0,_=t.length,c=e%u,l=e/u|0;for(t=t.slice();_--;)a=((i=c*(s=t[_]%u)+(r=l*s+(o=t[_]/u|0)*c)%u*u+a)/n|0)+(r/u|0)+l*o,t[_]=i%n;return a&&(t=[a].concat(t)),t}function e(t,e,n,r){var i,s;if(n!=r)s=n>r?1:-1;else for(i=s=0;i<n;i++)if(t[i]!=e[i]){s=t[i]>e[i]?1:-1;break}return s}function n(t,e,n,r){for(var i=0;n--;)t[n]-=i,i=t[n]<e[n]?1:0,t[n]=i*r+t[n]-e[n];for(;!t[0]&&t.length>1;t.splice(0,1));}return function(i,s,_,c,u){var l,m,p,f,d,g,w,y,E,b,I,B,v,N,k,R,S,T=i.s==s.s?1:-1,A=i.c,x=s.c;if(!(A&&A[0]&&x&&x[0]))return new Q(i.s&&s.s&&(A?!x||A[0]!=x[0]:x)?A&&0==A[0]||!x?0*T:T/0:NaN);for(E=(y=new Q(T)).c=[],T=_+(m=i.e-s.e)+1,u||(u=o,m=h(i.e/a)-h(s.e/a),T=T/a|0),p=0;x[p]==(A[p]||0);p++);if(x[p]>(A[p]||0)&&m--,T<0)E.push(1),f=!0;else{for(N=A.length,R=x.length,p=0,T+=2,(d=r(u/(x[0]+1)))>1&&(x=t(x,d,u),A=t(A,d,u),R=x.length,N=A.length),v=R,I=(b=A.slice(0,R)).length;I<R;b[I++]=0);S=x.slice(),S=[0].concat(S),k=x[0],x[1]>=u/2&&k++;do{if(d=0,(l=e(x,b,R,I))<0){if(B=b[0],R!=I&&(B=B*u+(b[1]||0)),(d=r(B/k))>1)for(d>=u&&(d=u-1),w=(g=t(x,d,u)).length,I=b.length;1==e(g,b,w,I);)d--,n(g,R<w?S:x,w,u),w=g.length,l=1;else 0==d&&(l=d=1),w=(g=x.slice()).length;if(w<I&&(g=[0].concat(g)),n(b,g,I,u),I=b.length,-1==l)for(;e(x,b,R,I)<1;)d++,n(b,R<I?S:x,I,u),I=b.length}else 0===l&&(d++,b=[0]);E[p++]=d,b[0]?b[I++]=A[v]||0:(b=[A[v]],I=1)}while((v++<N||null!=b[0])&&T--);f=null!=b[0],E[0]||E.splice(0,1)}if(u==o){for(p=1,T=E[0];T>=10;T/=10,p++);K(y,_+(y.e=p+m*a-1)+1,c,f)}else y.e=m,y.r=+f;return y}}(),N=/^(-?)0([xbo])(?=\w[\w.]*$)/i,k=/^([^.]+)\.$/,R=/^\.([^.]+)$/,S=/^-?(Infinity|NaN)$/,T=/^\s*\+(?=[\w.])|^\s+|\s+$/g,I=function(t,e,n,r){var s,o=n?e:e.replace(T,"");if(S.test(o))t.s=isNaN(o)?null:o<0?-1:1;else{if(!n&&(o=o.replace(N,(function(t,e,n){return s="x"==(n=n.toLowerCase())?16:"b"==n?2:8,r&&r!=s?t:e})),r&&(s=r,o=o.replace(k,"$1").replace(R,"0.$1")),e!=o))return new Q(o,s);if(Q.DEBUG)throw Error(i+"Not a"+(r?" base "+r:"")+" number: "+e);t.s=null}t.c=t.e=null},A.absoluteValue=A.abs=function(){var t=new Q(this);return t.s<0&&(t.s=1),t},A.comparedTo=function(t,e){return p(this,new Q(t,e))},A.decimalPlaces=A.dp=function(t,e){var n,r,i,s=this;if(null!=t)return f(t,0,l),null==e?e=C:f(e,0,8),K(new Q(s),t+s.e+1,e);if(!(n=s.c))return null;if(r=((i=n.length-1)-h(this.e/a))*a,i=n[i])for(;i%10==0;i/=10,r--);return r<0&&(r=0),r},A.dividedBy=A.div=function(t,e){return E(this,new Q(t,e),O,C)},A.dividedToIntegerBy=A.idiv=function(t,e){return E(this,new Q(t,e),0,1)},A.exponentiatedBy=A.pow=function(t,e){var s,o,_,c,u,l,h,m,p=this;if((t=new Q(t)).c&&!t.isInteger())throw Error(i+"Exponent not an integer: "+Y(t));if(null!=e&&(e=new Q(e)),u=t.e>14,!p.c||!p.c[0]||1==p.c[0]&&!p.e&&1==p.c.length||!t.c||!t.c[0])return m=new Q(Math.pow(+Y(p),u?t.s*(2-d(t)):+Y(t))),e?m.mod(e):m;if(l=t.s<0,e){if(e.c?!e.c[0]:!e.s)return new Q(NaN);(o=!l&&p.isInteger()&&e.isInteger())&&(p=p.mod(e))}else{if(t.e>9&&(p.e>0||p.e<-1||(0==p.e?p.c[0]>1||u&&p.c[1]>=24e7:p.c[0]<8e13||u&&p.c[0]<=9999975e7)))return c=p.s<0&&d(t)?-0:0,p.e>-1&&(c=1/c),new Q(l?1/c:c);L&&(c=n(L/a+2))}for(u?(s=new Q(.5),l&&(t.s=1),h=d(t)):h=(_=Math.abs(+Y(t)))%2,m=new Q(x);;){if(h){if(!(m=m.times(p)).c)break;c?m.c.length>c&&(m.c.length=c):o&&(m=m.mod(e))}if(_){if(0===(_=r(_/2)))break;h=_%2}else if(K(t=t.times(s),t.e+1,1),t.e>14)h=d(t);else{if(0===(_=+Y(t)))break;h=_%2}p=p.times(p),c?p.c&&p.c.length>c&&(p.c.length=c):o&&(p=p.mod(e))}return o?m:(l&&(m=x.div(m)),e?m.mod(e):c?K(m,L,C,undefined):m)},A.integerValue=function(t){var e=new Q(this);return null==t?t=C:f(t,0,8),K(e,e.e+1,t)},A.isEqualTo=A.eq=function(t,e){return 0===p(this,new Q(t,e))},A.isFinite=function(){return!!this.c},A.isGreaterThan=A.gt=function(t,e){return p(this,new Q(t,e))>0},A.isGreaterThanOrEqualTo=A.gte=function(t,e){return 1===(e=p(this,new Q(t,e)))||0===e},A.isInteger=function(){return!!this.c&&h(this.e/a)>this.c.length-2},A.isLessThan=A.lt=function(t,e){return p(this,new Q(t,e))<0},A.isLessThanOrEqualTo=A.lte=function(t,e){return-1===(e=p(this,new Q(t,e)))||0===e},A.isNaN=function(){return!this.s},A.isNegative=function(){return this.s<0},A.isPositive=function(){return this.s>0},A.isZero=function(){return!!this.c&&0==this.c[0]},A.minus=function(t,e){var n,r,i,s,_=this,c=_.s;if(e=(t=new Q(t,e)).s,!c||!e)return new Q(NaN);if(c!=e)return t.s=-e,_.plus(t);var u=_.e/a,l=t.e/a,m=_.c,p=t.c;if(!u||!l){if(!m||!p)return m?(t.s=-e,t):new Q(p?_:NaN);if(!m[0]||!p[0])return p[0]?(t.s=-e,t):new Q(m[0]?_:3==C?-0:0)}if(u=h(u),l=h(l),m=m.slice(),c=u-l){for((s=c<0)?(c=-c,i=m):(l=u,i=p),i.reverse(),e=c;e--;i.push(0));i.reverse()}else for(r=(s=(c=m.length)<(e=p.length))?c:e,c=e=0;e<r;e++)if(m[e]!=p[e]){s=m[e]<p[e];break}if(s&&(i=m,m=p,p=i,t.s=-t.s),(e=(r=p.length)-(n=m.length))>0)for(;e--;m[n++]=0);for(e=o-1;r>c;){if(m[--r]<p[r]){for(n=r;n&&!m[--n];m[n]=e);--m[n],m[r]+=o}m[r]-=p[r]}for(;0==m[0];m.splice(0,1),--l);return m[0]?$(t,m,l):(t.s=3==C?-1:1,t.c=[t.e=0],t)},A.modulo=A.mod=function(t,e){var n,r,i=this;return t=new Q(t,e),!i.c||!t.s||t.c&&!t.c[0]?new Q(NaN):!t.c||i.c&&!i.c[0]?new Q(i):(9==q?(r=t.s,t.s=1,n=E(i,t,0,3),t.s=r,n.s*=r):n=E(i,t,0,q),(t=i.minus(n.times(t))).c[0]||1!=q||(t.s=i.s),t)},A.multipliedBy=A.times=function(t,e){var n,r,i,s,_,c,l,m,p,f,d,g,w,y,E,b=this,I=b.c,B=(t=new Q(t,e)).c;if(!(I&&B&&I[0]&&B[0]))return!b.s||!t.s||I&&!I[0]&&!B||B&&!B[0]&&!I?t.c=t.e=t.s=null:(t.s*=b.s,I&&B?(t.c=[0],t.e=0):t.c=t.e=null),t;for(r=h(b.e/a)+h(t.e/a),t.s*=b.s,(l=I.length)<(f=B.length)&&(w=I,I=B,B=w,i=l,l=f,f=i),i=l+f,w=[];i--;w.push(0));for(y=o,E=u,i=f;--i>=0;){for(n=0,d=B[i]%E,g=B[i]/E|0,s=i+(_=l);s>i;)n=((m=d*(m=I[--_]%E)+(c=g*m+(p=I[_]/E|0)*d)%E*E+w[s]+n)/y|0)+(c/E|0)+g*p,w[s--]=m%y;w[s]=n}return n?++r:w.splice(0,1),$(t,w,r)},A.negated=function(){var t=new Q(this);return t.s=-t.s||null,t},A.plus=function(t,e){var n,r=this,i=r.s;if(e=(t=new Q(t,e)).s,!i||!e)return new Q(NaN);if(i!=e)return t.s=-e,r.minus(t);var s=r.e/a,_=t.e/a,c=r.c,u=t.c;if(!s||!_){if(!c||!u)return new Q(i/0);if(!c[0]||!u[0])return u[0]?t:new Q(c[0]?r:0*i)}if(s=h(s),_=h(_),c=c.slice(),i=s-_){for(i>0?(_=s,n=u):(i=-i,n=c),n.reverse();i--;n.push(0));n.reverse()}for((i=c.length)-(e=u.length)<0&&(n=u,u=c,c=n,e=i),i=0;e;)i=(c[--e]=c[e]+u[e]+i)/o|0,c[e]=o===c[e]?0:c[e]%o;return i&&(c=[i].concat(c),++_),$(t,c,_)},A.precision=A.sd=function(t,e){var n,r,i,s=this;if(null!=t&&t!==!!t)return f(t,1,l),null==e?e=C:f(e,0,8),K(new Q(s),t,e);if(!(n=s.c))return null;if(r=(i=n.length-1)*a+1,i=n[i]){for(;i%10==0;i/=10,r--);for(i=n[0];i>=10;i/=10,r++);}return t&&s.e+1>r&&(r=s.e+1),r},A.shiftedBy=function(t){return f(t,-9007199254740991,_),this.times("1e"+t)},A.squareRoot=A.sqrt=function(){var t,e,n,r,i,s=this,o=s.c,a=s.s,_=s.e,c=O+4,u=new Q("0.5");if(1!==a||!o||!o[0])return new Q(!a||a<0&&(!o||o[0])?NaN:o?s:1/0);if(0==(a=Math.sqrt(+Y(s)))||a==1/0?(((e=m(o)).length+_)%2==0&&(e+="0"),a=Math.sqrt(+e),_=h((_+1)/2)-(_<0||_%2),n=new Q(e=a==1/0?"5e"+_:(e=a.toExponential()).slice(0,e.indexOf("e")+1)+_)):n=new Q(a+""),n.c[0])for((a=(_=n.e)+c)<3&&(a=0);;)if(i=n,n=u.times(i.plus(E(s,i,c,1))),m(i.c).slice(0,a)===(e=m(n.c)).slice(0,a)){if(n.e<_&&--a,"9999"!=(e=e.slice(a-3,a+1))&&(r||"4999"!=e)){+e&&(+e.slice(1)||"5"!=e.charAt(0))||(K(n,n.e+O+2,1),t=!n.times(n).eq(s));break}if(!r&&(K(i,i.e+O+2,0),i.times(i).eq(s))){n=i;break}c+=4,a+=4,r=1}return K(n,n.e+O+1,C,t)},A.toExponential=function(t,e){return null!=t&&(f(t,0,l),t++),G(this,t,e,1)},A.toFixed=function(t,e){return null!=t&&(f(t,0,l),t=t+this.e+1),G(this,t,e)},A.toFormat=function(t,e,n){var r,s=this;if(null==n)null!=t&&e&&"object"==typeof e?(n=e,e=null):t&&"object"==typeof t?(n=t,t=e=null):n=H;else if("object"!=typeof n)throw Error(i+"Argument not an object: "+n);if(r=s.toFixed(t,e),s.c){var o,a=r.split("."),_=+n.groupSize,c=+n.secondaryGroupSize,u=n.groupSeparator||"",l=a[0],h=a[1],m=s.s<0,p=m?l.slice(1):l,f=p.length;if(c&&(o=_,_=c,c=o,f-=o),_>0&&f>0){for(o=f%_||_,l=p.substr(0,o);o<f;o+=_)l+=u+p.substr(o,_);c>0&&(l+=u+p.slice(o)),m&&(l="-"+l)}r=h?l+(n.decimalSeparator||"")+((c=+n.fractionGroupSize)?h.replace(new RegExp("\\d{"+c+"}\\B","g"),"$&"+(n.fractionGroupSeparator||"")):h):l}return(n.prefix||"")+r+(n.suffix||"")},A.toFraction=function(t){var e,n,r,s,o,_,u,l,h,p,f,d,g=this,w=g.c;if(null!=t&&(!(u=new Q(t)).isInteger()&&(u.c||1!==u.s)||u.lt(x)))throw Error(i+"Argument "+(u.isInteger()?"out of range: ":"not an integer: ")+Y(u));if(!w)return new Q(g);for(e=new Q(x),h=n=new Q(x),r=l=new Q(x),d=m(w),o=e.e=d.length-g.e-1,e.c[0]=c[(_=o%a)<0?a+_:_],t=!t||u.comparedTo(e)>0?o>0?e:h:u,_=P,P=1/0,u=new Q(d),l.c[0]=0;p=E(u,e,0,1),1!=(s=n.plus(p.times(r))).comparedTo(t);)n=r,r=s,h=l.plus(p.times(s=h)),l=s,e=u.minus(p.times(s=e)),u=s;return s=E(t.minus(n),r,0,1),l=l.plus(s.times(h)),n=n.plus(s.times(r)),l.s=h.s=g.s,f=E(h,r,o*=2,C).minus(g).abs().comparedTo(E(l,n,o,C).minus(g).abs())<1?[h,r]:[l,n],P=_,f},A.toNumber=function(){return+Y(this)},A.toPrecision=function(t,e){return null!=t&&f(t,1,l),G(this,t,e,2)},A.toString=function(t){var e,n=this,r=n.s,i=n.e;return null===i?r?(e="Infinity",r<0&&(e="-"+e)):e="NaN":(null==t?e=i<=D||i>=z?g(m(n.c),i):w(m(n.c),i,"0"):10===t&&V?e=w(m((n=K(new Q(n),O+i+1,C)).c),n.e,"0"):(f(t,2,F.length,"Base"),e=b(w(m(n.c),i,"0"),10,t,r,!0)),r<0&&n.c[0]&&(e="-"+e)),e},A.valueOf=A.toJSON=function(){return Y(this)},A._isBigNumber=!0,A[Symbol.toStringTag]="BigNumber",A[Symbol.for("nodejs.util.inspect.custom")]=A.valueOf,null!=y&&Q.set(y),Q}();const E=window.crypto;function b(t,e){if(0n===e)throw new Error("Division by zero");const n=y(t).dividedBy(e),r=BigInt(n.integerValue(y.ROUND_FLOOR).toFixed());return[r,BigInt(t-r*e)]}function I(t,e){if(0n===e)throw new Error("Division by zero");const n=t%e;return n<0n?n+e:n}function B(t,e){return I(k(t),e)}function v(t){return b(t,2n)[0]}function N(t,e){if(e<0)throw new Error(`Negative exponent ${e}`);return b(t,2n**BigInt(e))[0]}function k(t){return t<0n?BigInt(-t):BigInt(t)}function R(t,e){const n=k(t),r=k(e);return n<r?-1:n>r?1:0}function S(t){return k(t).toString(2).length}function T(t){return 0n===t?2**50:k(t).toString(2).split("").reverse().join("").indexOf("1")}function A(t){return k(t).toString(2).length}function x(t,e){if(e<0)throw new Error("Negative bit index");return 0n!=(t&1n<<BigInt(e))}function O(t,e){if(e<0)throw new Error("Negative bit index");return t|1n<<BigInt(e)}function C(t,e,n){if(e<0)throw new Error("Index cannot be negative.");const r=t<0n?-t:t,i=BigInt(e)-BigInt(n)+1n;let s;s=i>=0n?r>>i:r<<-i;return s&(1n<<BigInt(n))-1n}function D(t,e){return function(t,e){return 0n===e?t:D(e,t%e)}(k(t),k(e))}function z(t,e){const n=t<0n?-1n:1n,r=e<0n?-1n:1n;let i=k(t),s=k(e);const[o,a,_]=function(t,e){if(0n===e)return[1n,0n,t];const[n,r,i]=z(e,t%e),s=r,o=n-t/e*r;return[s,o,i]}(i,s);return[o*n,a*r,_]}function U(t,e){let n=BigInt(t);if(e<1)throw new Error("k must be greater than or equal to 1");if(n%2n==0n)throw new Error("a must be odd.");const r=2n**BigInt(e);if(n%=r,n<0n&&(n+=r),0n===n)throw new Error("Inverse does not exist");if(1n===n)return 1n;let i=1n;for(let t=1;t<e;t<<=1){let e=2n-i*n;e*=i,i=e%2n**(BigInt(t)<<1n)}return i<0n&&(i+=r),i}function P(t){if(t<0n)throw new Error("Square root of negative number");return 0n===t||1n===t?t:BigInt(y(t).squareRoot().integerValue(y.ROUND_DOWN).toFixed())}function M(t){return function(t){return P(P(t))}(k(t))}function q(t){const e=new Uint8Array(Math.ceil(S(t)/8));E.getRandomValues(e);let n=BigInt(0);for(let t=0;t<e.length;t++)n=n<<BigInt(8)|BigInt(e[t]);return n%t}function L(t,e,n){if(e<0n)throw new Error("Negative exponent not supported");let r=1n;for(t%=n;e>0n;)e%2n==1n&&(r=r*t%n),e>>=1n,t=t*t%n;return r}function H(t){return!(t<=1n)&&(2n===t||3n===t||t%2n!=0n&&t%3n!=0n&&(t<10000n?function(t){for(let e=5n;e*e<=t;e+=6n)if(t%e===0n||t%(e+2n)===0n)return!1;return!0}(t):function(t,e){let n=t-1n,r=0n;for(;n%2n==0n;)n/=2n,r++;for(let i=0;i<e;i++){let e=L(q(t-3n)+2n,n,t);if(1n===e||e===t-1n)continue;let i=!0;for(let n=1n;n<r;n++){if(e=L(e,2n,t),e===t-1n){i=!1;break}if(1n===e)return!1}if(i)return!1}return!0}(t,20)))}function F(t){if(t<2n)return 2n;if(2n===t)return 3n;let e=t%2n==0n?t+1n:t+2n;for(;;){if(H(e))return e;e+=2n}}function V(t,e){let n=BigInt(t),r=BigInt(e);if(0n===r)return 1n===k(n)?1:0;let i=1;if(n%2n==0n&&r%2n==0n)return 0;{let t=0;for(;r%2n==0n;)r/=2n,t++;if(t%2!=0){const t=(n%8n+8n)%8n;3n!==t&&5n!==t||(i=-1)}}for(r<0n&&(r=-r,n<0n&&(i=-i));;){if(0n===n)return 1n===r?i:0;let t=0;for(;n%2n==0n;)n/=2n,t++;if(t%2!=0){const t=r%8n;3n!==t&&5n!==t||(i=-i)}3n===(n%4n+4n)%4n&&3n===r%4n&&(i=-i),[n,r]=[r%k(n),k(n)]}}const Q=64,G=BigInt(2)**BigInt(Q),j=32,$=BigInt(1)<<BigInt(j),K=BigInt(1)<<BigInt(33);function Y(t,e,n,r){const i=t=>(t%G+G)%G,s=(t,e)=>(t+e)%G,o=(t,e)=>((t-e)%G+G)%G,a=(t,e)=>t*e%G,_=(t,e,n,r)=>{let i=t,s=e;const a=s<r?1n:0n;return s=o(s,r),i=o(i,n),i=o(i,a),[i,s]},c=(t,e,n,r)=>{const i=t*G+e,s=n*G+r;if(0n===s)throw new Error("Division by zero in div2");const o=i%s;return{q:i/s,rh:o/G,rl:o%G}},u=(t,e)=>{const n=t,r=e;if(0n===r)throw new Error("Division by zero in div1_simple");return{q:n/r,r:n%r}},l=t=>{if(!t)throw new Error("Assertion failed")};let h=i(t),m=i(e),p=i(n),f=i(r),d=0n,g=0n,w=0n,y=0n;const E=()=>[1,d,g,w,y];if(h<2n||p<2n)return[0,0n,0n,0n,0n];if(h>p||h==p&&m>f){if([h,m]=_(h,m,p,f),h<2)return[0,0n,0n,0n,0n];d=1n,g=1n,y=1n,w=0n}else{if([p,f]=_(p,f,h,m),p<2)return[0,0n,0n,0n,0n];d=1n,g=0n,w=1n,y=1n}let b=!1,I=!1;for(h<p&&(b=!0);;){if(!1===b){if(l(h>=p),h==p)return E();if(h<BigInt($)){h=s(h<<BigInt(j),m>>BigInt(j)),p=s(p<<BigInt(j),f>>BigInt(j));break}if(l(h>p),[h,m]=_(h,m,p,f),h<2n)return E();if(h<=p)g=s(g,d),y=s(y,w);else{const t=c(h,m,p,f);let e=t.q;if(m=t.rl,h=t.rh,h<2n)return g=s(g,a(e,d)),y=s(y,a(e,w)),E();e++,g=s(g,a(e,d)),y=s(y,a(e,w))}}else b=!1;if(l(p>=h),h==p)return E();if(p<$){h=s(h<<BigInt(j),m>>BigInt(j)),p=s(p<<BigInt(j),f>>BigInt(j)),I=!0;break}if([p,f]=_(p,f,h,m),p<2n)return E();if(p<=h)d=s(d,g),w=s(w,y);else{const t=c(p,f,h,m);let e=t.q;if(f=t.rl,p=t.rh,p<2)return d=s(d,a(e,g)),w=s(w,a(e,y)),E();e++,d=s(d,a(e,g)),w=s(w,a(e,y))}}for(;;){if(0==I){if(l(h>=p),h=o(h,p),h<K)break;if(h<=p)g=s(g,d),y=s(y,w);else{const t=u(h,p);let e=t.q;if(h=t.r,h<K){g=s(g,a(e,d)),y=s(y,a(e,w));break}e++,g=s(g,a(e,d)),y=s(y,a(e,w))}}else I=!1;if(l(p>=h),p=o(p,h),p<K)break;if(p<=h)d=s(d,g),w=s(w,y);else{const t=u(p,h);let e=t.q;if(p=t.r,p<K){d=s(d,a(e,g)),w=s(w,a(e,y));break}e++,d=s(d,a(e,g)),w=s(w,a(e,y))}}return E()}function J(t,e,n,r=!1){r&&(n*=Q);const i=(t,e)=>{const n=t=>{let e=0;for(let n=0;n<t.length&&"0"===t[n];n++)e++;return e};let r=t.toString(2),i=e.toString(2);r.length>i.length?i="0".repeat(r.length-i.length)+i:i.length>r.length&&(r="0".repeat(i.length-r.length)+r),r.length<128&&(r="0".repeat(128-r.length)+r),i.length<128&&(i="0".repeat(128-i.length)+i);let s=Math.min(n(r),n(i));return r=r.slice(s)+"0".repeat(s),i=i.slice(s)+"0".repeat(s),[BigInt("0b"+r.slice(0,Q)),BigInt("0b"+r.slice(Q,128)),BigInt("0b"+i.slice(0,Q)),BigInt("0b"+i.slice(Q,128))]},s=(t,e)=>k(t)>=BigInt(2)**BigInt(e);let o=BigInt(t),a=BigInt(e),_=!1;k(o)>k(a)&&([o,a]=[a,o],_=!0);let c=o<0n,u=a<0n;if(o=k(o),a=k(a),o<2n**BigInt(Q))return[o,a,1n,0n,0n,1n];let l=1n,h=0n,m=0n,p=1n,f=!0;for(;f;){let[t,e,r,_]=i(o,a),[c,u,d,g,w]=Y(t,e,r,_);if(0!==c){if([o,a]=[BigInt(w)*o-BigInt(d)*a,BigInt(u)*a-BigInt(g)*o],o<0n||a<0n)throw new Error("Assertion failed");f=s(o,n)||s(a,n);let t=BigInt(u)*l+BigInt(g)*h,e=BigInt(d)*l+BigInt(w)*h,r=BigInt(u)*m+BigInt(g)*p,i=BigInt(d)*m+BigInt(w)*p;l=t,h=e,m=r,p=i}else if(r<2){let t=o/a,e=o%a;o-=t*a,a=e,f=s(a,n),h+=t*l,p+=t*m}else if(t<2){let t=a/o,e=a%o;a-=t*o,o=e,f=s(o,n),l+=t*h,m+=t*p}else o<a?([o,a]=[o,a-o],f=s(a,n),l+=h,m+=p):([o,a]=[o-a,a],f=s(o,n),h+=l,p+=m)}return o=c?-o:o,a=u?-a:a,m=u!==c?-m:m,h=u!==c?-h:h,_&&([m,h]=[h,m],[l,p]=[p,l],[o,a]=[a,o]),[o,a,l,h,m,p]}class W{vec;constructor(t,e){this.vec=new Uint8Array(Math.max(S(t),S(e))+1);let n=0,r=0,i=x(t,0)?1:0,s=x(t,1)?1:0,o=x(t,2)?1:0,a=x(e,0)?1:0,_=x(e,1)?1:0,c=x(e,2)?1:0;for(let u=0;u<this.vec.length;u++){let l,h,m=Number(n==i)&&s^n,p=Number(r==a)&&_^r;n==i?l=0:(l=s^(o^s)&p?3:1,n=l>>1),r==a?h=0:(h=_^(c^_)&m?3:1,r=h>>1),this.set(u,l,h),i=s,s=o,o=x(t,u+3)?1:0,a=_,_=c,c=x(e,u+3)?1:0}0==this.vec[this.vec.length-1]&&(this.vec=this.vec.slice(0,this.vec.length-1))}size(){return this.vec.length}get(t){return t<0||t>=this.vec.length?0:this.vec[t]??0}set(t,e,n){t<0||t>=this.vec.length||(this.vec[t]=e|n<<4)}}class X{Ax;Bx;Ay;By;Cx;Cy;Dx;Dy;m00;m01;m10;m11;t0;t1;m;s;F;u;v;x;y;H;l;q;by;constructor(){this.Ax=0n,this.Bx=0n,this.Ay=0n,this.By=0n,this.Cx=0n,this.Cy=0n,this.Dx=0n,this.Dy=0n,this.m00=0n,this.m01=0n,this.m10=0n,this.m11=0n,this.t0=0n,this.t1=0n,this.m=0n,this.s=0n,this.F=0n,this.u=0n,this.v=0n,this.x=0n,this.y=0n,this.H=0n,this.l=0n,this.q=0n,this.by=0n}}class Z{a_m;b_m;c_m;constructor(t,e,n){this.a_m=t,this.b_m=e,this.c_m=n}clone(){return new Z(this.a_m,this.b_m,this.c_m)}a(){return this.a_m}b(){return this.b_m}c(){return this.c_m}discriminant(){return this.b_m**2n-4n*this.a_m*this.c_m}is_one(){return 1n==this.a_m}neg(){this.a_m!=this.c_m&&this.a_m!=this.b_m&&(this.b_m=-this.b_m)}eval(t,e){return this.a_m*t**2n+this.b_m*t*e+this.c_m*e**2n}lift(t){this.prime_to(t),this.b_m*=t,this.c_m*=t**2n,this.reduction()}lift_2exp(t){this.prime_to_2exp();let e=BigInt(t);this.b_m*=2n**e,this.c_m*=2n**(2n*e),this.reduction()}to_maximal_order(t,e,n=!0){if(t<=1n||0n==(t&t-1n))throw new Error("l must be a prime power.");if(0n===B(e,2n))throw new Error("deltaK must be odd.");this.prime_to(t);let[r,i,s]=z(t,this.a_m);this.b_m=this.b_m*r+this.a_m*i,this.set_c_from_disc(e),n&&this.reduction()}to_maximal_order_2exp(t,e,n=!0){if(0n!==B(e,2n))throw new Error("deltaK must be even.");this.prime_to_2exp();let r=BigInt(t),i=(1n-this.a_m*U(this.a_m,t))/2n**r;this.b_m=this.b_m*i,this.set_c_from_disc(e),n&&this.reduction()}kernel_representative(t,e){let n=1n,r=0n;const i=this.clone();for(i.to_maximal_order(t,e,!1),i.normalize();R(i.a_m,i.c_m)>0;){let t=r*e;r=r*i.b_m+n,n=n*i.b_m+t,i.rho()}if(1n!==i.a_m||1n!==i.b_m)throw new Error("The form is not in the kernel.");const s=D(n,r);n/=s,r/=s;return r=-r,I(function(t,e){let n=BigInt(t),r=BigInt(e);if(r<=0n)throw new Error("Modulus must be positive");if(1n===r)return 0n;if(n%=r,n<0n&&(n+=r),0n===n)throw new Error("Inverse does not exist");if(1n===n)return 1n;const[i,s,o]=z(n,r);if(1n!==o)throw new Error("Inverse does not exist");return(i%r+r)%r}(n,t)*r,t)}kernel_representative_2exp(t,e){let n=1n,r=0n;const i=new Z(this.a_m,this.b_m,this.c_m);for(i.to_maximal_order_2exp(t,e,!1),i.normalize();R(i.a_m,i.c_m)>0;){let t=r*e;r=r*i.b_m+n,n=n*i.b_m+t,i.rho()}if(1n!==i.a_m||0n!==i.b_m)throw new Error("The form is not in the kernel.");const s=T(n),o=2n**BigInt(s);n/=o,r/=o;const a=I(U(n,t),2n**BigInt(t));return r=-r,I(a*r,2n**BigInt(t))}set_c_from_disc(t){this.c_m=(this.b_m*this.b_m-t)/(4n*this.a_m)}normalize(){let[t,e]=function(t,e){if(0n===e)throw new Error("Division by zero");const n=y(t).dividedBy(e),r=BigInt(n.integerValue(y.ROUND_CEIL).toFixed());return[r,BigInt(t-r*e)]}(this.b_m,this.a_m);1n===B(t,2n)&&(e+=this.a_m),t=v(t),[this.b_m,e]=[e,this.b_m],e+=this.b_m,e=v(e),this.c_m-=t*e}rho(){[this.a_m,this.c_m]=[this.c_m,this.a_m],this.b_m=-this.b_m,this.normalize()}reduction(){let t=0;for(this.normalize();(t=R(this.a_m,this.c_m))>0;)this.rho();0==t&&this.b_m<0n&&(this.b_m=-this.b_m)}prime_to(t){let e=D(this.a_m,t);if(e>1n)if(e=D(this.c_m,t),e>1n){const t=this.a_m,e=this.b_m,n=this.c_m;this.a_m=t+e+n,this.b_m=-e-2n*t,this.c_m=t}else{const t=this.a_m;this.a_m=this.c_m,this.c_m=t,this.b_m=-this.b_m}}prime_to_2exp(){if(0n===B(this.a_m,2n))if(0n===B(this.c_m,2n)){const t=this.a_m,e=this.b_m,n=this.c_m;this.a_m=t+e+n,this.b_m=-e-2n*t,this.c_m=t}else{const t=this.a_m;this.a_m=this.c_m,this.c_m=t,this.b_m=-this.b_m}}static nucomp(t,e,n,r){let i=new X,s=new Z(0n,0n,0n);i.s=v(t.b_m+e.b_m),i.m=e.b_m-i.s,r&&([i.m,i.s]=[-i.s,-i.m]),[i.u,i.v,i.F]=z(t.a_m,e.a_m),1n==i.F?(i.Ax=1n,i.Bx=i.m*i.v,i.By=t.a()):0n===B(i.s,i.F)?(i.Ax=i.F,i.Bx=i.m*i.v,i.By=t.a()/i.Ax):([i.x,i.y,i.Ax]=z(i.F,i.s),i.H=i.F/i.Ax,i.t0=I(t.c_m,i.H),i.t1=I(e.c_m,i.H),i.t0=I(i.t0*i.v+i.t1*i.u,i.H),i.l=I(i.t0*i.y,i.H),i.By=t.a_m/i.Ax,i.Bx=(i.v*i.m+i.l*i.By)/i.H),i.Cy=e.a_m/i.Ax,i.Dy=i.s/i.Ax,i.Bx=I(i.Bx,i.By),i.by=i.By;const o=Math.ceil(A(n)/Q);return[i.Bx,i.by,i.m00,i.m01,i.m10,i.m11]=J(i.Bx,i.by,o),i.Ay=i.m10*i.Ax*-1n,i.Cx=(i.Bx*i.Cy-i.m*i.m11)/i.By,0n==i.Bx?i.Cy=(e.a_m*i.by-i.Ay*i.m)/t.a_m:i.Cy=(i.Cx*i.by+i.m)/i.Bx,i.Dx=(i.Bx*i.Dy-e.c_m*i.m11)/i.By,i.Dy-=i.Dx*i.m10,i.Dy/=i.m11,i.Ax*=i.m11,s.a_m=i.by*i.Cy-i.Ay*i.Dy,s.c_m=i.Bx*i.Cx-i.Ax*i.Dx,s.b_m=i.Ax*i.Dy+i.Ay*i.Dx-i.Bx*i.Cy-i.by*i.Cx,s.reduction(),s}static nudupl(t,e){let n=new X,r=new Z(0n,0n,0n);[n.m11,n.m01,n.Ax]=z(t.a_m,t.b_m),1n!==n.Ax?(r.a_m=t.a_m/n.Ax,r.b_m=t.b_m/n.Ax):(r.a_m=t.a_m,r.b_m=t.b_m),n.Dx=-t.c_m*n.m11,r.c_m=t.c_m*n.m01,[n.t0,r.c_m]=b(r.c_m,r.a_m),n.Dx=n.Dx-n.t0*r.b_m;const i=Math.ceil(A(e)/Q);return[r.c_m,r.a_m,n.m00,n.m01,n.m10,n.m11]=J(r.c_m,r.a_m,i),n.t1=-n.Ax*n.m10,n.Ax=n.Ax*n.m11,n.t0=n.Dx*n.m11-r.b_m*n.m01,r.b_m=r.b_m*n.m00-n.Dx*n.m10,[n.Dx,n.t0]=[n.t0,n.Dx],n.t0=r.a_m*r.c_m,r.a_m=r.a_m**2n-n.t1*r.b_m,r.c_m=r.c_m**2n-n.Ax*n.Dx,r.b_m=n.Ax*r.b_m+n.t1*n.Dx-2n*n.t0,r.reduction(),r}static nupow(t,e,n){let r=new Z(0n,0n,0n);if(0n===e)return new tt(t.discriminant()).one();let i=this.nudupl(t,n),s=[];s.push(new Z(t.a_m,t.b_m,t.c_m));for(let t=1;t<32;t++)s.push(this.nucomp(s[t-1],i,n,!1));let o=S(e)-1,a=0n;{let t=C(e,o,7);a=0x1n&t;let _=t+(0x1n&t),c=_.toString(2).split("").reverse().join("").indexOf("1"),u=c<7?c:6;_>>=BigInt(u),r=2n==_?i:s[Math.floor(Number(_)/2)];let l=o<6?u+1+o-7:u;for(let t=0;t<l;t++)r=this.nudupl(r,n);o-=7}for(;o>=0;){let t=C(e,o,7),_=0x1n&t;if(a==(t>>BigInt(6)&0x1n))r=this.nudupl(r,n),o-=1;else{let e=!!a,c=t+_;c=a?BigInt(128)-c:c,a=_;let u=c>0n?c.toString(2).split("").reverse().join("").indexOf("1"):6,l=u<7?u:6;c>>=BigInt(l);for(let t=0;t<7-l;t++)r=this.nudupl(r,n);r=this.nucomp(r,2n==c?i:s[Number(c>>1n)],n,e);let h=o<6?l+1+o-7:l;for(let t=0;t<h;t++)r=this.nudupl(r,n);o-=7}}return 0n!==a&&(r=this.nucomp(r,s[0],n,!0)),e<0n&&r.neg(),r}static nupow_2_forms(t,e,n,r,i){if(0n==e&&0n==r)return new tt(t.discriminant()).one();if(0n==e)return Z.nupow(n,r,i);if(0n==r)return Z.nupow(t,e,i);{let s=new Z(0n,0n,0n),o=[];o.push(new Z(t.a_m,t.b_m,t.c_m)),o.push(new Z(n.a_m,n.b_m,n.c_m)),o.push(Z.nucomp(t,n,i,!1)),o.push(Z.nucomp(t,n,i,!0));let a=new W(e,r),_=a.get(a.size()-1);1==_?s=o[0]:16==_?s=o[1]:17==_&&(s=o[2]);for(let t=a.size()-1;t>0;t--){let e=a.get(t-1);Z.nudupl(s,i),1==e?s=Z.nucomp(s,o[0],i,!1):3==e?s=Z.nucomp(s,o[0],i,!0):16==e?s=Z.nucomp(s,o[1],i,!1):48==e?s=Z.nucomp(s,o[1],i,!0):17==e?s=Z.nucomp(s,o[2],i,!1):19==e?s=Z.nucomp(s,o[3],i,!0):49==e?s=Z.nucomp(s,o[3],i,!1):51==e&&(s=Z.nucomp(s,o[2],i,!0))}return s}}static nupow_2_forms_2exp(t,e,n,r,i,s,o,a){if(0n===e)return new tt(t.discriminant()).one();if(S(e)<r)return Z.nupow(t,e,a);let _=I(e,2n**BigInt(n)),c=N(e,n),u=new W(_,c),l=new Array(40);l[0]=t,l[2]=s,l[8]=i,l[26]=o;for(let t=1,e=3,n=0;n<3;n++,t+=e,e*=3)for(let n=0;n<t;n++)l[e+n]=Z.nucomp(l[e-1],l[n],a,!1),l[e-n-2]=Z.nucomp(l[e-1],l[n],a,!0);let h=new tt(t.discriminant()).one();for(let t=u.size();t>2*r;t--){let e=u.get(t-1);if(h=Z.nudupl(h,a),0!=e){let t=0;t+=2&e?-9:1&e?9:0,t+=32&e?-27:16&e?27:0;let n=t<0;t=t<0?-t:t,h=Z.nucomp(h,l[t-1],a,n)}}for(let t=2*r;t>r;t--){let e=u.get(t-1),n=u.get(t-r-1);if(h=Z.nudupl(h,a),0!=e||0!=n){let t=0;t+=2&n?-1:1&n?1:0,t+=32&n?-3:16&n?3:0,t+=2&e?-9:1&e?9:0,t+=32&e?-27:16&e?27:0;let r=t<0;t=t<0?-t:t,h=Z.nucomp(h,l[t-1],a,r)}}return h}}class tt{kronecker(t){throw new Error("Method not implemented.")}disc_m;default_nucomp_bound_m;class_number_bound_m;constructor(t,e=0n){let n=BigInt(t);if(this.disc_m=n,this.default_nucomp_bound_m=0n,this.class_number_bound_m=e,n>0n)throw new Error("The discriminant must be negative.");let r=I(this.disc_m,4n);if(0n!=r&&1n!=r)throw new Error(`The discriminant must be congruent to 0 or 1 mod 4, disc = ${this.disc_m}`);this.default_nucomp_bound_m=M(this.disc_m)}disc(){return this.disc_m}default_nucomp_bound(){return this.default_nucomp_bound_m}one(){return new Z(1n,B(this.disc_m,2n),(B(this.disc_m,2n)-this.disc_m)/4n)}primeform(t){let e=new Z(t,function(t,e){let n=e-1n,r=0n;for(;0n===I(n,2n);)n=v(n),++r;let i=1n;for(;-1!==V(i,e);)i+=1n;let s=I(i**n,e),o=I(t**n,e),a=v(n+1n),_=I(t**a,e);for(;0n!==o&&1n!==o;){let t=0n;for(a=o;1n!==a;)t+=1n,a=I(a**2n,e);a=1n,a<<=r-t-1n;let n=I(s**a,e);r=t,s=I(n**2n,e),o=I(o*s,e),_=I(_*n,e)}return 0n===o&&(_=0n),_}(this.disc_m,t),0n);return B(e.b(),2n)!=B(this.disc_m,2n)&&(e=new Z(e.a(),t-e.b(),e.c())),e.set_c_from_disc(this.disc_m),e.reduction(),e}class_number_bound(){return 0n==this.class_number_bound_m&&(this.class_number_bound_m=this.calc_class_number_bound()),this.class_number_bound_m}calc_class_number_bound(){let t=function(t){const{precisionMargin:e=S(t),maxIterations:n=1e5,tolerance:r="1e-15"}={},i=y.config({});y.config({DECIMAL_PLACES:e});let s=y(t);if(s.isZero())throw new Error("Logarithm of zero is undefined.");const o=s.abs();if(o.isEqualTo(1))return BigInt(0);let a;try{let t=o,e=0;for(;t.comparedTo(new y(2))>=0;)t=t.sqrt(),e+=1;const i=new y(1).minus(t);let s=new y(0),_=i.times(i),c=new y(1),u=new y(0);for(let t=1;t<n;t++){const e=new y(t+1),n=new y(1).dividedBy(e);if(u=_.times(c).times(new y(2)).times(n),s=s.plus(u),_=_.times(i),c=c.plus(n),!s.isZero()&&u.dividedBy(s).abs().comparedTo(new y(r))<=0)break}s=s.times(new y(4).pow(e)),a=s.integerValue(y.ROUND_CEIL)}finally{y.config(i)}return BigInt(a.toFixed())}(this.disc_m),e=S(this.disc_m);y.config({DECIMAL_PLACES:e,POW_PRECISION:e});let n=new y(1),r=new y(0),i=new y(0);for(let e=2n;e<t;e=F(e)){let t=V(this.disc_m,e),s=0n;s=t<0?e+BigInt(-t):e-BigInt(t),r=new y(e),i=new y(s),r=r.div(i),n=n.multipliedBy(r)}let s=21n*P(k(this.disc_m));return n=n.div(88n),r=new y(s),r=r.multipliedBy(n),r=r.integerValue(y.ROUND_CEIL),this.class_number_bound_m=BigInt(r.toFixed()),this.class_number_bound_m}nucomp(t,e){return Z.nucomp(t,e,this.default_nucomp_bound_m,!1)}nucompinv(t,e){return Z.nucomp(t,e,this.default_nucomp_bound_m,!0)}nudupl(t){return Z.nudupl(t,this.default_nucomp_bound_m)}nudupl_niter(t,e){let n=this.default_nucomp_bound_m;if(e>0)for(let r=0;r<e;r++)t=Z.nudupl(t,n);return t}nupow(t,e){return Z.nupow(t,e,this.default_nucomp_bound_m)}nupow_2_forms(t,e,n,r){return Z.nupow_2_forms(t,e,n,r,this.default_nucomp_bound_m)}nupow_2_forms_2exp(t,e,n,r,i,s,o){return Z.nupow_2_forms_2exp(t,e,n,r,i,s,o,this.default_nucomp_bound_m)}}class et{random_bigint(t){if(t<0n)throw new Error("n must be greater than or equal to 0");if(0n===t)return 0n;const e=et.get_bytes(Math.ceil(S(t)/8));let n=BigInt(0);for(let t=0;t<e.length;t++)n=n<<BigInt(8)|BigInt(e[t]);return n%(t+1n)}random_bigint_2exp(t){return this.random_bigint(BigInt(2n**BigInt(t)))}random_bytes(t){if(t<0)throw new Error("n must be greater than or equal to 0");if(t>65536)throw new Error("n must be less than 65536");return 0===t?new Uint8Array(0):et.get_bytes(t)}random_bool(){return et.get_bytes(1)[0]%2==0}random_prime(t){if(t<=0)throw new Error("nbits_ must be greater than 0");if(t<=1)return 2n;if(2===t)return this.random_bool()?3n:2n;{let e;do{e=this.random_bigint_2exp(t),O(e,t-1),e=F(e)}while(S(e)!==t);return e}}static get_bytes(t){const e=new Uint8Array(t);return E.getRandomValues(e),e}}class nt{sk_m;constructor(t){this.sk_m=t}get sk(){return BigInt(this.sk_m)}static create_new(t,e){return new nt(e.random_bigint(t.secretkey_bound))}}class rt{pk_m;d_m;e_m;pk_e_precomp_m;pk_d_precomp_m;pk_de_precomp_m;constructor(t,e){this.pk_m=t,this.d_m=Math.floor((S(e.encrypt_randomness_bound)+1)/2),this.e_m=Math.floor(this.d_m/2)+1,this.pk_de_precomp_m=t.clone(),this.pk_d_precomp_m=t.clone(),this.pk_e_precomp_m=t.clone();for(let t=0;t<this.d_m+this.e_m;t++)t===this.e_m&&(this.pk_e_precomp_m=this.pk_de_precomp_m.clone()),t===this.d_m&&(this.pk_d_precomp_m=this.pk_de_precomp_m.clone()),this.pk_de_precomp_m=e.cl_g.nudupl(this.pk_de_precomp_m)}get pk(){return this.pk_m.clone()}exponentiation(t,e){return t.cl_g.nupow_2_forms_2exp(this.pk_m,e,this.d_m,this.e_m,this.pk_e_precomp_m,this.pk_d_precomp_m,this.pk_de_precomp_m)}static create_new(t,e){return new rt(t.power_of_h(e.sk),t)}}class it{m_m;constructor(t){this.m_m=t}get m(){return this.m_m}static create_new(t){return new it(t)}static decrypt(t,e,n){return new it(t.dlog_in_F(t.cl_delta.nucompinv(n.c2,t.cl_g.nupow(n.c1,e.sk))))}}class st{c1_m;c2_m;constructor(t,e){this.c1_m=t,this.c2_m=e}get c1(){return this.c1_m.clone()}get c2(){return this.c2_m.clone()}static create_new(t,e,n,r){let i=t.power_of_h(r),s=t.cl_delta.nucomp(t.power_of_f(n.m),e.exponentiation(t,r));return new st(i,s)}static add_ciphertexts(t,e,n,r,i){let s=t.cl_g.nucomp(t.cl_g.nucomp(n.c1,r.c1),t.power_of_h(i)),o=t.cl_delta.nucomp(t.cl_delta.nucomp(n.c2,r.c2),e.exponentiation(t,i));return new st(s,o)}static scal_ciphertexts(t,e,n,r,i){let s=t.cl_g.nucomp(t.power_of_h(i),t.cl_g.nupow(r.c1,n.m)),o=t.cl_delta.nucomp(t.cl_delta.nupow(r.c2,n.m),e.exponentiation(t,i));return new st(s,o)}}class ot{t_m;n_m;constructor(t,e){if(t<1)throw new Error("t must be greater than or equal to 1");if(e<t)throw new Error("n must be greater than or equal to t");if(!1===Number.isInteger(t))throw new Error("t must be an integer");if(!1===Number.isInteger(e))throw new Error("n must be an integer");this.t_m=t,this.n_m=e}get t(){return this.t_m}get n(){return this.n_m}}class at{M_m;Sie_m;constructor(t,e){if(t.length<1)throw new Error("M and Sie must have at least one row");this.M_m=t,this.Sie_m=e}get M(){return this.M_m}get Sie(){return this.Sie_m}get rows(){return this.M_m.length}get cols(){return this.M_m[0].length}}function _t(t,e){const n=t.length,r=t[0].length,i=e.length,s=e[0].length,o=n+i,a=r+s-1,_=Array.from({length:o},(()=>Array(a).fill(0)));for(let e=0;e<n;++e)_[e][0]=t[e][0];for(let t=0;t<i;++t)_[n+t][0]=e[t][0];for(let e=0;e<n;++e)for(let n=1;n<r;++n)_[e][n]=t[e][n];for(let t=0;t<i;++t)for(let i=1;i<s;++i)_[n+t][r+i-1]=e[t][i];return _}function ct(t,e){const n=t.length,r=t[0].length,i=e.length,s=e[0].length,o=n+i,a=r+s,_=Array.from({length:o},(()=>Array(a).fill(0)));for(let e=0;e<n;++e)_[e][0]=t[e][0],_[e][1]=t[e][0];for(let t=0;t<i;++t)_[n+t][1]=e[t][0];for(let e=0;e<n;++e)for(let n=1;n<r;++n)_[e][n+1]=t[e][n];for(let t=0;t<i;++t)for(let i=1;i<s;++i)_[n+t][r+i]=e[t][i];return _}function ut(t,e){if(e>t)return 0;if(0===e||e===t)return 1;e>t-e&&(e=t-e);let n=1;for(let r=0;r<e;r++)n*=t-r,n/=r+1;return n}function lt(t){const e=t.n,n=t.t,r=ut(e,n),i=function(t,e,n){const r=[[1]];let i=[[1]];for(let t=1;t<e;t++)i=ct(i,r);let s=i;for(let t=1;t<n;t++)s=_t(s,i);return s}(0,n,r),s=Array.from({length:r},(()=>Array(n).fill(0)));let o=0;for(let t=0;t<r;t++)for(let e=0;e<n;e++)s[t][e]=o++;return new at(i,s)}function ht(t,e,n,r){const i=function(t,e,n,r){const i=new Array(e);i[0]=t;for(let t=1;t<e;t++)i[t]=r.random_bigint(n.encrypt_randomness_bound);return i}(r,n.cols,t,e);return function(t,e){const n=t.M,r=t.Sie,i=t.rows,s=t.cols,o=Array.from({length:i},(()=>Array(s).fill(0n)));let a=0;for(const t of r){let r=0;for(const i of t){let t=BigInt(0);if(-1!==i)for(let r=0;r<s;r++)t+=e[r]*BigInt(n[i][r]);o[a][r]=t,r++}a++}return o}(n,i)}function mt(t,e,n){const r=function(t,e,n){let r=new Z(1n,1n,1n);for(let i=0;i<e.length;i++){const s=t.cl_delta.nupow(e[i],n[i]);r=t.cl_delta.nucomp(r,s)}return r}(t,n,function(t){const e=[BigInt(1)];for(let n=0;n<t;n++)e.push(BigInt(-1));return e}(n.length));let i=t.cl_delta.nucompinv(e.c2,r);return new it(t.dlog_in_F(i))}function pt(t,e,n){const r=[...t];let i=n-1;for(;i>=0&&r[i]===e-n+i;)i--;if(i>=0){r[i]++;for(let t=i+1;t<n;t++)r[t]=r[i]+t-i}return r}class ft{randgen;large_message_variant_used_m;n_m;k_m;m_m;cl_delta_k_m;cl_delta_m;h_m;distance_m;exponent_bound_m;d_m;e_m;h_e_precomp_m;h_d_precomp_m;h_de_precomp_m;constructor(t,e,n=0n){if(e<1)throw new Error("k must be greater than or equal to 1");this.randgen=new et,this.n_m=t,this.k_m=e,this.cl_delta_k_m=new tt(this.compute_delta_k(this.n_m),n),this.cl_delta_m=new tt(this.compute_delta(this.cl_delta_k_m.disc(),e)),this.distance_m=0n,this.m_m=2n**BigInt(this.k_m),this.large_message_variant_used_m=this.m_m**2n-1n+this.delta_k>0n;let r=2n;for(;1!==V(r,this.delta);)r=F(r);this.h_m=this.cl_delta_m.primeform(r),this.h_m=this.cl_delta_m.nudupl(this.h_m),this.h_m=this.raise_to_power_m(this.cl_delta_m,this.h_m),this.exponent_bound_m=this.cl_delta_k_m.class_number_bound(),this.distance_m=this.distance_m<2n?42n:this.distance_m,this.exponent_bound_m=this.exponent_bound_m*2n**(this.distance_m-2n),this.d_m=Math.floor((S(this.exponent_bound_m)+1)/2),this.e_m=Math.floor(this.d_m/2)+1,this.h_de_precomp_m=this.h_m.clone(),this.h_d_precomp_m=this.h_m.clone(),this.h_e_precomp_m=this.h_m.clone();for(let t=0;t<this.d_m+this.e_m;t++)t===this.e_m&&(this.h_e_precomp_m=this.h_de_precomp_m.clone()),t===this.d_m&&(this.h_d_precomp_m=this.h_de_precomp_m.clone()),this.h_de_precomp_m=this.cl_g.nudupl(this.h_de_precomp_m)}static create(t,e){return new ft(ft.random_n(t,new et),e)}get large_message_variant_used(){return this.large_message_variant_used_m}get n(){return this.n_m}get k(){return this.k_m}get m(){return this.m_m}get delta_k(){return this.cl_delta_k_m.disc()}get delta(){return this.cl_delta_m.disc()}get cl_delta_k(){return this.cl_delta_k_m}get cl_delta(){return this.cl_delta_m}get cl_g(){return this.cl_delta_m}get h(){return this.h_m}get secretkey_bound(){return this.exponent_bound_m}get cleartext_bound(){return this.m_m}get encrypt_randomness_bound(){return this.exponent_bound_m}get lambda_distance(){return this.distance_m}power_of_h(t){return this.cl_g.nupow_2_forms_2exp(this.h_m,t,this.d_m,this.e_m,this.h_e_precomp_m,this.h_d_precomp_m,this.h_de_precomp_m)}power_of_f(t){const e=T(t);if(e>=this.k_m)return this.cl_delta_m.one();{let n=1n,r=0n,i=0n,s=1n,o=this.delta_k-1n;for(let e=Number(this.k_m);e>0;e--){const a=I(n*n+r*i,this.m_m),_=I(n*r+r*s,this.m_m),c=I(i*n+s*i,this.m_m),u=I(i*r+s*s,this.m_m);n=a,r=_,i=c,s=u,x(t,e-1)&&(n=I(n*o+2n*i,this.m_m),r=I(r*o+2n*s,this.m_m),[n,i]=[i,n],[r,s]=[s,r])}let a=n+r,_=N(r,e),c=U(_,this.k_m-e);a=function(t,e){if(e<0)throw new Error("Negative exponent");const n=I(t,2n**BigInt(e));return x(n,e-1)?I(n-2n**BigInt(e),2n**BigInt(e)):n}(a*c,this.k_m-e),_=2n**BigInt(2*(this.k_m-e)),c=2n**BigInt(this.k_m-e+1)*a;let u=2n**BigInt(2*e+3)*this.n_m;return u+=a**2n,new Z(_,c,u)}}dlog_in_F(t){if(t.is_one())return 0n;let e=0n,n=0n;if(this.large_message_variant_used)n=t.kernel_representative_2exp(this.k_m+1,this.delta_k);else{const e=T(t.b())-1;n=U(N(t.b(),e+1),e)*2n**BigInt(this.k_m-e)}n=I(n,this.m_m);let r=1n;for(let t=0;t<this.k_m;t++){(n>0n?T(n):Number.MAX_SAFE_INTEGER)===t&&(e=O(e,t),n=this.F_kerphi_div(n,r)),r=this.F_kerphi_square(r)}return e}from_Cl_DeltaK_to_Cl_Delta(t){let e=t.clone();return e.lift_2exp(this.k_m+1),e=this.raise_to_power_m(this.cl_delta_m,e),3n!==I(e.a(),4n)&&3n!==I(e.c(),4n)||(e=this.cl_delta_m.nucomp(e,new Z(4n,4n,this.delta_k*2n**BigInt(2*(this.k_m-1))-1n))),e}keygen(t,e,n){if(void 0===t)return nt.create_new(this,this.randgen);if(void 0===e||void 0===n)return rt.create_new(this,t);let r=lt(new ot(e,n)),i=ht(this,this.randgen,r,t.sk),s=Array.from({length:n},(()=>Array())),o=Array.from({length:n},((t,e)=>e));for(let t=0;t<ut(n,e);t++){for(let n=0;n<e;n++)s[o[n]].push(i[t][n]);o=pt(o,n,e)}return s}create_cleartext(t){if(t<0n||t>=this.cleartext_bound)throw new Error("Cleartext out of bounds");return it.create_new(t)}encrypt(t,e,n){return void 0===n&&(n=this.randgen.random_bigint(this.encrypt_randomness_bound)),st.create_new(this,t,e,n)}decrypt(t,e){return it.decrypt(this,t,e)}partial_decryption(t,e){return function(t,e,n){return t.cl_g.nupow(e.c1,n)}(this,e,t)}combine_partial_decryption_results(t,e){return mt(this,t,e)}add_ciphertexts(t,e,n){return st.add_ciphertexts(this,t,e,n,this.randgen.random_bigint(this.encrypt_randomness_bound))}scal_ciphertexts(t,e,n){return st.scal_ciphertexts(this,t,e,n,this.randgen.random_bigint(this.encrypt_randomness_bound))}static random_n(t,e){let n=e.random_prime(Math.floor((t+1)/2));for(;3n!==I(n,8n)&&5n!==I(n,8n);)n=F(n);let r=e.random_prime(Math.floor(t/2));const i=3n===I(n,8n)?5n:3n;for(;I(r,8n)!==i;)r=F(r);return n*r}compute_delta_k(t){return-8n*t}compute_delta(t,e){return 2n**BigInt(2*(e+1))*t}raise_to_power_m(t,e){let n=e.clone();for(let e=0;e<this.k_m;e++)n=t.nudupl(n);return n}F_kerphi_square(t){return I(2n*t*U(I(I(t**2n,this.m_m)*this.delta_k+1n,this.m_m),this.k_m),this.m_m)}F_kerphi_div(t,e){return I((t-e)*U(I(1n-I(t*e,this.m_m)*this.delta_k,this.m_m),this.k_m),this.m_m)}}class dt{k_m;clhsm2k;constructor(t,e,n){if(this.k_m=e,n){if(n.k!==e)throw new Error(`CLHSM2k instance provided has k=${n.k} but expected k=${e}`);this.clhsm2k=n}else this.clhsm2k=ft.create(t,e)}static create_from_n(t,e,n=0n){return new dt(0,e,new ft(t,e,n))}keygen(t,e,n){return this.clhsm2k.keygen(t,e,n)}make_plaintext(t){return this.clhsm2k.create_cleartext(t)}get_plaintext_value(t){return new y(t.m)}encrypt(t,e){return this.clhsm2k.encrypt(t,e)}combine_partial_decryption_results(t,e){return this.clhsm2k.combine_partial_decryption_results(t,e)}deserialize_public_key(t){return new rt(this.deserialize_qfi_binary(t),this.clhsm2k)}serialize_partial_decryption_result(t){return this.serialize_qfi_binary(t)}deserialize_partial_decryption_result(t){return this.deserialize_qfi_binary(t)}serialize_ciphertext(t){const e=this.serialize_qfi_binary(t.c1),n=this.serialize_qfi_binary(t.c2),r=8+e.length+n.length,i=new Uint8Array(r);let s=BigInt(e.length);for(let t=0;t<8;t++)i[t]=Number(s&BigInt(255)),s>>=BigInt(8);for(let t=0;t<e.length;t++)i[t+8]=e[t];for(let t=0;t<n.length;t++)i[t+8+e.length]=n[t];return i}deserialize_ciphertext(t){let e=BigInt(0);for(let n=0;n<8;n++)e|=BigInt(t[n])<<BigInt(8*n);const n=Number(e),r=t.length-8-n;let i=new Uint8Array(n),s=new Uint8Array(r);for(let e=0;e<n;e++)i[e]=t[e+8];for(let e=0;e<r;e++)s[e]=t[e+8+n];return new st(this.deserialize_qfi_binary(i),this.deserialize_qfi_binary(s))}serialize_qfi_binary(t){const e=Math.ceil(A(t.a())/8),n=Math.ceil(A(t.b())/8),r=Math.ceil(A(t.c())/8),i=new Uint8Array(8+e+n+r);let s=BigInt(t.a()>0n?0:1)<<BigInt(63)|BigInt(t.b()>0n?0:1)<<BigInt(62)|BigInt(t.c()>0n?0:1)<<BigInt(61)|BigInt(e)<<BigInt(31)|BigInt(n)<<BigInt(1);for(let t=0;t<8;t++)i[t]=Number(s&BigInt(255)),s>>=BigInt(8);let o=k(t.a()),a=k(t.b()),_=k(t.c());for(let t=0;t<e;t++)i[t+8]=Number(o&BigInt(255)),o>>=BigInt(8);for(let t=0;t<n;t++)i[t+8+e]=Number(a&BigInt(255)),a>>=BigInt(8);for(let t=0;t<r;t++)i[t+8+e+n]=Number(_&BigInt(255)),_>>=BigInt(8);return i}deserialize_qfi_binary(t){let e=BigInt(0);for(let n=0;n<8;n++)e|=BigInt(t[n])<<BigInt(8*n);const n=!!Boolean(e>>BigInt(63)&BigInt(1)),r=!!Boolean(e>>BigInt(62)&BigInt(1)),i=!!Boolean(e>>BigInt(61)&BigInt(1)),s=Number(e>>BigInt(31)&BigInt(1073741823)),o=Number(e>>BigInt(1)&BigInt(1073741823)),a=t.length-8-s-o;let _=BigInt(0),c=BigInt(0),u=BigInt(0);for(let e=0;e<s;e++)_|=BigInt(t[e+8])<<BigInt(8*e);for(let e=0;e<o;e++)c|=BigInt(t[e+8+s])<<BigInt(8*e);for(let e=0;e<a;e++)u|=BigInt(t[e+8+s+o])<<BigInt(8*e);return n&&(_=-_),r&&(c=-c),i&&(u=-u),new Z(_,c,u)}}function gt(t,e,n,r){const i=[];let s=0,o=0;for(let r=0;r<t.length;r++)if(t[r]===e.charCodeAt(0)&&o<n-1){const e=r;i.push(t.slice(s,e)),s=r+1,o++}return s<t.length&&i.push(t.slice(s)),i}function wt(t,e,n=!1){const r=t.reduce(((t,e)=>t+e.length),0),i=new Uint8Array(r+(t.length+(n?0:-1))*e.length);let s=0;for(let r=0;r<t.length;r++)i.set(t[r],s),s+=t[r].length,(r<t.length-1||n)&&(i.set((new TextEncoder).encode(e),s),s+=e.length);return i}function yt(t){return(new TextEncoder).encode(t.toString())}function Et(t){return(new TextEncoder).encode(t.toString())}function bt(t){return parseInt((new TextDecoder).decode(t),10)}function It(t){return BigInt((new TextDecoder).decode(t))}function Bt(t){let e=[];for(let n=0;n<t.length;n++)e.push(t[n]);let n=String.fromCharCode.apply(null,e);return btoa(n)}function vt(t){const e=atob(t),n=e.length,r=new Uint8Array(n);for(let t=0;t<n;t++)r[t]=e.charCodeAt(t);return r}var Nt,kt,Rt,St,Tt,At,xt,Ot,Ct,Dt,zt,Ut,Pt,Mt,qt,Lt;t.ComputeOperationType=void 0,(Nt=t.ComputeOperationType||(t.ComputeOperationType={}))[Nt.UNARY=0]="UNARY",Nt[Nt.BINARY=1]="BINARY",Nt[Nt.TERNARY=2]="TERNARY",t.ComputeOperation=void 0,(kt=t.ComputeOperation||(t.ComputeOperation={}))[kt.DECRYPT=0]="DECRYPT",kt[kt.REENCRYPT=1]="REENCRYPT",kt[kt.ADD=2]="ADD",kt[kt.SUBTRACT=3]="SUBTRACT",kt[kt.MULTIPLY=4]="MULTIPLY",kt[kt.DIVIDE=5]="DIVIDE",kt[kt.LT=6]="LT",kt[kt.GT=7]="GT",kt[kt.EQ=8]="EQ",kt[kt.NEQ=9]="NEQ",kt[kt.LTEQ=10]="LTEQ",kt[kt.GTEQ=11]="GTEQ",t.DataType=void 0,(Rt=t.DataType||(t.DataType={}))[Rt.SINGLE=0]="SINGLE",Rt[Rt.TENSOR=1]="TENSOR",Rt[Rt.TENSOR_ID=2