Пример #1
0
def hash_chain(pairing_group=None,
               start_pg_elem=None,
               hops_num=0,
               cached_hash_chain_elem={},
               to_cache=True):
    """
    Compute the pairing group element after the given number of hops using the hash chain by the given starting pairing
    group element.
    :param pairing_group: pairing group used for the hash chain
    :param start_pg_elem: starting pairing group element
    :param hops_num: number of hash operations to perform
    :return: the resulting pairing group element
    """

    res_pg_elem = start_pg_elem
    if to_cache:
        cached_hash_chain_elem[0] = res_pg_elem
    #print('START PG ELEM', res_pg_elem)

    if len(cached_hash_chain_elem) > hops_num:
        #print('CACHED HASH CHAIN #', hops_num, cached_hash_chain_elem[hops_num])
        return cached_hash_chain_elem[hops_num]

    # Compute the hash chain hops
    for i in range(hops_num):
        r = pairing_group.init(ZR,
                               int(hashPair(res_pg_elem).decode('utf-8'), 16))
        #print('R PG ELEM', r)
        res_pg_elem = res_pg_elem**r
        #print('NEW PG ELEM', res_pg_elem)
        if to_cache:
            cached_hash_chain_elem[i + 1] = res_pg_elem

    return res_pg_elem
Пример #2
0
def hash_chain(pairing_group=None, start_pg_elem=None, hops_num=0):
    """
    Compute the pairing group element after the given number of hops using the hash chain by the given starting pairing
    group element.
    :param pairing_group: pairing group used for the hash chain
    :param start_pg_elem: starting pairing group element
    :param hops_num: number of hash operations to perform
    :return: the resulting pairing group element
    """

    # Compute the hash chain hops
    res_pg_elem = start_pg_elem
    for i in range(hops_num):
        r = pairing_group.init(ZR,
                               int(hashPair(res_pg_elem).decode('utf-8'), 16))
        res_pg_elem = res_pg_elem**r

    return res_pg_elem
Пример #3
0
    #     fin.write('AA')
    #     fin.seek(0, 0)
    #     print(fin.read())

    exit(0)

    pair_g = pg.pairing_group_create()
    # last_elem = pg.random_pairing_group_elem_gen(pair_g)
    # max_hops = 100

    b = b'eJxNU0FuxDAI/EqUcw7g2Ab3K1UVbau97W3bSlXVv5cBnN1DLBtjmBkmv+txfNwu9/txrC/L+v7zeb2v22LR78vt6+rR18bb0nRbZGyL2r6LfXZmqrkMjRsmS2lkBwu2ZgG2ZVi+2K3u8w13u+447VGO2ZZWsMF9sZ3iJcpZSt0jv8+sgmaMG9TesxE6RwEGINSlnqg8GzXjRBJ8KnhJBvpZA7fYUJmkJF879DIx2kFqBohDCCkRZJ6KSItmKAk6Ll6hqBjo93jrhB2kBmNkQDfPIkl+rh02qAzUvU/UI6HVlO1BgylBhjrlPGGsGsgQVc0bcJaeLM/ZQFFPgeJ1n4UxEU0keNBmbuMTAOcUR04+IhKPPA/qwiryFJSW3LyRq4NmqOLUIXlOESjdTmXSlJ5kwp4UCFBkOqfnrCG9TgWR6JrCru7A8XAkurlN3Fzs+TkHzH2kVdwHFBZrqYzk8JwTRMGPohSS1TSBjCeF23SxjpQbLohfiUOscQ4WrkEbfHWczh8pqqba+vb3Dz4zux0='
    el = bytesToObject(b, pair_g)

    import re_enc_engine.pairing_group_primitives as eng_pg

    r = pair_g.init(ZR, int(hashPair(el).decode('utf-8'), 16))
    h_el = el**r
    # el = pair_g.random(G2)
    # el0 = pair_g.random(G2)
    #el0 = eng_pg.hash_chain(pair_g, el, 1)
    #print('\n\n')
    print(el.type, objectToBytes(el, pair_g))
    print(el.type, objectToBytes(r, pair_g))
    print(el.type, objectToBytes(h_el, pair_g))
    print('\n\n')
    el1 = pair_g.init(ZR, value=1)
    el2 = pair_g.init(ZR, value=2)
    el3 = pair_g.init(ZR, value=4)
    el4 = pair_g.init(ZR,
                      value=int.from_bytes(objectToBytes(el1, pair_g),
                                           'little'))
Пример #4
0
    # ========== MODULE DECRYPTION ========== #
    ciphertext['policy'] = '(DEPT1 and TEAM1)'
    for k in range(sample_num):
        # Their solution
        starting = time() * 1000.0
        for i in range(iterations):
            c = abe.decrypt(ciphertext, pk, sk, pairing_group)
        sample_mod_dec += ((time() * 1000.0) - starting)
    print('MODULE DEC =', sample_mod_dec / sample_num)

    # ========== HASH CHAIN DECRYPTION ========== #
    for k in range(sample_num):
        # Our solution
        starting = time() * 1000.0
        cpabe.decrypt(pk, sk, ciphertext)
        for j in range(iterations - 1):
            r = pairing_group.init(ZR, int(hashPair(pg_elem).decode('utf-8'), 16))
            res_pg_elem = pg_elem ** r
        sample_hc_dec += ((time() * 1000.0) - starting)
    print('HASH CHAIN DEC =', sample_hc_dec / sample_num)

    # ========== MODULE HASH CHAIN DECRYPTION ========== #
    for k in range(sample_num):
        # Our solution
        starting = time() * 1000.0
        c = abe.decrypt(ciphertext, pk, sk, pairing_group)
        d = pg.hash_chain(pairing_group, c, iterations - 1)
        sample_mod_hc_dec += ((time() * 1000.0) - starting)
    print('MODULE HASH CHAIN DEC =', sample_mod_hc_dec / sample_num)
Пример #5
0
def hash_p(elem: pairing.pc_element) -> bytes:
    sha3 = hashlib.sha3_256()
    hashed = hashPair(elem)
    sha3.update(hashed)
    return sha3.digest()