-
Notifications
You must be signed in to change notification settings - Fork 47
/
mod.rs
615 lines (528 loc) · 21.8 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
use crate::{
assets::AssetVault,
utils::serde::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable},
AccountError, Digest, Felt, Hasher, Word, ZERO,
};
pub mod account_id;
pub use account_id::{
AccountId, AccountStorageMode, AccountType, ACCOUNT_ISFAUCET_MASK, ACCOUNT_STORAGE_MASK_SHIFT,
ACCOUNT_TYPE_MASK_SHIFT,
};
pub mod auth;
pub use auth::AuthSecretKey;
mod builder;
pub use builder::AccountBuilder;
pub mod code;
pub use code::{procedure::AccountProcedureInfo, AccountCode};
mod component;
pub use component::AccountComponent;
pub mod delta;
pub use delta::{
AccountDelta, AccountStorageDelta, AccountVaultDelta, FungibleAssetDelta,
NonFungibleAssetDelta, NonFungibleDeltaAction, StorageMapDelta,
};
mod seed;
pub use seed::{get_account_seed, get_account_seed_single};
mod storage;
pub use storage::{AccountStorage, AccountStorageHeader, StorageMap, StorageSlot, StorageSlotType};
mod header;
pub use header::AccountHeader;
mod data;
pub use data::AccountData;
// ACCOUNT
// ================================================================================================
/// An account which can store assets and define rules for manipulating them.
///
/// An account consists of the following components:
/// - Account ID, which uniquely identifies the account and also defines basic properties of the
/// account.
/// - Account vault, which stores assets owned by the account.
/// - Account storage, which is a key-value map (both keys and values are words) used to store
/// arbitrary user-defined data.
/// - Account code, which is a set of Miden VM programs defining the public interface of the
/// account.
/// - Account nonce, a value which is incremented whenever account state is updated.
///
/// Out of the above components account ID is always immutable (once defined it can never be
/// changed). Other components may be mutated throughout the lifetime of the account. However,
/// account state can be changed only by invoking one of account interface methods.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Account {
id: AccountId,
vault: AssetVault,
storage: AccountStorage,
code: AccountCode,
nonce: Felt,
}
impl Account {
// CONSTRUCTORS
// --------------------------------------------------------------------------------------------
/// Creates and returns a new [Account] instantiated with the specified code, storage, and
/// account seed.
///
/// The returned account has an empty asset vault and the nonce which is initialized to ZERO.
///
/// # Errors
/// Returns an error if deriving account ID from the specified seed fails.
pub fn new(
seed: Word,
code: AccountCode,
storage: AccountStorage,
) -> Result<Self, AccountError> {
let id = AccountId::new(seed, code.commitment(), storage.commitment())?;
let vault = AssetVault::default();
let nonce = ZERO;
Ok(Self { id, vault, storage, code, nonce })
}
/// Returns an [Account] instantiated with the provided components.
pub fn from_parts(
id: AccountId,
vault: AssetVault,
storage: AccountStorage,
code: AccountCode,
nonce: Felt,
) -> Self {
Self { id, vault, storage, code, nonce }
}
/// Creates an account's [`AccountCode`] and [`AccountStorage`] from the provided components.
///
/// This merges all libraries of the components into a single
/// [`MastForest`](vm_processor::MastForest) to produce the [`AccountCode`]. For each
/// procedure in the resulting forest, the storage offset and size are set so that the
/// procedure can only access the storage slots of the component in which it was defined and
/// each component's storage offset is the total number of slots in the previous components.
/// To illustrate, given two components with one and two storage slots respectively:
///
/// - RpoFalcon512 Component: Component slot 0 stores the public key.
/// - Custom Component: Component slot 0 stores a custom [`StorageSlot::Value`] and component
/// slot 1 stores a custom [`StorageSlot::Map`].
///
/// When combined, their assigned slots in the [`AccountStorage`] would be:
///
/// - The RpoFalcon512 Component has offset 0 and size 1: Account slot 0 stores the public key.
/// - The Custom Component has offset 1 and size 2: Account slot 1 stores the value and account
/// slot 2 stores the map.
///
/// The resulting commitments from code and storage can then be used to construct an
/// [`AccountId`]. Finally, a new account can then be instantiated from those parts using
/// [`Account::new`].
///
/// If the account type is faucet the reserved slot (slot 0) will be initialized.
/// - For Fungible Faucets the value is [`StorageSlot::empty_value`].
/// - For Non-Fungible Faucets the value is [`StorageSlot::empty_map`].
///
/// If the storage needs to be initialized with certain values in that slot, those can be added
/// after construction with the standard set methods for items and maps.
///
/// # Errors
///
/// Returns an error if:
/// - Any of the components does not support `account_type`.
/// - The number of procedures in all merged libraries is 0 or exceeds
/// [`AccountCode::MAX_NUM_PROCEDURES`].
/// - Two or more libraries export a procedure with the same MAST root.
/// - The number of [`StorageSlot`]s of all components exceeds 255.
/// - [`MastForest::merge`](vm_processor::MastForest::merge) fails on all libraries.
pub fn initialize_from_components(
account_type: AccountType,
components: &[AccountComponent],
) -> Result<(AccountCode, AccountStorage), AccountError> {
validate_components_support_account_type(components, account_type)?;
let code = AccountCode::from_components_unchecked(components, account_type)?;
let storage = AccountStorage::from_components(components, account_type)?;
Ok((code, storage))
}
/// Returns a new [`AccountBuilder`]. See its documentation for details.
pub fn builder() -> AccountBuilder {
AccountBuilder::new()
}
// PUBLIC ACCESSORS
// --------------------------------------------------------------------------------------------
/// Returns hash of this account.
///
/// Hash of an account is computed as hash(id, nonce, vault_root, storage_commitment,
/// code_commitment). Computing the account hash requires 2 permutations of the hash
/// function.
pub fn hash(&self) -> Digest {
hash_account(
self.id,
self.nonce,
self.vault.commitment(),
self.storage.commitment(),
self.code.commitment(),
)
}
/// Returns hash of this account as used for the initial account state hash in transaction
/// proofs.
///
/// For existing accounts, this is exactly the same as [Account::hash()], however, for new
/// accounts this value is set to [crate::EMPTY_WORD]. This is because when a transaction is
/// executed against a new account, public input for the initial account state is set to
/// [crate::EMPTY_WORD] to distinguish new accounts from existing accounts. The actual hash of
/// the initial account state (and the initial state itself), are provided to the VM via the
/// advice provider.
pub fn init_hash(&self) -> Digest {
if self.is_new() {
Digest::default()
} else {
self.hash()
}
}
/// Returns unique identifier of this account.
pub fn id(&self) -> AccountId {
self.id
}
/// Returns the account type
pub fn account_type(&self) -> AccountType {
self.id.account_type()
}
/// Returns a reference to the vault of this account.
pub fn vault(&self) -> &AssetVault {
&self.vault
}
/// Returns a reference to the storage of this account.
pub fn storage(&self) -> &AccountStorage {
&self.storage
}
/// Returns a reference to the code of this account.
pub fn code(&self) -> &AccountCode {
&self.code
}
/// Returns nonce for this account.
pub fn nonce(&self) -> Felt {
self.nonce
}
/// Returns true if this account can issue assets.
pub fn is_faucet(&self) -> bool {
self.id.is_faucet()
}
/// Returns true if this is a regular account.
pub fn is_regular_account(&self) -> bool {
self.id.is_regular_account()
}
/// Returns true if this account is public.
pub fn is_public(&self) -> bool {
self.id.is_public()
}
/// Returns true if the account is new (i.e. it has not been initialized yet).
pub fn is_new(&self) -> bool {
self.nonce == ZERO
}
// DATA MUTATORS
// --------------------------------------------------------------------------------------------
/// Applies the provided delta to this account. This updates account vault, storage, and nonce
/// to the values specified by the delta.
///
/// # Errors
/// Returns an error if:
/// - Applying vault sub-delta to the vault of this account fails.
/// - Applying storage sub-delta to the storage of this account fails.
/// - The nonce specified in the provided delta smaller than or equal to the current account
/// nonce.
pub fn apply_delta(&mut self, delta: &AccountDelta) -> Result<(), AccountError> {
// update vault; we don't check vault delta validity here because `AccountDelta` can contain
// only valid vault deltas
self.vault
.apply_delta(delta.vault())
.map_err(AccountError::AssetVaultUpdateError)?;
// update storage
self.storage.apply_delta(delta.storage())?;
// update nonce
if let Some(nonce) = delta.nonce() {
self.set_nonce(nonce)?;
}
Ok(())
}
/// Sets the nonce of this account to the specified nonce value.
///
/// # Errors
/// Returns an error if:
/// - The new nonce is smaller than the actual account nonce
/// - The new nonce is equal to the actual account nonce
pub fn set_nonce(&mut self, nonce: Felt) -> Result<(), AccountError> {
if self.nonce.as_int() >= nonce.as_int() {
return Err(AccountError::NonceNotMonotonicallyIncreasing {
current: self.nonce.as_int(),
new: nonce.as_int(),
});
}
self.nonce = nonce;
Ok(())
}
// TEST HELPERS
// --------------------------------------------------------------------------------------------
#[cfg(any(feature = "testing", test))]
/// Returns a mutable reference to the vault of this account.
pub fn vault_mut(&mut self) -> &mut AssetVault {
&mut self.vault
}
}
// SERIALIZATION
// ================================================================================================
impl Serializable for Account {
fn write_into<W: ByteWriter>(&self, target: &mut W) {
let Account { id, vault, storage, code, nonce } = self;
id.write_into(target);
vault.write_into(target);
storage.write_into(target);
code.write_into(target);
nonce.write_into(target);
}
fn get_size_hint(&self) -> usize {
self.id.get_size_hint()
+ self.vault.get_size_hint()
+ self.storage.get_size_hint()
+ self.code.get_size_hint()
+ self.nonce.get_size_hint()
}
}
impl Deserializable for Account {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
let id = AccountId::read_from(source)?;
let vault = AssetVault::read_from(source)?;
let storage = AccountStorage::read_from(source)?;
let code = AccountCode::read_from(source)?;
let nonce = Felt::read_from(source)?;
Ok(Self::from_parts(id, vault, storage, code, nonce))
}
}
// HELPERS
// ================================================================================================
/// Returns hash of an account with the specified ID, nonce, vault root, storage commitment, and
/// code commitment.
///
/// Hash of an account is computed as hash(id, nonce, vault_root, storage_commitment,
/// code_commitment). Computing the account hash requires 2 permutations of the hash function.
pub fn hash_account(
id: AccountId,
nonce: Felt,
vault_root: Digest,
storage_commitment: Digest,
code_commitment: Digest,
) -> Digest {
let mut elements = [ZERO; 16];
elements[0] = id.into();
elements[3] = nonce;
elements[4..8].copy_from_slice(&*vault_root);
elements[8..12].copy_from_slice(&*storage_commitment);
elements[12..].copy_from_slice(&*code_commitment);
Hasher::hash_elements(&elements)
}
/// Validates that all `components` support the given `account_type`.
fn validate_components_support_account_type(
components: &[AccountComponent],
account_type: AccountType,
) -> Result<(), AccountError> {
for (component_index, component) in components.iter().enumerate() {
if !component.supports_type(account_type) {
return Err(AccountError::UnsupportedComponentForAccountType {
account_type,
component_index,
});
}
}
Ok(())
}
// TESTS
// ================================================================================================
#[cfg(test)]
mod tests {
use assembly::Assembler;
use miden_crypto::{
utils::{Deserializable, Serializable},
Felt, Word,
};
use vm_processor::Digest;
use super::{AccountDelta, AccountStorageDelta, AccountVaultDelta};
use crate::{
accounts::{
Account, AccountComponent, AccountType, StorageMap, StorageMapDelta, StorageSlot,
},
testing::storage::{
build_account, build_account_delta, build_assets, AccountStorageDeltaBuilder,
},
AccountError,
};
#[test]
fn test_serde_account() {
let init_nonce = Felt::new(1);
let (asset_0, _) = build_assets();
let word = [Felt::new(1), Felt::new(2), Felt::new(3), Felt::new(4)];
let storage_slot = StorageSlot::Value(word);
let account = build_account(vec![asset_0], init_nonce, vec![storage_slot]);
let serialized = account.to_bytes();
let deserialized = Account::read_from_bytes(&serialized).unwrap();
assert_eq!(deserialized, account);
}
#[test]
fn test_serde_account_delta() {
let final_nonce = Felt::new(2);
let (asset_0, asset_1) = build_assets();
let storage_delta = AccountStorageDeltaBuilder::default()
.add_cleared_items([0])
.add_updated_values([(1_u8, [Felt::new(1), Felt::new(2), Felt::new(3), Felt::new(4)])])
.build()
.unwrap();
let account_delta =
build_account_delta(vec![asset_1], vec![asset_0], final_nonce, storage_delta);
let serialized = account_delta.to_bytes();
let deserialized = AccountDelta::read_from_bytes(&serialized).unwrap();
assert_eq!(deserialized, account_delta);
}
#[test]
fn valid_account_delta_is_correctly_applied() {
// build account
let init_nonce = Felt::new(1);
let (asset_0, asset_1) = build_assets();
// build storage slots
let storage_slot_value_0 =
StorageSlot::Value([Felt::new(1), Felt::new(2), Felt::new(3), Felt::new(4)]);
let storage_slot_value_1 =
StorageSlot::Value([Felt::new(5), Felt::new(6), Felt::new(7), Felt::new(8)]);
let mut storage_map = StorageMap::with_entries([
(
Digest::new([Felt::new(101), Felt::new(102), Felt::new(103), Felt::new(104)]),
[Felt::new(1_u64), Felt::new(2_u64), Felt::new(3_u64), Felt::new(4_u64)],
),
(
Digest::new([Felt::new(105), Felt::new(106), Felt::new(107), Felt::new(108)]),
[Felt::new(5_u64), Felt::new(6_u64), Felt::new(7_u64), Felt::new(8_u64)],
),
])
.unwrap();
let storage_slot_map = StorageSlot::Map(storage_map.clone());
let mut account = build_account(
vec![asset_0],
init_nonce,
vec![storage_slot_value_0, storage_slot_value_1, storage_slot_map],
);
// update storage map
let new_map_entry = (
Digest::new([Felt::new(101), Felt::new(102), Felt::new(103), Felt::new(104)]),
[Felt::new(9_u64), Felt::new(10_u64), Felt::new(11_u64), Felt::new(12_u64)],
);
let updated_map =
StorageMapDelta::from_iters([], [(new_map_entry.0.into(), new_map_entry.1)]);
storage_map.insert(new_map_entry.0, new_map_entry.1);
// build account delta
let final_nonce = Felt::new(2);
let storage_delta = AccountStorageDeltaBuilder::default()
.add_cleared_items([0])
.add_updated_values([(1, [Felt::new(1), Felt::new(2), Felt::new(3), Felt::new(4)])])
.add_updated_maps([(2, updated_map)])
.build()
.unwrap();
let account_delta =
build_account_delta(vec![asset_1], vec![asset_0], final_nonce, storage_delta);
// apply delta and create final_account
account.apply_delta(&account_delta).unwrap();
let final_account = build_account(
vec![asset_1],
final_nonce,
vec![
StorageSlot::Value(Word::default()),
StorageSlot::Value([Felt::new(1), Felt::new(2), Felt::new(3), Felt::new(4)]),
StorageSlot::Map(storage_map),
],
);
// assert account is what it should be
assert_eq!(account, final_account);
}
#[test]
#[should_panic]
fn valid_account_delta_with_unchanged_nonce() {
// build account
let init_nonce = Felt::new(1);
let (asset, _) = build_assets();
let mut account =
build_account(vec![asset], init_nonce, vec![StorageSlot::Value(Word::default())]);
// build account delta
let storage_delta = AccountStorageDeltaBuilder::default()
.add_cleared_items([0])
.add_updated_values([(1_u8, [Felt::new(1), Felt::new(2), Felt::new(3), Felt::new(4)])])
.build()
.unwrap();
let account_delta = build_account_delta(vec![], vec![asset], init_nonce, storage_delta);
// apply delta
account.apply_delta(&account_delta).unwrap()
}
#[test]
#[should_panic]
fn valid_account_delta_with_decremented_nonce() {
// build account
let init_nonce = Felt::new(2);
let (asset, _) = build_assets();
let mut account =
build_account(vec![asset], init_nonce, vec![StorageSlot::Value(Word::default())]);
// build account delta
let final_nonce = Felt::new(1);
let storage_delta = AccountStorageDeltaBuilder::default()
.add_cleared_items([0])
.add_updated_values([(1_u8, [Felt::new(1), Felt::new(2), Felt::new(3), Felt::new(4)])])
.build()
.unwrap();
let account_delta = build_account_delta(vec![], vec![asset], final_nonce, storage_delta);
// apply delta
account.apply_delta(&account_delta).unwrap()
}
#[test]
fn empty_account_delta_with_incremented_nonce() {
// build account
let init_nonce = Felt::new(1);
let word = [Felt::new(1), Felt::new(2), Felt::new(3), Felt::new(4)];
let storage_slot = StorageSlot::Value(word);
let mut account = build_account(vec![], init_nonce, vec![storage_slot]);
// build account delta
let final_nonce = Felt::new(2);
let account_delta = AccountDelta::new(
AccountStorageDelta::default(),
AccountVaultDelta::default(),
Some(final_nonce),
)
.unwrap();
// apply delta
account.apply_delta(&account_delta).unwrap()
}
/// Tests that initializing code and storage from a component which does not support the given
/// account type returns an error.
#[test]
fn test_account_unsupported_component_type() {
let code1 = "export.foo add end";
let library1 = Assembler::default().assemble_library([code1]).unwrap();
// This component support all account types except the regular account with updatable code.
let component1 = AccountComponent::new(library1, vec![])
.unwrap()
.with_supported_type(AccountType::FungibleFaucet)
.with_supported_type(AccountType::NonFungibleFaucet)
.with_supported_type(AccountType::RegularAccountImmutableCode);
let err = Account::initialize_from_components(
AccountType::RegularAccountUpdatableCode,
&[component1],
)
.unwrap_err();
assert!(matches!(
err,
AccountError::UnsupportedComponentForAccountType {
account_type: AccountType::RegularAccountUpdatableCode,
component_index: 0
}
))
}
/// Two components who export a procedure with the same MAST root should fail to convert into
/// code and storage.
#[test]
fn test_account_duplicate_exported_mast_root() {
let code1 = "export.foo add eq.1 end";
let code2 = "export.bar add eq.1 end";
let library1 = Assembler::default().assemble_library([code1]).unwrap();
let library2 = Assembler::default().assemble_library([code2]).unwrap();
let component1 = AccountComponent::new(library1, vec![]).unwrap().with_supports_all_types();
let component2 = AccountComponent::new(library2, vec![]).unwrap().with_supports_all_types();
let err = Account::initialize_from_components(
AccountType::RegularAccountUpdatableCode,
&[component1, component2],
)
.unwrap_err();
assert!(matches!(err, AccountError::AccountCodeMergeError(_)))
}
}