Example #1
0
def compute_commitment(zeth_note: ZethNote) -> bytes:
    """
    Used by the recipient of a payment to recompute the commitment and check
    the membership in the tree to confirm the validity of a payment
    """
    # inner_k = poseidon(r || a_pk[:94] || rho[:94] || v)
    inputs = []
    inputs.append(
        int.from_bytes(bytes.fromhex(zeth_note.trap_r), byteorder="big"))
    apk = digest_to_binary_string(bytes.fromhex(zeth_note.apk))
    first_94bits_apk = apk[2:96]
    rho = digest_to_binary_string(bytes.fromhex(zeth_note.rho))
    first_94bits_rho = rho[2:96]
    value = digest_to_binary_string(bytes.fromhex(zeth_note.value))
    left_leg_bin = first_94bits_apk + first_94bits_rho + value + "00"
    left_leg = int(left_leg_bin, 2)
    inputs.append(left_leg)
    cm_field = poseidon(inputs).to_bytes(32, byteorder="big")
    return cm_field
Example #2
0
def compute_nullifier(zeth_note: ZethNote,
                      spending_authority_ask: OwnershipSecretKey) -> bytes:
    """
    Returns nf = blake2s(1110 || [a_sk]_252 || rho)
    """
    binary_ask = digest_to_binary_string(spending_authority_ask)
    first_252bits_ask = binary_ask[:252]
    left_leg_bin = "1110" + first_252bits_ask
    left_leg = int(left_leg_bin, 2).to_bytes(32, byteorder='big')
    blake_hash = blake2s()
    blake_hash.update(left_leg)
    blake_hash.update(bytes.fromhex(zeth_note.rho))
    return blake_hash.digest()
Example #3
0
def _derive_a_pk(a_sk: OwnershipSecretKey) -> OwnershipPublicKey:
    """
    Returns a_pk = blake2s(1100 || [a_sk]_252 || 0^256)
    """
    binary_a_sk = digest_to_binary_string(a_sk)
    first_252bits_ask = binary_a_sk[:252]
    left_leg_bin = "1100" + first_252bits_ask
    left_leg_hex = "{0:0>4X}".format(int(left_leg_bin, 2))
    zeroes = "0000000000000000000000000000000000000000000000000000000000000000"
    a_pk = blake2s(
        encode_abi(["bytes32", "bytes32"],
                   [bytes.fromhex(left_leg_hex),
                    bytes.fromhex(zeroes)])).digest()
    return OwnershipPublicKey(a_pk)
Example #4
0
def _derive_a_pk(a_sk: OwnershipSecretKey) -> OwnershipPublicKey:
    """
    Returns a_pk = blake2s(1100 || [a_sk]_250 || 0^254)
    """
    inputs = []
    binary_a_sk = digest_to_binary_string(a_sk)
    index : int = 0
    for i in binary_a_sk:
        if i == "0":
            index= index+1
        else:
            break
    non_zero_a_sk = binary_a_sk[index:]
    a_sk_254 = non_zero_a_sk.zfill(254)
    first_250bits_ask = a_sk_254[:250]
    left_leg_bin = "0010" + first_250bits_ask
    left_leg = int(left_leg_bin, 2)
    zeroes = 0
    inputs.append(left_leg)
    inputs.append(zeroes)
    a_pk = poseidon(inputs).to_bytes(32, byteorder="big")
    return OwnershipPublicKey(a_pk)
Example #5
0
def compute_nullifier(zeth_note: ZethNote,
                      spending_authority_ask: OwnershipSecretKey) -> bytes:
    """
    Returns nf = poseidon(1010 || [a_sk]_250 || rho)
    """
    binary_a_sk = digest_to_binary_string(spending_authority_ask)
    index: int = 0
    for i in binary_a_sk:
        if i == "0":
            index = index + 1
        else:
            break
    non_zero_a_sk = binary_a_sk[index:]
    a_sk_254 = non_zero_a_sk.zfill(254)
    first_250bits_ask = a_sk_254[:250]
    left_leg_bin = "1010" + first_250bits_ask
    left_leg = int(left_leg_bin, 2)
    inputs = []
    inputs.append(left_leg)

    inputs.append(int.from_bytes(bytes.fromhex(zeth_note.rho),
                                 byteorder="big"))
    return poseidon(inputs).to_bytes(32, byteorder="big")