Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
jleni committed May 10, 2024
1 parent d494f24 commit ee2ce4f
Show file tree
Hide file tree
Showing 31 changed files with 314 additions and 417 deletions.
4 changes: 3 additions & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ AlignConsecutiveMacros:
Enabled: true
AcrossEmptyLines: true
AcrossComments: false

BinPackParameters: false
AllowAllParametersOfDeclarationOnNextLine: true
PenaltyBreakBeforeFirstCallParameter: 99999
10 changes: 6 additions & 4 deletions app/rust/include/commitments.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
void compute_note_commitment(uint8_t *inputptr, const uint8_t *rcmptr, const uint64_t value, const uint8_t *diversifier_ptr,
const uint8_t *pkd);
#pragma once

void compute_note_commitment_fullpoint(uint8_t *inputptr, const uint8_t *rcmptr, const uint64_t value,
const uint8_t *diversifier_ptr, const uint8_t *pkd);
void compute_note_commitment(
uint8_t *inputptr, const uint8_t *rcmptr, const uint64_t value, const uint8_t *diversifier_ptr, const uint8_t *pkd);

void compute_note_commitment_fullpoint(
uint8_t *inputptr, const uint8_t *rcmptr, const uint64_t value, const uint8_t *diversifier_ptr, const uint8_t *pkd);

void compute_value_commitment(const uint64_t value, const uint8_t *rcmptr, uint8_t *output);

Expand Down
2 changes: 2 additions & 0 deletions app/rust/include/notes.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#pragma once

void rseed_get_esk_epk(const uint8_t *seed_ptr, uint8_t *d_ptr, uint8_t *output_esk_ptr, uint8_t *output_epk_ptr);

void rseed_get_rcm(const uint8_t *input, uint8_t *output_ptr);
Expand Down
39 changes: 23 additions & 16 deletions app/rust/include/zip32.h
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
void get_pkd(const uint8_t *seed_ptr, const uint32_t pos, const uint8_t *diversifier_ptr, uint8_t *pkd);
#pragma once

void
get_pkd_from_seed(const uint8_t *seed_ptr, const uint32_t pos, const uint8_t *start_index, uint8_t *diversifier_ptr,
uint8_t *pkd);
#define ZIP32_SIZE 32

void get_diversifier_list(const uint8_t *sk_ptr, uint8_t *diversifier_list);
void get_pkd(const uint8_t (*seed_ptr)[ZIP32_SIZE],
uint32_t zip32_account,
const uint8_t *diversifier_ptr,
uint8_t (*pkd)[32]);

void get_pkd_from_seed(const uint8_t (*seed_ptr)[ZIP32_SIZE],
uint32_t zip32_account,
const uint8_t *start_index,
uint8_t *diversifier_ptr,
uint8_t (*pkd)[32]);

bool diversifier_is_valid(const uint8_t *diversifier);

void get_diversifier_list_withstartindex(const uint8_t *seed_ptr, const uint32_t pos, const uint8_t *startindex,
uint8_t *diversifier_list);
void diversifier_get_list(const uint8_t (*seed_ptr)[ZIP32_SIZE],
uint32_t zip32_account,
const uint8_t *startindex,
uint8_t *diversifier_list);

void
get_default_diversifier_without_start_index(const uint8_t *see_ptr, const uint32_t pos, uint8_t *default_diversifier);
void diversifier_find_valid(const uint8_t (*seed_ptr)[ZIP32_SIZE], uint32_t zip32_account, uint8_t *default_diversifier);

void zip32_child_ask_nsk(const uint8_t *seed_ptr, uint8_t *ask, uint8_t *nsk, const uint32_t pos);
void zip32_child_ask_nsk(const uint8_t (*seed_ptr)[ZIP32_SIZE], uint32_t pos, uint8_t *ask, uint8_t *nsk);

void zip32_nsk_from_seed(const uint8_t *seed_ptr, uint32_t zip32_account, uint8_t *nsk);
void zip32_nsk_from_seed(const uint8_t (*seed_ptr)[ZIP32_SIZE], uint32_t zip32_account, uint8_t *nsk);

