Aztec 백서

Aztec

원문

bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 75쪽 중 24쪽

net/docs/aztec-nr/framework-description/advanced/partial_notes.md) --- # Using Capsules Capsules provide per-contract non-volatile storage in the PXE. Data is stored locally (not onchain), scoped per contract address, and persists until explicitly deleted. ## Basic usage[​](#basic-usage "Direct link to Basic usage") ``` use aztec::oracle::capsules; // Capsule operations are unconstrained, so these values are typically // passed in as parameters from the calling context. let contract_address: AztecAddress = /* self.address */; let slot: Field = 1; // scope is an AztecAddress used for capsule isolation, allowing multiple // independent namespaces within the same contract. let scope: AztecAddress = /* e.g. the account address */; // Store data at a slot (overwrites existing data) capsules::store(contract_address, slot, value, scope); // Load data (returns Option<T>) let result: Option<MyStruct> = capsules::load(contract_address, slot, scope); // Delete data at a slot capsules::delete(contract_address, slot, scope); // Copy contiguous slots (supports overlapping regions) // copy(contract_address, src_slot, dst_slot, num_entries: u32, scope) capsules::copy(contract_address, src_slot, dst_slot, 3, scope); ``` Types must implement `Serialize` and `Deserialize` traits. warning All capsule operations are `unconstrained`. Data loaded from capsules should be validated in constrained contexts. Contracts can only access their own capsules. ## CapsuleArray[​](#capsulearray "Direct link to CapsuleArray") `CapsuleArray` provides dynamic array storage backed by capsules: ``` use aztec::capsules::CapsuleArray; use aztec::protocol::hash::sha256_to_field; // Use a hash for base_slot to avoid collisions with other storage global BASE_SLOT: Field = sha256_to_field("MY_CONTRACT::MY_ARRAY".as_bytes()); let array: CapsuleArray<Field> = CapsuleArray::at(contract_address, BASE_SLOT, scope); array.push(value); // Append to end let value = array.get(index); // Read at index (throws if out of bounds) let length = array.len(); // Get current size (returns u32) array.remove(index); // Delete & shift elements (index is u32) // Iterate and optionally remove elements array.for_each(|index, value| { if some_condition(value) { array.remove(index); // Safe to remove current element only } }); ``` `for_each` Safety It is safe to remove the current element during `for_each`, but **do not push new elements** during iteration. Storage Layout CapsuleArray stores length at the base slot, with elements in consecutive slots (base+1 for index 0, base+2 for index 1, etc.). Ensure sufficient space between different array base slots. --- # Partial notes Token standard Where this page refers to a concrete token, it assumes the [AIP-20 fungible token standard](/developers/testnet/docs/aztec-nr/standards/aip-20.md). The partial-note primitive (`UintNote` / `PartialUintNote`) is token-agnostic; AIP-20 is one standard built on top of it. ## What are partial notes?[​](#what-are-partial-notes "Direct link to What are partial notes?") Partial notes are notes created with incomplete data, usually during private execution, which can be completed with additional information that becomes available later, usually during public execution. Let's say, for example, we have a `UintNote`: uint\_note\_def ``` #[derive(Deserialize, Eq, Serialize, Packable)] #[custom_note] pub struct UintNote { /// The number stored in the note. pub value: u128, } ``` > [Source code: noir-projects/aztec-nr/uint-note/src/uint\_note.nr#L30-L37](https://github.com/aztec-labs-eng/aztec-node/blob/v6.0.0-rc.1/noir-projects/aztec-nr/uint-note/src/uint_note.nr#L30-L37) The `UintNote` struct itself only contains the `value` field. Additional fields including `owner`, `randomness`, and `storage_slot` are passed as parameters during note hash computation. When creating the note locally during private execution, the `owner` and `storage_slot` are known, but the `value` potentially is not (e.g., it depends on some onchain dynamic variable). First, a **partial note** can be created during private execution that commits to the `owner` and `randomness`, and then the note is *"completed"* to create a full note by later adding the `storage_slot` and `value` fields, usually during public execution. ![](/assets/ideal-img/partial-notes.167e271.640.png) ## Use cases[​](#use-cases "Direct link to Use cases") Partial notes are useful when part of the note struct is a value that depends on dynamic, public onchain data that isn't available during private execution, such as: * AMM swap prices * Current gas prices * Time-dependent interest accrual They are also useful as **payment endpoints**: a recipient can create a partial note ahead of time and share the commitment with prospective senders. Senders later complete the partial note to pay the recipient, with no action needed from the recipient at payment time. See [partial notes as payment endpoints](/developers/testnet/docs/aztec-nr/framework-description/advanced/partial_notes_as_payment_endpoints.md) for the full design. ## The completer[​](#the-completer "Direct link to The completer") A partial note is finalized by a later completion step that supplies the public fields (`storage_slot` and `value`). At that point its private preimage (`owner` and `randomness`) is not re-derived or re-checked, and the partial note itself is just a `Field` that can be copied and shared freely. If anyone holding it could complete it, they could insert a note with an arbitrary, unbacked `value` into the note hash tree, or complete the note at the wrong time or with the wrong values. To prevent this, the creator designates a **completer** at creation time. During the constrained private execution that creates the partial note, the contract records a validity commitment `H(partial_commitment, completer)` in the nullifier tree. Completion recomputes this commitment and asserts it exists, using its presence as proof that a legitimate, constrained execution created the partial note and authorized this specific completer to supply the public values and finalize it. With AIP-20, the completer is chosen explicitly when calling `initialize_transfer_commitment`; completion (`transfer_private_to_commitment` / `transfer_public_to_commitment`) binds the completer to the caller's `msg_sender` and debits a separately authorized `from` account. For `UintNote`, the fields split cleanly across the two phases: | Field | Fixed at | How | | -------------- | ------------------------------- | ---------------------------------------------------------------------------------------------- | | `owner` | Partial note creation (private) | Committed in `partial_commitment = H(owner, randomness)` | | `randomness` | Partial note creation (private) | Same commitment; fresh per note, blinds the owner | | `completer` | Partial note creation (private) | Bound in the validity commitment `H(partial_commitment, completer)`; not part of the note hash | | `storage_slot` | Completion (public or private) | Hashed into `note_hash = H(storage_slot, partial_commitment, value)` | | `value` | Completion (public or private) | Same hash; supplied by the completer's call | (`storage_slot` is typically known during private execution too; it is just not bound into the note hash until completion.) The creator fixes who gets paid (`owner`) and who may finalize (`completer`); the completer later fixes how much (`value`). Funds therefore flow from the completing side to the note's owner: in AIP-20, completion debits the authorized `from` account (the completer itself, or a payer who authorized it) and credits the `owner` chosen by the creator. ## Single-use semantics[​](#single-use-semantics "Direct link to Single-use semantics") Each partial note is intended to be completed exactly once. The protocol does not enforce this directly: completion checks that a validity commitment exists in the nullifier tree but does not consume it, so a partial note can technically be completed more than once. However, reuse is unsafe for two independent reasons: 1. **Privacy.** The completion log is tagged by `H(partial_commitment)`. Two completions of the same partial note emit logs with the same tag, which publicly links those completions as paying the same recipient. 2. **Discovery.** The recipient's Private eXecution Environment (PXE) treats the partial note as pending until the first matching completion log is found. After the first match, the pending entry is removed. A second completion against the same commitment may not be discovered by the recipient's wallet, so the funds are effectively lost. This is why an AIP-20 commitment should be completed only once. A second `transfer_private_to_commitment` (or `transfer_public_to_commitment`) against the same commitment is not found by the recipient's log processing on the second pass, so the amount is most likely lost. The takeaway: treat each partial note as a one-shot object. To accept multiple payments, create multiple partial notes. ## Completion in public and private contexts[​](#completion-in-public-and-private-contexts "Direct link to Completion in public and private contexts") `PartialUintNote` supports completion in two contexts: * `complete` runs in a public function (AIP-20's `transfer_public_to_commitment`). The storage slot and value are emitted in a public log tagged by the partial note's commitment. Anyone observing the chain learns the amount. * `complete_from_private` runs in a private function (AIP-20's `transfer_private_to_commitment`). The same storage slot and value are emitted in a private log with the same tag. The payload is plaintext, but it is only discoverable by a party that can derive the tag, and the tag derives from the partial note's commitment. For private→private completion, the privacy of the amount depends on whether the partial note's commitment itself is held secret. If the commitment is published publicly (e.g., in an onchain registry), anyone can derive the tag and read the amount from the private log payload. If the commitment is shared only with prospective senders, the amount stays hidden from outside observers. One additional protocol constraint: `complete_from_private` requires the validity commitment to be settled in a prior transaction. A partial note cannot be both created and completed in the same private transaction. The public completion path has no such restriction. ## Implementation[​](#implementation "Direct link to Implementation") All notes in Aztec use the partial note format internally. This ensures that notes produce identical note hashes regardless of whether they were created as complete notes (with all fields known in private) or as partial notes (completed later in public). By having all notes follow the same two-phase hash commitment process, the protocol maintains consistency and allows notes created through different flows to behave identically. ### Note structure example[​](#note-structure-example "Direct link to Note structure example") The `UintNote` struct contains only the `value` field: uint\_note\_def ``` #[derive(Deserialize, Eq, Serialize, Packable)] #[custom_note] pub struct UintNote { /// The number stored in the note. pub value: u128, } ``` > [Source code: noir-projects/aztec-nr/uint-note/src/uint\_note.nr#L30-L37](https://github.com/aztec-labs-eng/aztec-node/blob/v6.0.0-rc.1/noir-projects/aztec-nr/uint-note/src/uint_note.nr#L30-L37) ### Two-phase commitment process[​](#two-phase-commitment-process "Direct link to Two-phase commitment process") **Phase 1: partial commitment (private execution)** The private fields (`owner` and `randomness`) are committed during local, private execution: compute\_partial\_commitment ``` fn compute_partial_commitment(owner: AztecAddress, randomness: Field) -> Field { poseidon2_hash_with_separator( [owner.to_field(), randomness], DOM_SEP__PARTIAL_NOTE_COMMITMENT, ) } ``` > [Source code: noir-projects/aztec-nr/uint-note/src/uint\_note.nr#L145-L152](https://github.com/aztec-labs-eng/aztec-node/blob/v6.0.0-rc.1/noir-projects/aztec-nr/uint-note/src/uint_note.nr#L145-L152) This creates a partial note commitment: ``` partial_commitment = H(owner, randomness) ``` **Phase 2: note completion (public execution)** The note is completed by hashing the partial commitment with the public value: compute\_complete\_note\_hash ``` fn compute_complete_note_hash(self, storage_slot: Field, value: u128) -> Field { compute_note_hash(storage_slot, [self.commitment, value.to_field()]) } ``` > [Source code: noir-projects/aztec-nr/uint-note/src/uint\_note.nr#L257-L261](https://github.com/aztec-labs-eng/aztec-node/blob/v6.0.0-rc.1/noir-projects/aztec-nr/uint-note/src/uint_note.nr#L257-L261) The resulting structure is a nested commitment: ``` note_hash = H(storage_slot, H(owner, randomness), value) = H(storage_slot, partial_commitment, value) ``` ## Universal note format[​](#universal-note-format "Direct link to Universal note format") All notes in Aztec use the partial note format internally, even when all data is known during private execution. This ensures consistent note hash computation regardless of how the note was created. When a note is created with all fields known (including `owner`, `storage_slot`, `randomness`, and `value`): 1. A partial commitment is computed from the private fields (`owner`, `randomness`) 2. The partial commitment is immediately completed with the `storage_slot` and `value` fields compute\_note\_hash ``` fn compute_note_hash(self, owner: AztecAddress, storage_slot: Field, randomness: Field) -> Field { // Partial notes can be implemented by having the note hash be either the result of multiscalar multiplication // (MSM), or two rounds of poseidon. MSM results in more constraints and is only required when multiple // variants of partial notes are supported. Because UintNote has just one variant (where the value is public), // we use poseidon instead. // We must compute the same note hash as would be produced by a partial note created and completed with the // same values, so that notes all behave the same way regardless of how they were created. To achieve this, we // perform both steps of the partial note computation. // First we create the partial note from a commitment to the private content. let partial_note = PartialUintNote { commitment: compute_partial_commitment(owner, randomness) }; // Then compute the completion note hash. In a real partial note this step would be performed in public. partial_note.compute_complete_note_hash(storage_slot, self.value) } ``` > [Source code: noir-projects/aztec-nr/uint-note/src/uint\_note.nr#L40-L57](https://github.com/aztec-labs-eng/aztec-node/blob/v6.0.0-rc.1/noir-projects/aztec-nr/uint-note/src/uint_note.nr#L40-L57) This two-step process ensures that notes with identical field values produce identical note hashes, regardless of whether they were created as partial notes or complete notes. ## Partial notes in practice[​](#partial-notes-in-practice "Direct link to Partial notes in practice") To understand how to use partial notes in practice, [this AMM contract](https://github.com/aztec-labs-eng/aztec-node/tree/v6.0.0-rc.1/noir-projects/noir-contracts/contracts/app/amm_contract) uses partial notes to initiate and complete the swap of `token1` to `token2`. Since the exchange rate is onchain, it cannot be known ahead of time while executing in private so a full note cannot be created. Instead, a partial note is created for the `owner` swapping the tokens. This partial note is then completed during public execution once the exchange rate can be read. For a different application of the same primitive, where the partial note represents an offer to be paid rather than a deferred DeFi settlement, see [partial notes as payment endpoints](/developers/testnet/docs/aztec-nr/framework-description/advanced/partial_notes_as_payment_endpoints.md). --- # Partial notes as payment endpoints Assumed token standard This page assumes the [AIP-20 fungible token standard](/developers/testnet/docs/aztec-nr/standards/aip-20.md), which exposes commitment-based transfers directly: the completer is an explicit argument to `initialize_transfer_commitment`, and completion debits a separately authorized account. The example `token_contract` in aztec-packages also supports a commitment-based flow (the two-step `prepare_private_balance_increase` plus `finalize_transfer_to_private`), but its prepare step takes no `completer` argument. It always sets the completer to `msg_sender`, the caller of `prepare_private_balance_increase`. The private relayer flow described on this page is still achievable there, but the chosen completer (for example, the relayer) must itself be the caller of `prepare`, rather than the recipient naming an arbitrary completer in a single self-issued call as AIP-20 allows. ## The problem[​](#the-problem "Direct link to The problem") Consider a naming service that resolves `alice.aztec` to something that anyone can pay. If the name resolves to a stable Aztec address, every sender and every registry observer can link payments to the same recipient identifier, even when the payment notes themselves are private. The recipient is forced to share that identifier with every sender, so two senders can collude to confirm they paid the same person. A privacy-conscious naming service should let a sender pay `alice.aztec` without: * learning the recipient's actual Aztec address, * producing a sender-visible or registry-visible link between any two payments to the same name, * requiring the recipient to take any action when the payment is made. [Partial notes](/developers/testnet/docs/aztec-nr/framework-description/advanced/partial_notes.md) provide the primitive. This page covers how to use them to back a stable name with a rotating supply of unlinkable, one-shot payment endpoints. The term *payment endpoint* in this page is application-level. Aztec's protocol terminology stays with "partial note," "completer," and "completion log." ## What lives where[​](#what-lives-where "Direct link to What lives where") The trick is that the name never maps to an address at all. It maps to a list of partial note commitments, and senders pay into a commitment. Concretely, for `alice.aztec`: 1. **Alice's wallet creates partial notes ahead of time.** Each one is a commitment `H(alice_address, randomness_i)` with fresh randomness per note. Her address is inside the hash, blinded by the randomness, so the commitment reveals nothing about her. 2. **The chain records that the note can be completed, and by whom.** Creating each partial note writes a *validity commitment* `H(partial_commitment, completer)` to the nullifier tree. This is not the partial note itself, it binds the partial note's commitment to its designated completer, and is what the token checks at completion to confirm the completer is legitimate. It is also just a hash, so no address is visible. 3. **The lookup channel stores `alice.aztec → [c1, c2, c3, ...]`.** The *lookup channel* is wherever the name resolves: an ENS text record, a JSON file Alice hosts, an onchain registry contract. The commitments are opaque `Field` values that can be stored as a plain array, and this mapping is the only thing senders ever read. 4. **Alice's Private eXecution Environment (PXE) holds the preimages.** Her wallet knows which commitments are hers and watches for their completion logs, so payments land without any action from her. The rest of this page unpacks each piece: how a partial note works as an offer to be paid, who is allowed to complete one, and where the lookup channel can live. ## A partial note as an offer to be paid[​](#a-partial-note-as-an-offer-to-be-paid "Direct link to A partial note as an offer to be paid") A partial note is a commitment to `(owner, randomness)`.
프로젝트로 돌아가기