js-randomness-predictor
Version:
Predict Math.random output in Node, Deno, Bun, Chrome, Firefox, and Safari
2 lines (1 loc) • 10.7 kB
JavaScript
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class m extends Error{constructor(t){t=t===void 0?"Cannot solve state. Unable to make accurate predictions.":t,super(t),this.name="UnsatError",Object.setPrototypeOf(this,new.target.prototype)}}class b extends Error{constructor(t){super(t),this.name="InsufficientSequenceLengthError",Object.setPrototypeOf(this,new.target.prototype)}}class y extends Error{constructor(t){super(t),this.name="UnexpectedRuntimeError",Object.setPrototypeOf(this,new.target.prototype)}}class d{static isDeno(){return typeof globalThis.Deno<"u"&&typeof Deno.version<"u"&&typeof Deno.version.deno=="string"}static isBun(){return typeof globalThis.Bun<"u"&&typeof Bun.version<"u"&&typeof process.versions.bun=="string"}static isNode(){return!this.isDeno()&&!this.isBun()&&typeof process.versions.node<"u"&&typeof process.versions.node=="string"}static isFirefox(){return typeof window<"u"&&navigator.userAgent.indexOf("Firefox")>-1}static isChrome(){return typeof window!==void 0&&navigator.userAgent.indexOf("Chrome")>-1}static isSafari(){return typeof window!==void 0&&navigator.userAgent.indexOf("Safari")>-1}static type(){if(this.isNode())return"node";if(this.isDeno())return"deno";if(this.isBun())return"bun";if(this.isChrome())return"chrome";if(this.isSafari())return"safari";if(this.isFirefox())return"firefox";throw new y}}function I(){if(d.isChrome())return"chrome";if(d.isSafari())return"safari";if(d.isFirefox())return"firefox"}async function M(){const o="https://z3-tawny.vercel.app/z3-built.js",t="https://z3-tawny.vercel.app/z3-built.wasm";try{const[s,n]=await Promise.all([fetch(o),fetch(t)]);(!s.ok||!n.ok)&&console.error("js-randomness-predictor Failed to fetch z3 files",{jsResponse:s.ok,wasmResponse:n.ok});const i=await s.text(),r=new Blob([i],{type:"text/javascript"}),h=URL.createObjectURL(r);var e={locateFile:(a,u)=>a.endsWith(".wasm")?t:u+a,onRuntimeInitialized:()=>{console.log("js-randomness-predictor initialized.")},wasmBinaryFile:t,mainScriptUrlOrBlob:h};window.Module=e;const c=document.createElement("script");c.src=h,c.id="z3-built-jsrp",document.body.appendChild(c),(function a(){try{if(typeof initZ3>"u")setTimeout(()=>a(),300);else if(window.initZ3){window.initZ3=initZ3;return}}catch(u){console.error("js-randomness-predictor something went wrong during initialize()",u)}})()}catch(s){console.error("js-randomness-predictor an error occurred during loading:",s)}}let v;async function w(){if(v)return v;if(!window.initZ3||!window.Module)throw new Error("Z3 not initialized properly! window.initZ3 and window.Module should be set via IIFE in entry!");const o=window.initZ3,t=async()=>await o(window.Module),e=await(await Promise.resolve().then(()=>require("./index-DR-KzNOG.cjs")).then(i=>i.index)).init(t),{createApi:s}=await Promise.resolve().then(()=>require("./index-DsZgriX6.cjs")).then(i=>i.index),n={...e,...s(e.Z3)};return v=n,n}function l(o){return o&0xffffffffffffffffn}class f{constructor(){}static symbolic(t){let e=t[0];e=e.xor(e.shl(23)),e=e.xor(e.lshr(17)),e=e.xor(t[1]),e=e.xor(t[1].lshr(26)),t[0]=t[1],t[1]=e}static symbolicArithmeticShiftRight(t){let e=t[0];e=e.xor(e.shl(23)),e=e.xor(e.shr(17)),e=e.xor(t[1]),e=e.xor(t[1].shr(26)),t[0]=t[1],t[1]=e}static concreteBackwards(t){let e=t[1]^t[0]>>26n^t[0];e=l(e^e>>17n^e>>34n^e>>51n),e=l(e^e<<23n^e<<46n),t[1]=t[0],t[0]=e}static concrete(t){let e=t[0];e^=l(e<<23n),e^=l(e>>17n),e^=t[1],e^=l(t[1]>>26n),t[0]=t[1],t[1]=e}static concreteArithmeticShiftRight(t){let e=l(t[0]);e^=l(e<<23n),e^=this.#s(e,17n),e^=t[1],e^=this.#s(t[1],26n),t[0]=t[1],t[1]=l(e)}static#s(t,e){const s=l(t>>e);if((t&1n<<63n)===0n)return s;const i=(1n<<e)-1n<<64n-e;return s|i}}class g{#s=Math.pow(2,53);#t=[0n,0n];constructor(t){this.sequence=t}async predictNext(){this.#t[0]===0n&&this.#t[1]===0n&&await this.#e();const t=this.#i(this.#t[0]);return f.concreteBackwards(this.#t),t}async#e(){try{const{Context:t}=await w(),e=t("main"),s=new e.Solver,n=e.BitVec.const("ss0",64),i=e.BitVec.const("ss1",64),r=[n,i],h=[...this.sequence].reverse();for(const a of h){f.symbolic(r);const u=this.#n(a);s.add(r[0].lshr(11).eq(e.BitVec.val(u,64)))}if(await s.check()!=="sat")throw new m;const c=s.model();this.#t=[c.get(n).value(),c.get(i).value()]}catch(t){return Promise.reject(t)}}#n(t){const e=Math.floor(t*this.#s);return BigInt(e)}#i(t){return Number(t>>11n)/this.#s}}class B{#s=0x1fffffffffffffn;#t=Math.pow(2,53);#e=[0n,0n];constructor(t){this.sequence=t}async predictNext(){return this.#e[0]===0n&&this.#e[1]===0n&&await this.#n(),f.concrete(this.#e),this.#r(l(this.#e[0]+this.#e[1]))}async#n(){try{const{Context:t}=await w(),e=t("main"),s=new e.Solver,n=e.BitVec.const("ss0",64),i=e.BitVec.const("ss1",64),r=[n,i];for(const a of this.sequence){f.symbolic(r);const u=this.#i(a),p=r[0].add(r[1]).and(e.BitVec.val(this.#s,64));s.add(p.eq(e.BitVec.val(u,64)))}if(await s.check()!=="sat")throw new m;const h=s.model(),c=[h.get(n).value(),h.get(i).value()];for(const a of this.sequence)f.concrete(c);this.#e=c}catch(t){return Promise.reject(t)}}#i(t){return BigInt(Math.floor(t*this.#t))}#r(t){return Number(t&this.#s)/this.#t}}class S{#s=64;#t=4;#e=0x000fffffffffffffn;#n=0x3ff0000000000000n;#i=Math.pow(2,53);#r=this.#h();#c;#o=[0n,0n];constructor(t){if(t&&t.length>=this.#s)throw new Error(`sequence.length must be less than '${this.#s}', got '${t.length}'`);if(!t){if(!d.isNode())throw new y("Expected NodeJS runtime! Unable to auto-generate sequence, please provide one.");t=Array.from({length:this.#t},Math.random)}this.sequence=t,this.#c=this.#a()}async predictNext(){this.#o[0]===0n&&this.#o[1]===0n&&await this.#f();const t=this.#c.toDouble(this.#o);return f.concreteBackwards(this.#o),t}setNodeVersion(t){this.#r=t,this.#c=this.#a()}#h(){const[t,e,s]=process.versions.node.split(".").map(Number);return{major:t,minor:e,patch:s}}#a(){const{major:t}=this.#r;return t<=11?{recoverMantissa:e=>{const s=Buffer.alloc(8);return s.writeDoubleLE(e+1,0),s.readBigUInt64LE(0)&this.#e},toDouble:e=>{const s=e[0]+e[1],n=Buffer.alloc(8);return n.writeBigUInt64LE(s&this.#e|this.#n,0),n.readDoubleLE(0)-1},constrainMantissa:(e,s,n,i)=>{const r=s[0].add(s[1]).and(i.BitVec.val(this.#e,64));n.add(r.eq(i.BitVec.val(e,64)))}}:t<=23?{recoverMantissa:e=>{const s=Buffer.alloc(8);return s.writeDoubleLE(e+1,0),s.readBigUInt64LE(0)&this.#e},toDouble:e=>{const s=Buffer.alloc(8);return s.writeBigUInt64LE(e[0]>>12n|this.#n,0),s.readDoubleLE(0)-1},constrainMantissa:(e,s,n,i)=>{n.add(s[0].lshr(12).eq(i.BitVec.val(e,64)))}}:{recoverMantissa:e=>{const s=Math.floor(e*this.#i);return BigInt(s)},toDouble:e=>Number(e[0]>>11n)/this.#i,constrainMantissa:(e,s,n,i)=>{n.add(s[0].lshr(11).eq(i.BitVec.val(e,64)))}}}async#f(){try{const{Context:t}=await w(),e=t("main"),s=new e.Solver,n=e.BitVec.const("ss0",64),i=e.BitVec.const("ss1",64),r=[n,i],h=[...this.sequence].reverse();for(const a of h){f.symbolic(r);const u=this.#c.recoverMantissa(a);this.#c.constrainMantissa(u,r,s,e)}if(await s.check()!=="sat")throw new m;const c=s.model();this.#o=[c.get(n).value(),c.get(i).value()]}catch(t){return Promise.reject(t)}}}class x{#s=6;#t=[0n,0n];#e;#n;#i=0x1fffffffffffffn;#r=Math.pow(2,53);constructor(t){if(t.length<this.#s)throw new b(`sequence length must be >= 6 : got ${t.length}`);this.#e=e=>f.symbolicArithmeticShiftRight(e),this.#n=e=>f.concreteArithmeticShiftRight(e),this.sequence=t}async predictNext(){return this.#t[0]===0n&&this.#t[1]===0n&&await this.#c(()=>this.#o(),()=>(this.#e=t=>f.symbolic(t),this.#n=t=>f.concrete(t),this.#o())),this.#n(this.#t),this.#a(l(this.#t[0]+this.#t[1]))}async#c(t,e){try{return await t()}catch{return await e()}}async#o(){try{const{Context:t}=await w(),e=t("main"),s=new e.Solver,n=e.BitVec.const("ss0",64),i=e.BitVec.const("ss1",64),r=[n,i];for(const a of this.sequence){this.#e(r);const u=this.#h(a),p=r[0].add(r[1]).and(e.BitVec.val(this.#i,64));s.add(p.eq(e.BitVec.val(u,64)))}if(await s.check()!=="sat")throw new m;const h=s.model(),c=[h.get(n).value(),h.get(i).value()];for(const a of this.sequence)this.#n(c);this.#t=c}catch(t){return Promise.reject(t)}}#h(t){return BigInt(Math.floor(t*this.#r))}#a(t){return Number(t&this.#i)/this.#r}}function N(o=1){const t=[];for(let e=0;e<o;e++)t.push(Math.random());return t}class _{#s=6;#t=[0n,0n];#e=t=>f.symbolic(t);#n=t=>f.concrete(t);#i=0x1fffffffffffffn;#r=Math.pow(2,53);constructor(t){if(t&&t.length<this.#s)throw new b(`sequence length must be >= 6 : got ${t.length}`);if(!t){if(!d.isBun())throw new y("Expected Bun runtime! Unable to auto-generate sequence, please provide one.");t=N(this.#s)}this.sequence=t}async predictNext(){return this.#t[0]===0n&&this.#t[1]===0n&&await this.#c(()=>this.#o(),()=>(this.#e=t=>f.symbolicArithmeticShiftRight(t),this.#n=t=>f.concreteArithmeticShiftRight(t),this.#o())),this.#n(this.#t),this.#a(l(this.#t[0]+this.#t[1]))}async#c(t,e){try{return await t()}catch{return await e()}}async#o(){try{const{Context:t}=await w(),e=t("main"),s=new e.Solver,n=e.BitVec.const("ss0",64),i=e.BitVec.const("ss1",64),r=[n,i];for(const a of this.sequence){this.#e(r);const u=this.#h(a),p=r[0].add(r[1]).and(e.BitVec.val(this.#i,64));s.add(p.eq(e.BitVec.val(u,64)))}if(await s.check()!=="sat")throw new m;const h=s.model(),c=[h.get(n).value(),h.get(i).value()];for(const a of this.sequence)this.#n(c);this.#t=c}catch(t){return Promise.reject(t)}}#h(t){return BigInt(Math.floor(t*this.#r))}#a(t){return Number(t&this.#i)/this.#r}}class E{#s=Math.pow(2,53);#t=[0n,0n];constructor(t){if(!t){if(!d.isDeno())throw new y("Expected Deno runtime! Unable to auto-generate sequence, please provide one.");t=Array.from({length:4},Math.random)}this.sequence=t}async predictNext(){this.#t[0]===0n&&this.#t[1]===0n&&await this.#e();const t=this.#i(this.#t[0]);return f.concreteBackwards(this.#t),t}async#e(){try{const{Context:t}=await w(),e=t("main"),s=new e.Solver,n=e.BitVec.const("ss0",64),i=e.BitVec.const("ss1",64),r=[n,i],h=[...this.sequence].reverse();for(const a of h){f.symbolic(r);const u=this.#n(a);s.add(r[0].lshr(11).eq(e.BitVec.val(u,64)))}if(await s.check()!=="sat")throw new m;const c=s.model();this.#t=[c.get(n).value(),c.get(i).value()]}catch(t){return Promise.reject(t)}}#n(t){const e=Math.floor(t*this.#s);return BigInt(e)}#i(t){return Number(t>>11n)/this.#s}}M();const A=o=>new S(o),R=o=>new B(o),T=o=>new g(o),P=o=>new x(o),C=o=>new _(o),V=o=>new E(o);exports.BunRandomnessPredictor=_;exports.ChromeRandomnessPredictor=g;exports.DenoRandomnessPredictor=E;exports.FirefoxRandomnessPredictor=B;exports.NodeRandomnessPredictor=S;exports.SafariRandomnessPredictor=x;exports.bun=C;exports.chrome=T;exports.deno=V;exports.firefox=R;exports.getCurrentBrowser=I;exports.node=A;exports.safari=P;