Skip to content

Commit

Permalink
Field name changes in MpnDeposit/MpnWithdraw
Browse files Browse the repository at this point in the history
  • Loading branch information
keyvank committed Sep 6, 2023
1 parent 6409624 commit 0546cb9
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 48 deletions.
2 changes: 1 addition & 1 deletion src/blockchain/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl Nonced for MpnTransaction {

impl Nonced for MpnWithdraw {
fn nonce(&self) -> u32 {
self.zk_nonce
self.mpn_withdraw_nonce
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/client/explorer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,33 +506,33 @@ impl From<&(Address, Amount)> for ExplorerStaker {

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct ExplorerMpnDeposit {
pub zk_address: String,
pub mpn_address: String,
pub payment: ExplorerContractDeposit,
}

impl From<&MpnDeposit> for ExplorerMpnDeposit {
fn from(obj: &MpnDeposit) -> Self {
Self {
zk_address: obj.zk_address.to_string(),
mpn_address: obj.mpn_address.to_string(),
payment: (&obj.payment).into(),
}
}
}

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct ExplorerMpnWithdraw {
pub zk_address: String,
pub zk_nonce: u32,
pub zk_sig: String,
pub mpn_address: String,
pub mpn_withdraw_nonce: u32,
pub mpn_sig: String,
pub payment: ExplorerContractWithdraw,
}

impl From<&MpnWithdraw> for ExplorerMpnWithdraw {
fn from(obj: &MpnWithdraw) -> Self {
Self {
zk_address: obj.zk_address.to_string(),
zk_nonce: obj.zk_nonce,
zk_sig: "".into(), // TODO: Convert sig to hex
mpn_address: obj.mpn_address.to_string(),
mpn_withdraw_nonce: obj.mpn_withdraw_nonce,
mpn_sig: "".into(), // TODO: Convert sig to hex
payment: (&obj.payment).into(),
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl GeneralTransaction {
pub_key: mpn_tx.src_pub_key.clone(),
}),
GeneralTransaction::MpnWithdraw(mpn_withdraw) => NonceGroup::MpnWithdraw(MpnAddress {
pub_key: mpn_withdraw.zk_address.clone(),
pub_key: mpn_withdraw.mpn_address.clone(),
}),
}
}
Expand All @@ -175,7 +175,7 @@ impl GeneralTransaction {
GeneralTransaction::TransactionAndDelta(tx_delta) => tx_delta.tx.nonce,
GeneralTransaction::MpnDeposit(mpn_deposit) => mpn_deposit.payment.nonce,
GeneralTransaction::MpnTransaction(mpn_tx) => mpn_tx.nonce,
GeneralTransaction::MpnWithdraw(mpn_withdraw) => mpn_withdraw.zk_nonce,
GeneralTransaction::MpnWithdraw(mpn_withdraw) => mpn_withdraw.mpn_withdraw_nonce,
}
}
pub fn fee(&self) -> Money {
Expand All @@ -199,7 +199,7 @@ impl GeneralTransaction {
}),
GeneralTransaction::MpnWithdraw(mpn_withdraw) => {
GeneralAddress::MpnAddress(MpnAddress {
pub_key: mpn_withdraw.zk_address.clone(),
pub_key: mpn_withdraw.mpn_address.clone(),
})
}
}
Expand Down
18 changes: 9 additions & 9 deletions src/core/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,31 +203,31 @@ pub struct ContractWithdraw<H: Hash, S: SignatureScheme> {

#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, Default)]
pub struct MpnDeposit<H: Hash, S: SignatureScheme, ZS: ZkSignatureScheme> {
pub zk_address: ZS::Pub,
pub mpn_address: ZS::Pub,
pub payment: ContractDeposit<H, S>,
}

#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, Default)]
pub struct MpnWithdraw<H: Hash, S: SignatureScheme, ZS: ZkSignatureScheme> {
pub zk_address: ZS::Pub,
pub zk_nonce: u32,
pub zk_sig: ZS::Sig,
pub mpn_address: ZS::Pub,
pub mpn_withdraw_nonce: u32,
pub mpn_sig: ZS::Sig,
pub payment: ContractWithdraw<H, S>,
}

impl<H: Hash, S: SignatureScheme, ZS: ZkSignatureScheme> MpnWithdraw<H, S, ZS> {
pub fn verify_calldata<ZH: ZkHasher>(&self) -> bool {
let mut preimage: Vec<ZkScalar> = self.zk_address.clone().into();
preimage.push((self.zk_nonce as u64).into());
preimage.extend(&self.zk_sig.clone().into());
let mut preimage: Vec<ZkScalar> = self.mpn_address.clone().into();
preimage.push((self.mpn_withdraw_nonce as u64).into());
preimage.extend(&self.mpn_sig.clone().into());
self.payment.calldata == ZH::hash(&preimage)
}
pub fn verify_signature<ZH: ZkHasher>(&self) -> bool {
let msg = ZH::hash(&[
self.payment.fingerprint(),
ZkScalar::from(self.zk_nonce as u64),
ZkScalar::from(self.mpn_withdraw_nonce as u64),
]);
ZS::verify(&self.zk_address, msg, &self.zk_sig)
ZS::verify(&self.mpn_address, msg, &self.mpn_sig)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/mpn/circuits/deposit_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl Circuit<BellmanFr> for DepositCircuit {

// Pub-key only needs to reside on curve if tx is enabled, which is checked in the main loop
let pub_key =
AllocatedPoint::alloc(&mut *cs, || Ok(trans.tx.zk_address.0.decompress()))?;
AllocatedPoint::alloc(&mut *cs, || Ok(trans.tx.mpn_address.0.decompress()))?;

tx_wits.push((
Boolean::Is(enabled.clone()),
Expand Down
9 changes: 5 additions & 4 deletions src/mpn/circuits/withdraw_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,11 @@ impl Circuit<BellmanFr> for WithdrawCircuit {

// Pub-key only needs to reside on curve if tx is enabled, which is checked in the main loop
let pub_key =
AllocatedPoint::alloc(&mut *cs, || Ok(trans.tx.zk_address.0.decompress()))?;
let nonce = AllocatedNum::alloc(&mut *cs, || Ok((trans.tx.zk_nonce as u64).into()))?;
let sig_r = AllocatedPoint::alloc(&mut *cs, || Ok(trans.tx.zk_sig.r))?;
let sig_s = AllocatedNum::alloc(&mut *cs, || Ok(trans.tx.zk_sig.s.into()))?;
AllocatedPoint::alloc(&mut *cs, || Ok(trans.tx.mpn_address.0.decompress()))?;
let nonce =
AllocatedNum::alloc(&mut *cs, || Ok((trans.tx.mpn_withdraw_nonce as u64).into()))?;
let sig_r = AllocatedPoint::alloc(&mut *cs, || Ok(trans.tx.mpn_sig.r))?;
let sig_s = AllocatedNum::alloc(&mut *cs, || Ok(trans.tx.mpn_sig.s.into()))?;

tx_wits.push((
Boolean::Is(enabled.clone()),
Expand Down
10 changes: 5 additions & 5 deletions src/mpn/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub fn deposit<K: KvStore, B: Blockchain<K>>(
}

let mpn_addr = MpnAddress {
pub_key: tx.zk_address.clone(),
pub_key: tx.mpn_address.clone(),
};
let mut new_index = None;
let account_index = if let Some(ind) = db.get_mpn_account_indices(mpn_addr.clone())?.first()
Expand Down Expand Up @@ -75,7 +75,7 @@ pub fn deposit<K: KvStore, B: Blockchain<K>>(
let mut isolated = mirror.mirror();
let mut isolated_state_size = state_size;
if rejected_pub_keys.contains(&src_pub)
|| (acc.address != Default::default() && tx.zk_address.0.decompress() != acc.address)
|| (acc.address != Default::default() && tx.mpn_address.0.decompress() != acc.address)
|| (acc_token.is_some() && acc_token.unwrap().token_id != tx.payment.amount.token_id)
{
rejected.push(tx.clone());
Expand Down Expand Up @@ -107,7 +107,7 @@ pub fn deposit<K: KvStore, B: Blockchain<K>>(
}

let mut updated_acc = MpnAccount {
address: tx.zk_address.0.decompress(),
address: tx.mpn_address.0.decompress(),
tokens: acc.tokens.clone(),
withdraw_nonce: acc.withdraw_nonce,
tx_nonce: acc.tx_nonce,
Expand Down Expand Up @@ -191,8 +191,8 @@ pub fn deposit<K: KvStore, B: Blockchain<K>>(
for (i, trans) in transitions.iter().enumerate() {
use crate::zk::ZkHasher;
let calldata = crate::core::ZkHasher::hash(&[
ZkScalar::from(trans.tx.zk_address.0.decompress().0),
ZkScalar::from(trans.tx.zk_address.0.decompress().1),
ZkScalar::from(trans.tx.mpn_address.0.decompress().0),
ZkScalar::from(trans.tx.mpn_address.0.decompress().1),
]);
state_builder
.batch_set(&ZkDeltaPairs(
Expand Down
20 changes: 10 additions & 10 deletions src/mpn/withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn withdraw<K: KvStore, B: Blockchain<K>>(
}

let mpn_addr = MpnAddress {
pub_key: tx.zk_address.clone(),
pub_key: tx.mpn_address.clone(),
};
let account_index = if let Some(ind) = db.get_mpn_account_indices(mpn_addr.clone())?.first()
{
Expand Down Expand Up @@ -73,18 +73,18 @@ pub fn withdraw<K: KvStore, B: Blockchain<K>>(

let mut isolated = mirror.mirror();
let mut isolated_state_size = state_size;
if (acc.address != Default::default() && tx.zk_address.0.decompress() != acc.address)
if (acc.address != Default::default() && tx.mpn_address.0.decompress() != acc.address)
|| !tx.verify_calldata::<ZkHasher>()
|| !tx.verify_signature::<ZkHasher>()
|| tx.zk_nonce != acc.withdraw_nonce + 1
|| tx.mpn_withdraw_nonce != acc.withdraw_nonce + 1
|| tx.payment.amount.token_id != acc_token.token_id
|| tx.payment.amount.amount > acc_token.amount
{
rejected.push(tx.clone());
continue;
} else {
let mut updated_acc = MpnAccount {
address: tx.zk_address.0.decompress(),
address: tx.mpn_address.0.decompress(),
tokens: acc.tokens.clone(),
tx_nonce: acc.tx_nonce,
withdraw_nonce: acc.withdraw_nonce + 1,
Expand Down Expand Up @@ -205,12 +205,12 @@ pub fn withdraw<K: KvStore, B: Blockchain<K>>(
for (i, trans) in transitions.iter().enumerate() {
use crate::zk::ZkHasher;
let calldata = crate::core::ZkHasher::hash(&[
ZkScalar::from(trans.tx.zk_address.0.decompress().0),
ZkScalar::from(trans.tx.zk_address.0.decompress().1),
ZkScalar::from(trans.tx.zk_nonce as u64),
ZkScalar::from(trans.tx.zk_sig.r.0),
ZkScalar::from(trans.tx.zk_sig.r.1),
ZkScalar::from(trans.tx.zk_sig.s),
ZkScalar::from(trans.tx.mpn_address.0.decompress().0),
ZkScalar::from(trans.tx.mpn_address.0.decompress().1),
ZkScalar::from(trans.tx.mpn_withdraw_nonce as u64),
ZkScalar::from(trans.tx.mpn_sig.r.0),
ZkScalar::from(trans.tx.mpn_sig.r.1),
ZkScalar::from(trans.tx.mpn_sig.s),
]);
state_builder
.batch_set(&ZkDeltaPairs(
Expand Down
14 changes: 7 additions & 7 deletions src/wallet/tx_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct TxBuilder {
private_key: <Signer as SignatureScheme>::Priv,
zk_private_key: <ZkSigner as ZkSignatureScheme>::Priv,
address: Address,
zk_address: <ZkSigner as ZkSignatureScheme>::Pub,
mpn_address: <ZkSigner as ZkSignatureScheme>::Pub,
}

impl TxBuilder {
Expand All @@ -33,7 +33,7 @@ impl TxBuilder {
let (vrf_public_key, vrf_private_key) = Vrf::generate_keys(&mut chacha_rng);
Self {
address: pk,
zk_address: zk_pk,
mpn_address: zk_pk,
private_key: sk,
zk_private_key: zk_sk,
vrf_public_key,
Expand All @@ -50,7 +50,7 @@ impl TxBuilder {
self.vrf_public_key.clone()
}
pub fn get_zk_address(&self) -> <ZkSigner as ZkSignatureScheme>::Pub {
self.zk_address.clone()
self.mpn_address.clone()
}
pub fn get_mpn_address(&self) -> MpnAddress {
MpnAddress {
Expand Down Expand Up @@ -353,7 +353,7 @@ impl TxBuilder {
};
self.sign_deposit(&mut tx);
MpnDeposit {
zk_address: to.pub_key,
mpn_address: to.pub_key,
payment: tx,
}
}
Expand Down Expand Up @@ -402,9 +402,9 @@ impl TxBuilder {
.unwrap();
tx.calldata = calldata_builder.compress().unwrap().state_hash;
MpnWithdraw {
zk_address: self.get_zk_address(),
zk_nonce: nonce,
zk_sig: sig,
mpn_address: self.get_zk_address(),
mpn_withdraw_nonce: nonce,
mpn_sig: sig,
payment: tx,
}
}
Expand Down

0 comments on commit 0546cb9

Please sign in to comment.