g1lib
Version:
An ubiquitous static javascript toolbox lib for Ǧ1 / Duniter ecosystem with reliability in mind.
2 lines (1 loc) • 11.5 kB
JavaScript
let nt;const E=BigInt(0),u=BigInt(1),R=BigInt(2),V=BigInt("7237005577332262213973186563042994240857116359379907606001950938285454250989"),b=Object.freeze({a:BigInt(-1),d:BigInt("37095705934669439343138083508754565189542113879843219016388785533085940283555"),P:BigInt("57896044618658097711785492504343953926634992332820282019728792003956564819949"),l:V,n:V,h:BigInt(8),Gx:BigInt("15112221349535400772501151409588531511454012693041857206046113283949847762202"),Gy:BigInt("46316835694926478169428394003475163141307993866256225615783033603165251855960")}),P=BigInt("0x10000000000000000000000000000000000000000000000000000000000000000"),T=BigInt("19681161376707505956807079304988542015446066515923890162744021073123829784752");BigInt("6853475219497561581579357271197624642482790079785650197046958215289687604742");const et=BigInt("25063068953384623474111414158702152701244531502492656460079210482610430750235"),rt=BigInt("54469307008909316920995813868745141605393597292927456921205312896311721017578"),st=BigInt("1159843021668779879193775521855586647937357759715417654439879720876111806838"),ot=BigInt("40440834346308536858101042469323190826248399146238708352240133220865137265952");class w{constructor(t,n,s,i){this.x=t,this.y=n,this.z=s,this.t=i}static fromAffine(t){if(!(t instanceof m))throw new TypeError("ExtendedPoint#fromAffine: expected Point");return t.equals(m.ZERO)?w.ZERO:new w(t.x,t.y,u,r(t.x*t.y))}static toAffineBatch(t){const n=ft(t.map(s=>s.z));return t.map((s,i)=>s.toAffine(n[i]))}static normalizeZ(t){return this.toAffineBatch(t).map(this.fromAffine)}equals(t){j(t);const{x:n,y:s,z:i}=this,{x:o,y:c,z:f}=t,a=r(n*f),h=r(o*i),y=r(s*f),l=r(c*i);return a===h&&y===l}negate(){return new w(r(-this.x),this.y,this.z,r(-this.t))}double(){const{x:t,y:n,z:s}=this,{a:i}=b,o=r(t*t),c=r(n*n),f=r(R*r(s*s)),a=r(i*o),h=t+n,y=r(r(h*h)-o-c),l=a+c,d=l-f,p=a-c,g=r(y*d),B=r(l*p),x=r(y*p),I=r(d*l);return new w(g,B,I,x)}add(t){j(t);const{x:n,y:s,z:i,t:o}=this,{x:c,y:f,z:a,t:h}=t,y=r((s-n)*(f+c)),l=r((s+n)*(f-c)),d=r(l-y);if(d===E)return this.double();const p=r(i*R*h),g=r(o*R*a),B=g+p,x=l+y,I=g-p,_=r(B*d),J=r(x*I),k=r(B*I),tt=r(d*x);return new w(_,J,tt,k)}subtract(t){return this.add(t.negate())}precomputeWindow(t){const n=1+256/t,s=[];let i=this,o=i;for(let c=0;c<n;c++){o=i,s.push(o);for(let f=1;f<2**(t-1);f++)o=o.add(i),s.push(o);i=o.double()}return s}wNAF(t,n){!n&&this.equals(w.BASE)&&(n=m.BASE);const s=n&&n._WINDOW_SIZE||1;if(256%s)throw new Error("Point#wNAF: Invalid precomputation window, must be power of 2");let i=n&&M.get(n);i||(i=this.precomputeWindow(s),n&&s!==1&&(i=w.normalizeZ(i),M.set(n,i)));let o=w.ZERO,c=w.ZERO;const f=1+256/s,a=2**(s-1),h=BigInt(2**s-1),y=2**s,l=BigInt(s);for(let d=0;d<f;d++){const p=d*a;let g=Number(t&h);if(t>>=l,g>a&&(g-=y,t+=u),g===0){let B=i[p];d%2&&(B=B.negate()),c=c.add(B)}else{let B=i[p+Math.abs(g)-1];g<0&&(B=B.negate()),o=o.add(B)}}return w.normalizeZ([o,c])[0]}multiply(t,n){return this.wNAF(X(t,b.l),n)}multiplyUnsafe(t){let n=X(t,b.l,!1);const s=w.BASE,i=w.ZERO;if(n===E)return i;if(this.equals(i)||n===u)return this;if(this.equals(s))return this.wNAF(n);let o=i,c=this;for(;n>E;)n&u&&(o=o.add(c)),c=c.double(),n>>=u;return o}isSmallOrder(){return this.multiplyUnsafe(b.h).equals(w.ZERO)}isTorsionFree(){return this.multiplyUnsafe(b.l).equals(w.ZERO)}toAffine(t=q(this.z)){const{x:n,y:s,z:i}=this,o=r(n*t),c=r(s*t);if(r(i*t)!==u)throw new Error("invZ was invalid");return new m(o,c)}fromRistrettoBytes(){W()}toRistrettoBytes(){W()}fromRistrettoHash(){W()}}w.BASE=new w(b.Gx,b.Gy,u,r(b.Gx*b.Gy));w.ZERO=new w(E,u,u,E);function j(e){if(!(e instanceof w))throw new TypeError("ExtendedPoint expected")}function D(e){if(!(e instanceof A))throw new TypeError("RistrettoPoint expected")}function W(){throw new Error("Legacy method: switch to RistrettoPoint")}class A{constructor(t){this.ep=t}static calcElligatorRistrettoMap(t){const{d:n}=b,s=r(T*t*t),i=r((s+u)*st);let o=BigInt(-1);const c=r((o-n*s)*r(s+n));let{isValid:f,value:a}=Y(i,c),h=r(a*t);v(h)||(h=r(-h)),f||(a=h),f||(o=s);const y=r(o*(s-u)*ot-c),l=a*a,d=r((a+a)*c),p=r(y*et),g=r(u-l),B=r(u+l);return new w(r(d*B),r(g*p),r(p*B),r(d*g))}static hashToCurve(t){t=N(t,64);const n=H(t.slice(0,32)),s=this.calcElligatorRistrettoMap(n),i=H(t.slice(32,64)),o=this.calcElligatorRistrettoMap(i);return new A(s.add(o))}static fromHex(t){t=N(t,32);const{a:n,d:s}=b,i="RistrettoPoint.fromHex: the hex is not valid encoding of RistrettoPoint",o=H(t);if(!lt(U(o),t)||v(o))throw new Error(i);const c=r(o*o),f=r(u+n*c),a=r(u-n*c),h=r(f*f),y=r(a*a),l=r(n*s*h-y),{isValid:d,value:p}=L(r(l*y)),g=r(p*a),B=r(p*g*l);let x=r((o+o)*g);v(x)&&(x=r(-x));const I=r(f*B),_=r(x*I);if(!d||v(_)||I===E)throw new Error(i);return new A(new w(x,I,u,_))}toRawBytes(){let{x:t,y:n,z:s,t:i}=this.ep;const o=r(r(s+n)*r(s-n)),c=r(t*n),f=r(c*c),{value:a}=L(r(o*f)),h=r(a*o),y=r(a*c),l=r(h*y*i);let d;if(v(i*l)){let g=r(n*T),B=r(t*T);t=g,n=B,d=r(h*rt)}else d=y;v(t*l)&&(n=r(-n));let p=r((s-n)*d);return v(p)&&(p=r(-p)),U(p)}toHex(){return Z(this.toRawBytes())}toString(){return this.toHex()}equals(t){D(t);const n=this.ep,s=t.ep,i=r(n.x*s.y)===r(n.y*s.x),o=r(n.y*s.y)===r(n.x*s.x);return i||o}add(t){return D(t),new A(this.ep.add(t.ep))}subtract(t){return D(t),new A(this.ep.subtract(t.ep))}multiply(t){return new A(this.ep.multiply(t))}multiplyUnsafe(t){return new A(this.ep.multiplyUnsafe(t))}}A.BASE=new A(w.BASE);A.ZERO=new A(w.ZERO);const M=new WeakMap;class m{constructor(t,n){this.x=t,this.y=n}_setWindowSize(t){this._WINDOW_SIZE=t,M.delete(this)}static fromHex(t,n=!0){const{d:s,P:i}=b;t=N(t,32);const o=t.slice();o[31]=t[31]&-129;const c=z(o);if(n&&c>=i)throw new Error("Expected 0 < hex < P");if(!n&&c>=P)throw new Error("Expected 0 < hex < 2**256");const f=r(c*c),a=r(f-u),h=r(s*f+u);let{isValid:y,value:l}=Y(a,h);if(!y)throw new Error("Point.fromHex: invalid y coordinate");const d=(l&u)===u;return(t[31]&128)!==0!==d&&(l=r(-l)),new m(l,c)}static async fromPrivateKey(t){return(await $(t)).point}toRawBytes(){const t=U(this.y);return t[31]|=this.x&u?128:0,t}toHex(){return Z(this.toRawBytes())}toX25519(){const{y:t}=this,n=r((u+t)*q(u-t));return U(n)}isTorsionFree(){return w.fromAffine(this).isTorsionFree()}equals(t){return this.x===t.x&&this.y===t.y}negate(){return new m(r(-this.x),this.y)}add(t){return w.fromAffine(this).add(w.fromAffine(t)).toAffine()}subtract(t){return this.add(t.negate())}multiply(t){return w.fromAffine(this).multiply(t,this).toAffine()}}m.BASE=new m(b.Gx,b.Gy);m.ZERO=new m(E,u);function K(...e){if(!e.every(s=>s instanceof Uint8Array))throw new Error("Expected Uint8Array list");if(e.length===1)return e[0];const t=e.reduce((s,i)=>s+i.length,0),n=new Uint8Array(t);for(let s=0,i=0;s<e.length;s++){const o=e[s];n.set(o,i),i+=o.length}return n}const it=Array.from({length:256},(e,t)=>t.toString(16).padStart(2,"0"));function Z(e){if(!(e instanceof Uint8Array))throw new Error("Uint8Array expected");let t="";for(let n=0;n<e.length;n++)t+=it[e[n]];return t}function F(e){if(typeof e!="string")throw new TypeError("hexToBytes: expected string, got "+typeof e);if(e.length%2)throw new Error("hexToBytes: received invalid unpadded hex");const t=new Uint8Array(e.length/2);for(let n=0;n<t.length;n++){const s=n*2,i=e.slice(s,s+2),o=Number.parseInt(i,16);if(Number.isNaN(o)||o<0)throw new Error("Invalid byte sequence");t[n]=o}return t}function Q(e){const n=e.toString(16).padStart(64,"0");return F(n)}function U(e){return Q(e).reverse()}function v(e){return(r(e)&u)===u}function z(e){if(!(e instanceof Uint8Array))throw new Error("Expected Uint8Array");return BigInt("0x"+Z(Uint8Array.from(e).reverse()))}const ct=BigInt("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");function H(e){return r(z(e)&ct)}function r(e,t=b.P){const n=e%t;return n>=E?n:t+n}function q(e,t=b.P){if(e===E||t<=E)throw new Error(`invert: expected positive integers, got n=${e} mod=${t}`);let n=r(e,t),s=t,i=E,o=u;for(;n!==E;){const f=s/n,a=s%n,h=i-o*f;s=n,n=a,i=o,o=h}if(s!==u)throw new Error("invert: does not exist");return r(i,t)}function ft(e,t=b.P){const n=new Array(e.length),s=e.reduce((o,c,f)=>c===E?o:(n[f]=o,r(o*c,t)),u),i=q(s,t);return e.reduceRight((o,c,f)=>c===E?o:(n[f]=r(o*n[f],t),r(o*c,t)),i),n}function S(e,t){const{P:n}=b;let s=e;for(;t-- >E;)s*=s,s%=n;return s}function at(e){const{P:t}=b,n=BigInt(5),s=BigInt(10),i=BigInt(20),o=BigInt(40),c=BigInt(80),a=e*e%t*e%t,h=S(a,R)*a%t,y=S(h,u)*e%t,l=S(y,n)*y%t,d=S(l,s)*l%t,p=S(d,i)*d%t,g=S(p,o)*p%t,B=S(g,c)*g%t,x=S(B,c)*g%t,I=S(x,s)*l%t;return{pow_p_5_8:S(I,R)*e%t,b2:a}}function Y(e,t){const n=r(t*t*t),s=r(n*n*t),i=at(e*s).pow_p_5_8;let o=r(e*n*i);const c=r(t*o*o),f=o,a=r(o*T),h=c===e,y=c===r(-e),l=c===r(-e*T);return h&&(o=f),(y||l)&&(o=a),v(o)&&(o=r(-o)),{isValid:h||y,value:o}}function L(e){return Y(u,e)}function ut(e){return r(z(e),b.l)}function lt(e,t){if(e.length!==t.length)return!1;for(let n=0;n<e.length;n++)if(e[n]!==t[n])return!1;return!0}function N(e,t){const n=e instanceof Uint8Array?Uint8Array.from(e):F(e);if(typeof t=="number"&&n.length!==t)throw new Error(`Expected ${t} bytes`);return n}function X(e,t,n=!0){if(!t)throw new TypeError("Specify max value");if(typeof e=="number"&&Number.isSafeInteger(e)&&(e=BigInt(e)),typeof e=="bigint"&&e<t){if(n){if(E<e)return e}else if(E<=e)return e}throw new TypeError("Expected valid scalar: 0 < scalar < max")}function wt(e){return e[0]&=248,e[31]&=127,e[31]|=64,e}function ht(e){if(e=typeof e=="bigint"||typeof e=="number"?Q(X(e,P)):N(e),e.length!==32)throw new Error("Expected 32 bytes");return e}function yt(e){const t=wt(e.slice(0,32)),n=e.slice(32,64),s=ut(t),i=m.BASE.multiply(s),o=i.toRawBytes();return{head:t,prefix:n,scalar:s,point:i,pointBytes:o}}let G;async function $(e){return yt(await C.sha512(ht(e)))}m.BASE._setWindowSize(8);const O={node:nt,web:typeof self=="object"&&"crypto"in self?self.crypto:void 0},C={bytesToHex:Z,hexToBytes:F,concatBytes:K,getExtendedPublicKey:$,mod:r,invert:q,TORSION_SUBGROUP:["0100000000000000000000000000000000000000000000000000000000000000","c7176a703d4dd84fba3c0b760d10670f2a2053fa2c39ccc64ec7fd7792ac037a","0000000000000000000000000000000000000000000000000000000000000080","26e8958fc2b227b045c3f489f2ef98f0d5dfac05d3c63339b13802886d53fc05","ecffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f","26e8958fc2b227b045c3f489f2ef98f0d5dfac05d3c63339b13802886d53fc85","0000000000000000000000000000000000000000000000000000000000000000","c7176a703d4dd84fba3c0b760d10670f2a2053fa2c39ccc64ec7fd7792ac03fa"],hashToPrivateScalar:e=>{if(e=N(e),e.length<40||e.length>1024)throw new Error("Expected 40-1024 bytes of private key as per FIPS 186");return r(z(e),b.l-u)+u},randomBytes:(e=32)=>{if(O.web)return O.web.getRandomValues(new Uint8Array(e));throw new Error("The environment doesn't have randomBytes function")},randomPrivateKey:()=>C.randomBytes(32),sha512:async(...e)=>{const t=K(...e);if(O.web){const n=await O.web.subtle.digest("SHA-512",t.buffer);return new Uint8Array(n)}else throw new Error("The environment doesn't have sha512 function")},precompute(e=8,t=m.BASE){const n=t.equals(m.BASE)?t:new m(t.x,t.y);return n._setWindowSize(e),n.multiply(R),n},sha512Sync:void 0};Object.defineProperties(C,{sha512Sync:{configurable:!1,get(){return G},set(e){G||(G=e)}}});const dt=window.fetch,pt={fetch:dt};function Bt(e){const t=this;return t.hosts=e,t.query=n=>gt(t,n),t}async function gt(e,t){return(await pt.fetch(e.hosts[0]+t)).json()}export{Bt as DataPodClient,pt as mockableDeps};