starcoin-framework

Module 0x1::Secp256k1

This module implements ECDSA signatures based on the prime-order secp256k1 ellptic curve (i.e., cofactor is 1).

use 0x1::Errors;
use 0x1::Option;

Struct ECDSARawPublicKey

A 64-byte ECDSA public key.

struct ECDSARawPublicKey has copy, drop, store
Fields
bytes: vector<u8>

Struct ECDSASignature

A 64-byte ECDSA signature.

struct ECDSASignature has copy, drop, store
Fields
bytes: vector<u8>

Constants

An error occurred while deserializing, for example due to wrong input size.

const E_DESERIALIZE: u64 = 1;

The size of a secp256k1-based ECDSA public key, in bytes.

const RAW_PUBLIC_KEY_NUM_BYTES: u64 = 64;

The size of a secp256k1-based ECDSA signature, in bytes.

const SIGNATURE_NUM_BYTES: u64 = 64;

Function ecdsa_signature_from_bytes

Constructs an ECDSASignature struct from the given 64 bytes.

public fun ecdsa_signature_from_bytes(bytes: vector<u8>): Secp256k1::ECDSASignature
Implementation
public fun ecdsa_signature_from_bytes(bytes: vector<u8>): ECDSASignature {
    assert!(Vector::length(&bytes) == SIGNATURE_NUM_BYTES, Errors::invalid_argument(E_DESERIALIZE));
    ECDSASignature { bytes }
}

Function ecdsa_raw_public_key_from_64_bytes

Constructs an ECDSARawPublicKey struct, given a 64-byte raw representation.

public fun ecdsa_raw_public_key_from_64_bytes(bytes: vector<u8>): Secp256k1::ECDSARawPublicKey
Implementation
public fun ecdsa_raw_public_key_from_64_bytes(bytes: vector<u8>): ECDSARawPublicKey {
    assert!(Vector::length(&bytes) == RAW_PUBLIC_KEY_NUM_BYTES, Errors::invalid_argument(E_DESERIALIZE));
    ECDSARawPublicKey { bytes }
}

Function ecdsa_raw_public_key_to_bytes

Serializes an ECDSARawPublicKey struct to 64-bytes.

public fun ecdsa_raw_public_key_to_bytes(pk: &Secp256k1::ECDSARawPublicKey): vector<u8>
Implementation
public fun ecdsa_raw_public_key_to_bytes(pk: &ECDSARawPublicKey): vector<u8> {
    *&pk.bytes
}

Function ecdsa_signature_to_bytes

Serializes an ECDSASignature struct to 64-bytes.

public fun ecdsa_signature_to_bytes(sig: &Secp256k1::ECDSASignature): vector<u8>
Implementation
public fun ecdsa_signature_to_bytes(sig: &ECDSASignature): vector<u8> {
    *&sig.bytes
}

Function ecdsa_recover

Recovers the signer’s raw (64-byte) public key from a secp256k1 ECDSA signature given the recovery_id and the signed message (32 byte digest).

Note that an invalid signature, or a signature from a different message, will result in the recovery of an incorrect public key. This recovery algorithm can only be used to check validity of a signature if the signer’s public key (or its hash) is known beforehand.

public fun ecdsa_recover(message: vector<u8>, recovery_id: u8, signature: &Secp256k1::ECDSASignature): Option::Option<Secp256k1::ECDSARawPublicKey>
Implementation
public fun ecdsa_recover(
    message: vector<u8>,
    recovery_id: u8,
    signature: &ECDSASignature,
): Option<ECDSARawPublicKey> {
    let (pk, success) = ecdsa_recover_internal(message, recovery_id, *&signature.bytes);
    if (success) {
        Option::some(ecdsa_raw_public_key_from_64_bytes(pk))
    } else {
        Option::none<ECDSARawPublicKey>()
    }
}

Function ecdsa_recover_internal

Returns (public_key, true) if signature verifies on message under the recovered public_key and returns ([], false) otherwise.

fun ecdsa_recover_internal(message: vector<u8>, recovery_id: u8, signature: vector<u8>): (vector<u8>, bool)
Implementation
native fun ecdsa_recover_internal(
    message: vector<u8>,
    recovery_id: u8,
    signature: vector<u8>
): (vector<u8>, bool);
Specification
pragma opaque;