def test_redeem(self):
        ## run service
        checker_service_process = Process(
            target=tumbler_contract.run_checker_service)
        checker_service_process.start()
        time.sleep(0.1)

        ## create transaction
        # init
        init_transaction = tumbler.init()
        token = init_transaction['transaction']['outputs'][0]

        # initialise petition
        create_transaction = tumbler.create_tumbler((token, ), None, None, vvk,
                                                    auth_sig)
        old_list = create_transaction['transaction']['outputs'][1]

        # some crypto
        # ------------------------------------
        hasher = sha256()
        hasher.update(dumps(ID).encode('utf8'))
        hasher.update(dumps(merchant_addr).encode('utf8'))
        m = Bn.from_binary(hasher.digest())
        (priv, pub) = elgamal_keygen(bp_params)
        (cm, c, proof_s) = prepare_blind_sign(bp_params, m, pub)
        enc_sigs = [
            blind_sign(bp_params, ski, cm, c, pub, proof_s) for ski in sk
        ]
        (h, enc_epsilon) = zip(*enc_sigs)
        sigs = [(h[0], elgamal_dec(bp_params, priv, enc))
                for enc in enc_epsilon]
        sig = aggregate_th_sign(bp_params, sigs)
        sig = randomize(bp_params, sig)
        # reveal ID and merchant addr
        #print(verify(bp_params, vvk, m, sig))
        # ------------------------------------

        # add signature to th petition
        transaction = tumbler.redeem(
            (old_list, ),
            None,
            (dumps(ID), dumps(merchant_addr)),
            sig,
            vvk,
        )

        ## submit transaction
        response = requests.post('http://127.0.0.1:5000/' +
                                 tumbler_contract.contract_name + '/redeem',
                                 json=transaction_to_solution(transaction))
        self.assertTrue(response.json()['success'])

        ## stop service
        checker_service_process.terminate()
        checker_service_process.join()
    def test_sign(self):
        ## run service
        checker_service_process = Process(
            target=petition_contract.run_checker_service)
        checker_service_process.start()
        time.sleep(0.1)

        ## create transaction
        # init
        init_transaction = petition.init()
        token = init_transaction['transaction']['outputs'][0]

        # initialise petition
        create_petition_transaction = petition.create_petition(
            (token, ), None, None, UUID, options, priv_owner, pub_owner, vvk)
        old_petition = create_petition_transaction['transaction']['outputs'][1]
        old_list = create_petition_transaction['transaction']['outputs'][2]

        # some crypto
        # ------------------------------------
        (priv_signer, pub_signer) = elgamal_keygen(bp_params)
        m = priv_signer
        (cm, c, proof_s) = prepare_blind_sign(bp_params, m, pub_signer)
        enc_sigs = [
            blind_sign(bp_params, ski, cm, c, pub_signer, proof_s)
            for ski in sk
        ]
        (h, enc_epsilon) = zip(*enc_sigs)
        sigs = [(h[0], elgamal_dec(bp_params, priv_signer, enc))
                for enc in enc_epsilon]
        sig = aggregate_th_sign(bp_params, sigs)
        sig = randomize(bp_params, sig)
        (kappa, nu, proof_v) = show_coconut_petition(bp_params, vvk, m, UUID)
        #print(coconut_petition_verify(bp_params, vvk, kappa, sig, proof_v, UUID, nu))
        # ------------------------------------

        # add signature to th petition
        start = time.time()
        transaction = petition.sign((old_petition, old_list), None,
                                    (dumps([1, 0]), ), priv_signer, sig, vvk)
        end = time.time()
        print((end - start) * 1000)

        ## submit transaction
        response = requests.post('http://127.0.0.1:5000/' +
                                 petition_contract.contract_name + '/sign',
                                 json=transaction_to_solution(transaction))
        self.assertTrue(response.json()['success'])

        ## stop service
        checker_service_process.terminate()
        checker_service_process.join()
