-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimize bls #122
base: main
Are you sure you want to change the base?
Optimize bls #122
Changes from all commits
fce94db
033fecb
2e20ccf
47c61e5
9146ccf
1c55f5a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,21 +33,41 @@ | |
#include <stdint.h> | ||
#include <string.h> | ||
|
||
int provide_pubkey(bip32_path_with_curve_t const* const path_with_curve) { | ||
tz_exc read_path_with_curve(derivation_type_t derivation_type, | ||
buffer_t* buf, | ||
bip32_path_with_curve_t* path_with_curve, | ||
cx_ecfp_public_key_t* pubkey) { | ||
tz_exc exc = SW_OK; | ||
cx_err_t error = CX_OK; | ||
bip32_path_with_curve_t tmp_path_with_curve = {0}; | ||
|
||
TZ_ASSERT_NOT_NULL(buf); | ||
TZ_ASSERT_NOT_NULL(path_with_curve); | ||
|
||
uint8_t resp[1u + MAX_SIGNATURE_SIZE] = {0}; | ||
size_t offset = 0; | ||
tmp_path_with_curve.derivation_type = derivation_type; | ||
TZ_ASSERT(read_bip32_path(buf, &tmp_path_with_curve.bip32_path), EXC_WRONG_VALUES); | ||
|
||
// Do not derive the public key if the two path_with_curve are equal | ||
if (!bip32_path_with_curve_eq(path_with_curve, &tmp_path_with_curve)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems my previous comment is addressed here. you can still think if a setup_path_with_curve makes sense. |
||
memmove(path_with_curve, &tmp_path_with_curve, sizeof(bip32_path_with_curve_t)); | ||
if (pubkey != NULL) { | ||
CX_CHECK(generate_public_key(pubkey, path_with_curve)); | ||
} | ||
} | ||
|
||
// Application could be PIN-locked, and pubkey->W_len would then be 0, | ||
// so throwing an error rather than returning an empty key | ||
TZ_ASSERT(os_global_pin_is_validated() == BOLOS_UX_OK, EXC_SECURITY); | ||
end: | ||
TZ_CONVERT_CX(); | ||
return exc; | ||
} | ||
|
||
cx_ecfp_public_key_t* pubkey = (cx_ecfp_public_key_t*) &(tz_ecfp_public_key_t){0}; | ||
CX_CHECK(generate_public_key(pubkey, path_with_curve)); | ||
int provide_pubkey(cx_ecfp_public_key_t const* const pubkey) { | ||
tz_exc exc = SW_OK; | ||
cx_err_t error = CX_OK; | ||
|
||
TZ_ASSERT_NOT_NULL(pubkey); | ||
|
||
uint8_t resp[1u + MAX_SIGNATURE_SIZE] = {0}; | ||
size_t offset = 0; | ||
|
||
resp[offset] = pubkey->W_len; | ||
offset++; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -161,12 +161,13 @@ int select_signing_key(buffer_t *cdata, derivation_type_t derivation_type) { | |
|
||
clear_data(); | ||
|
||
TZ_ASSERT(read_bip32_path(cdata, &global.path_with_curve.bip32_path), EXC_WRONG_VALUES); | ||
TZ_CHECK(read_path_with_curve(derivation_type, | ||
cdata, | ||
&global.path_with_curve, | ||
(cx_ecfp_public_key_t *) &global.public_key)); | ||
|
||
TZ_ASSERT(cdata->size == cdata->offset, EXC_WRONG_LENGTH); | ||
|
||
global.path_with_curve.derivation_type = derivation_type; | ||
|
||
return io_send_sw(SW_OK); | ||
|
||
end: | ||
|
@@ -214,27 +215,49 @@ int handle_sign(buffer_t *cdata, const bool last, const bool with_hash) { | |
break; | ||
case MAGIC_BYTE_UNSAFE_OP: | ||
// Parse the operation. It will be verified in `baking_sign_complete`. | ||
TZ_CHECK(parse_operations(cdata, &G.maybe_ops.v, &global.path_with_curve)); | ||
TZ_CHECK(parse_operations(cdata, | ||
&G.maybe_ops.v, | ||
(cx_ecfp_public_key_t *) &global.public_key)); | ||
break; | ||
default: | ||
TZ_FAIL(EXC_PARSE_ERROR); | ||
} | ||
|
||
CX_CHECK( | ||
cx_hash_no_throw((cx_hash_t *) &G.hash_state.state, 0, cdata->ptr, cdata->size, NULL, 0)); | ||
#ifndef TARGET_NANOS | ||
// There is no need to hash the message if it is not used for signing or if it is not sent at | ||
// the end. | ||
if (with_hash || (global.path_with_curve.derivation_type != DERIVATION_TYPE_BLS12_381)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the purpose of with_hash here. Is it used for BLS. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean if the derivation type is BLS dont hash at all. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
#endif | ||
CX_CHECK(cx_hash_no_throw((cx_hash_t *) &G.hash_state.state, | ||
0, | ||
cdata->ptr, | ||
cdata->size, | ||
NULL, | ||
0)); | ||
#ifndef TARGET_NANOS | ||
} | ||
#endif | ||
|
||
#ifndef TARGET_NANOS | ||
memmove(G.message, cdata->ptr, cdata->size); | ||
G.message_len = cdata->size; | ||
#endif | ||
|
||
if (last) { | ||
CX_CHECK(cx_hash_no_throw((cx_hash_t *) &G.hash_state.state, | ||
CX_LAST, | ||
NULL, | ||
0, | ||
G.final_hash, | ||
sizeof(G.final_hash))); | ||
#ifndef TARGET_NANOS | ||
// There is no need to hash the message if it is not used for signing or if it is not sent | ||
// at the end. | ||
if (with_hash || (global.path_with_curve.derivation_type != DERIVATION_TYPE_BLS12_381)) { | ||
#endif | ||
CX_CHECK(cx_hash_no_throw((cx_hash_t *) &G.hash_state.state, | ||
CX_LAST, | ||
NULL, | ||
0, | ||
G.final_hash, | ||
sizeof(G.final_hash))); | ||
#ifndef TARGET_NANOS | ||
} | ||
#endif | ||
|
||
G.maybe_ops.is_valid = parse_operations_final(&G.parse_state, &G.maybe_ops.v); | ||
|
||
|
@@ -288,7 +311,12 @@ static int perform_signature(bool const send_hash) { | |
|
||
size_t signature_size = MAX_SIGNATURE_SIZE; | ||
|
||
CX_CHECK(sign(resp + offset, &signature_size, &global.path_with_curve, message, message_len)); | ||
CX_CHECK(sign(resp + offset, | ||
&signature_size, | ||
&global.path_with_curve, | ||
(cx_ecfp_public_key_t *) &global.public_key, | ||
message, | ||
message_len)); | ||
|
||
offset += signature_size; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The derivation type is selected at the time of setup. It can not be changed during signing. so it makes sense to derivce the public key when read_path_with_curve is called from handle_setup. Rather create a new function setup_path_with_curve and derive public key there along with a call to read_path_with_curve. Call setup_path_with_curve from handle_setup and read_path_with_curve from other places.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
SETUP
instruction is not required to sign:AUTHORIZE_BAKING
will setup the authorized key too.SIGN
with index 0x00 will tell which public key to sign withThe aims of this function is too make sure that, every time the stored Bip32-path-with-curve is updated, the stored PK is updated to.