Skip to content

mycelium-com/elliptic

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

90 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Crypto primitives wrapper

What is this?

It's a ECC primitives abstraction library.

  • Provides abstract API for underlying ECC cryptography.
  • Support for operations with ED25519 and secp256k1 curves.
  • Provides hierarchical deterministic key derivation functionality for ED25519 and secp256k1.
  • Both private and public derivation modes are supported.

API

Two separate sets of API functions are defined for ECC and HD key operations.

ECDSA operations

/*
 * ECC context structure
 */
typedef struct EllipticContext {
    /*
     * Underlying curve identifier
     *
     *  EllipticInvalid   0
     *  EllipticED25519   1
     *  EllipticSecp256K1 2
     */
    int EllipticType;
    
    /*
     * Flag for the context type.
     *
     * Signing and verification context   1
     * Verification context               0
     */
    int HasPrivate;
    
    /*
     * Private key bytes
     */
    uint8_t PrivateKey[32];
    
    /*
     * Public key bytes
     *
     * Notes: 
     *   secp256k1 keys are stored here in the compressed form only.
     *   ed25519 keys are always prefixed with 0x03 byte.
     */
    uint8_t PublicKey[33];
} EllipticContext;
/*
 * Initialize ECC context with either private or the public key.
 *
 *  Notes:
 *   You must specify either EllipticED25519 or EllipticSecp256K1 for the underlying curve type.
 *   If the private key is provided then you don't need to provide a public key.
 *   Underlying secp256k1 implementation is only supporting a compressed public key representation.
 *   ED25519 public keys must be always prefixed with 0x03 byte.
 */
int elliptic_init(EllipticContext *ctx, int type, const uint8_t *key, const uint8_t *public_key);
/*
 * Sign digest using provided ECC context.
 */
int elliptic_sign(EllipticContext *ctx, const uint8_t *digest, size_t digest_size, uint8_t *signature);
/*
 * Verify signature using provided ECC context.
 */
int elliptic_verify(EllipticContext *ctx, const uint8_t *digest, size_t digest_size, const uint8_t *signature, size_t signature_size);

HD key operations

/*
 * HD context structure
 */
typedef struct EllipticHDContext {
    /*
     * Number of consequent parents for this HD node.
     */
    unsigned char nDepth;

    /*
     * Parent public key fingerprint.
     */
    unsigned char vchFingerprint[4];
    
    /*
     * Child index
     */
    unsigned int nChild;
    
    /*
     * Chain code, essentially a second half of BIP32 hash
     */
    unsigned char chaincode[32];
    
    /*
     * ECC context
     */
    EllipticContext context;
} EllipticHDContext;
/*
 * Initialize HD context with extended public key.
 */
int elliptic_hd_import_pub(EllipticHDContext *ctx, int type, const uint8_t binary[BIP32_EXTKEY_SIZE]);
/*
 * Initialize HD context with extended private key.
 */
int elliptic_hd_import_priv(EllipticHDContext *ctx, int type, const uint8_t binary[BIP32_EXTKEY_SIZE]);
/*
 * Export extended public key from context.
 */
int elliptic_hd_export_pub(const EllipticHDContext *ctx, uint8_t binary[BIP32_EXTKEY_SIZE]);
/*
 * Export extended private key from context.
 */
int elliptic_hd_export_priv(const EllipticHDContext *ctx, uint8_t binary[BIP32_EXTKEY_SIZE]);
/*
 * Initialization of new HD key derivation context with given seed data.
 * You must specify either EllipticED25519 or EllipticSecp256K1 for underlying curve type.
 */
int elliptic_hd_init(EllipticHDContext *ctx, int type, const uint8_t *seed, size_t seed_len);
/*
 * Derive children HD context.
 */
int elliptic_hd_derive(const EllipticHDContext *ctx, EllipticHDContext *child_ctx, unsigned int nChild, int priv);
/*
 * Initialize new context with a copy of public data.
 */
void elliptic_hd_neuter(const EllipticHDContext *ctx, EllipticHDContext *child_ctx);

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published