void zip32_ovk(const uint8_t *seed_ptr, const uint32_t zip32_account, uint8_t *ovk);
void zip32_ovk(const uint8_t (*seed_ptr)[ZIP32_SIZE], uint32_t zip32_account, uint8_t *ovk);

void zip32_child_proof_key(const uint8_t *seed_ptr, const uint32_t account, uint8_t *ak_ptr, uint8_t *nsk_ptr);
void zip32_child_proof_key(const uint8_t (*seed_ptr)[ZIP32_SIZE], uint32_t account, uint8_t *ak_ptr, uint8_t *nsk_ptr);

void zip32_ivk(const uint8_t (*seed_ptr)[ZIP32_SIZE], uint32_t zip32_account, uint8_t *ivk);

///////////////////////////// checked
void zip32_ivk(const uint8_t *seed_ptr, const uint32_t zip32_account, uint8_t *ivk);
void zip32_fvk(const uint8_t *seed_ptr, uint32_t pos, uint8_t *fvk_ptr);
void zip32_fvk(const uint8_t (*seed_ptr)[ZIP32_SIZE], uint32_t zip32_account, uint8_t *fvk_ptr);
53 changes: 26 additions & 27 deletions app/rust/src/zip32_extern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ use crate::types::{
diversifier_zero, Diversifier, DiversifierList10, DiversifierList20, DiversifierList4,
FullViewingKey, IvkBytes, NskBytes, Zip32Seed,
};
use crate::zip32::{
diversifier_find_valid, diversifier_group_hash_light, zip32_sapling_derive, zip32_sapling_fvk,
use crate::zip32::{diversifier_group_hash_light, zip32_sapling_derive, zip32_sapling_fvk,
};
use crate::{sapling, zip32};

Expand All @@ -34,20 +33,20 @@ pub extern "C" fn zip32_ivk(seed_ptr: *const Zip32Seed, account: u32, ivk_ptr: *
// This only tries to find ONE diversifier!!!
// Related to handleGetKeyIVK
#[no_mangle]
pub extern "C" fn get_default_diversifier_without_start_index(
pub extern "C" fn diversifier_find_valid(
seed_ptr: *const [u8; 32],
account: u32,
zip32_account: u32,
div_ptr: *mut Diversifier,
) {
let seed = unsafe { &*seed_ptr };
let path = [ZIP32_PURPOSE, ZIP32_COIN_TYPE, account];
let path = [ZIP32_PURPOSE, ZIP32_COIN_TYPE, zip32_account];
let div_out = unsafe { &mut *div_ptr };

let key_bundle = zip32_sapling_derive(seed, &path);
let dk = key_bundle.dk();

let start = diversifier_zero();
div_out.copy_from_slice(&diversifier_find_valid(&dk, &start));
div_out.copy_from_slice(&zip32::diversifier_find_valid(&dk, &start));
}

//this function is consistent with zecwallet code
Expand Down Expand Up @@ -136,14 +135,14 @@ pub extern "C" fn zip32_nsk_from_seed(
// This will generate a list of 20 diversifiers starting from the given diversifier
// related to handleGetDiversifierList
#[no_mangle]
pub extern "C" fn get_diversifier_list_withstartindex(
pub extern "C" fn diversifier_get_list(
seed_ptr: *const [u8; 32],
account: u32,
zip32_account: u32,
start_index: *const Diversifier,
diversifier_list_ptr: *mut DiversifierList20,
) {
let seed = unsafe { &*seed_ptr };
let path = [ZIP32_PURPOSE, ZIP32_COIN_TYPE, account];
let path = [ZIP32_PURPOSE, ZIP32_COIN_TYPE, zip32_account];
let start = unsafe { &*start_index };
let diversifier = unsafe { &mut *diversifier_list_ptr };

Expand All @@ -152,6 +151,23 @@ pub extern "C" fn get_diversifier_list_withstartindex(
zip32::diversifier_get_list_large(&key_bundle.dk(), start, diversifier);
}

#[no_mangle]
pub extern "C" fn get_pkd(
seed_ptr: *const Zip32Seed,
account: u32,
diversifier_ptr: *const Diversifier,
pkd_ptr: *mut [u8; 32],
) {
let ivk_ptr = &mut [0u8; 32];
let diversifier = unsafe { &*diversifier_ptr };
let pkd = unsafe { &mut *pkd_ptr };

zip32_ivk(seed_ptr, account, ivk_ptr);

let tmp_pkd = zip32::pkd_default(ivk_ptr, diversifier);
pkd.copy_from_slice(&tmp_pkd)
}

#[no_mangle]
pub extern "C" fn get_pkd_from_seed(
seed_ptr: *const Zip32Seed,
Expand All @@ -168,7 +184,7 @@ pub extern "C" fn get_pkd_from_seed(
let key_bundle = zip32_sapling_derive(seed, &path);
let dk = key_bundle.dk();

div_out.copy_from_slice(&diversifier_find_valid(&dk, start));
div_out.copy_from_slice(&zip32::diversifier_find_valid(&dk, start));

let ivk = sapling_asknsk_to_ivk(&key_bundle.ask(), &key_bundle.nsk());
let tmp_pkd = zip32::pkd_default(&ivk, div_out);
Expand All @@ -177,23 +193,6 @@ pub extern "C" fn get_pkd_from_seed(
pkd_out.copy_from_slice(&tmp_pkd);
}

#[no_mangle]
pub extern "C" fn get_pkd(
seed_ptr: *const [u8; 32],
account: u32,
diversifier_ptr: *const Diversifier,
pkd_ptr: *mut [u8; 32],
) {
let ivk_ptr = &mut [0u8; 32];
let diversifier = unsafe { &*diversifier_ptr };
let pkd = unsafe { &mut *pkd_ptr };

zip32_ivk(seed_ptr, account, ivk_ptr);

let tmp_pkd = zip32::pkd_default(ivk_ptr, diversifier);
pkd.copy_from_slice(&tmp_pkd)
}

#[no_mangle]
pub extern "C" fn randomized_secret_from_seed(
seed_ptr: *const [u8; 32],
Expand Down
7 changes: 6 additions & 1 deletion app/src/addr.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ zxerr_t addr_getNumItems(uint8_t *num_items) {
return zxerr_ok;
}

zxerr_t addr_getItem(int8_t displayIdx, char *outKey, uint16_t outKeyLen, char *outVal, uint16_t outValLen, uint8_t pageIdx,
zxerr_t addr_getItem(int8_t displayIdx,
char *outKey,
uint16_t outKeyLen,
char *outVal,
uint16_t outValLen,
uint8_t pageIdx,
uint8_t *pageCount) {
ZEMU_LOGF(200, "[addr_getItem] %d/%d\n", displayIdx, pageIdx)

Expand Down
11 changes: 8 additions & 3 deletions app/src/addr.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
#pragma once
#include <stdint.h>

#include "zxerror.h"
#include "coin.h"
#include "zxerror.h"

#ifdef __cplusplus
extern "C" {
Expand All @@ -33,8 +33,13 @@ typedef struct {
zxerr_t addr_getNumItems(uint8_t *num_items);

/// Gets an specific item from the address view (including paging)
zxerr_t addr_getItem(int8_t displayIdx, char *outKey, uint16_t outKeyLen, char *outValue, uint16_t outValueLen,
uint8_t pageIdx, uint8_t *pageCount);
zxerr_t addr_getItem(int8_t displayIdx,
char *outKey,
uint16_t outKeyLen,
char *outValue,
uint16_t outValueLen,
uint8_t pageIdx,
uint8_t *pageCount);

#ifdef __cplusplus
}
Expand Down
26 changes: 20 additions & 6 deletions app/src/c_api/rust.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ zxerr_t c_zcash_blake2b_expand_seed(const uint8_t *a, uint32_t a_len, const uint
return zxerr_ok;
}

zxerr_t c_zcash_blake2b_expand_vec_two(const uint8_t *a, uint32_t a_len, const uint8_t *b, uint32_t b_len, const uint8_t *c,
uint32_t c_len, uint8_t *out) {
zxerr_t c_zcash_blake2b_expand_vec_two(
const uint8_t *a, uint32_t a_len, const uint8_t *b, uint32_t b_len, const uint8_t *c, uint32_t c_len, uint8_t *out) {
if (a == NULL || b == NULL || c == NULL || out == NULL) {
return zxerr_no_data;
}
Expand All @@ -102,8 +102,16 @@ zxerr_t c_zcash_blake2b_expand_vec_two(const uint8_t *a, uint32_t a_len, const u
return zxerr_ok;
}

zxerr_t c_zcash_blake2b_expand_vec_four(const uint8_t *a, uint32_t a_len, const uint8_t *b, uint32_t b_len, const uint8_t *c,
uint32_t c_len, const uint8_t *d, uint32_t d_len, const uint8_t *e, uint32_t e_len,
zxerr_t c_zcash_blake2b_expand_vec_four(const uint8_t *a,
uint32_t a_len,
const uint8_t *b,
uint32_t b_len,
const uint8_t *c,
uint32_t c_len,
const uint8_t *d,
uint32_t d_len,
const uint8_t *e,
uint32_t e_len,
uint8_t *out) {
if (a == NULL || b == NULL || c == NULL || d == NULL || e == NULL || out == NULL) {
return zxerr_no_data;
Expand All @@ -120,8 +128,14 @@ zxerr_t c_zcash_blake2b_expand_vec_four(const uint8_t *a, uint32_t a_len, const
return zxerr_ok;
}

zxerr_t zcash_blake2b_hash_two(const uint8_t *perso, uint32_t perso_len, const uint8_t *a, uint32_t a_len, const uint8_t *b,
uint32_t b_len, uint8_t *out, uint32_t out_len) {
zxerr_t zcash_blake2b_hash_two(const uint8_t *perso,
uint32_t perso_len,
const uint8_t *a,
uint32_t a_len,
const uint8_t *b,
uint32_t b_len,
uint8_t *out,
uint32_t out_len) {
if (perso == NULL || a == NULL || b == NULL || out == NULL) {
return zxerr_no_data;
}
Expand Down
7 changes: 6 additions & 1 deletion app/src/common/tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,12 @@ zxerr_t tx_getNumItems(uint8_t *num_items) {
return zxerr_ok;
}

zxerr_t tx_getItem(int8_t displayIdx, char *outKey, uint16_t outKeyLen, char *outVal, uint16_t outValLen, uint8_t pageIdx,
zxerr_t tx_getItem(int8_t displayIdx,
char *outKey,
uint16_t outKeyLen,
char *outVal,
uint16_t outValLen,
uint8_t pageIdx,
uint8_t *pageCount) {
uint8_t numItems = 0;

Expand Down
9 changes: 7 additions & 2 deletions app/src/common/tx.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,10 @@ const char *tx_parse();
zxerr_t tx_getNumItems(uint8_t *num_items);

/// Gets an specific item from the transaction (including paging)
zxerr_t tx_getItem(int8_t displayIdx, char *outKey, uint16_t outKeyLen, char *outValue, uint16_t outValueLen,
uint8_t pageIdx, uint8_t *pageCount);
zxerr_t tx_getItem(int8_t displayIdx,
char *outKey,
uint16_t outKeyLen,
char *outValue,
uint16_t outValueLen,
uint8_t pageIdx,
uint8_t *pageCount);
4 changes: 2 additions & 2 deletions app/src/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
#pragma once

// CRYPTO File
#define CHECKSUM_LENGTH 4
#define CHECKSUM_LENGTH 4

#define ED25519_SK_SIZE 64
#define ED25519_SK_SIZE 64

// FIXME: there is a difference in the seed size with rust
#define ZIP32_SEED_SIZE 64
Expand Down

0 comments on commit ee2ce4f

Please sign in to comment.