/* esm.sh - @noble/curves@1.9.7/abstract/modular */
import{_validateObject as K,anumber as Q,bitMask as Y,bytesToNumberBE as y,bytesToNumberLE as N,ensureBytes as U,numberToBytesBE as L,numberToBytesLE as T}from"../utils.mjs";var h=BigInt(0),a=BigInt(1),E=BigInt(2),M=BigInt(3),O=BigInt(4),A=BigInt(5),k=BigInt(7),V=BigInt(8),G=BigInt(9),Z=BigInt(16);function m(t,n){let r=t%n;return r>=h?r:n+r}function F(t,n,r){return C(I(r),t,n)}function tt(t,n,r){let o=t;for(;n-- >h;)o*=o,o%=r;return o}function x(t,n){if(t===h)throw new Error("invert: expected non-zero number");if(n<=h)throw new Error("invert: expected positive modulus, got "+n);let r=m(t,n),o=n,s=h,d=a,u=a,i=h;for(;r!==h;){let f=o/r,w=o%r,g=s-u*f,e=d-i*f;o=r,r=w,s=u,d=i,u=g,i=e}if(o!==a)throw new Error("invert: does not exist");return m(s,n)}function S(t,n,r){if(!t.eql(t.sqr(n),r))throw new Error("Cannot find square root")}function j(t,n){let r=(t.ORDER+a)/O,o=t.pow(n,r);return S(t,o,n),o}function J(t,n){let r=(t.ORDER-A)/V,o=t.mul(n,E),s=t.pow(o,r),d=t.mul(n,s),u=t.mul(t.mul(d,E),s),i=t.mul(d,t.sub(u,t.ONE));return S(t,i,n),i}function W(t){let n=I(t),r=z(t),o=r(n,n.neg(n.ONE)),s=r(n,o),d=r(n,n.neg(o)),u=(t+k)/Z;return(i,l)=>{let f=i.pow(l,u),w=i.mul(f,o),g=i.mul(f,s),e=i.mul(f,d),c=i.eql(i.sqr(w),l),q=i.eql(i.sqr(g),l);f=i.cmov(f,w,c),w=i.cmov(e,g,q);let v=i.eql(i.sqr(w),l),B=i.cmov(f,w,v);return S(i,B,l),B}}function z(t){if(t<M)throw new Error("sqrt is not defined for small field");let n=t-a,r=0;for(;n%E===h;)n/=E,r++;let o=E,s=I(t);for(;b(s,o)===1;)if(o++>1e3)throw new Error("Cannot find square root: probably non-prime P");if(r===1)return j;let d=s.pow(o,n),u=(n+a)/E;return function(l,f){if(l.is0(f))return f;if(b(l,f)!==1)throw new Error("Cannot find square root");let w=r,g=l.mul(l.ONE,d),e=l.pow(f,n),c=l.pow(f,u);for(;!l.eql(e,l.ONE);){if(l.is0(e))return l.ZERO;let q=1,v=l.sqr(e);for(;!l.eql(v,l.ONE);)if(q++,v=l.sqr(v),q===w)throw new Error("Cannot find square root");let B=a<<BigInt(w-q-1),_=l.pow(g,B);w=q,g=l.sqr(_),e=l.mul(e,g),c=l.mul(c,_)}return c}}function X(t){return t%O===M?j:t%V===A?J:t%Z===G?W(t):z(t)}var et=(t,n)=>(m(t,n)&a)===a,$=["create","isValid","is0","neg","inv","sqrt","sqr","eql","add","sub","mul","pow","div","addN","subN","mulN","sqrN"];function nt(t){let n={ORDER:"bigint",MASK:"bigint",BYTES:"number",BITS:"number"},r=$.reduce((o,s)=>(o[s]="function",o),n);return K(t,r),t}function C(t,n,r){if(r<h)throw new Error("invalid exponent, negatives unsupported");if(r===h)return t.ONE;if(r===a)return n;let o=t.ONE,s=n;for(;r>h;)r&a&&(o=t.mul(o,s)),s=t.sqr(s),r>>=a;return o}function P(t,n,r=!1){let o=new Array(n.length).fill(r?t.ZERO:void 0),s=n.reduce((u,i,l)=>t.is0(i)?u:(o[l]=u,t.mul(u,i)),t.ONE),d=t.inv(s);return n.reduceRight((u,i,l)=>t.is0(i)?u:(o[l]=t.mul(u,o[l]),t.mul(u,i)),d),o}function rt(t,n,r){return t.mul(n,typeof r=="bigint"?x(r,t.ORDER):t.inv(r))}function b(t,n){let r=(t.ORDER-a)/E,o=t.pow(n,r),s=t.eql(o,t.ONE),d=t.eql(o,t.ZERO),u=t.eql(o,t.neg(t.ONE));if(!s&&!d&&!u)throw new Error("invalid Legendre symbol result");return s?1:d?0:-1}function ot(t,n){return b(t,n)===1}function p(t,n){n!==void 0&&Q(n);let r=n!==void 0?n:t.toString(2).length,o=Math.ceil(r/8);return{nBitLength:r,nByteLength:o}}function I(t,n,r=!1,o={}){if(t<=h)throw new Error("invalid field: expected ORDER > 0, got "+t);let s,d,u=!1,i;if(typeof n=="object"&&n!=null){if(o.sqrt||r)throw new Error("cannot specify opts in two arguments");let e=n;e.BITS&&(s=e.BITS),e.sqrt&&(d=e.sqrt),typeof e.isLE=="boolean"&&(r=e.isLE),typeof e.modFromBytes=="boolean"&&(u=e.modFromBytes),i=e.allowedLengths}else typeof n=="number"&&(s=n),o.sqrt&&(d=o.sqrt);let{nBitLength:l,nByteLength:f}=p(t,s);if(f>2048)throw new Error("invalid field: expected ORDER of <= 2048 bytes");let w,g=Object.freeze({ORDER:t,isLE:r,BITS:l,BYTES:f,MASK:Y(l),ZERO:h,ONE:a,allowedLengths:i,create:e=>m(e,t),isValid:e=>{if(typeof e!="bigint")throw new Error("invalid field element: expected bigint, got "+typeof e);return h<=e&&e<t},is0:e=>e===h,isValidNot0:e=>!g.is0(e)&&g.isValid(e),isOdd:e=>(e&a)===a,neg:e=>m(-e,t),eql:(e,c)=>e===c,sqr:e=>m(e*e,t),add:(e,c)=>m(e+c,t),sub:(e,c)=>m(e-c,t),mul:(e,c)=>m(e*c,t),pow:(e,c)=>C(g,e,c),div:(e,c)=>m(e*x(c,t),t),sqrN:e=>e*e,addN:(e,c)=>e+c,subN:(e,c)=>e-c,mulN:(e,c)=>e*c,inv:e=>x(e,t),sqrt:d||(e=>(w||(w=X(t)),w(g,e))),toBytes:e=>r?T(e,f):L(e,f),fromBytes:(e,c=!0)=>{if(i){if(!i.includes(e.length)||e.length>f)throw new Error("Field.fromBytes: expected "+i+" bytes, got "+e.length);let v=new Uint8Array(f);v.set(e,r?0:v.length-e.length),e=v}if(e.length!==f)throw new Error("Field.fromBytes: expected "+f+" bytes, got "+e.length);let q=r?N(e):y(e);if(u&&(q=m(q,t)),!c&&!g.isValid(q))throw new Error("invalid field element: outside of range 0..ORDER");return q},invertBatch:e=>P(g,e),cmov:(e,c,q)=>q?c:e});return Object.freeze(g)}function it(t,n){if(!t.isOdd)throw new Error("Field doesn't have isOdd");let r=t.sqrt(n);return t.isOdd(r)?r:t.neg(r)}function st(t,n){if(!t.isOdd)throw new Error("Field doesn't have isOdd");let r=t.sqrt(n);return t.isOdd(r)?t.neg(r):r}function lt(t,n,r=!1){t=U("privateHash",t);let o=t.length,s=p(n).nByteLength+8;if(s<24||o<s||o>1024)throw new Error("hashToPrivateScalar: expected "+s+"-1024 bytes of input, got "+o);let d=r?N(t):y(t);return m(d,n-a)+a}function H(t){if(typeof t!="bigint")throw new Error("field order must be bigint");let n=t.toString(2).length;return Math.ceil(n/8)}function D(t){let n=H(t);return n+Math.ceil(n/2)}function ct(t,n,r=!1){let o=t.length,s=H(n),d=D(n);if(o<16||o<d||o>1024)throw new Error("expected "+d+"-1024 bytes of input, got "+o);let u=r?N(t):y(t),i=m(u,n-a)+a;return r?T(i,s):L(i,s)}export{I as Field,rt as FpDiv,P as FpInvertBatch,ot as FpIsSquare,b as FpLegendre,C as FpPow,X as FpSqrt,st as FpSqrtEven,it as FpSqrtOdd,H as getFieldBytesLength,D as getMinHashLength,lt as hashToPrivateScalar,x as invert,et as isNegativeLE,ct as mapHashToField,m as mod,p as nLength,F as pow,tt as pow2,z as tonelliShanks,nt as validateField};
/*! Bundled license information:

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