Skip to content

Commit

Permalink
Add kappa, refactor parse_prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
AllFi committed May 15, 2023
1 parent 3e3b0a4 commit 3c21aa6
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 18 deletions.
8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@ default-members = [
"libzkbob-rs",
"libzkbob-rs-node",
"libs/kvdb-web",
]
]

[patch."https://github.com/zkbob/libzeropool-zkbob"]
libzeropool = { git = "https://github.com/zkbob//libzeropool-zkbob", branch = "feature/encryption-optimization", package = "libzeropool-zkbob" }

[patch.crates-io]
libzeropool = { git = "https://github.com/zkbob//libzeropool-zkbob", branch = "feature/encryption-optimization", package = "libzeropool-zkbob" }
2 changes: 1 addition & 1 deletion libzkbob-rs-node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ exclude = ["index.node"]
crate-type = ["cdylib"]

[dependencies]
libzkbob-rs = { version = "1.0.0", features = ["native"] }
libzkbob-rs = { path = "../libzkbob-rs", features = ["native"] }
#libzkbob-rs = { path = "../libzkbob-rs", features = ["native"] }
neon = { version = "0.10.0", default-features = false, features = ["channel-api", "napi-6", "promise-api", "task-api"] }
# FIXME: Using a random fork for now
Expand Down
2 changes: 2 additions & 0 deletions libzkbob-rs-wasm/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ impl UserAccount {
.into_iter()
.map(|bulk| -> Vec<ParseResult> {
let eta = &self.inner.borrow().keys.eta;
let kappa = &self.inner.borrow().keys.kappa;
let params = &self.inner.borrow().params;
let range = from_index.unwrap_or(0)..to_index.unwrap_or(u64::MAX);
let bulk_results: Vec<ParseResult> = vec_into_iter(bulk.txs)
Expand All @@ -486,6 +487,7 @@ impl UserAccount {
&tx.memo,
Some(&tx.tx_hash),
eta,
kappa,
params
).ok()
})
Expand Down
28 changes: 16 additions & 12 deletions libzkbob-rs-wasm/src/client/tx_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use libzkbob_rs::libzeropool::{
self,
symcipher_decryption_keys,
decrypt_account_no_validate,
decrypt_note_no_validate
decrypt_note_no_validate, Version
},
key::{
self,derive_key_p_d
Expand All @@ -21,7 +21,6 @@ use libzkbob_rs::{
keys::Keys,
utils::zero_account,
delegated_deposit::{
DELEGATED_DEPOSIT_FLAG,
MEMO_DELEGATED_DEPOSIT_SIZE,
MemoDelegatedDeposit
}
Expand Down Expand Up @@ -127,7 +126,9 @@ impl TxParser {
let sk = Num::<Fs>::from_uint(NumRepr(Uint::from_little_endian(sk)))
.ok_or_else(|| js_err!("Invalid spending key"))?;
let params = &self.params;
let eta = Keys::derive(sk, params).eta;
let keys = Keys::derive(sk, params);
let eta = keys.eta;
let kappa = &keys.kappa;

let txs: Vec<IndexedTx> = serde_wasm_bindgen::from_value(txs.to_owned()).map_err(|err| js_err!(&err.to_string()))?;

Expand All @@ -137,7 +138,7 @@ impl TxParser {
let memo = hex::decode(memo).unwrap();
let commitment = hex::decode(commitment).unwrap();

parse_tx(index, &commitment, &memo, None, &eta, params)
parse_tx(index, &commitment, &memo, None, &eta, kappa, params)
})
.partition(Result::is_ok);

Expand Down Expand Up @@ -184,9 +185,11 @@ impl TxParser {
) -> Result<Vec<TxMemoChunk>, JsValue> {
let sk = Num::<Fs>::from_uint(NumRepr(Uint::from_little_endian(sk)))
.ok_or_else(|| js_err!("Invalid spending key"))?;
let eta = Keys::derive(sk, &self.params).eta;
let keys = Keys::derive(sk, &self.params);
let eta = keys.eta;
let kappa = keys.kappa;
//(index, chunk, key)
let result = symcipher_decryption_keys(eta, memo, &self.params).unwrap_or(vec![]);
let result = symcipher_decryption_keys(eta, &kappa, memo, &self.params).unwrap_or(vec![]);

let chunks = result
.iter()
Expand Down Expand Up @@ -232,6 +235,7 @@ pub fn parse_tx(
memo: &Vec<u8>,
tx_hash: Option<&Vec<u8>>,
eta: &Num<Fr>,
kappa: &[u8; 32],
params: &PoolParams
) -> Result<ParseResult, ParseError> {
if memo.len() < 4 {
Expand Down Expand Up @@ -318,7 +322,7 @@ pub fn parse_tx(
.take(num_hashes as usize)
.map(|bytes| Num::from_uint_reduced(NumRepr(Uint::from_little_endian(bytes))));

let pair = cipher::decrypt_out(*eta, &memo, params);
let pair = cipher::decrypt_out(*eta, kappa, &memo, params);

match pair {
Some((account, notes)) => {
Expand Down Expand Up @@ -403,10 +407,10 @@ pub fn parse_tx(
}

fn parse_prefix(memo: &[u8]) -> (bool, u32) {
let prefix = (&memo[0..4]).read_u32::<LittleEndian>().unwrap();
let is_delegated_deposit = prefix & DELEGATED_DEPOSIT_FLAG > 0;
match is_delegated_deposit {
true => (true, (prefix ^ DELEGATED_DEPOSIT_FLAG)),
false => (false, prefix)
let num_items = (&memo[0..2]).read_u16::<LittleEndian>().unwrap();
let version = Version::from_u16((&memo[2..4]).read_u16::<LittleEndian>().unwrap()).unwrap();
match version {
Version::DelegatedDeposit => (true, num_items as u32),
_ => (false, num_items as u32)
}
}
4 changes: 2 additions & 2 deletions libzkbob-rs/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ where

/// Attempts to decrypt account and notes.
pub fn decrypt_pair(&self, data: Vec<u8>) -> Option<(Account<P::Fr>, Vec<Note<P::Fr>>)> {
cipher::decrypt_out(self.keys.eta, &data, &self.params)
cipher::decrypt_out(self.keys.eta, &self.keys.kappa, &data, &self.params)
}

fn initial_account(&self) -> Account<P::Fr> {
Expand Down Expand Up @@ -493,7 +493,7 @@ where
// No need to include all the zero notes in the encrypted transaction
let out_notes = &out_notes[0..num_real_out_notes];

cipher::encrypt(&entropy, keys.eta, out_account, out_notes, &self.params)
cipher::encrypt(&entropy, &keys.kappa, out_account, out_notes, &self.params)
};

// Hash input account + notes filling remaining space with non-hashed zeroes
Expand Down
6 changes: 4 additions & 2 deletions libzkbob-rs/src/keys.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use libzeropool::{
fawkes_crypto::ff_uint::PrimeField,
fawkes_crypto::ff_uint::{Num, NumRepr, Uint},
native::key::{derive_key_a, derive_key_eta},
native::key::{derive_key_a, derive_key_eta, derive_key_kappa},
native::params::PoolParams,
};
use serde::{Deserialize, Serialize};
Expand All @@ -15,13 +15,15 @@ pub struct Keys<P: PoolParams> {
pub sk: Num<P::Fs>,
pub a: Num<P::Fr>,
pub eta: Num<P::Fr>,
pub kappa: [u8; 32],
}

impl<P: PoolParams> Keys<P> {
pub fn derive(sk: Num<P::Fs>, params: &P) -> Self {
let a = derive_key_a(sk, params).x;
let eta = derive_key_eta(a, params);
let kappa = derive_key_kappa(eta);

Keys { sk, a, eta }
Keys { sk, a, eta, kappa }
}
}

0 comments on commit 3c21aa6

Please sign in to comment.