import argparse import struct from decimal import * import os from z3 import * MAX_UNUSED_THREADS = 2 # Calculates xs128p (XorShift128Plus) def xs128p(state0, state1): s1 = state0 & 0xFFFFFFFFFFFFFFFF s0 = state1 & 0xFFFFFFFFFFFFFFFF s1 ^= (s1 << 23) & 0xFFFFFFFFFFFFFFFF s1 ^= (s1 >> 17) & 0xFFFFFFFFFFFFFFFF s1 ^= s0 & 0xFFFFFFFFFFFFFFFF s1 ^= (s0 >> 26) & 0xFFFFFFFFFFFFFFFF state0 = state1 & 0xFFFFFFFFFFFFFFFF state1 = s1 & 0xFFFFFFFFFFFFFFFF generated = state0 & 0xFFFFFFFFFFFFFFFF return state0, state1, generated def sym_xs128p(sym_state0, sym_state1): # Symbolically represent xs128p s1 = sym_state0 s0 = sym_state1 s1 ^= (s1 << 23) s1 ^= LShR(s1, 17) s1 ^= s0 s1 ^= LShR(s0, 26) sym_state0 = sym_state1 sym_state1 = s1 # end symbolic execution return sym_state0, sym_state1 # Symbolic execution of xs128p def sym_floor_random(slvr, sym_state0, sym_state1, generated, multiple): sym_state0, sym_state1 = sym_xs128p(sym_state0, sym_state1) # "::ToDouble" calc = LShR(sym_state0, 12) """ Symbolically compatible Math.floor expression. Here's how it works: 64-bit floating point numbers are represented using IEEE 754 (https://en.wikipedia.org/wiki/Double-precision_floating-point_format) which describes how bit vectors represent decimal values. In our specific case, we're dealing with a function (Math.random) that only generates numbers in the range [0, 1). This allows us to make some assumptions in how we deal with floating point numbers (like ignoring parts of the bitvector entirely). The 64bit floating point is laid out as follows [1 bit sign][11 bit expr][52 bit "mantissa"] The formula to calculate the value is as follows: (-1)^sign * (1 + Sigma_{i=1 -> 52}(M_{52 - i} * 2^-i)) * 2^(expr - 1023) Therefore 0_01111111111_1100000000000000000000000000000000000000000000000000 is equal to "1.75" sign => 0 => ((-1) ^ 0) => 1 expr => 1023 => 2^(expr - 1023) => 1 mantissa => => (1 + sum(M_{52 - i} * 2^-i) => 1.75 1 * 1 * 1.75 = 1.75 :) Clearly we can ignore the sign as our numbers are entirely non-negative. Additionally, we know that our values are between 0 and 1 (exclusive) and therefore the expr MUST be, at most, 1023, always. What about the expr? """ lower = from_double(Decimal(generated) / Decimal(multiple)) upper = from_double((Decimal(generated) + 1) / Decimal(multiple)) lower_mantissa = (lower & 0x000FFFFFFFFFFFFF) upper_mantissa = (upper & 0x000FFFFFFFFFFFFF) upper_expr = (upper >> 52) & 0x7FF slvr.add(And(lower_mantissa <= calc, Or(upper_mantissa >= calc, upper_expr == 1024))) return sym_state0, sym_state1 def solve_instance(points, multiple, unknown_leading=False): # setup symbolic state for xorshift128+ ostate0, ostate1 = BitVecs('ostate0 ostate1', 64) sym_state0 = ostate0 sym_state1 = ostate1 set_option("parallel.enable", True) set_option("parallel.threads.max", ( max(os.cpu_count() - MAX_UNUSED_THREADS, 1))) # will use max or max cpu thread support, whatever is smaller slvr = SolverFor( "QF_BV") # This type of problem is much faster computed using QF_BV (also, if branching happens, we can use parallelization) # run symbolic xorshift128+ algorithm for three iterations # using the recovered numbers as constraints if unknown_leading: # we want to try to predict one value ahead so let's slide one unknown into the calculation sym_state0, sym_state1 = sym_xs128p(sym_state0, sym_state1) for point in points: sym_state0, sym_state1 = sym_floor_random(slvr, sym_state0, sym_state1, point, multiple) if slvr.check() == sat: # get a solved state m = slvr.model() state0 = m[ostate0].as_long() state1 = m[ostate1].as_long() return state0, state1 else: print("Failed to find a valid solution") return None, None def solve(points, multiple, lead): if lead > 0: last_state0 = None last_state1 = None for i in range(0, int(lead)): last_state0, last_state1 = solve_instance(points, multiple, True) state0, state1, output = xs128p(last_state0, last_state1) new_point = math.floor(multiple * to_double(output)) points = [new_point] + points return last_state0, last_state1 else: return solve_instance(points, multiple) def to_double(value): """ https://github.com/v8/v8/blob/master/src/base/utils/random-number-generator.h#L111 """ double_bits = (value >> 12) | 0x3FF0000000000000 return struct.unpack('d', struct.pack(' (https://github.com/v8/v8/blob/4b9b23521e6fd42373ebbcb20ebe03bf445494f9/src/math-random.cc#L64) # > vs (https://github.com/v8/v8/commit/ac66c97cfddc1e9fd89b494950ecf8a1a260bc80#diff-202872834c682708e9294600f73e4d15L115) (PRE SEPT 2018) # ----------------------------------------------------------------------------------------------------------------------------------------------------------- """ lead, multiple, gen, points = get_args() if gen is not None: state0, state1, count = gen for i in range(count): state0, state1, output = xs128p(state0, state1) print(math.floor(multiple * to_double(output))) else: state0, state1 = solve(points, multiple, lead) if state0 is not None and state1 is not None: print("{},{}".format(state0, state1)) main()