/* esm.sh - @noble/curves@1.9.7/abstract/montgomery */
import{_validateObject as ot,abytes as ct,aInRange as U,bytesToNumberLE as D,ensureBytes as M,numberToBytesLE as st,randomBytes as rt}from"../utils.mjs";import{mod as it}from"./modular.mjs";var B=BigInt(0),l=BigInt(1),f=BigInt(2);function at(m){return ot(m,{adjustScalarBytes:"function",powPminus2:"function"}),Object.freeze({...m})}function Bt(m){let R=at(m),{P:v,type:x,adjustScalarBytes:T,powPminus2:N,randomBytes:V}=R,c=x==="x25519";if(!c&&x!=="x448")throw new Error("invalid type");let q=V||rt,F=c?255:448,i=c?32:56,H=BigInt(c?9:5),J=BigInt(c?121665:39081),E=c?f**BigInt(254):f**BigInt(447),Q=c?BigInt(8)*f**BigInt(251)-l:BigInt(4)*f**BigInt(445)-l,W=E+Q+l,e=t=>it(t,v),P=A(H);function A(t){return st(e(t),i)}function X(t){let n=M("u coordinate",t,i);return c&&(n[31]&=127),e(D(n))}function Y(t){return D(T(M("scalar",t,i)))}function p(t,n){let o=Z(X(n),Y(t));if(o===B)throw new Error("invalid private or public key received");return A(o)}function I(t){return p(t,P)}function y(t,n,o){let g=e(t*(n-o));return n=e(n-g),o=e(o+g),{x_2:n,x_3:o}}function Z(t,n){U("u",t,B,v),U("scalar",n,E,W);let o=n,g=t,s=l,r=B,a=t,u=l,d=B;for(let _=BigInt(F-1);_>=B;_--){let j=o>>_&l;d^=j,{x_2:s,x_3:a}=y(d,s,a),{x_2:r,x_3:u}=y(d,r,u),d=j;let w=s+r,S=e(w*w),K=s-r,k=e(K*K),z=S-k,nt=a+u,et=a-u,L=e(et*w),C=e(nt*K),G=L+C,O=L-C;a=e(G*G),u=e(g*e(O*O)),s=e(S*k),r=e(z*(S+e(J*z)))}({x_2:s,x_3:a}=y(d,s,a)),{x_2:r,x_3:u}=y(d,r,u);let tt=N(r);return e(s*tt)}let h={secretKey:i,publicKey:i,seed:i},b=(t=q(i))=>(ct(t,h.seed),t);function $(t){let n=b(t);return{secretKey:n,publicKey:I(n)}}return{keygen:$,getSharedSecret:(t,n)=>p(t,n),getPublicKey:t=>I(t),scalarMult:p,scalarMultBase:I,utils:{randomSecretKey:b,randomPrivateKey:b},GuBytes:P.slice(),lengths:h}}export{Bt as montgomery};
/*! Bundled license information:

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