Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add checksum field to accounts #84

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/mpn/circuits/deposit_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ impl Circuit<BellmanFr> for DepositCircuit {
let src_balances_hash_wit =
AllocatedNum::alloc(&mut *cs, || Ok(trans.before_balances_hash.into()))?;

let src_checksum_wit = AllocatedNum::alloc(&mut *cs, || Ok(trans.checksum.into()))?;

let src_token_id_wit = AllocatedNum::alloc(&mut *cs, || {
Ok(Into::<ZkScalar>::into(trans.before_balance.token_id).into())
})?;
Expand Down Expand Up @@ -206,6 +208,7 @@ impl Circuit<BellmanFr> for DepositCircuit {
&src_withdraw_nonce_wit.clone().into(),
&src_addr_wit.x.clone().into(),
&src_addr_wit.y.clone().into(),
&src_checksum_wit.clone().into(),
&src_balances_hash_wit.clone().into(),
],
)?;
Expand Down Expand Up @@ -272,6 +275,7 @@ impl Circuit<BellmanFr> for DepositCircuit {
&src_withdraw_nonce_wit.clone().into(),
&tx_pub_key_wit.x.clone().into(),
&tx_pub_key_wit.y.clone().into(),
&src_checksum_wit.clone().into(), // TODO: NEW CHECKSUM!
&new_balances_hash_wit,
],
)?;
Expand Down
10 changes: 10 additions & 0 deletions src/mpn/circuits/update_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ impl Circuit<BellmanFr> for UpdateCircuit {
// Sender address should be on curve in case transaction slot is non-empty
src_addr_wit.assert_on_curve(&mut *cs, &enabled_wit)?;

let src_checksum_wit =
AllocatedPoint::alloc(&mut *cs, || Ok(trans.src_before.checksum))?;

let src_before_balances_hash =
AllocatedNum::alloc(&mut *cs, || Ok(trans.src_before_balances_hash.into()))?;
let dst_before_balances_hash =
Expand Down Expand Up @@ -245,6 +248,7 @@ impl Circuit<BellmanFr> for UpdateCircuit {
&src_withdraw_nonce_wit.clone().into(),
&src_addr_wit.x.clone().into(),
&src_addr_wit.y.clone().into(),
&src_checksum_wit.clone().into(),
&src_before_balances_hash.clone().into(),
],
)?;
Expand Down Expand Up @@ -323,6 +327,7 @@ impl Circuit<BellmanFr> for UpdateCircuit {
&src_withdraw_nonce_wit.clone().into(),
&src_addr_wit.x.clone().into(),
&src_addr_wit.y.clone().into(),
&src_checksum_wit.clone().into(), // TODO: UPDATE CHECKSUM!
&src_balance_final_root,
],
)?;
Expand All @@ -340,6 +345,9 @@ impl Circuit<BellmanFr> for UpdateCircuit {
// Destination address should be on curve in case transaction slot is non-empty
tx_dst_addr_wit.assert_on_curve(&mut *cs, &enabled_wit)?;

let dst_checksum_wit =
AllocatedNum::alloc(&mut *cs, || Ok(trans.dst_before.CHECKSUM.into()))?;

let tx_dst_index_wit = UnsignedInteger::alloc(
&mut *cs,
(trans.dst_index as u64).into(),
Expand All @@ -362,6 +370,7 @@ impl Circuit<BellmanFr> for UpdateCircuit {
&dst_withdraw_nonce_wit.clone().into(),
&dst_addr_wit.x.clone().into(),
&dst_addr_wit.y.clone().into(),
&dst_checksum_wit.clone().into(),
&dst_before_balances_hash.clone().into(),
],
)?;
Expand Down Expand Up @@ -397,6 +406,7 @@ impl Circuit<BellmanFr> for UpdateCircuit {
&dst_withdraw_nonce_wit.clone().into(),
&tx_dst_addr_wit.x.clone().into(),
&tx_dst_addr_wit.y.clone().into(),
&dst_checksum_wit.clone().into(), // TODO: UPDATE CHECKSUM!
&dst_balance_final_root,
],
)?;
Expand Down
4 changes: 4 additions & 0 deletions src/mpn/circuits/withdraw_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ impl Circuit<BellmanFr> for WithdrawCircuit {
let src_addr_wit = AllocatedPoint::alloc(&mut *cs, || Ok(trans.before.address))?;
src_addr_wit.assert_on_curve(&mut *cs, &enabled_wit)?;

let src_checksum_wit = AllocatedPoint::alloc(&mut *cs, || Ok(trans.before.checksum))?;

let src_balances_before_token_hash_wit =
AllocatedNum::alloc(&mut *cs, || Ok(trans.before_token_hash.into()))?;

Expand Down Expand Up @@ -347,6 +349,7 @@ impl Circuit<BellmanFr> for WithdrawCircuit {
&src_withdraw_nonce_wit.clone().into(),
&src_addr_wit.x.clone().into(),
&src_addr_wit.y.clone().into(),
&src_checksum_wit.clone().into(),
&src_balances_before_token_hash_wit.clone().into(),
],
)?;
Expand Down Expand Up @@ -391,6 +394,7 @@ impl Circuit<BellmanFr> for WithdrawCircuit {
+ Number::constant::<CS>(BellmanFr::one())),
&tx_pub_key_wit.x.clone().into(),
&tx_pub_key_wit.y.clone().into(),
&src_checksum_wit.clone().into(), // TODO: NEW CHECKSUM!
&balance_final_root,
],
)?;
Expand Down
1 change: 1 addition & 0 deletions src/zk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub struct MpnAccount {
pub withdraw_nonce: u32, // Increased on MpnWithdrawals
pub address: jubjub::PointAffine,
pub tokens: HashMap<u64, Money>,
pub checksum: ZkScalar,
}

