/* esm.sh - @noble/curves@1.9.7/abstract/hash-to-curve */
import{_validateObject as C,abytes as g,bytesToNumberBE as R,concatBytes as b,isBytes as j,isHash as k,utf8ToBytes as y}from"../utils.mjs";import{FpInvertBatch as L,mod as Z}from"./modular.mjs";var M=R;function d(r,t){if(x(r),x(t),r<0||r>=1<<8*t)throw new Error("invalid I2OSP input: "+r);let n=Array.from({length:t}).fill(0);for(let e=t-1;e>=0;e--)n[e]=r&255,r>>>=8;return new Uint8Array(n)}function N(r,t){let n=new Uint8Array(r.length);for(let e=0;e<r.length;e++)n[e]=r[e]^t[e];return n}function x(r){if(!Number.isSafeInteger(r))throw new Error("number expected")}function O(r){if(!j(r)&&typeof r!="string")throw new Error("DST must be Uint8Array or string");return typeof r=="string"?y(r):r}function U(r,t,n,e){g(r),x(n),t=O(t),t.length>255&&(t=e(b(y("H2C-OVERSIZE-DST-"),t)));let{outputLen:i,blockLen:o}=e,s=Math.ceil(n/i);if(n>65535||s>255)throw new Error("expand_message_xmd: invalid lenInBytes");let c=b(t,d(t.length,1)),f=d(0,o),p=d(n,2),a=new Array(s),h=e(b(f,r,p,d(0,1),c));a[0]=e(b(h,d(1,1),c));for(let u=1;u<=s;u++){let _=[N(h,a[u-1]),d(u+1,1),c];a[u]=e(b(..._))}return b(...a).slice(0,n)}function V(r,t,n,e,i){if(g(r),x(n),t=O(t),t.length>255){let o=Math.ceil(2*e/8);t=i.create({dkLen:o}).update(y("H2C-OVERSIZE-DST-")).update(t).digest()}if(n>65535||t.length>255)throw new Error("expand_message_xof: invalid lenInBytes");return i.create({dkLen:n}).update(r).update(d(n,2)).update(t).update(d(t.length,1)).digest()}function l(r,t,n){C(n,{p:"bigint",m:"number",k:"number",hash:"function"});let{p:e,k:i,m:o,hash:s,expand:c,DST:f}=n;if(!k(n.hash))throw new Error("expected valid hash");g(r),x(t);let p=e.toString(2).length,a=Math.ceil((p+i)/8),h=t*o*a,m;if(c==="xmd")m=U(r,f,h,s);else if(c==="xof")m=V(r,f,h,i,s);else if(c==="_internal_pass")m=r;else throw new Error('expand must be "xmd" or "xof"');let u=new Array(t);for(let _=0;_<t;_++){let E=new Array(o);for(let w=0;w<o;w++){let A=a*(w+_*o),v=m.subarray(A,A+a);E[w]=Z(M(v),e)}u[_]=E}return u}function T(r,t){let n=t.map(e=>Array.from(e).reverse());return(e,i)=>{let[o,s,c,f]=n.map(h=>h.reduce((m,u)=>r.add(r.mul(m,e),u))),[p,a]=L(r,[s,f],!0);return e=r.mul(o,p),i=r.mul(i,r.mul(c,a)),{x:e,y:i}}}var F=y("HashToScalar-");function z(r,t,n){if(typeof t!="function")throw new Error("mapToCurve() must be defined");function e(o){return r.fromAffine(t(o))}function i(o){let s=o.clearCofactor();return s.equals(r.ZERO)?r.ZERO:(s.assertValidity(),s)}return{defaults:n,hashToCurve(o,s){let c=Object.assign({},n,s),f=l(o,2,c),p=e(f[0]),a=e(f[1]);return i(p.add(a))},encodeToCurve(o,s){let c=n.encodeDST?{DST:n.encodeDST}:{},f=Object.assign({},n,c,s),p=l(o,1,f),a=e(p[0]);return i(a)},mapToCurve(o){if(!Array.isArray(o))throw new Error("expected array of bigints");for(let s of o)if(typeof s!="bigint")throw new Error("expected array of bigints");return i(e(o))},hashToScalar(o,s){let c=r.Fn.ORDER,f=Object.assign({},n,{p:c,m:1,DST:F},s);return l(o,1,f)[0][0]}}}export{F as _DST_scalar,z as createHasher,U as expand_message_xmd,V as expand_message_xof,l as hash_to_field,T as isogenyMap};
//# sourceMappingURL=hash-to-curve.mjs.map