Skip to content

Commit

Permalink
docs: including "real" code in keys docs
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Aug 23, 2023
1 parent ad1c2cb commit 911a102
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 43 deletions.
48 changes: 8 additions & 40 deletions docs/docs/concepts/foundation/accounts/keys.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,11 @@ Typically, each account in Aztec is backed by two separate keys:

Signing keys allow their holder to act as their corresponding account in Aztec, similarly to the keys used for an Ethereum account. If a signing key is leaked, the user can potentially lose all their funds.

Since Aztec implements full [signature abstraction](./main.md), signing keys depend on the account contract implementation for each user. Usually, an account contract will validate a signature of the incoming payload against a known public key.

```noir
fn entrypoint(
inputs: pub PrivateContextInputs,
payload: pub EntrypointPayload,
signature: pub [u8;64],
) -> distinct pub abi::PrivateCircuitPublicInputs {
// Initialize context and load public key
// ...
// Verify payload signature
let payload_bytes: [u8; entrypoint::ENTRYPOINT_PAYLOAD_SIZE_IN_BYTES] = payload.to_be_bytes();
let payload_hash: [u8; 32] = std::hash::sha256(payload_bytes);
// Verify signature of the payload hash
let verification = std::schnorr::verify_signature(public_key.x, public_key.y, signature, payload_hash);
assert(verification == true);
// Execute calls and return
// ...
}
```
Since Aztec implements full [signature abstraction](./main.md), signing keys depend on the account contract implementation for each user. Usually, an account contract will validate a signature of the incoming payload against a known public key.

This is a snippet of our Schnorr Account contract implementation, which uses Schnorr signatures for authentication:

#include_code entrypoint /yarn-project/noir-contracts/src/contracts/schnorr_account_contract/src/main.nr rust

Still, different accounts may use different signing schemes, may require multi-factor authentication, or _may not even use signing keys_ and instead rely on other authentication mechanisms. Read [how to write an account contract](../../../dev_docs/wallets/writing_an_account_contract.md) for a full example of how to manage authentication.

Expand Down Expand Up @@ -89,16 +71,7 @@ In a future version, encryption keys will be differentiated between incoming and

An application in Noir can access the encryption public key for a given address using the oracle call `get_public_key`, which you can then use for calls such as `emit_encrypted_log`:

```noir
let encryption_public_key = get_public_key(recipient);
context = emit_encrypted_log(
context,
application_contract_address,
storage_slot,
encryption_public_key,
note.serialise(),
);
```
#include_code encrypted /yarn-project/noir-libs/value-note/src/utils.nr rust

:::info
In order to be able to provide the public encryption key for a given address, that public key needs to have been registered in advance. At the moment, there is no broadcasting mechanism for public keys, which means that you will need to manually register all addresses you intend to send encrypted notes to. You can do this via the `registerRecipient` method of the Aztec RPC server, callable either via aztec.js or the CLI. Note that any accounts you own that have been added to the RPC server are automatically registered.
Expand All @@ -110,13 +83,8 @@ In addition to deriving encryption keys, the privacy master key is used for deri

An application in Noir can request a nullifier from the current user for computing the nullifier of a note via the `get_secret_key` oracle call:

```noir
fn compute_nullifier(self) -> Field {
let siloed_note_hash = compute_siloed_note_hash(ValueNoteMethods, self);
let secret = get_secret_key(self.owner);
dep::std::hash::pedersen([siloed_note_hash, secret])[0]
}
```
#include_code nullifier /yarn-project/noir-libs/value-note/src/value_note.nr rust

### Scoped keys

:::warning
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ contract SchnorrAccount {
use crate::public_key_note::PublicKeyNoteMethods;
use crate::public_key_note::PUBLIC_KEY_NOTE_LEN;

// docs:start:entrypoint

fn entrypoint(
inputs: pub PrivateContextInputs,
payload: pub EntrypointPayload, // contains a set of arguments, selectors, targets and a nonce
Expand All @@ -53,6 +55,8 @@ contract SchnorrAccount {
let verification = std::schnorr::verify_signature(public_key.x, public_key.y, signature, message_bytes);
assert(verification == true);

// docs:end:entrypoint

// Execute calls
payload.execute_calls(&mut context);

Expand Down
14 changes: 11 additions & 3 deletions yarn-project/noir-libs/value-note/src/utils.nr
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,22 @@ fn send_note(
balance.insert(context, &mut note);

// Emit the newly created encrypted note preimages via oracle calls.
// docs:start:encrypted

let application_contract_address = (*context).this_address();
let note_storage_slot = balance.storage_slot;
let encryption_pub_key = get_public_key(recipient);
let encrypted_data = note.serialise();

emit_encrypted_log(
context,
(*context).this_address(),
balance.storage_slot,
application_contract_address,
note_storage_slot,
encryption_pub_key,
note.serialise(),
encrypted_data,
);

// docs:end:encrypted
}

/*
Expand Down
4 changes: 4 additions & 0 deletions yarn-project/noir-libs/value-note/src/value_note.nr
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ impl ValueNote {
])[0]
}

// docs:start:nullifier

fn compute_nullifier(self) -> Field {
let note_hash_for_nullify = compute_note_hash_for_read_or_nullify(ValueNoteMethods, self);
let owner_nullifying_public_key = get_public_key(self.owner);
Expand All @@ -63,6 +65,8 @@ impl ValueNote {
])[0]
}

// docs:end:nullifier

fn set_header(&mut self, header: NoteHeader) {
self.header = header;
}
Expand Down

0 comments on commit 911a102

Please sign in to comment.