Пример #1
0
def env(k, static, z2p, z2f, z2a, a2z, f2z, p2z, pump):
    sid = ('one', 1, 2)
    static.write((('sid', sid), ('crupt', )))

    transcript = []

    def _p2z():
        while True:
            m = waits(p2z)
            transcript.append('p2z: ' + str(m.msg))
            print('p2z: ' + str(m.msg))
            pump.write('')

    def _a2z():
        while True:
            m = waits(a2z)
            transcript.append('a2z:' + str(m.msg))
            print('a2z:' + str(m.msg))
            pump.write('')

    gevent.spawn(_p2z)
    gevent.spawn(_a2z)

    z2p.write(((sid, 1), ('commit', 0)), 3)
    waits(pump)

    z2p.write(((sid, 1), ('reveal', )), 1)
    waits(pump)

    print('transcript', transcript)
    return transcript
Пример #2
0
def env1(static, z2p, z2f, z2a, z2w, a2z, p2z, f2z, w2z, pump):
    n = 3
    sid = ('one', (1, 2, 3), 3)
    static.write(('sid', sid))

    z2p.write(((sid, 1), ('input', 1)), n * (4 * n + 1))
    waits(pump, a2z)  #wait_for(a2z)

    z2a.write(('A2W', ('get-leaks', )))
    m = waits(a2z, pump)  #wait_for(a2z)
Пример #3
0
    def env_delay(self, d):
        # first send this to the emulated wrapper
        self.sim_channels['z2a'].write(('A2W', ('delay', d)))
        assert waits(self.sim_pump, self.sim_channels['a2z']).msg == 'OK'

        # now send it to the ideal world wrapper
        self.write('a2w', ('delay', d))
        assert waits(self.pump, self.channels['w2a']).msg == 'OK'
        self.internal_delay += d

        self.pump.write("dump")
Пример #4
0
    def wrapper_poll(self):
        # The ideal wrapper decreased its delay, so we do the same
        self.internal_delay -= 1
        if self.internal_delay == 0:
            self.write('a2w', ('delay', 1), 1)
            m = waits(self.channels['w2a'])
            assert m.msg == 'OK', str(m)
            self.internal_delay += 1
            self.total_extra_delay_added += 1

        # simulate the 'poll' call
        r, m = self.sim_poll()
        self.sim_get_leaks()

        self.log.debug('\t\t\033[94m poll Simulation finished\033[0m')
        if r == self.sim_channels['p2z']:
            # If we got output from the party, it outputed a committed value (or bot)
            # tell the ideal wrapper to execute the corresponding codeblock
            self.sim_party_output(m)
        elif r == self.sim_channels['a2z']:
            # Forward any crupt party output to the environment
            self.write('a2z', m.msg)
        else:
            # Something was executed from the wrapper in the simulation, we already
            # got the leaks above
            self.pump.write('dump')
Пример #5
0
    def sim_get_leaks(self):
        # Ask for leaks
        #print('ask for leaks')
        leaks = self.sim_write_and_wait('z2a', ('A2W', ('get-leaks', )), 0,
                                        'a2z')
        n = 0
        if len(leaks.msg):
            # check new schedules in in simulated wrapper
            for x in leaks.msg:
                fro, s, i = x
                if s[0] == 'schedule': n += 1
                #else: print('COOKIES')

        # add delay to ideal-world wrapper
        log.debug('\t\tAdd n={} delay to ideal world wrapper'.format(n))
        self.internal_delay += n
        if self.internal_delay == n:
            self.internal_delay += 1
            # TODO having to add +1 if internal and ideal delays are the same
            log.debug("\t\t the delays are the same")
            self.write('a2w', ('delay', n + 1))
        else:
            self.write('a2w', ('delay', n))
        m = waits(self.pump, self.channels['w2a'])
        assert m.msg == "OK", str(m.msg)

        #print('Leaks', len(leaks.msg))
        return leaks
