Skip to content

Commit a6fee2e

Browse files
committed
Simplify ZkappApplication
1 parent 25cb4f0 commit a6fee2e

File tree

4 files changed

+39
-46
lines changed

4 files changed

+39
-46
lines changed

ledger/src/zkapps/intefaces.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ pub trait GlobalSlotSinceGenesisInterface {
197197
type Bool: BoolInterface;
198198

199199
fn equal(&self, other: &Self, w: &mut Self::W) -> Self::Bool;
200+
fn exists_no_check(self, w: &mut Self::W) -> Self;
200201
}
201202

202203
pub trait GlobalSlotSpanInterface {
@@ -477,7 +478,7 @@ where
477478
fn set_last_action_slot(&mut self, slot: Self::GlobalSlot);
478479
}
479480

480-
pub trait LedgerInterface {
481+
pub trait LedgerInterface: LedgerIntf + Clone + ToFieldElements<Fp> {
481482
type W: WitnessGenerator<Fp>;
482483
type AccountUpdate: AccountUpdateInterface;
483484
type Account: AccountInterface;
@@ -567,15 +568,12 @@ pub trait ZkappApplication
567568
where
568569
Self: Sized,
569570
{
570-
type Ledger: LedgerIntf
571-
+ Clone
572-
+ ToFieldElements<Fp>
573-
+ LedgerInterface<
574-
W = Self::WitnessGenerator,
575-
AccountUpdate = Self::AccountUpdate,
576-
Account = Self::Account,
577-
Bool = Self::Bool,
578-
>;
571+
type Ledger: LedgerInterface<
572+
W = Self::WitnessGenerator,
573+
AccountUpdate = Self::AccountUpdate,
574+
Account = Self::Account,
575+
Bool = Self::Bool,
576+
>;
579577
type SignedAmount: SignedAmountInterface<W = Self::WitnessGenerator, Bool = Self::Bool, Amount = Self::Amount>
580578
+ std::fmt::Debug
581579
+ Clone
@@ -588,10 +586,11 @@ where
588586
SignedAmount = Self::SignedAmount,
589587
>;
590588
type Index: IndexInterface + Clone + ToFieldElements<Fp>;
591-
type GlobalSlotSinceGenesis: GlobalSlotSinceGenesisInterface<W = Self::WitnessGenerator, Bool = Self::Bool>
592-
+ ToFieldElements<Fp>;
589+
type GlobalSlotSinceGenesis: GlobalSlotSinceGenesisInterface<
590+
W = Self::WitnessGenerator,
591+
Bool = Self::Bool,
592+
>;
593593
type StackFrame: StackFrameInterface<W = Self::WitnessGenerator, Calls = Self::CallForest, Bool = Self::Bool>
594-
+ ToFieldElements<Fp>
595594
+ std::fmt::Debug
596595
+ Clone;
597596
type CallForest: CallForestInterface<

ledger/src/zkapps/non_snark.rs

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,12 @@ pub struct NonSnarkHandler<L>(PhantomData<L>);
6262
pub struct ZkappNonSnark<L>(PhantomData<L>);
6363

6464
impl<
65-
L: LedgerIntf
66-
+ Clone
67-
+ ToFieldElements<Fp>
68-
+ LedgerInterface<
69-
W = (),
70-
AccountUpdate = AccountUpdate,
71-
Account = crate::Account,
72-
Bool = bool,
73-
>,
65+
L: LedgerInterface<
66+
W = (),
67+
AccountUpdate = AccountUpdate,
68+
Account = crate::Account,
69+
Bool = bool,
70+
>,
7471
> ZkappApplication for ZkappNonSnark<L>
7572
{
7673
type Ledger = L;
@@ -128,15 +125,12 @@ impl<F: FieldWitness> WitnessGenerator<F> for () {
128125
}
129126

130127
impl<
131-
L: LedgerIntf
132-
+ Clone
133-
+ ToFieldElements<Fp>
134-
+ LedgerInterface<
135-
W = (),
136-
AccountUpdate = AccountUpdate,
137-
Account = crate::Account,
138-
Bool = bool,
139-
>,
128+
L: LedgerInterface<
129+
W = (),
130+
AccountUpdate = AccountUpdate,
131+
Account = crate::Account,
132+
Bool = bool,
133+
>,
140134
> ZkappHandler for NonSnarkHandler<L>
141135
{
142136
type Z = ZkappNonSnark<L>;
@@ -322,15 +316,12 @@ impl TokenIdInterface for TokenId {
322316
}
323317

324318
impl<
325-
L: LedgerIntf
326-
+ Clone
327-
+ ToFieldElements<Fp>
328-
+ LedgerInterface<
329-
W = (),
330-
AccountUpdate = AccountUpdate,
331-
Account = crate::Account,
332-
Bool = bool,
333-
>,
319+
L: LedgerInterface<
320+
W = (),
321+
AccountUpdate = AccountUpdate,
322+
Account = crate::Account,
323+
Bool = bool,
324+
>,
334325
> LocalStateInterface for zkapp_logic::LocalState<ZkappNonSnark<L>>
335326
{
336327
type Z = ZkappNonSnark<L>;
@@ -426,11 +417,9 @@ impl GlobalSlotSinceGenesisInterface for Slot {
426417
fn equal(&self, other: &Self, w: &mut Self::W) -> Self::Bool {
427418
self == other
428419
}
429-
}
430420

431-
impl<F: FieldWitness> ToFieldElements<F> for StackFrame {
432-
fn to_field_elements(&self, fields: &mut Vec<F>) {
433-
unreachable!()
421+
fn exists_no_check(self, w: &mut Self::W) -> Self {
422+
self
434423
}
435424
}
436425

ledger/src/zkapps/snark.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,10 @@ impl GlobalSlotSinceGenesisInterface for SnarkGlobalSlot {
655655
fn equal(&self, other: &Self, w: &mut Self::W) -> Self::Bool {
656656
<Self as CheckedNat<_, 32>>::equal(self, other, w).var()
657657
}
658+
659+
fn exists_no_check(self, w: &mut Self::W) -> Self {
660+
w.exists_no_check(self)
661+
}
658662
}
659663

660664
fn signature_verifies(

ledger/src/zkapps/zkapp_logic.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,11 @@ fn update_action_state<Z: ZkappApplication>(
290290
Boolean::True => s2,
291291
Boolean::False => s1,
292292
});
293-
let last_action_slot = w.exists_no_check(match is_empty.as_boolean() {
293+
let last_action_slot = match is_empty.as_boolean() {
294294
Boolean::True => last_action_slot,
295295
Boolean::False => txn_global_slot,
296-
});
296+
}
297+
.exists_no_check(w);
297298
([s1_new, s2, s3, s4, s5], last_action_slot)
298299
}
299300

0 commit comments

Comments
 (0)