Skip to content

Commit

Permalink
[bls] add bls
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 dcbd91b commit 88b6299
Show file tree
Hide file tree
Showing 4 changed files with 44 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
15 changes: 13 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) ((0 <= type) && (type < DERIVATION_TYPE_UNSET))
Expand All @@ -75,6 +77,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 @@ -186,6 +190,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;

typedef cx_ecfp_public_key_t cx_ecfp_compressed_public_key_t;
Expand All @@ -205,9 +210,15 @@ typedef struct {
size_t W_len;
uint8_t W[33];
} tz_ecfp_secp256_compressed_public_key_t;
typedef struct {
cx_curve_t curve;
size_t W_len;
uint8_t W[48];
} tz_ecfp_bls_compressed_public_key_t;
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 88b6299

Please sign in to comment.