Skip to content

Commit

Permalink
[bls] add the bls curve to the curves handled
Browse files Browse the repository at this point in the history
 - gen pk
 - sign
 - parsable in operations
  • Loading branch information
spalmer25 committed Oct 17, 2024
1 parent 8183e5a commit 9823975
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
25 changes: 25 additions & 0 deletions src/keys.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "crypto_helpers.h"

#include "crypto.h"
#include "keys.h"

/***** Bip32 path *****/
Expand Down Expand Up @@ -79,6 +80,14 @@ cx_err_t generate_public_key(cx_ecfp_public_key_t *public_key,
CX_SHA512));
break;
}
case DERIVATION_TYPE_BLS12_381: {
public_key->curve = CX_CURVE_BLS12_381_G1;
public_key->W_len = 97;
CX_CHECK(bip32_derive_get_pubkey_bls(bip32_path->components,
bip32_path->length,
((cx_ecfp_384_public_key_t *) public_key)->W));
break;
}
default:
return CX_INVALID_PARAMETER;
}
Expand Down Expand Up @@ -132,6 +141,12 @@ static cx_err_t public_key_hash(uint8_t *const hash_out,
compressed->W[0] = 0x02 + (public_key->W[64] & 0x01);
break;
}
case DERIVATION_TYPE_BLS12_381: {
compressed->curve = public_key->curve;
compressed->W_len = 48;
memcpy(compressed->W, public_key->W + 1, compressed->W_len);
break;
}
default:
return CX_INVALID_PARAMETER;
}
Expand Down Expand Up @@ -232,6 +247,16 @@ cx_err_t sign(uint8_t *const out,
out[0] |= 0x01;
}
} break;
#ifndef TARGET_NANOS
case DERIVATION_TYPE_BLS12_381: {
CX_CHECK(bip32_derive_with_seed_bls_sign_hash(bip32_path->components,
bip32_path->length,
(uint8_t const *) PIC(in),
in_size,
out,
out_size));
} break;
#endif
default:
error = CX_INVALID_PARAMETER;
}
Expand Down
16 changes: 14 additions & 2 deletions src/keys.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,16 @@ typedef enum {
DERIVATION_TYPE_SECP256K1 = 0x01,
DERIVATION_TYPE_SECP256R1 = 0x02,
DERIVATION_TYPE_BIP32_ED25519 = 0x03,
DERIVATION_TYPE_UNSET = 0x04
DERIVATION_TYPE_BLS12_381 = 0x04,
DERIVATION_TYPE_UNSET = 0x05
} derivation_type_t;

typedef enum {
SIGNATURE_TYPE_ED25519 = 0,
SIGNATURE_TYPE_SECP256K1 = 1,
SIGNATURE_TYPE_SECP256R1 = 2,
SIGNATURE_TYPE_UNSET = 3
SIGNATURE_TYPE_BLS12_381 = 3,
SIGNATURE_TYPE_UNSET = 4
} signature_type_t;

#define DERIVATION_TYPE_IS_SET(type) \
Expand All @@ -77,6 +79,8 @@ static inline signature_type_t derivation_type_to_signature_type(
case DERIVATION_TYPE_ED25519:
case DERIVATION_TYPE_BIP32_ED25519:
return SIGNATURE_TYPE_ED25519;
case DERIVATION_TYPE_BLS12_381:
return SIGNATURE_TYPE_BLS12_381;
default:
return SIGNATURE_TYPE_UNSET;
}
Expand Down Expand Up @@ -188,6 +192,7 @@ static inline bool bip32_path_with_curve_eq(bip32_path_with_curve_t volatile con
*/
typedef union {
cx_ecfp_256_public_key_t pk_256; ///< edpk, sppk and p2pk keys
cx_ecfp_384_public_key_t pk_384; ///< BLpk keys
} tz_ecfp_public_key_t;

/**
Expand All @@ -212,12 +217,19 @@ typedef struct {
size_t W_len; ///< Compressed public key length in bytes
uint8_t W[33]; ///< Compressed public key value
} tz_ecfp_secp256_compressed_public_key_t;
/** BLS compressed public key */
typedef struct {
cx_curve_t curve; ///< Curve identifier
size_t W_len; ///< Compressed public key length in bytes
uint8_t W[48]; ///< Compressed public key value
} tz_ecfp_bls_compressed_public_key_t;
/**
* @brief This structure represents elliptic curve compressed public key handled
*/
typedef union {
tz_ecfp_ed25519_compressed_public_key_t pk_ed25519; ///< edpk keys
tz_ecfp_secp256_compressed_public_key_t pk_secp256; ///< sppk and p2pk keys
tz_ecfp_bls_compressed_public_key_t pk_bls; ///< BLpk keys
} tz_ecfp_compressed_public_key_t;

/**
Expand Down
1 change: 1 addition & 0 deletions src/operations.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ union public_key {
uint8_t edpk[32]; ///< raw public key for a edpk key
uint8_t sppk[33]; ///< raw public key for a sppk key
uint8_t p2pk[33]; ///< raw public key for a p2pk key
uint8_t blpk[48]; ///< raw public key for a BLpk key
} __attribute__((packed));

/**
Expand Down
5 changes: 5 additions & 0 deletions src/to_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ static int pkh_to_string(char *const dest,
data.prefix[1] = 161u;
data.prefix[2] = 164u;
break;
case SIGNATURE_TYPE_BLS12_381:
data.prefix[0] = 6u;
data.prefix[1] = 161u;
data.prefix[2] = 166u;
break;
default:
return -1;
}
Expand Down

0 comments on commit 9823975

Please sign in to comment.