/* esm.sh - @noble/curves@1.9.7/abstract/curve */
import{bitLen as _,bitMask as B,validateObject as z}from"../utils.mjs";import{Field as k,FpInvertBatch as G,nLength as L,validateField as Z}from"./modular.mjs";var b=BigInt(0),g=BigInt(1);function A(n,t){let r=t.negate();return n?r:t}function $(n,t){let r=G(n.Fp,t.map(e=>e.Z));return t.map((e,o)=>n.fromAffine(e.toAffine(r[o])))}function y(n,t){if(!Number.isSafeInteger(n)||n<=0||n>t)throw new Error("invalid window size, expected [1.."+t+"], got W="+n)}function E(n,t){y(n,t);let r=Math.ceil(t/n)+1,e=2**(n-1),o=2**n,s=B(n),i=BigInt(n);return{windows:r,windowSize:e,mask:s,maxNumber:o,shiftBy:i}}function N(n,t,r){let{windowSize:e,mask:o,maxNumber:s,shiftBy:i}=r,a=Number(n&o),f=n>>i;a>e&&(a-=s,f+=g);let c=t*e,l=c+Math.abs(a)-1,u=a===0,h=a<0,d=t%2!==0;return{nextN:f,offset:l,isZero:u,isNeg:h,isNegF:d,offsetF:c}}function v(n,t){if(!Array.isArray(n))throw new Error("array expected");n.forEach((r,e)=>{if(!(r instanceof t))throw new Error("invalid point at index "+e)})}function j(n,t){if(!Array.isArray(n))throw new Error("array of scalars expected");n.forEach((r,e)=>{if(!t.isValid(r))throw new Error("invalid scalar at index "+e)})}var F=new WeakMap,R=new WeakMap;function x(n){return R.get(n)||1}function S(n){if(n!==b)throw new Error("invalid wNAF")}var I=class{constructor(t,r){this.BASE=t.BASE,this.ZERO=t.ZERO,this.Fn=t.Fn,this.bits=r}_unsafeLadder(t,r,e=this.ZERO){let o=t;for(;r>b;)r&g&&(e=e.add(o)),o=o.double(),r>>=g;return e}precomputeWindow(t,r){let{windows:e,windowSize:o}=E(r,this.bits),s=[],i=t,a=i;for(let f=0;f<e;f++){a=i,s.push(a);for(let c=1;c<o;c++)a=a.add(i),s.push(a);i=a.double()}return s}wNAF(t,r,e){if(!this.Fn.isValid(e))throw new Error("invalid scalar");let o=this.ZERO,s=this.BASE,i=E(t,this.bits);for(let a=0;a<i.windows;a++){let{nextN:f,offset:c,isZero:l,isNeg:u,isNegF:h,offsetF:d}=N(e,a,i);e=f,l?s=s.add(A(h,r[d])):o=o.add(A(u,r[c]))}return S(e),{p:o,f:s}}wNAFUnsafe(t,r,e,o=this.ZERO){let s=E(t,this.bits);for(let i=0;i<s.windows&&e!==b;i++){let{nextN:a,offset:f,isZero:c,isNeg:l}=N(e,i,s);if(e=a,!c){let u=r[f];o=o.add(l?u.negate():u)}}return S(e),o}getPrecomputes(t,r,e){let o=F.get(r);return o||(o=this.precomputeWindow(r,t),t!==1&&(typeof e=="function"&&(o=e(o)),F.set(r,o))),o}cached(t,r,e){let o=x(t);return this.wNAF(o,this.getPrecomputes(o,t,e),r)}unsafe(t,r,e,o){let s=x(t);return s===1?this._unsafeLadder(t,r,o):this.wNAFUnsafe(s,this.getPrecomputes(s,t,e),r,o)}createCache(t,r){y(r,this.bits),R.set(t,r),F.delete(t)}hasCache(t){return x(t)!==1}};function K(n,t,r,e){let o=t,s=n.ZERO,i=n.ZERO;for(;r>b||e>b;)r&g&&(s=s.add(o)),e&g&&(i=i.add(o)),o=o.double(),r>>=g,e>>=g;return{p1:s,p2:i}}function q(n,t,r,e){v(r,n),j(e,t);let o=r.length,s=e.length;if(o!==s)throw new Error("arrays of points and scalars must have equal length");let i=n.ZERO,a=_(BigInt(o)),f=1;a>12?f=a-3:a>4?f=a-2:a>0&&(f=2);let c=B(f),l=new Array(Number(c)+1).fill(i),u=Math.floor((t.BITS-1)/f)*f,h=i;for(let d=u;d>=0;d-=f){l.fill(i);for(let w=0;w<s;w++){let m=e[w],O=Number(m>>BigInt(d)&c);l[O]=l[O].add(r[w])}let p=i;for(let w=l.length-1,m=i;w>0;w--)m=m.add(l[w]),p=p.add(m);if(h=h.add(p),d!==0)for(let w=0;w<f;w++)h=h.double()}return h}function P(n,t,r,e){y(e,t.BITS),v(r,n);let o=n.ZERO,s=2**e-1,i=Math.ceil(t.BITS/e),a=B(e),f=r.map(c=>{let l=[];for(let u=0,h=c;u<s;u++)l.push(h),h=h.add(c);return l});return c=>{if(j(c,t),c.length>r.length)throw new Error("array of scalars must be smaller than array of points");let l=o;for(let u=0;u<i;u++){if(l!==o)for(let d=0;d<e;d++)l=l.double();let h=BigInt(i*e-(u+1)*e);for(let d=0;d<c.length;d++){let p=c[d],w=Number(p>>h&a);w&&(l=l.add(f[d][w-1]))}}return l}}function W(n){return Z(n.Fp),z(n,{n:"bigint",h:"bigint",Gx:"field",Gy:"field"},{nBitLength:"isSafeInteger",nByteLength:"isSafeInteger"}),Object.freeze({...L(n.n,n.nBitLength),...n,p:n.Fp.ORDER})}function M(n,t,r){if(t){if(t.ORDER!==n)throw new Error("Field.ORDER must match order: Fp == p, Fn == n");return Z(t),t}else return k(n,{isLE:r})}function C(n,t,r={},e){if(e===void 0&&(e=n==="edwards"),!t||typeof t!="object")throw new Error(`expected valid ${n} CURVE object`);for(let f of["p","n","h"]){let c=t[f];if(!(typeof c=="bigint"&&c>b))throw new Error(`CURVE.${f} must be positive bigint`)}let o=M(t.p,r.Fp,e),s=M(t.n,r.Fn,e),a=["Gx","Gy","a",n==="weierstrass"?"b":"d"];for(let f of a)if(!o.isValid(t[f]))throw new Error(`CURVE.${f} must be valid field element of CURVE.Fp`);return t=Object.freeze(Object.assign({},t)),{CURVE:t,Fp:o,Fn:s}}export{C as _createCurveFields,K as mulEndoUnsafe,A as negateCt,$ as normalizeZ,q as pippenger,P as precomputeMSMUnsafe,W as validateBasic,I as wNAF};
/*! Bundled license information:

@noble/curves/esm/abstract/curve.js:
  (*! noble-curves - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
*/
//# sourceMappingURL=curve.mjs.map