Last active
December 20, 2015 14:49
-
-
Save defuse/6149343 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Here's an EXTREMELY abstract environment where oi.js is broken. | |
# Assumptions: | |
# - There are no asynchronous interupts | |
# - Each line of javascript code takes 1 unit of time. | |
# - The context switching quantum is 10 | |
# - millis() changes on every context switch. | |
# - millis() ONLY changes during a context switch. | |
# | |
# Under these assumptions, let Q be the time remaining before the :wmlt loop | |
# guard runs for the first time. Then n = floor((Q+1)/2)%2. i.e. under these | |
# assumptions (which might be relaxed probabilistically), the output bits depend | |
# on the amount of quantum that has been used before the loop, in other words, | |
# it's like a PRNG whose # of possible states is the number of time units in | |
# a quantum. | |
# | |
# Also, Q in the next iteration is 4+(Q%2). | |
# | |
# So... If "quantum running out" dominates (and all of thsoe other assumptions | |
# are somewhat true), then it's possible to (to some unknown degree) predict | |
# future bits from previous ones.. | |
# | |
# Actually, under these assumptions you don't even need to break it, since the | |
# output is always 010101010101010101010101.... :) | |
# STATE | CODE | |
# :wt | while (true) { | |
# :neq0 | n = 0 | |
# :then | then = millis()+1 | |
# :wmlt | while (millis() < then) { | |
# :inv | n = !n | |
# | } | |
# :out | output(n) | |
# | } | |
$millis = 0 | |
$n = 0 | |
$then = 0 | |
$state = :wt | |
def contextSwitch | |
$millis += 1 | |
end | |
def nextLine | |
case $state | |
when :wt | |
$state = :neq0 | |
when :neq0 | |
$n = 0 | |
$state = :then | |
when :then | |
$then = $millis + 1 | |
$state = :wmlt | |
when :wmlt | |
if $millis < $then | |
$state = :inv | |
else | |
$state = :out | |
end | |
when :inv | |
if $n == 0 | |
$n = 1 | |
else | |
$n = 0 | |
end | |
$state = :wmlt | |
when :out | |
puts $n | |
$state = :wt | |
end | |
end | |
loop do | |
q = 10 | |
while q > 0 | |
nextLine() | |
q -= 1 | |
end | |
contextSwitch() | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Update: n = floor((Q+1)/2)%2