Skip to content

Commit 4fe7bf8

Browse files
committed
Refactor InSnarkCheck and add ZkappCheckOps to make it generic
1 parent 4b24507 commit 4fe7bf8

File tree

3 files changed

+208
-73
lines changed

3 files changed

+208
-73
lines changed

ledger/src/scan_state/transaction_logic.rs

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,7 @@ pub mod zkapp_command {
936936
currency::{MinMax, Sgn},
937937
GenesisConstant, GENESIS_CONSTANT,
938938
},
939-
zkapps::snark::zkapp_check::InSnarkCheck,
939+
zkapps::snark::{zkapp_check::InSnarkCheck, ZkappCheckOps},
940940
AuthRequired, MutableFp, MyCow, Permissions, SetVerificationKey, ToInputs, TokenSymbol,
941941
VerificationKey, VerificationKeyWire, VotingFor, ZkAppAccount, ZkAppUri,
942942
};
@@ -1976,7 +1976,11 @@ pub mod zkapp_command {
19761976
.epoch_data("next_epoch_data", &s.next_epoch_data)
19771977
}
19781978

1979-
pub fn checked_zcheck(&self, s: &ProtocolStateView, w: &mut Witness<Fp>) -> Boolean {
1979+
pub fn checked_zcheck<Ops: ZkappCheckOps>(
1980+
&self,
1981+
s: &ProtocolStateView,
1982+
w: &mut Witness<Fp>,
1983+
) -> Boolean {
19801984
let Self {
19811985
snarked_ledger_hash,
19821986
blockchain_length,
@@ -2006,12 +2010,13 @@ pub mod zkapp_command {
20062010
// Reverse to match OCaml order of the list, while still executing `checked_zcheck`
20072011
// in correct order
20082012
[
2009-
(epoch_length, ClosedInterval::min_max).checked_zcheck(&view.epoch_length, w),
2010-
(lock_checkpoint, Fp::zero).checked_zcheck(&view.lock_checkpoint, w),
2011-
(start_checkpoint, Fp::zero).checked_zcheck(&view.start_checkpoint, w),
2013+
(epoch_length, ClosedInterval::min_max)
2014+
.checked_zcheck::<Ops>(&view.epoch_length, w),
2015+
(lock_checkpoint, Fp::zero).checked_zcheck::<Ops>(&view.lock_checkpoint, w),
2016+
(start_checkpoint, Fp::zero).checked_zcheck::<Ops>(&view.start_checkpoint, w),
20122017
(total_currency, ClosedInterval::min_max)
2013-
.checked_zcheck(&view.ledger.total_currency, w),
2014-
(hash, Fp::zero).checked_zcheck(&view.ledger.hash, w),
2018+
.checked_zcheck::<Ops>(&view.ledger.total_currency, w),
2019+
(hash, Fp::zero).checked_zcheck::<Ops>(&view.ledger.hash, w),
20152020
]
20162021
};
20172022

@@ -2022,21 +2027,21 @@ pub mod zkapp_command {
20222027
// in correct order
20232028
let bools = [
20242029
(global_slot_since_genesis, ClosedInterval::min_max)
2025-
.checked_zcheck(&s.global_slot_since_genesis, w),
2026-
(total_currency, ClosedInterval::min_max).checked_zcheck(&s.total_currency, w),
2030+
.checked_zcheck::<Ops>(&s.global_slot_since_genesis, w),
2031+
(total_currency, ClosedInterval::min_max)
2032+
.checked_zcheck::<Ops>(&s.total_currency, w),
20272033
(min_window_density, ClosedInterval::min_max)
2028-
.checked_zcheck(&s.min_window_density, w),
2034+
.checked_zcheck::<Ops>(&s.min_window_density, w),
20292035
(blockchain_length, ClosedInterval::min_max)
2030-
.checked_zcheck(&s.blockchain_length, w),
2031-
(snarked_ledger_hash, Fp::zero).checked_zcheck(&s.snarked_ledger_hash, w),
2036+
.checked_zcheck::<Ops>(&s.blockchain_length, w),
2037+
(snarked_ledger_hash, Fp::zero).checked_zcheck::<Ops>(&s.snarked_ledger_hash, w),
20322038
]
20332039
.into_iter()
20342040
.rev()
20352041
.chain(staking_epoch_data.into_iter().rev())
2036-
.chain(next_epoch_data.into_iter().rev())
2037-
.collect::<Vec<_>>();
2042+
.chain(next_epoch_data.into_iter().rev());
20382043

2039-
Boolean::all(&bools, w)
2044+
Ops::boolean_all(bools, w)
20402045
}
20412046

20422047
/// https://github.com/MinaProtocol/mina/blob/3753a8593cc1577bcf4da16620daf9946d88e8e5/src/lib/mina_base/zkapp_precondition.ml#L1303
@@ -2245,7 +2250,7 @@ pub mod zkapp_command {
22452250
ret
22462251
}
22472252

2248-
fn checked_zchecks(
2253+
fn checked_zchecks<Ops: ZkappCheckOps>(
22492254
&self,
22502255
account: &crate::Account,
22512256
new_account: Boolean,
@@ -2277,12 +2282,12 @@ pub mod zkapp_command {
22772282
let mut checks: Vec<(TransactionFailure, _)> = [
22782283
(
22792284
AccountIsNewPreconditionUnsatisfied,
2280-
(&is_new, || Boolean::False).checked_zcheck(&new_account, w),
2285+
(&is_new, || Boolean::False).checked_zcheck::<Ops>(&new_account, w),
22812286
),
22822287
(
22832288
AccountProvedStatePreconditionUnsatisfied,
22842289
(&proved_state, || Boolean::False)
2285-
.checked_zcheck(&zkapp_account.proved_state.to_boolean(), w),
2290+
.checked_zcheck::<Ops>(&zkapp_account.proved_state.to_boolean(), w),
22862291
),
22872292
]
22882293
.into_iter()
@@ -2293,7 +2298,7 @@ pub mod zkapp_command {
22932298
.enumerate()
22942299
.rev()
22952300
.map(|(i, (s, account_s))| {
2296-
let b = (s, Fp::zero).checked_zcheck(account_s, w);
2301+
let b = (s, Fp::zero).checked_zcheck::<Ops>(account_s, w);
22972302
(AccountAppStatePreconditionUnsatisfied(i as u64), b)
22982303
})
22992304
.collect::<Vec<_>>();
@@ -2307,30 +2312,31 @@ pub mod zkapp_command {
23072312
.iter()
23082313
.map(|account_s| {
23092314
(action_state, ZkAppAccount::empty_action_state)
2310-
.checked_zcheck(account_s, w)
2315+
.checked_zcheck::<Ops>(account_s, w)
23112316
})
23122317
.collect();
23132318
(
23142319
AccountActionStatePreconditionUnsatisfied,
2315-
Boolean::any(&bools, w),
2320+
Ops::boolean_any(bools, w),
23162321
)
23172322
},
23182323
(
23192324
AccountDelegatePreconditionUnsatisfied,
23202325
(delegate, CompressedPubKey::empty)
2321-
.checked_zcheck(&*account.delegate_or_empty(), w),
2326+
.checked_zcheck::<Ops>(&*account.delegate_or_empty(), w),
23222327
),
23232328
(
23242329
AccountReceiptChainHashPreconditionUnsatisfied,
2325-
(receipt_chain_hash, Fp::zero).checked_zcheck(&account.receipt_chain_hash.0, w),
2330+
(receipt_chain_hash, Fp::zero)
2331+
.checked_zcheck::<Ops>(&account.receipt_chain_hash.0, w),
23262332
),
23272333
(
23282334
AccountNoncePreconditionUnsatisfied,
2329-
(nonce, ClosedInterval::min_max).checked_zcheck(&account.nonce, w),
2335+
(nonce, ClosedInterval::min_max).checked_zcheck::<Ops>(&account.nonce, w),
23302336
),
23312337
(
23322338
AccountBalancePreconditionUnsatisfied,
2333-
(balance, ClosedInterval::min_max).checked_zcheck(&account.balance, w),
2339+
(balance, ClosedInterval::min_max).checked_zcheck::<Ops>(&account.balance, w),
23342340
),
23352341
])
23362342
.collect::<Vec<_>>();
@@ -2452,17 +2458,18 @@ pub mod zkapp_command {
24522458
MyCow::Borrow(&self.0)
24532459
}
24542460

2455-
pub fn checked_zcheck<Fun>(
2461+
pub fn checked_zcheck<Ops, Fun>(
24562462
&self,
24572463
new_account: Boolean,
24582464
account: &crate::Account,
24592465
mut check: Fun,
24602466
w: &mut Witness<Fp>,
24612467
) where
2468+
Ops: ZkappCheckOps,
24622469
Fun: FnMut(TransactionFailure, Boolean, &mut Witness<Fp>),
24632470
{
24642471
let this = self.to_full();
2465-
for (failure, passed) in this.checked_zchecks(account, new_account, w) {
2472+
for (failure, passed) in this.checked_zchecks::<Ops>(account, new_account, w) {
24662473
check(failure, passed, w);
24672474
}
24682475
}

ledger/src/zkapps/non_snark.rs

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::{
1010
field::{Boolean, FieldWitness, ToBoolean},
1111
to_field_elements::ToFieldElements,
1212
transaction::Check,
13+
witness::Witness,
1314
},
1415
scan_state::{
1516
currency::{Amount, Balance, Index, Magnitude, Signed, Slot, SlotSpan, TxnVersion},
@@ -42,6 +43,7 @@ use super::{
4243
TxnVersionInterface, VerificationKeyHashInterface, WitnessGenerator, ZkappApplication,
4344
ZkappHandler,
4445
},
46+
snark::NonSnarkOps,
4547
zkapp_logic,
4648
};
4749

@@ -147,34 +149,60 @@ impl<
147149
local_state: &mut zkapp_logic::LocalState<ZkappNonSnark<L>>,
148150
w: &mut Self::W,
149151
) {
150-
let AccountPreconditions(precondition_account) = &account_update.body.preconditions.account;
151-
let check = |failure, b| {
152-
zkapp_logic::LocalState::<ZkappNonSnark<L>>::add_check(local_state, failure, b, w);
152+
let precondition_account = &account_update.body.preconditions.account;
153+
let check = |failure, b: Boolean, _: &mut Witness<Fp>| {
154+
zkapp_logic::LocalState::<ZkappNonSnark<L>>::add_check(
155+
local_state,
156+
failure,
157+
b.as_bool(),
158+
w,
159+
);
153160
};
154-
precondition_account.zcheck(new_account, check, account);
161+
let mut w = Witness::empty();
162+
precondition_account.checked_zcheck::<NonSnarkOps, _>(
163+
new_account.to_boolean(),
164+
account,
165+
check,
166+
&mut w,
167+
);
168+
// let AccountPreconditions(precondition_account) = &account_update.body.preconditions.account;
169+
// let check = |failure, b| {
170+
// zkapp_logic::LocalState::<ZkappNonSnark<L>>::add_check(local_state, failure, b, w);
171+
// };
172+
// precondition_account.zcheck(new_account, check, account);
155173
}
156174

157175
fn check_protocol_state_precondition(
158176
protocol_state_predicate: &zkapp_command::ZkAppPreconditions,
159177
global_state: &mut Self::GlobalState,
160178
w: &mut Self::W,
161179
) -> Self::Bool {
180+
let mut w = Witness::empty();
162181
protocol_state_predicate
163-
.zcheck(&global_state.protocol_state)
164-
.is_ok()
182+
.checked_zcheck::<NonSnarkOps>(&global_state.protocol_state, &mut w)
183+
.as_bool()
184+
// protocol_state_predicate
185+
// .zcheck(&global_state.protocol_state)
186+
// .is_ok()
165187
}
166188

167189
fn check_valid_while_precondition(
168190
valid_while: &zkapp_command::Numeric<crate::scan_state::currency::Slot>,
169191
global_state: &mut Self::GlobalState,
170192
w: &mut Self::W,
171193
) -> Self::Bool {
172-
valid_while
173-
.zcheck(
174-
|| "valid_while_precondition".to_string(),
175-
&global_state.block_global_slot,
176-
)
177-
.is_ok()
194+
use crate::zkapps::snark::zkapp_check::InSnarkCheck;
195+
use zkapp_command::ClosedInterval;
196+
let mut w = Witness::empty();
197+
(valid_while, ClosedInterval::min_max)
198+
.checked_zcheck::<NonSnarkOps>(&global_state.block_global_slot, &mut w)
199+
.as_bool()
200+
// valid_while
201+
// .zcheck(
202+
// || "valid_while_precondition".to_string(),
203+
// &global_state.block_global_slot,
204+
// )
205+
// .is_ok()
178206
}
179207

180208
fn init_account(

0 commit comments

Comments
 (0)