impl MpnAccount {
Expand Down
14 changes: 8 additions & 6 deletions src/zk/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ impl<H: ZkHasher> KvStoreStateManager<H> {
mpn_contract_id: ContractId,
index: u64,
) -> Result<MpnAccount, StateManagerError> {
let cells = (0..4)
let cells = (0..5)
.map(|i| Self::get_data(db, mpn_contract_id, &ZkDataLocator(vec![index, i as u64])))
.collect::<Result<Vec<ZkScalar>, StateManagerError>>()?;
let mut token_indices = HashSet::new();
for (k, _) in db
.pairs(keys::local_value(
&mpn_contract_id,
&ZkDataLocator(vec![index, 4]),
&ZkDataLocator(vec![index, 5]),
true,
))?
.into_iter()
Expand All @@ -116,12 +116,12 @@ impl<H: ZkHasher> KvStoreStateManager<H> {
let tok = Self::get_data(
db,
mpn_contract_id,
&ZkDataLocator(vec![index, 4, i as u64, 0]),
&ZkDataLocator(vec![index, 5, i as u64, 0]),
)?;
let bal = Self::get_data(
db,
mpn_contract_id,
&ZkDataLocator(vec![index, 4, i as u64, 1]),
&ZkDataLocator(vec![index, 5, i as u64, 1]),
)?;
let tok_is_zero: bool = tok.is_zero().into();
if !tok_is_zero {
Expand All @@ -132,6 +132,7 @@ impl<H: ZkHasher> KvStoreStateManager<H> {
tx_nonce: cells[0].try_into()?,
withdraw_nonce: cells[1].try_into()?,
address: jubjub::PointAffine(cells[2], cells[3]),
checksum: cells[4],
tokens,
})
}
Expand Down Expand Up @@ -174,6 +175,7 @@ impl<H: ZkHasher> KvStoreStateManager<H> {
(acc.withdraw_nonce as u64).into(),
acc.address.0,
acc.address.1,
acc.checksum,
];
vals.into_iter()
.enumerate()
Expand All @@ -191,14 +193,14 @@ impl<H: ZkHasher> KvStoreStateManager<H> {
Self::set_data(
db,
mpn_contract_id,
ZkDataLocator(vec![index, 4, *ind as u64, 0]),
ZkDataLocator(vec![index, 5, *ind as u64, 0]),
money.token_id.into(),
size_diff,
)?;
Self::set_data(
db,
mpn_contract_id,
ZkDataLocator(vec![index, 4, *ind as u64, 1]),
ZkDataLocator(vec![index, 5, *ind as u64, 1]),
ZkScalar::from(money.amount),
size_diff,
)?;
Expand Down