Пример #6
0
    def sim_party_output(self, m):
        # If we got output from the party, it outputed a committed value (or bot)
        # tell the ideal wrapper to execute the corresponding codeblock
        fro, msg = m.msg
        _sid, _pid = fro
        self.log.debug(
            '\033[91m Got some output from pid={}, msg={}\033[0m'.format(
                _pid, msg))

        if isdishonest(_sid, _pid):
            self.tick(1)
            # forward this output to the environment
            self.write('a2z', msg)
            # don't do anything else since corrupt output in the ideal world doesn't
            #     get delivered
            return
        elif not self.dealer_input:
            assert len(self.internal_run_queue) == 0
            # If output and not dealer input, dealer is crupt. Call input on functonality
            self.tick(1)
            assert isdishonest(self.sid, 1)
            n = len(self.parties)
            self.write('a2p', ((self.sid, 1), ('P2F', ((self.sid, 'F_bracha'),
                                                       ('input', msg)))),
                       n * (4 * n + 1))
            m = waits(self.pump, self.channels['p2a'])
            _fro, _msg = m.msg
            self.dealer_input = msg
            assert type(msg) == int, str(msg)
            assert _msg == 'OK', str('fro={}, msg={}'.format(_fro, _msg))
            # Now get leaks, and populate self.pid_to_queue
            #leaks = self.get_ideal_wrapper_leaks()
            self.tick(1)
            self.write('a2w', ('get-leaks', ))
            m = wait_for(self.channels['w2a'])
            msg = m.msg
            pid_idx = None
            for leak in msg:
                self.tick(1)
                sender, msg, imp = leak
                if sender == (self.sid, 'F_bracha'):
                    if msg[0] == 'schedule':
                        if not pid_idx: pid_idx = 1
                        self.add_output_schedule(msg, pid_idx)
                        pid_idx += 1
            if pid_idx: assert pid_idx - 1 == len(self.parties)
        self.expect_output = True

        # If dealer gave input to the functionality
        idx = self.pid_to_queue[_pid]
        self.internal_run_queue.pop(idx)

        for p in self.pid_to_queue:
            self.tick(1)
            if p > _pid:
                i = self.pid_to_queue[p]
                self.pid_to_queue[p] -= 1
        self.write('a2w', ('exec', idx))
Пример #7
0
 def env_exec(self, rnd, idx):
     # pass the exec onto the internel wrapper and check for output by some party
     self.sim_channels['z2a'].write(('A2W', ('exec', rnd, idx)))
     m = waits(self.sim_pump, self.sim_channels['a2z'],
               self.sim_channels['p2z'])
     if m:
         self.write('a2z', m)
     else:
         self.pump.write("dump")
Пример #8
0
    def sim_get_leaks(self):
        # Ask for leaks from the simulated wrapper
        self.log.debug('sin_get_leaks asking for leaks')
        self.tick(1)
        leaks = self.sim_write_and_wait('z2a', ('A2W', ('get-leaks', )), 0,
                                        'a2z')
        n = 0
        self.log.debug('\n\t leaks = {} \n'.format(leaks))

        if len(leaks.msg):
            # check and count new "schedules" in in simulated wrapper
            for x in leaks.msg:
                fro, s, i = x
                if s[0] == 'schedule':
                    n += 1

        self.tick(1)
        # add delay from new "schedules" in simulated wrapper to ideal-world wrapper
        self.log.debug('Add n={} delay to ideal world wrapper'.format(n))
        self.internal_delay += n
        self.write('a2w', ('delay', n), n)
        m = waits(self.pump, self.channels['w2a'])
        assert m.msg == "OK", str(m.msg)
        self.sim_leaks.extend(leaks.msg)
Пример #9
0
 def sim_write_and_wait(self, ch, msg, imp, *waiters):
     self.sim_channels[ch].write(msg, imp)
     return waits(self.sim_pump, *[self.sim_channels[w] for w in waiters])
Пример #10
0
 def _p2z():
     while True:
         m = waits(p2z)
         transcript.append('p2z: ' + str(m.msg))
         pump.write('dump')
