Skip to content

Commit

Permalink
improve difersifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
jleni committed May 9, 2024
1 parent 23de48b commit f265bfc
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 247 deletions.
9 changes: 1 addition & 8 deletions app/rust/include/zip32.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,21 @@ get_pkd_from_seed(const uint8_t *seed_ptr, const uint32_t pos, const uint8_t *st

void get_diversifier_list(const uint8_t *sk_ptr, uint8_t *diversifier_list);

void get_diversifier_fromlist(const uint8_t *diversifier_list, uint8_t *diversifier);

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 get_default_diversifier_list_withstartindex(const uint8_t *seed_ptr, const uint32_t pos, 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 zip32_master(const uint8_t *seed_ptr, uint8_t *sk_ptr, uint8_t *dk_ptr);

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

void zip32_nsk_from_seed(const uint8_t *seed_ptr, 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_child_proof_key(const uint8_t *seed_ptr, uint8_t *ak_ptr, uint8_t *nsk_ptr, const uint32_t pos);
void zip32_child_proof_key(const uint8_t *seed_ptr, const uint32_t account, uint8_t *ak_ptr, uint8_t *nsk_ptr);


///////////////////////////// checked
Expand Down
21 changes: 10 additions & 11 deletions app/rust/src/commitments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub fn group_hash_from_diversifier(diversifier_ptr: *const Diversifier, gd_ptr:
let diversifier = unsafe { &*diversifier_ptr };
let gd = unsafe { &mut *gd_ptr };
let gd_tmp = zip32::pkd_group_hash(diversifier);

gd.copy_from_slice(&gd_tmp);
}

Expand Down Expand Up @@ -100,7 +101,7 @@ pub fn value_commitment_step2(rcm: &[u8; 32]) -> ExtendedPoint {
#[cfg(test)]
mod tests {
use crate::commitments_extern::{compute_note_commitment, compute_nullifier};
use crate::types::diversifier_zero;
use crate::types::{diversifier_zero, NskBytes};
use crate::utils::into_fixed_array;

use super::*;
Expand Down Expand Up @@ -243,7 +244,7 @@ mod tests {

#[test]
fn test_get_nf() {
let account = 1;
let account = 0;
let pos: u64 = 2578461368;

let seed: [u8; 32] = [
Expand All @@ -257,18 +258,16 @@ mod tests {
0xce, 0x11, 0xdd, 0xe8,
];

let mut nsk = [0u8; 32];
zip32_nsk_from_seed(&seed, account, &mut nsk);

let mut nsk: NskBytes = [0u8; 32];
let mut nf = [0u8; 32];

zip32_nsk_from_seed(&seed, account, &mut nsk);
compute_nullifier(&cm, pos, &nsk, &mut nf);

let nftest: [u8; 32] = [
0x25, 0xf1, 0xf2, 0xcf, 0x5e, 0x2c, 0x2b, 0xc3, 0x1d, 0x7, 0xb6, 0x6f, 0x4d, 0x54,
0xf0, 0x90, 0xad, 0x89, 0xb1, 0x98, 0x89, 0x3f, 0x12, 0xad, 0xae, 0x44, 0x7d, 0xdf,
0x84, 0xe2, 0x14, 0x5a,
];
assert_eq!(nf, nftest);
assert_eq!(
hex::encode(nf),
"ce0df155d652565ccab59ae392a569c4f2283df4dc8a26bfd48e178bfceed436"
);
}

#[test]
Expand Down
6 changes: 3 additions & 3 deletions app/rust/src/commitments_extern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::bolos::c_zemu_log_stack;
use crate::cryptoops::{add_to_point, extended_to_bytes, extended_to_u_bytes};
use crate::pedersen::multiply_with_pedersen_base;
use crate::sapling::sapling_nsk_to_nk;
use crate::types::Diversifier;
use crate::types::{Diversifier, NfBytes, NskBytes};
use crate::{commitments, cryptoops};
use jubjub::Fr;

Expand All @@ -12,8 +12,8 @@ use jubjub::Fr;
pub extern "C" fn compute_nullifier(
ncm_ptr: *const [u8; 32],
note_pos: u64,
nsk_ptr: *const [u8; 32],
output_ptr: *mut [u8; 32],
nsk_ptr: *const NskBytes,
output_ptr: *mut NfBytes,
) {
c_zemu_log_stack(b"compute_nullifier\x00".as_ref());
let ncm = unsafe { *ncm_ptr };
Expand Down
6 changes: 4 additions & 2 deletions app/rust/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ pub type DiversifierList10 = [u8; 110];

pub type DiversifierList20 = [u8; 220];

pub fn diversifier_list10_zero() -> DiversifierList10 {
[0u8; 110]
pub fn diversifier_list20_zero() -> DiversifierList20 {
[0u8; 220]
}

pub type AskBytes = [u8; 32];
Expand All @@ -30,6 +30,8 @@ pub type OvkBytes = [u8; 32];

pub type DkBytes = [u8; 32];

pub type NfBytes = [u8; 32];

// This can be between 32 and 252 bytes
// FIXME: move to 64 to align with ed25519 private key?
pub type Zip32Seed = [u8; 32];
Expand Down

0 comments on commit f265bfc

Please sign in to comment.