Skip to content

Commit ab19bed

Browse files
committed
Rename InSnarkCheck to ZkappCheck
1 parent 4fe7bf8 commit ab19bed

File tree

5 files changed

+287
-275
lines changed

5 files changed

+287
-275
lines changed

ledger/src/scan_state/transaction_logic.rs

Lines changed: 61 additions & 61 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, ZkappCheckOps},
939+
zkapps::checks::{ZkappCheck, ZkappCheckOps},
940940
AuthRequired, MutableFp, MyCow, Permissions, SetVerificationKey, ToInputs, TokenSymbol,
941941
VerificationKey, VerificationKeyWire, VotingFor, ZkAppAccount, ZkAppUri,
942942
};
@@ -1536,12 +1536,14 @@ pub mod zkapp_command {
15361536
}
15371537
}
15381538

1539+
// TODO: Remove this. It's been replaced by `ZkappCheck`
15391540
pub trait OutSnarkCheck {
15401541
type A;
15411542
type B;
15421543

1543-
/// zkapp check
1544-
fn zcheck<F: Fn() -> String>(&self, label: F, x: &Self::B) -> Result<(), String>;
1544+
/// zkapp check.
1545+
// name is `out_zheck`, to differentiate with `zcheck`. It means out of snark
1546+
fn out_zcheck<F: Fn() -> String>(&self, label: F, x: &Self::B) -> Result<(), String>;
15451547
}
15461548