Пример #11
0
def env(static, z2p, z2f, z2a, z2w, a2z, p2z, f2z, w2z, pump):
    delta = 3
    n = 4
    sid = ('one', tuple(range(1, n + 1)), delta)
    static.write((('sid', sid), ('crupt', (sid, 1), (sid, 10))))

    transcript = []

    def _a2z():
        while True:
            m = waits(a2z)
            transcript.append('a2z: ' + str(m.msg))
            pump.write('dump')

    def _p2z():
        while True:
            m = waits(p2z)
            transcript.append('p2z: ' + str(m.msg))
            pump.write('dump')

    g1 = gevent.spawn(_a2z)
    g2 = gevent.spawn(_p2z)

    def t(s):
        transcript.append('cmd: ' + str(s))

    def channel_id(fro, to, r):
        s = ('one', (sid, fro), (sid, to), r)
        return (s, 'F_chan')

    z2a.write(('A2P', ((sid, 1), ('P2F', ((channel_id(1, 2, 1)),
                                          ('send', ('VAL', 1)))))), 4 * n)
    waits(pump)
    z2a.write(('A2P', ((sid, 1), ('P2F', ((channel_id(1, 3, 1)),
                                          ('send', ('VAL', 2)))))), 4 * n)
    waits(pump)

    z2a.write(('A2W', ('get-leaks', )), n * (4 * n + 1))
    waits(pump)

    for _ in range(4):
        z2w.write(('poll', ), 1)
        waits(pump)

    z2a.write(('A2W', ('exec', 0)))
    waits(pump)

    for _ in range(6):
        z2a.write(('A2W', ('exec', 0)))
        waits(pump)

    z2a.write(('A2P', ((sid, 1), ('P2F', ((channel_id(1, 2, 7)),
                                          ('send', ('ECHO', 1)))))), 3)
    waits(pump)
    z2a.write(('A2P', ((sid, 1), ('P2F', ((channel_id(1, 3, 7)),
                                          ('send', ('ECHO', 2)))))), 3)
    waits(pump)

    for _ in range(3):
        z2w.write(('poll', ), 1)
        waits(pump)

    for _ in range(8):
        z2a.write(('A2W', ('exec', 0)))
        waits(pump)

    z2a.write(('A2P', ((sid, 1), ('P2F', ((channel_id(1, 2, 7)),
                                          ('send', ('READY', 1)))))), 0)
    waits(pump)

    z2a.write(('A2W', ('exec', 0)))
    waits(pump)

    z2a.write(('A2P', ((sid, 1), ('P2F', ((channel_id(1, 3, 7)),
                                          ('send', ('READY', 2)))))), 0)
    waits(pump)

    z2a.write(('A2W', ('exec', 0)))
    waits(pump)

    gevent.kill(g1)
    gevent.kill(g2)

    return transcript
Пример #12
0
def env1(static, z2p, z2f, z2a, z2w, a2z, p2z, f2z, w2z, pump):
    delta = 3
    n = 3
    sid = ('one', tuple(range(1, n + 1)), delta)
    static.write((('sid', sid), ('crupt', )))

    transcript = []

    def _a2z():
        while True:
            m = waits(a2z)
            transcript.append('a2z: ' + str(m.msg))
            pump.write('dump')

    def _p2z():
        while True:
            m = waits(p2z)
            transcript.append('p2z: ' + str(m.msg))
            pump.write('dump')

    g1 = gevent.spawn(_a2z)
    g2 = gevent.spawn(_p2z)

    def t(s):
        transcript.append('cmd: ' + str(s))

    z2p.write(((sid, 1), ('input', 2)), n * (4 * n + 1))
    waits(pump)

    def channel_id(fro, to, r):
        s = ('one', (sid, fro), (sid, to), r, delta)
        return (s, 'F_chan')

    z2a.write(('A2W', ('get-leaks', )), n * (4 * n + 1))
    waits(pump)

    for _ in range(4):
        z2w.write(('poll', ), 1)
        waits(pump)

    for _ in range(3):
        z2w.write(('poll', ), 1)
        waits(pump)

    for _ in range(3):
        z2w.write(('poll', ), 1)
        waits(pump)

    for _ in range(1):
        z2w.write(('poll', ), 1)
        waits(pump)

    for _ in range(3):
        z2w.write(('poll', ), 1)
        waits(pump)

    for _ in range(3):
        z2w.write(('poll', ), 1)
        waits(pump)

    for _ in range(3):
        z2w.write(('poll', ), 1)
        waits(pump)

    z2a.write(('A2W', ('delay', 3)), 3)
    waits(pump)

    for _ in range(4):
        z2w.write(('poll', ), 1)
        waits(pump)

    z2w.write(('poll', ), 1)
    waits(pump)

    z2w.write(('poll', ), 1)
    waits(pump)

    z2w.write(('poll', ), 1)
    waits(pump)

    z2w.write(('poll', ), 1)
    waits(pump)

    z2w.write(('poll', ), 1)
    waits(pump)

    z2w.write(('poll', ), 1)
    waits(pump)

    z2w.write(('poll', ), 1)
    waits(pump)

    gevent.kill(g1)
    gevent.kill(g2)

    print('Transcript', transcript)
    return transcript
Пример #13
0
 def _a2z():
     while True:
         m = waits(a2z)
         transcript.append('a2z:' + str(m.msg))
         print('a2z:' + str(m.msg))
         pump.write('')