Exemple #3
0
def generate_signature():
    (priv_key, pub_key) = elgamal_keygen(bp_params)
    m = priv_key
    (cm, c, proof_s) = prepare_blind_sign(bp_params, m, pub_key)
    enc_sigs = [
        blind_sign(bp_params, ski, cm, c, pub_key, proof_s) for ski in sk
    ]
    (h, enc_epsilon) = zip(*enc_sigs)
    sigs = [(h[0], elgamal_dec(bp_params, priv_key, enc))
            for enc in enc_epsilon]
    sig = aggregate_th_sign(bp_params, sigs)
    sig = randomize(bp_params, sig)
    return priv_key, sig
def main():
    coconut.contract._populate_empty_checkers()
    print "operation\t\tmean (ms)\t\tsd (ms)\t\truns"

    # == init ===============
    init_tx = coconut.init()

    # == create ===============
    # gen
    times = []
    for i in range(RUNS):
        start_time = time.time()
        coconut.create((init_tx['transaction']['outputs'][0], ), None, None, q,
                       t, n, callback, vvk, auth_sig)
        end_time = time.time()
        times.append((end_time - start_time) * 1000)
    mean = numpy.mean(times)
    sd = numpy.std(times)
    print "[g] create tx\t{:.10f}\t\t{:.10f}\t{}".format(mean, sd, RUNS)

    # check
    create_tx = coconut.create((init_tx['transaction']['outputs'][0], ), None,
                               None, q, t, n, callback, vvk, auth_sig)
    solution = transaction_to_solution(create_tx)
    times = []
    for i in range(RUNS):
        start_time = time.time()
        coconut.contract.checkers['create'](
            solution['inputs'],
            solution['referenceInputs'],
            solution['parameters'],
            solution['outputs'],
            solution['returns'],
            solution['dependencies'],
        )
        end_time = time.time()
        times.append((end_time - start_time) * 1000)
    mean = numpy.mean(times)
    sd = numpy.std(times)
    print "[c] create tx\t{:.10f}\t\t{:.10f}\t{}".format(mean, sd, RUNS)

    # == request ===============
    # gen
    times = []
    for i in range(RUNS):
        start_time = time.time()
        coconut.request((create_tx['transaction']['outputs'][1], ), None, None,
                        clear_m, hidden_m, pub)
        end_time = time.time()
        times.append((end_time - start_time) * 1000)
    mean = numpy.mean(times)
    sd = numpy.std(times)
    print "[g] request tx\t{:.10f}\t\t{:.10f}\t{}".format(mean, sd, RUNS)

    # check
    request_tx = coconut.request((create_tx['transaction']['outputs'][1], ),
                                 None, None, clear_m, hidden_m, pub)
    solution = transaction_to_solution(request_tx)
    times = []
    for i in range(RUNS):
        start_time = time.time()
        coconut.contract.checkers['request'](
            solution['inputs'],
            solution['referenceInputs'],
            solution['parameters'],
            solution['outputs'],
            solution['returns'],
            solution['dependencies'],
        )
        end_time = time.time()
        times.append((end_time - start_time) * 1000)
    mean = numpy.mean(times)
    sd = numpy.std(times)
    print "[c] request tx\t{:.10f}\t\t{:.10f}\t{}".format(mean, sd, RUNS)

    # == issue ===============
    # gen
    times = []
    for i in range(RUNS):
        start_time = time.time()
        coconut.issue((request_tx['transaction']['outputs'][1], ), None, None,
                      sk[0], 0)
        end_time = time.time()
        times.append((end_time - start_time) * 1000)
    mean = numpy.mean(times)
    sd = numpy.std(times)
    print "[g] issue tx\t{:.10f}\t\t{:.10f}\t{}".format(mean, sd, RUNS)

    # check
    issue_tx = coconut.issue((request_tx['transaction']['outputs'][1], ), None,
                             None, sk[0], 0)
    solution = transaction_to_solution(issue_tx)
    times = []
    for i in range(RUNS):
        start_time = time.time()
        coconut.contract.checkers['issue'](
            solution['inputs'],
            solution['referenceInputs'],
            solution['parameters'],
            solution['outputs'],
            solution['returns'],
            solution['dependencies'],
        )
        end_time = time.time()
        times.append((end_time - start_time) * 1000)
    mean = numpy.mean(times)
    sd = numpy.std(times)
    print "[c] issue tx\t{:.10f}\t\t{:.10f}\t{}".format(mean, sd, RUNS)

    # == verify ===============
    # gen

    # issue t signatures
    old_request = request_tx['transaction']['outputs'][1]
    for i in range(t):
        transaction = coconut.issue((old_request, ), None, None, sk[i], i)
        old_request = transaction['transaction']['outputs'][0]

    # some crypto
    # ------------------------------------
    packet = loads(old_request)['sigs']
    (indexes, packed_enc_sigs) = zip(*packet)
    (h, packed_enc_epsilon) = zip(*packed_enc_sigs)
    enc_epsilon = [(unpackG1(params, x[0]), unpackG1(params, x[1]))
                   for x in packed_enc_epsilon]
    dec_sigs = [(unpackG1(params, h[0]), elgamal_dec(params, priv, enc))
                for enc in enc_epsilon]
    aggr = aggregate_th_sign(params, dec_sigs)
    aggr = randomize(params, aggr)
    packed_sig = (pack(aggr[0]), pack(aggr[1]))
    # ------------------------------------

    times = []
    for i in range(RUNS):
        start_time = time.time()
        coconut.verify(None, (create_tx['transaction']['outputs'][1], ),
                       (packed_sig, ), clear_m, hidden_m)
        end_time = time.time()
        times.append((end_time - start_time) * 1000)
    mean = numpy.mean(times)
    sd = numpy.std(times)
    print "[g] verify tx\t{:.10f}\t\t{:.10f}\t{}".format(mean, sd, RUNS)

    # check
    verify_tx = coconut.verify(None,
                               (create_tx['transaction']['outputs'][1], ),
                               (packed_sig, ), clear_m, hidden_m)
    solution = transaction_to_solution(verify_tx)
    times = []
    for i in range(RUNS):
        start_time = time.time()
        coconut.contract.checkers['verify'](
            solution['inputs'],
            solution['referenceInputs'],
            solution['parameters'],
            solution['outputs'],
            solution['returns'],
            solution['dependencies'],
        )
        end_time = time.time()
        times.append((end_time - start_time) * 1000)
    mean = numpy.mean(times)
    sd = numpy.std(times)
    print "[c] verify tx\t{:.10f}\t\t{:.10f}\t{}".format(mean, sd, RUNS)
    '''
bp_params = bp_setup()  # bp system's parameters
(sk, vk, vvk) = ttp_th_keygen(bp_params, t, n)  # signers keys

# tumbler parameter
ID = 10  # random ID
merchant_addr = 'merchant_addr'  # merchant address

# some crypto
# ------------------------------------
packed_vvk = (pack(vvk[0]), pack(vvk[1]), pack(vvk[2]))
instance = {'type': 'TList', 'list': [], 'vvk': packed_vvk}
hasher = sha256()
hasher.update(dumps(instance).encode('utf8'))
m = Bn.from_binary(hasher.digest())
auth_sigs = [sign(bp_params, ski, m) for ski in sk]
auth_sig = aggregate_th_sign(bp_params, auth_sigs)
#print(verify(bp_params, vvk, m, auth_sig))


class Test(unittest.TestCase):
    # --------------------------------------------------------------
    # test init
    # --------------------------------------------------------------
    def test_init(self):
        ## run service
        checker_service_process = Process(
            target=tumbler_contract.run_checker_service)
        checker_service_process.start()
        time.sleep(0.1)

        ## create transaction
# some crypto
# ------------------------------------
packed_vvk = (pack(vvk[0]), pack(vvk[1]), [pack(vvk[2][i]) for i in range(q)])
instance = {
    'type': 'CoCoInstance',
    'q': q,
    't': t,
    'n': n,
    'callback': callback,
    'verifier': packed_vvk
}
hasher = sha256()
hasher.update(dumps(instance).encode('utf8'))
m = Bn.from_binary(hasher.digest())
sigs = [mix_sign(params, ski, None, [], [m]) for ski in sk]
auth_sig = aggregate_th_sign(params, sigs)
#print(mix_verify(params, vvk, None, sig, None, [m]))
# ------------------------------------

##
RUNS = 10000


def main():
    coconut.contract._populate_empty_checkers()
    print "operation\t\tmean (ms)\t\tsd (ms)\t\truns"

    # == init ===============
    init_tx = coconut.init()

    # == create ===============
    def test_verify(self):
        with coconut_contract.test_service():

            ## create transactions
            # init
            init_transaction = coconut.init()
            token = init_transaction['transaction']['outputs'][0]
            # create instance
            create_transaction = coconut.create(
                (token,),
                None,
                None,
                q,
                t,
                n,
                callback, 
                vvk,
                sig
            )
            instance = create_transaction['transaction']['outputs'][1]
            # request
            request_transaction = coconut.request(
                (instance,),
                None,
                None,
                clear_m, 
                hidden_m, 
                pub
            )
            old_request = request_transaction['transaction']['outputs'][1]

            # issue t signatures
            for i in range(t):
                transaction = coconut.issue(
                    (old_request,),
                    None,
                    None,
                    sk[i],
                    i
                )
                old_request = transaction['transaction']['outputs'][0]

            # some crypto
            # ------------------------------------
            packet = loads(old_request)['sigs']
            (indexes, packed_enc_sigs) = zip(*packet)
            (h, packed_enc_epsilon) = zip(*packed_enc_sigs)
            enc_epsilon = [(unpackG1(params,x[0]), unpackG1(params,x[1])) for x in packed_enc_epsilon]
            dec_sigs = [(unpackG1(params,h[0]), elgamal_dec(params, priv, enc)) for enc in enc_epsilon]
            aggr = aggregate_th_sign(params, dec_sigs)
            aggr = randomize(params, aggr)
            packed_sig = (pack(aggr[0]),pack(aggr[1]))
            # ------------------------------------

            # verify signature
            start_time = time.time()
            transaction = coconut.verify(
                None,
                (instance,),
                (packed_sig,),
                clear_m,
                hidden_m
            )
            end_time = time.time(); print((end_time-start_time)*1000)

            ## submit t ransaction
            response = requests.post(
                'http://127.0.0.1:5000/' + coconut_contract.contract_name 
                + '/verify', json=transaction_to_solution(transaction)
            )
            self.assertTrue(response.json()['success'])

            print("\n\n=================== VERIFICATION ===================\n")
            print(transaction['transaction']['returns'][0])
            print("\n====================================================\n\n")
    def test_issue(self):
        with coconut_contract.test_service():

            ## create transactions
            # init
            init_transaction = coconut.init()
            token = init_transaction['transaction']['outputs'][0]
            # create instance
            create_transaction = coconut.create(
                (token,),
                None,
                None,
                q,
                t,
                n,
                callback, 
                vvk,
                sig
            )
            instance = create_transaction['transaction']['outputs'][1]
            # request
            request_transaction = coconut.request(
                (instance,),
                None,
                None,
                clear_m, 
                hidden_m, 
                pub
            )
            old_request = request_transaction['transaction']['outputs'][1]

            # issue a signatures
            transaction = coconut.issue(
                (old_request,),
                None,
                None,
                sk[0],
                0
            )
            old_request = transaction['transaction']['outputs'][0]

            ## submit transaction
            response = requests.post(
                'http://127.0.0.1:5000/' + coconut_contract.contract_name 
                + '/issue', json=transaction_to_solution(transaction)
            )
            self.assertTrue(response.json()['success'])

            # issue the other t-1 signatures
            for i in range(1,t):
                transaction = coconut.issue(
                    (old_request,),
                    None,
                    None,
                    sk[i],
                    i
                )
                old_request = transaction['transaction']['outputs'][0]

            # some crypto - to show that this actually works
            # ------------------------------------
            packet = loads(old_request)['sigs']
            (indexes, packed_enc_sigs) = zip(*packet)
            (h, packed_enc_epsilon) = zip(*packed_enc_sigs)
            enc_epsilon = [(unpackG1(params,x[0]), unpackG1(params,x[1])) for x in packed_enc_epsilon]
            dec_sigs = [(unpackG1(params,h[0]), elgamal_dec(params, priv, enc)) for enc in enc_epsilon]
            aggr = aggregate_th_sign(params, dec_sigs)
            aggr = randomize(params, aggr)
            (kappa, proof_v) = show_mix_sign(params, vvk, hidden_m)
            print("\n\n=================== VERIFICATION ===================\n")
            print(mix_verify(params, vvk, kappa, aggr, proof_v, clear_m))
            print("\n====================================================\n\n")
def main():
    petition.contract._populate_empty_checkers()
    print "operation\t\tmean (ms)\t\tsd (ms)\t\truns"

    # == init ===============
    init_tx = petition.init()

    # == create_petition ===============
    # gen
    times = []
    for i in range(RUNS):
        start_time = time.time()
        petition.create_petition(
            (init_tx['transaction']['outputs'][0],),
            None,
            None,
            UUID,
            options,
            priv_owner,
            pub_owner,
            vvk
        )
        end_time = time.time()
        times.append((end_time-start_time)*1000)
    mean = numpy.mean(times)
    sd = numpy.std(times)
    print "[g] create_petition tx\t{:.10f}\t\t{:.10f}\t{}".format(mean, sd, RUNS)

    # check
    create_petition_tx = petition.create_petition(
        (init_tx['transaction']['outputs'][0],),
        None,
        None,
        UUID,
        options,
        priv_owner,
        pub_owner,
        vvk
    )
    solution = transaction_to_solution(create_petition_tx)
    times = []
    for i in range(RUNS):
        start_time = time.time()
        petition.contract.checkers['create_petition'](
            solution['inputs'],
            solution['referenceInputs'],
            solution['parameters'],
            solution['outputs'],
            solution['returns'],
            solution['dependencies'],
        )
        end_time = time.time()
        times.append((end_time-start_time)*1000)
    mean = numpy.mean(times)
    sd = numpy.std(times)
    print "[c] create_petition tx\t{:.10f}\t\t{:.10f}\t{}".format(mean, sd, RUNS)


    # == sign ===============
    # gen
    old_petition = create_petition_tx['transaction']['outputs'][1]
    old_list = create_petition_tx['transaction']['outputs'][2]

    # some crypto
    # ------------------------------------
    (priv_signer, pub_signer) = elgamal_keygen(bp_params)
    m = priv_signer
    (cm, c, proof_s) = prepare_blind_sign(bp_params, m, pub_signer)
    enc_sigs = [blind_sign(bp_params, ski, cm, c, pub_signer, proof_s) for ski in sk]
    (h, enc_epsilon) = zip(*enc_sigs)
    sigs = [(h[0], elgamal_dec(bp_params, priv_signer, enc)) for enc in enc_epsilon]
    sig = aggregate_th_sign(bp_params, sigs)
    sig = randomize(bp_params, sig)
    (kappa, nu, proof_v) = show_coconut_petition(bp_params, vvk, m, UUID)
    #print(coconut_petition_verify(bp_params, vvk, kappa, sig, proof_v, UUID, nu))
    # ------------------------------------

    times = []
    for i in range(RUNS):
        start_time = time.time()
        petition.sign(
            (old_petition, old_list),
            None,
            (dumps([1, 0]),),
            priv_signer,
            sig,
            vvk
            )
        end_time = time.time()
        times.append((end_time-start_time)*1000)
    mean = numpy.mean(times)
    sd = numpy.std(times)
    print "[g] sign tx\t\t{:.10f}\t\t{:.10f}\t{}".format(mean, sd, RUNS)

    # check
    sign_tx = petition.sign(
        (old_petition, old_list),
        None,
        (dumps([1, 0]),),
        priv_signer,
        sig,
        vvk
    )
    solution = transaction_to_solution(sign_tx)
    times = []
    for i in range(RUNS):
        start_time = time.time()
        petition.contract.checkers['sign'](
            solution['inputs'],
            solution['referenceInputs'],
            solution['parameters'],
            solution['outputs'],
            solution['returns'],
            solution['dependencies'],
        )
        end_time = time.time()
        times.append((end_time-start_time)*1000)
    mean = numpy.mean(times)
    sd = numpy.std(times)
    print "[c] sign tx\t\t{:.10f}\t\t{:.10f}\t{}".format(mean, sd, RUNS)

    '''