15471549
impl<T> OutSnarkCheck for T
@@ -1552,7 +1554,7 @@ pub mod zkapp_command {
15521554
type B = T;
15531555

15541556
/// zkapp check
1555-
fn zcheck<F: Fn() -> String>(&self, label: F, rhs: &Self::B) -> Result<(), String> {
1557+
fn out_zcheck<F: Fn() -> String>(&self, label: F, rhs: &Self::B) -> Result<(), String> {
15561558
if self == rhs {
15571559
Ok(())
15581560
} else {
@@ -1625,7 +1627,7 @@ pub mod zkapp_command {
16251627
type B = T;
16261628

16271629
/// zkapp check
1628-
fn zcheck<F: Fn() -> String>(&self, label: F, rhs: &Self::B) -> Result<(), String> {
1630+
fn out_zcheck<F: Fn() -> String>(&self, label: F, rhs: &Self::B) -> Result<(), String> {
16291631
/*println!(
16301632
"bounds check lower {:?} rhs {:?} upper {:?}",
16311633
self.lower, rhs, self.upper
@@ -1740,11 +1742,11 @@ pub mod zkapp_command {
17401742
T: OutSnarkCheck<A = T>,
17411743
{
17421744
/// zkapp check
1743-
pub fn zcheck<F: Fn() -> String>(&self, label: F, rhs: &T::B) -> Result<(), String> {
1745+
pub fn out_zcheck<F: Fn() -> String>(&self, label: F, rhs: &T::B) -> Result<(), String> {
17441746
// println!("[rust] check {}, {:?}", label, ret);
17451747
match self {
17461748
Self::Ignore => Ok(()),
1747-
Self::Check(t) => t.zcheck(label, rhs),
1749+
Self::Check(t) => t.out_zcheck(label, rhs),
17481750
}
17491751
}
17501752
}
@@ -1807,8 +1809,8 @@ pub mod zkapp_command {
18071809
impl EpochLedger {
18081810
pub fn epoch_ledger(&self, t: &protocol_state::EpochLedger<Fp>) -> Result<(), String> {
18091811
self.hash
1810-
.zcheck(|| "epoch_ledger_hash".to_string(), &t.hash)?;
1811-
self.total_currency.zcheck(
1812+
.out_zcheck(|| "epoch_ledger_hash".to_string(), &t.hash)?;
1813+
self.total_currency.out_zcheck(
18121814
|| "epoch_ledger_total_currency".to_string(),
18131815
&t.total_currency,
18141816
)
@@ -1915,16 +1917,16 @@ pub mod zkapp_command {
19151917
) -> Result<(), String> {
19161918
self.ledger.epoch_ledger(&t.ledger)?;
19171919
// ignore seed
1918-
self.start_checkpoint.zcheck(
1920+
self.start_checkpoint.out_zcheck(
19191921
|| format!("{}_{}", label, "start_checkpoint"),
19201922
&t.start_checkpoint,
19211923
)?;
1922-
self.lock_checkpoint.zcheck(
1924+
self.lock_checkpoint.out_zcheck(
19231925
|| format!("{}_{}", label, "lock_checkpoint"),
19241926
&t.lock_checkpoint,
19251927
)?;
19261928
self.epoch_length
1927-
.zcheck(|| format!("{}_{}", label, "epoch_length"), &t.epoch_length)
1929+
.out_zcheck(|| format!("{}_{}", label, "epoch_length"), &t.epoch_length)
19281930
}
19291931

19301932
pub fn gen() -> Self {
@@ -1957,16 +1959,16 @@ pub mod zkapp_command {
19571959

19581960
impl ZkAppPreconditions {
19591961
/// zkapp check
1960-
pub fn zcheck(&self, s: &ProtocolStateView) -> Result<(), String> {
1962+
pub fn out_zcheck(&self, s: &ProtocolStateView) -> Result<(), String> {
19611963
self.snarked_ledger_hash
1962-
.zcheck(|| "snarker_ledger_hash".to_string(), &s.snarked_ledger_hash)?;
1964+
.out_zcheck(|| "snarker_ledger_hash".to_string(), &s.snarked_ledger_hash)?;
19631965
self.blockchain_length
1964-
.zcheck(|| "blockchain_length".to_string(), &s.blockchain_length)?;
1966+
.out_zcheck(|| "blockchain_length".to_string(), &s.blockchain_length)?;
19651967
self.min_window_density
1966-
.zcheck(|| "min_window_density".to_string(), &s.min_window_density)?;
1968+
.out_zcheck(|| "min_window_density".to_string(), &s.min_window_density)?;
19671969
self.total_currency
1968-
.zcheck(|| "total_currency".to_string(), &s.total_currency)?;
1969-
self.global_slot_since_genesis.zcheck(
1970+
.out_zcheck(|| "total_currency".to_string(), &s.total_currency)?;
1971+
self.global_slot_since_genesis.out_zcheck(
19701972
|| "global_slot_since_genesis".to_string(),
19711973
&s.global_slot_since_genesis,
19721974
)?;
@@ -1976,7 +1978,7 @@ pub mod zkapp_command {
19761978
.epoch_data("next_epoch_data", &s.next_epoch_data)
19771979
}
19781980

1979-
pub fn checked_zcheck<Ops: ZkappCheckOps>(
1981+
pub fn zcheck<Ops: ZkappCheckOps>(
19801982
&self,
19811983
s: &ProtocolStateView,
19821984
w: &mut Witness<Fp>,
@@ -2007,34 +2009,31 @@ pub mod zkapp_command {
20072009
lock_checkpoint,
20082010
epoch_length,
20092011
} = epoch_data;
2010-
// Reverse to match OCaml order of the list, while still executing `checked_zcheck`
2012+
// Reverse to match OCaml order of the list, while still executing `zcheck`
20112013
// in correct order
20122014
[
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),
2015+
(epoch_length, ClosedInterval::min_max).zcheck::<Ops>(&view.epoch_length, w),
2016+
(lock_checkpoint, Fp::zero).zcheck::<Ops>(&view.lock_checkpoint, w),
2017+
(start_checkpoint, Fp::zero).zcheck::<Ops>(&view.start_checkpoint, w),
20172018
(total_currency, ClosedInterval::min_max)
2018-
.checked_zcheck::<Ops>(&view.ledger.total_currency, w),
2019-
(hash, Fp::zero).checked_zcheck::<Ops>(&view.ledger.hash, w),
2019+
.zcheck::<Ops>(&view.ledger.total_currency, w),
2020+
(hash, Fp::zero).zcheck::<Ops>(&view.ledger.hash, w),
20202021
]
20212022
};
20222023

20232024
let next_epoch_data = epoch_data(next_epoch_data, &s.next_epoch_data, w);
20242025
let staking_epoch_data = epoch_data(staking_epoch_data, &s.staking_epoch_data, w);
20252026

2026-
// Reverse to match OCaml order of the list, while still executing `checked_zcheck`
2027+
// Reverse to match OCaml order of the list, while still executing `zcheck`
20272028
// in correct order
20282029
let bools = [
20292030
(global_slot_since_genesis, ClosedInterval::min_max)
2030-
.checked_zcheck::<Ops>(&s.global_slot_since_genesis, w),
2031-
(total_currency, ClosedInterval::min_max)
2032-
.checked_zcheck::<Ops>(&s.total_currency, w),
2031+
.zcheck::<Ops>(&s.global_slot_since_genesis, w),
2032+
(total_currency, ClosedInterval::min_max).zcheck::<Ops>(&s.total_currency, w),
20332033
(min_window_density, ClosedInterval::min_max)
2034-
.checked_zcheck::<Ops>(&s.min_window_density, w),
2035-
(blockchain_length, ClosedInterval::min_max)
2036-
.checked_zcheck::<Ops>(&s.blockchain_length, w),
2037-
(snarked_ledger_hash, Fp::zero).checked_zcheck::<Ops>(&s.snarked_ledger_hash, w),
2034+
.zcheck::<Ops>(&s.min_window_density, w),
2035+
(blockchain_length, ClosedInterval::min_max).zcheck::<Ops>(&s.blockchain_length, w),
2036+
(snarked_ledger_hash, Fp::zero).zcheck::<Ops>(&s.snarked_ledger_hash, w),
20382037
]
20392038
.into_iter()
20402039
.rev()
@@ -2175,16 +2174,16 @@ pub mod zkapp_command {
21752174

21762175
impl Account {
21772176
/// zkapp check
2178-
pub fn zcheck<F>(&self, new_account: bool, mut check: F, a: &account::Account)
2177+
pub fn out_zcheck<F>(&self, new_account: bool, mut check: F, a: &account::Account)
21792178
where
21802179
F: FnMut(TransactionFailure, bool),
21812180
{
2182-
self.zchecks(new_account, a)
2181+
self.out_zchecks(new_account, a)
21832182
.iter()
21842183
.for_each(|(failure, res)| check(failure.clone(), res.is_ok()))
21852184
}
21862185

2187-
fn zchecks(
2186+
fn out_zchecks(
21882187
&self,
21892188
new_account: bool,
21902189
a: &account::Account,
@@ -2196,20 +2195,21 @@ pub mod zkapp_command {
21962195
let mut ret = vec![
21972196
(
21982197
TransactionFailure::AccountBalancePreconditionUnsatisfied,
2199-
self.balance.zcheck(|| "balance".to_string(), &a.balance),
2198+
self.balance
2199+
.out_zcheck(|| "balance".to_string(), &a.balance),
22002200
),
22012201
(
22022202
TransactionFailure::AccountNoncePreconditionUnsatisfied,
2203-
self.nonce.zcheck(|| "nonce".to_string(), &a.nonce),
2203+
self.nonce.out_zcheck(|| "nonce".to_string(), &a.nonce),
22042204
),
22052205
(
22062206
TransactionFailure::AccountReceiptChainHashPreconditionUnsatisfied,
22072207
self.receipt_chain_hash
2208-
.zcheck(|| "receipt_chain_hash".to_string(), &a.receipt_chain_hash.0),
2208+
.out_zcheck(|| "receipt_chain_hash".to_string(), &a.receipt_chain_hash.0),
22092209
),
22102210
(
22112211
TransactionFailure::AccountDelegatePreconditionUnsatisfied,
2212-
self.delegate.zcheck(
2212+
self.delegate.out_zcheck(
22132213
|| "delegate".to_string(),
22142214
&a.delegate.clone().unwrap_or_else(invalid_public_key),
22152215
),
@@ -2218,7 +2218,7 @@ pub mod zkapp_command {
22182218
TransactionFailure::AccountActionStatePreconditionUnsatisfied,
22192219
match zkapp.action_state.iter().find(|state| {
22202220
self.action_state
2221-
.zcheck(|| "".to_string(), &**state)
2221+
.out_zcheck(|| "".to_string(), &**state)
22222222
.is_ok()
22232223
}) {
22242224
None => Err("Action state mismatch".to_string()),
@@ -2230,27 +2230,28 @@ pub mod zkapp_command {
22302230
for (i, (c, v)) in self.state.iter().zip(zkapp.app_state.iter()).enumerate() {
22312231
ret.push((
22322232
TransactionFailure::AccountAppStatePreconditionUnsatisfied(i as u64),
2233-
c.zcheck(|| format!("state[{}]", i), &*v),
2233+
c.out_zcheck(|| format!("state[{}]", i), v),
22342234
));
22352235
}
22362236

22372237
let mut ret2 = vec![
22382238
(
22392239
TransactionFailure::AccountProvedStatePreconditionUnsatisfied,
22402240
self.proved_state
2241-
.zcheck(|| "proved_state".to_string(), &zkapp.proved_state),
2241+
.out_zcheck(|| "proved_state".to_string(), &zkapp.proved_state),
22422242
),
22432243
(
22442244
TransactionFailure::AccountIsNewPreconditionUnsatisfied,
2245-
self.is_new.zcheck(|| "is_new".to_string(), &new_account),
2245+
self.is_new
2246+
.out_zcheck(|| "is_new".to_string(), &new_account),
22462247
),
22472248
];
22482249

22492250
ret.append(&mut ret2);
22502251
ret
22512252
}
22522253

2253-
fn checked_zchecks<Ops: ZkappCheckOps>(
2254+
fn zchecks<Ops: ZkappCheckOps>(
22542255
&self,
22552256
account: &crate::Account,
22562257
new_account: Boolean,
@@ -2273,7 +2274,7 @@ pub mod zkapp_command {
22732274
let is_new = is_new.map(ToBoolean::to_boolean);
22742275
let proved_state = proved_state.map(ToBoolean::to_boolean);
22752276

2276-
// NOTE: Here we need to execute all `checked_zcheck` in the exact same order than OCaml
2277+
// NOTE: Here we need to execute all `zcheck` in the exact same order than OCaml
22772278
// so we execute them in reverse order (compared to OCaml): OCaml evaluates from right
22782279
// to left.
22792280
// We then have to reverse the resulting vector, to match OCaml resulting list.
@@ -2282,12 +2283,12 @@ pub mod zkapp_command {
22822283
let mut checks: Vec<(TransactionFailure, _)> = [
22832284
(
22842285
AccountIsNewPreconditionUnsatisfied,
2285-
(&is_new, || Boolean::False).checked_zcheck::<Ops>(&new_account, w),
2286+
(&is_new, || Boolean::False).zcheck::<Ops>(&new_account, w),
22862287
),
22872288
(
22882289
AccountProvedStatePreconditionUnsatisfied,
22892290
(&proved_state, || Boolean::False)
2290-
.checked_zcheck::<Ops>(&zkapp_account.proved_state.to_boolean(), w),
2291+
.zcheck::<Ops>(&zkapp_account.proved_state.to_boolean(), w),
22912292
),
22922293
]
22932294
.into_iter()
@@ -2298,7 +2299,7 @@ pub mod zkapp_command {
22982299
.enumerate()
22992300
.rev()
23002301
.map(|(i, (s, account_s))| {
2301-
let b = (s, Fp::zero).checked_zcheck::<Ops>(account_s, w);
2302+
let b = (s, Fp::zero).zcheck::<Ops>(account_s, w);
23022303
(AccountAppStatePreconditionUnsatisfied(i as u64), b)
23032304
})
23042305
.collect::<Vec<_>>();
@@ -2312,7 +2313,7 @@ pub mod zkapp_command {
23122313
.iter()
23132314
.map(|account_s| {
23142315
(action_state, ZkAppAccount::empty_action_state)
2315-
.checked_zcheck::<Ops>(account_s, w)
2316+
.zcheck::<Ops>(account_s, w)
23162317
})
23172318
.collect();
23182319
(
@@ -2323,20 +2324,19 @@ pub mod zkapp_command {
23232324
(
23242325
AccountDelegatePreconditionUnsatisfied,
23252326
(delegate, CompressedPubKey::empty)
2326-
.checked_zcheck::<Ops>(&*account.delegate_or_empty(), w),
2327+
.zcheck::<Ops>(&*account.delegate_or_empty(), w),
23272328
),
23282329
(
23292330
AccountReceiptChainHashPreconditionUnsatisfied,
2330-
(receipt_chain_hash, Fp::zero)
2331-
.checked_zcheck::<Ops>(&account.receipt_chain_hash.0, w),
2331+
(receipt_chain_hash, Fp::zero).zcheck::<Ops>(&account.receipt_chain_hash.0, w),
23322332
),
23332333
(
23342334
AccountNoncePreconditionUnsatisfied,
2335-
(nonce, ClosedInterval::min_max).checked_zcheck::<Ops>(&account.nonce, w),
2335+
(nonce, ClosedInterval::min_max).zcheck::<Ops>(&account.nonce, w),
23362336
),
23372337
(
23382338
AccountBalancePreconditionUnsatisfied,
2339-
(balance, ClosedInterval::min_max).checked_zcheck::<Ops>(&account.balance, w),
2339+
(balance, ClosedInterval::min_max).zcheck::<Ops>(&account.balance, w),
23402340
),
23412341
])
23422342
.collect::<Vec<_>>();
@@ -2458,7 +2458,7 @@ pub mod zkapp_command {
24582458
MyCow::Borrow(&self.0)
24592459
}
24602460

2461-
pub fn checked_zcheck<Ops, Fun>(
2461+
pub fn zcheck<Ops, Fun>(
24622462
&self,
24632463
new_account: Boolean,
24642464
account: &crate::Account,
@@ -2469,7 +2469,7 @@ pub mod zkapp_command {
24692469
Fun: FnMut(TransactionFailure, Boolean, &mut Witness<Fp>),
24702470
{
24712471
let this = self.to_full();
2472-
for (failure, passed) in this.checked_zchecks::<Ops>(account, new_account, w) {
2472+
for (failure, passed) in this.zchecks::<Ops>(account, new_account, w) {
24732473
check(failure, passed, w);
24742474
}
24752475
}
@@ -5862,14 +5862,14 @@ where
58625862
match eff {
58635863
Eff::CheckValidWhilePrecondition(valid_while, global_state) => PerformResult::Bool(
58645864
valid_while
5865-
.zcheck(
5865+
.out_zcheck(
58665866
|| "valid_while_precondition".to_string(),
58675867
&global_state.block_global_slot,
58685868
)
58695869
.is_ok(),
58705870
),
58715871
Eff::CheckProtocolStatePrecondition(pred, global_state) => {
5872-
PerformResult::Bool(pred.zcheck(&global_state.protocol_state).is_ok())
5872+
PerformResult::Bool(pred.out_zcheck(&global_state.protocol_state).is_ok())
58735873
}
58745874
Eff::CheckAccountPrecondition(account_update, account, new_account, local_state) => {
58755875
let local_state = {
@@ -5878,7 +5878,7 @@ where
58785878
let check = |failure, b| {
58795879
_local_state = _local_state.add_check(failure, b);
58805880
};
5881-
precondition_account.zcheck(new_account, check, &account);
5881+
precondition_account.out_zcheck(new_account, check, &account);
58825882
_local_state
58835883
};
58845884
PerformResult::LocalState(Box::new(local_state))

0 commit comments

Comments
 (0)