Exemple #1
0
def main(rounds=80):
    sys.setrecursionlimit(100000)

    a = sources.digital_source_int_circuit(0x67452301, 32)
    b = sources.digital_source_int_circuit(0xEFCDAB89, 32)
    c = sources.digital_source_int_circuit(0x98BADCFE, 32)
    d = sources.digital_source_int_circuit(0x10325476, 32)
    e = sources.digital_source_int_circuit(0xC3D2E1F0, 32)

    message_circuit = sources.digital_source_int_circuit(random.getrandbits(512), 512)

    h0, h1, h2, h3, h4 = builder.block_operation(message_circuit, a, b, c, d, e, rounds)

    # Concatenate results
    h01 = circuit.stack_circuits('h01', h0, h1)
    h012 = circuit.stack_circuits('h012', h01, h2)
    h0123 = circuit.stack_circuits('h0123', h012, h3)

    h = circuit.stack_circuits('H', h0123, h4)

    g = to_graph(message_circuit._outputs)

    # All gates/nodes that input hooks into
    # a = set()
    # for gate in message_circuit._outputs:
    #     a |= set(g.neighbors(gate))
    #     g.remove_node(gate)
    #
    # g.add_node('source')
    # for gate in a:
    #     g.add_edge('source', gate)
    #
    # g.add_node('sink')
    # for gate in h._outputs:
    #     g.add_edge(gate, 'sink')

    g.add_node('source')
    for gate in message_circuit._outputs:
        g.add_edge('source', gate, capacity=1)

    g.add_node('sink')
    for gate in h._outputs:
        g.add_edge(gate, 'sink', capacity=1)

    print '\n'
    print '---- Min-Cut on Reduced Rounds %d Rounds ----' % rounds
    print 'Number of nodes in circuit graph: %d' % len(g.nodes())
    print 'Number of edges in circuit graph: %d' % len(g.edges())
    print 'Total number of instantiated components: %d' % ComponentBase.count

    mc = nx.max_flow(g, 'source', 'sink')

    print 'Min-cut size: %d' % mc
Exemple #2
0
def sha1(message, rounds=80):
    """
    Runs the sha-1 block operation on a 512-bit message/chunk.
    """
    # Initial constants
    a = digital_source_int_circuit(0x67452301, 32)
    b = digital_source_int_circuit(0xEFCDAB89, 32)
    c = digital_source_int_circuit(0x98BADCFE, 32)
    d = digital_source_int_circuit(0x10325476, 32)
    e = digital_source_int_circuit(0xC3D2E1F0, 32)

    h0, h1, h2, h3, h4 = block_operation(message, a, b, c, d, e, rounds)

    # Concatenate results
    h01 = stack_circuits('h01', h0, h1)
    h012 = stack_circuits('h012', h01, h2)
    h0123 = stack_circuits('h0123', h012, h3)
    h = stack_circuits('H', h0123, h4)

    result = int(''.join(map(str, h.evaluate())), 2)
    return result, h
Exemple #3
0
    def test_function(self):
        c1 = circuit.Circuit('top', 2, 1)
        c2 = circuit.Circuit('bottom', 2, 1)

        c1._inputs = [1, 2]
        c1._outputs = [3]
        c2._inputs = [4, 5]
        c2._outputs = [6]

        c3 = circuit.stack_circuits('stacked', c1, c2)
        self.assertEqual('stacked', c3.name)
        self.assertEqual([1, 2, 4, 5], c3._inputs)
        self.assertEqual([3, 6], c3._outputs)