Exemple #10
0
def main():
    tumbler.contract._populate_empty_checkers()
    print "operation\t\tmean (ms)\t\tsd (ms)\t\truns"

    # == init ===============
    init_tx = tumbler.init()

    # == create_tumbler ===============
    # gen
    times = []
    for i in range(RUNS):
        start_time = time.time()
        tumbler.create_tumbler(
            (init_tx['transaction']['outputs'][0],),
            None,
            None,
            vvk,
            auth_sig
        )
        end_time = time.time()
        times.append((end_time-start_time)*1000)
    mean = numpy.mean(times)
    sd = numpy.std(times)
    print "[g] create_tumbler tx\t{:.10f}\t\t{:.10f}\t{}".format(mean, sd, RUNS)

    # check
    create_tumbler_tx = tumbler.create_tumbler(
        (init_tx['transaction']['outputs'][0],),
        None,
        None,
        vvk,
        auth_sig
    )
    solution = transaction_to_solution(create_tumbler_tx)
    times = []
    for i in range(RUNS):
        start_time = time.time()
        tumbler.contract.checkers['create_tumbler'](
            solution['inputs'],
            solution['referenceInputs'],
            solution['parameters'],
            solution['outputs'],
            solution['returns'],
            solution['dependencies'],
        )
        end_time = time.time()
        times.append((end_time-start_time)*1000)
    mean = numpy.mean(times)
    sd = numpy.std(times)
    print "[c] create_tumbler tx\t{:.10f}\t\t{:.10f}\t{}".format(mean, sd, RUNS)


    # == redeem ===============
    # gen

    # some crypto
    # ------------------------------------
    hasher = sha256()
    hasher.update(dumps(ID).encode('utf8'))
    hasher.update(dumps(merchant_addr).encode('utf8'))
    m = Bn.from_binary(hasher.digest())
    (priv, pub) = elgamal_keygen(bp_params)
    (cm, c, proof_s) = prepare_blind_sign(bp_params, m, pub)
    enc_sigs = [blind_sign(bp_params, ski, cm, c, pub, proof_s) for ski in sk]
    (h, enc_epsilon) = zip(*enc_sigs)
    sigs = [(h[0], elgamal_dec(bp_params, priv, enc)) for enc in enc_epsilon]
    sig = aggregate_th_sign(bp_params, sigs)
    sig = randomize(bp_params, sig)
    # reveal ID and merchant addr
    #print(verify(bp_params, vvk, m, sig))
    # ------------------------------------

    times = []
    for i in range(RUNS):
        start_time = time.time()
        tumbler.redeem(
            (create_tumbler_tx['transaction']['outputs'][1],),
            None,
            (dumps(ID),dumps(merchant_addr)),
            sig,
            vvk,
        )
        end_time = time.time()
        times.append((end_time-start_time)*1000)
    mean = numpy.mean(times)
    sd = numpy.std(times)
    print "[g] redeem tx\t\t{:.10f}\t\t{:.10f}\t{}".format(mean, sd, RUNS)

    # check
    redeem_tx = tumbler.redeem(
        (create_tumbler_tx['transaction']['outputs'][1],),
        None,
        (dumps(ID),dumps(merchant_addr)),
        sig,
        vvk,
    )
    solution = transaction_to_solution(redeem_tx)
    times = []
    for i in range(RUNS):
        start_time = time.time()
        tumbler.contract.checkers['redeem'](
            solution['inputs'],
            solution['referenceInputs'],
            solution['parameters'],
            solution['outputs'],
            solution['returns'],
            solution['dependencies'],
        )
        end_time = time.time()
        times.append((end_time-start_time)*1000)
    mean = numpy.mean(times)
    sd = numpy.std(times)
    print "[c] redeem tx\t\t{:.10f}\t\t{:.10f}\t{}".format(mean, sd, RUNS)

    '''