Skip to content

Commit

Permalink
Combine the computation of the extended full viewing key parts.
Browse files Browse the repository at this point in the history
  • Loading branch information
murisi committed Aug 30, 2024
1 parent 0db9de4 commit 467c9b9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 24 deletions.
1 change: 1 addition & 0 deletions app/rust/include/rslib.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ void diversifier_find_valid(uint32_t zip32_account, uint8_t *default_diversifier
void zip32_dk(uint32_t zip32_account, uint8_t *dk);
void zip32_chain_code(uint32_t zip32_account, uint8_t *chain_code);
void zip32_parent_fvk_tag(uint32_t zip32_account, uint8_t *fvk_tag);
void zip32_xfvk(uint32_t zip32_account, uint8_t *fvk_tag, uint8_t *chain_code, uint8_t *fvk, uint8_t *dk);
23 changes: 23 additions & 0 deletions app/rust/src/zip32_extern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,26 @@ pub extern "C" fn zip32_parent_fvk_tag(

fvk_tag.copy_from_slice(&tag);
}

#[no_mangle]
pub extern "C" fn zip32_xfvk(
account: u32,
fvk_tag_ptr: *mut FvkTagBytes,
cc_ptr: *mut Zip32MasterChainCode,
fvk_ptr: *mut FullViewingKey,
dk_ptr: *mut DkBytes,
) {
let path = [ZIP32_PURPOSE, ZIP32_COIN_TYPE, account];
let fvk_tag = unsafe { &mut *fvk_tag_ptr };
let cc = unsafe { &mut *cc_ptr };
let fvk_out = unsafe { &mut *fvk_ptr };
let dk = unsafe { &mut *dk_ptr };

let (key_bundle, chain_code, tag) = zip32_sapling_derive(&path);

fvk_tag.copy_from_slice(&tag);
cc.copy_from_slice(&chain_code);
let fvk = zip32_sapling_fvk(&key_bundle);
fvk_out.to_bytes_mut().copy_from_slice(fvk.to_bytes());
dk.copy_from_slice(&key_bundle.dk());
}
26 changes: 6 additions & 20 deletions app/src/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -487,27 +487,15 @@ static zxerr_t computeKeys(keys_t * saplingKeys) {
return zxerr_no_data;
}

// Compute ask, nsk, ovk
zip32_child_ask_nsk(hdPath[2], saplingKeys->ask, saplingKeys->nsk);
zip32_ovk(hdPath[2], saplingKeys->ovk);
// Compute chain code, fvk, parent fvk tag, dk
zip32_xfvk(hdPath[2], saplingKeys->parent_fvk_tag, saplingKeys->chain_code, saplingKeys->fvk, saplingKeys->dk);

// Compute ak, nk, ivk
CHECK_PARSER_OK(generate_key(saplingKeys->ask, SpendingKeyGenerator, saplingKeys->ak));
CHECK_PARSER_OK(generate_key(saplingKeys->nsk, ProofGenerationKeyGenerator, saplingKeys->nk));
CHECK_PARSER_OK(computeIVK(saplingKeys->ak, saplingKeys->nk, saplingKeys->ivk));
// Compute ask, nsk
zip32_child_ask_nsk(hdPath[2], saplingKeys->ask, saplingKeys->nsk);

// Compute diversifier
diversifier_find_valid(hdPath[2], saplingKeys->diversifier);

// Compute dk
zip32_dk(hdPath[2], saplingKeys->dk);

// Compute chain code
zip32_chain_code(hdPath[2], saplingKeys->chain_code);

// Compute parent full viewing key tag
zip32_parent_fvk_tag(hdPath[2], saplingKeys->parent_fvk_tag);

// Compute address
get_pkd(hdPath[2], saplingKeys->diversifier, saplingKeys->address);

Expand Down Expand Up @@ -535,17 +523,15 @@ __Z_INLINE zxerr_t copyKeys(keys_t *saplingKeys, key_kind_e requestedKeys, uint8
memcpy(output + 1, saplingKeys->parent_fvk_tag, TAG_LENGTH);
memcpy(output + 5, &hdPath[hdPathLen - 1], TAG_LENGTH);
memcpy(output + 9, saplingKeys->chain_code, KEY_LENGTH);
memcpy(output + 41, saplingKeys->ak, KEY_LENGTH);
memcpy(output + 73, saplingKeys->nk, KEY_LENGTH);
memcpy(output + 105, saplingKeys->ovk, KEY_LENGTH);
memcpy(output + 41, saplingKeys->fvk, KEY_LENGTH*3);
memcpy(output + 137, saplingKeys->dk, KEY_LENGTH);
break;

case ProofGenerationKey:
if (outputLen < 2 * KEY_LENGTH) {
return zxerr_buffer_too_small;
}
memcpy(output, saplingKeys->ak, KEY_LENGTH);
memcpy(output, saplingKeys->fvk, KEY_LENGTH);
memcpy(output + KEY_LENGTH, saplingKeys->nsk, KEY_LENGTH);
break;

Expand Down
6 changes: 2 additions & 4 deletions app/src/keys_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,14 @@ typedef uint8_t ivk_t[KEY_LENGTH];
typedef uint8_t ovk_t[KEY_LENGTH];
typedef uint8_t d_t[DIVERSIFIER_LENGTH];
typedef uint8_t fvk_tag_t[TAG_LENGTH];
typedef uint8_t fvk_t[KEY_LENGTH*3];

typedef uint8_t public_address_t[KEY_LENGTH];

typedef struct {
ask_t ask;
ak_t ak;
nsk_t nsk;
nk_t nk;
ivk_t ivk;
ovk_t ovk;
fvk_t fvk;
d_t diversifier;
dk_t dk;
chain_code_t chain_code;
Expand Down

0 comments on commit 467c9b9

Please sign in to comment.