Example #1
0
def ec_pubkey_tweak_mul(ctx, pubkey, tweak):
    '''Tweak a public key by multiplying it by a tweak value.
    Args:
        ctx     (secp256k1_context*):   pointer to a context object
                                        initialized for validation (cannot be
                                        NULL)
        pubkey  (secp2561_pubkey*):     pointer to a public key object
        tweak   (bytes):                pointer to a 32-byte tweak
    Returns:
        (int, secp256k1_pubkey*):       (0 if the tweak was out of range
                                        (chance of around 1 in 2^128 for
                                        uniformly random 32-byte arrays, or
                                        equal to zero. 1 otherwise,
                                        pointer to a public key object)
    '''
    # Validate context
    utils.validate_context(ctx)

    # Validate public key
    utils.validate_public_key(pubkey)

    # Validate tweak
    utils.validate_tweak_ser(tweak)

    return (lib.secp256k1_ec_pubkey_tweak_mul(ctx, pubkey, tweak), pubkey)
Example #2
0
def ec_pubkey_tweak_add(ctx, pubkey, tweak):
    ''' Tweak a public key by adding tweak times the generator to it.
    Args:
        ctx     (secp256k1_context*):   pointer to a context object (cannot be
                                        NULL)
        pubkey  (secp256k1_pubkey*):    pointer to a public key object
        tweak   (bytes):                pointer to a 32-byte tweak
    Returns:
        (int, secp256k1_pubkey*):       (0 if the tweak was out of range
                                        (change of around 1 in 2^128 for
                                        uniformly random 32-byte arrays),
                                        or if the resulting public key
                                        would be invalid (only when the
                                        tweak is the complement of the
                                        corresponding private key). 1
                                        otherwise,
                                        a pointer to a secp256k1_pubkey
                                        containing tweaked public key)
    '''
    # Validate context
    utils.validate_context(ctx)

    # Validate public key
    utils.validate_public_key(pubkey)

    # Validate tweak
    utils.validate_tweak_ser(tweak)

    return (lib.secp256k1_ec_pubkey_tweak_add(ctx, pubkey, tweak), pubkey)
Example #3
0
def ec_pubkey_serialize(ctx, pubkey, flags):
    '''Serialize a pubkey object into a serialized byte sequence.
    Args:
        ctx     (secp256k1_context*):       a secp256k1 context object
        pubkey  (secp256k1_pubkey*):        a pointer to a secp256k1_pubkey
                                            containing an initialized public
                                            key
        flags   (int):                      SECP256K1_EC_COMPRESSED if
                                            serialization should be in
                                            compressed format, otherwise
                                            SECP256K1_EC_UNCOMPRESSED
    Returns:
        (int, ctype 'char[33]', size_t*):   (1,
                                            a pointer to a 65-byte (if
                                            compressed==0) or 33-byte (if
                                            compressed==1) byte array to place
                                            the serialized key in,
                                            Pointer to an integer which is
                                            initially set to the size of the
                                            output, and is overwritten with the
                                            written size)
    '''
    # Validate context
    utils.validate_context(ctx)

    # Validate public key
    utils.validate_public_key(pubkey)

    # Validate flags
    if flags == lib.SECP256K1_EC_COMPRESSED:
        publen = 33
    elif flags == lib.SECP256K1_EC_UNCOMPRESSED:
        publen = 65
    else:
        raise ValueError('Invalid serialized compression format flag.')

    # Pointer to a 33- or 65-byte array to place the serialized key in
    output = ffi.new('char[]', publen)

    # Pointer to an integer which is initially set to the size of the output,
    # and is overwritten with the written size
    outputlen = ffi.new('size_t *', publen)
    output_length = int(ffi.cast('uint32_t', outputlen[0]))

    return (lib.secp256k1_ec_pubkey_serialize(ctx, output, outputlen, pubkey,
                                              flags), output[0:output_length],
            outputlen)
Example #4
0
def ec_pubkey_negate(ctx, pubkey):
    '''Negates a public key in place.
    Args:
        ctx     (secp256k1_context*):   a secp256k1 context object
        pubkey  (secp256k1_pubkey*):    pointer to the public key to be negated
                                        (cannot be NULL)
    Returns:
        (int, secp256k1_pubkey*):       (1 always,
                                        pointer to the public key to be negated
                                        (cannot be NULL))
    '''
    # Validate context
    utils.validate_context(ctx)

    # Validate public key
    utils.validate_public_key(pubkey)

    return (lib.secp256k1_ec_pubkey_negate(ctx, pubkey), pubkey)
Example #5
0
def ecdsa_verify(ctx, sig, msg32, pubkey):
    '''Verify an ECDSA signature.
    To avoid accepting malleable signatures, only ECDSA signatures in lower-S
    form are accepted.
    If you need to accept ECDSA signatures from sources that do not obey this
    rule, apply secp256k1_ecdsa_signature_normalize to the signature prior to
    validation, but be aware that doing so results in malleable signatures.
    For details, see the comments for that function.
    Args:
        ctx     (secp256k1_context*):           a secp256k1 context object,
                                                initialized for verification
        sig     (secp256k1_ecdsa_signature*):   the signature being verified
                                                (cannot be NULL)
        msg32   (bytes):                        the 32-byte message hash being
                                                verified (cannot be NULL)
        pubkey  (secp256k1_pubkey*):            pointer to an initialized
                                                public key to verify with
                                                (cannot be NULL)
    Returns:
                (int):                          1: correct signature
                                                0: incorrect or unparseable
                                                signature
    '''
    # Validate context
    utils.validate_context(ctx)

    # Validate pubkey
    utils.validate_public_key(pubkey)

    # Validate sig
    utils.validate_signature(sig)

    # Validate msg32
    utils.validate_msg32_ser(msg32)

    return lib.secp256k1_ecdsa_verify(ctx, sig, msg32, pubkey)