UNPKG

js-randomness-predictor

Version:

Predict Math.random output in Node, Deno, Bun, Chrome, Firefox, and Safari

70 lines (69 loc) 3.66 kB
import { BitVec, Solver, Context, Z3HighLevel, Z3LowLevel } from "z3-solver-jsrp"; /********************************************************************************************************* * CONSTANTS *********************************************************************************************************/ /** The env var KEY (not the value) that determines which runtime the CLI uses. */ export declare const EXECUTION_RUNTIME_ENV_VAR_KEY = "JSRP_RUNTIME"; export declare const DEFAULT_NUMBER_OF_PREDICTIONS = 10; export declare const V8_MAX_PREDICTIONS = 64; export declare const NODE_MAJOR_VERSIONS: readonly [0, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]; export declare const RUNTIMES: readonly ["node", "bun", "deno", "chrome", "firefox", "safari"]; export declare const SERVER_RUNTIMES: readonly ["node", "bun", "deno"]; export declare const JAVASCRIPT_ENGINES: readonly ["v8", "javascriptcore", "spidermonkey"]; export declare const BROWSER_RUNTIMES: readonly ["chrome", "safari", "firefox"]; export declare const IS_SERVER_RUNTIME: Record<RuntimeType, boolean>; export declare const IS_BROWSER_RUNTIME: Record<RuntimeType, boolean>; export declare const JAVASCRIPT_ENGINE_REQUIRED_SEQUENCE_LENGTH: Record<EngineType, number>; export declare const RUNTIME_ENGINE: Record<RuntimeType, EngineType>; export declare const ENGINE_RUNTIME: Record<"v8" | "javascriptcore" | "spidermonkey", ("node" | "bun" | "deno" | "chrome" | "firefox" | "safari")[]>; export declare const DEFAULT_SEQUENCE_LENGTH: Record<RuntimeType, number>; export declare const SUPPORTED_BROWSER_RUNTIMES: Record<BrowserRuntimeType, boolean>; /********************************************************************************************************* * INTERFACES *********************************************************************************************************/ export interface Predictor { sequence: number[]; predictNext(): Promise<number>; setNodeVersion?(version: SemanticVersion): void; } export interface PredictorArgs { environment: RuntimeType; sequence?: number[]; envVersion?: NodeJsMajorVersion; predictions?: number; export?: string; force?: boolean; } /********************************************************************************************************* * TYPES *********************************************************************************************************/ export type Z3Api = Z3HighLevel & Z3LowLevel; export type RuntimeType = (typeof RUNTIMES)[number]; export type EngineType = (typeof JAVASCRIPT_ENGINES)[number]; export type ServerRuntimeType = (typeof SERVER_RUNTIMES)[number]; export type BrowserRuntimeType = (typeof BROWSER_RUNTIMES)[number]; export type NodeJsMajorVersion = (typeof NODE_MAJOR_VERSIONS)[number]; export type Pair<T> = [T, T]; export type SymbolicXorShiftFn = (symbolicState: Pair<BitVec>) => void; export type ConcreteXorShiftFn = (concreteState: Pair<bigint>) => void; export type RecoverMantissaFn = (n: number) => bigint; export type ConstrainMantissaFn = (mantissa: bigint, symbolicState: Pair<BitVec>, solver: Solver, context: Context) => void; export type ToDoubleFn = (concreteState: Pair<bigint>) => number; export type SemanticVersion = { major: number; minor: number; patch: number; }; export type PredictorResult = { sequence: number[]; predictions: number[]; actual: string | number[]; isCorrect?: boolean; _warnings?: string[]; _info?: string[]; }; export type StateConversionMap = { recoverMantissa: RecoverMantissaFn; constrainMantissa: ConstrainMantissaFn; toDouble: ToDoubleFn; };