Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(frontend): aztec syntactic sugar (feature flagged) #2403

Merged
merged 23 commits into from
Aug 29, 2023

Conversation

Maddiaa0
Copy link
Member

@Maddiaa0 Maddiaa0 commented Aug 22, 2023

Description

Linked work

Performs some code generation on aztec annotated functions.
The goal of this PR is to abstract the app developer away from boilerplate code that is required to make noir compatible with the aztec execution environment.

For example what originally was:

    fn mint(
        inputs: PrivateContextInputs,
        amount: Field, 
        owner: Field
    ) -> distinct pub abi::PrivateCircuitPublicInputs {
        let storage = Storage::init();
        let mut context = PrivateContext::new(inputs, abi::hash_args([amount, owner]));

        // Insert new note to a set of user notes and emit the newly created encrypted note preimage via oracle call.
        let owner_balance = storage.balances.at(owner);
        send_note(&mut context, owner_balance, amount, owner);
        emit_unencrypted_log(&mut context, "Coins minted");

        // Return private circuit public inputs. All private functions need to return this as it is part of the input of the private kernel..
        context.finish()
    }

can instead be written as:

   #[aztec(private)]
    fn mint(
        amount: Field, 
        owner: Field
    )  {
        let storage = Storage::init();

        // Insert new note to a set of user notes and emit the newly created encrypted note preimage via oracle call.
        let owner_balance = storage.balances.at(owner);
        send_note(&mut context, owner_balance, amount, owner);
        emit_unencrypted_log(&mut context, "Coins minted");
    }

This will reduce the barrier for entry to new devs, making the syntax alot more approachable for them.

What does it abstract away
There is an execution context created in each function that allows the developer to:

  • create commitments
  • consume l1 messages
  • interact with oracles required by the aztec-noir library
    among many other things.

This context is created by providing set inputs to each circuit (depending on the execution environment) and by hashing the other circuit inputs.

At the end of each call the inputs for the kernel iteration must be set as public inputs (returned). This is done with a call to context.finish().

Each macro:

  • adds the input to the function
  • hashes the args
  • creates a context object
  • inserts a context.finish() call

As the "macro" inserts the "context" into the ast, the context object is available to them as it it was something like solidity's block or msg structures.

subnote:
The storage pattern has not been added to this just yet as we have not concretely standardised this pattern. This shall be follow up work

Documentation

  • This PR requires documentation updates when merged.

    • I will submit a noir-lang/docs PR.
    • I will request for and support Dev Rel's help in documenting this PR.

Additional Context

PR Checklist*

  • I have tested the changes locally.
  • I have formatted the changes with Prettier and/or cargo fmt on default settings.

@jfecher
Copy link
Contributor

jfecher commented Aug 22, 2023

can instead be written as:

   #[aztec(private)]
    fn mint(
        amount: Field, 
        owner: Field
    )  {
        let storage = Storage::init();

        // Insert new note to a set of user notes and emit the newly created encrypted note preimage via oracle call.
        let owner_balance = storage.balances.at(owner);
        send_note(&mut context, owner_balance, amount, owner);
        emit_unencrypted_log(&mut context, "Coins minted");
    }

Can you make it so the let storage = Storage::init(); line is inserted implicitly as well?

crates/noirc_driver/src/lib.rs Outdated Show resolved Hide resolved
crates/noirc_frontend/src/hir/def_map/mod.rs Outdated Show resolved Hide resolved
crates/noirc_frontend/src/hir/def_map/aztec_helper.rs Outdated Show resolved Hide resolved
crates/noirc_frontend/src/hir/def_map/aztec_helper.rs Outdated Show resolved Hide resolved
Base automatically changed from md/flex-attributes to master August 22, 2023 17:21
@Maddiaa0 Maddiaa0 requested a review from kevaundray August 29, 2023 12:46
@Maddiaa0
Copy link
Member Author

Can you make it so the let storage = Storage::init(); line is inserted implicitly as well?

We havent finalized this standard in the Aztec team. Once it is final i will make a followup pr

@Maddiaa0 Maddiaa0 marked this pull request as ready for review August 29, 2023 12:49
@kevaundray kevaundray requested a review from jfecher August 29, 2023 13:27
Copy link
Contributor

@jfecher jfecher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm considering this temporary code so I only skimmed it. We can add to it/fix it later if needed

@jfecher jfecher enabled auto-merge August 29, 2023 14:00
@jfecher jfecher added this pull request to the merge queue Aug 29, 2023
Merged via the queue into master with commit a894a6e Aug 29, 2023
@jfecher jfecher deleted the md/aztec-builtin branch August 29, 2023 14:36
Maddiaa0 added a commit to AztecProtocol/aztec-packages that referenced this pull request Aug 29, 2023
…1735)

## Overview

Now that noir-lang/noir#2403 has been merged
into noir and released under the `aztec` tag. This PR now builds!

This cleans up the syntax for noir programs, making them less verbose
and easier to get started with.
For example what originally was:
```rust
    fn mint(
        inputs: PrivateContextInputs,
        amount: Field, 
        owner: Field
    ) -> distinct pub abi::PrivateCircuitPublicInputs {
        let storage = Storage::init();
        let mut context = PrivateContext::new(inputs, abi::hash_args([amount, owner]));

        // Insert new note to a set of user notes and emit the newly created encrypted note preimage via oracle call.
        let owner_balance = storage.balances.at(owner);
        send_note(&mut context, owner_balance, amount, owner);
        emit_unencrypted_log(&mut context, "Coins minted");

        // Return private circuit public inputs. All private functions need to return this as it is part of the input of the private kernel..
        context.finish()
    }
```
can instead be written as:
```rust
   #[aztec(private)]
    fn mint(
        amount: Field, 
        owner: Field
    )  {
        let storage = Storage::init();

        // Insert new note to a set of user notes and emit the newly created encrypted note preimage via oracle call.
        let owner_balance = storage.balances.at(owner);
        send_note(&mut context, owner_balance, amount, owner);
        emit_unencrypted_log(&mut context, "Coins minted");
    }
```
@ludamad
Copy link
Collaborator

ludamad commented Aug 29, 2023

based

TomAFrench added a commit that referenced this pull request Aug 30, 2023
* master: (42 commits)
  fix(ssa): Handle right shift with constants (#2481)
  chore(noir): Release 0.10.4 (#2354)
  fix: Divide by zero should fail to satisfy constraints for `Field` and ints (#2475)
  fix(ssa): Remove padding from ToRadix call with constant inputs (#2479)
  fix: Implement handling of array aliasing in the mem2reg optimization pass (#2463)
  chore: resolve `Instruction` inputs fully before checking against cache (#2472)
  chore: Move independent `run_test` function into nargo core (#2468)
  feat: Standard library functions can now be called with closure args  (#2471)
  feat(frontend): aztec syntactic sugar (feature flagged) (#2403)
  chore(ci): enforce compliance with `cargo fmt` (#2467)
  chore(ci): Allow releases to have additional feature flags (#2405)
  feat: Add `assert_eq` keyword (#2137)
  fix(ssa): Do not optimize for allocates in constant folding (#2466)
  feat(ssa): Reuse existing results for duplicated instructions with no side-effects (#2460)
  fix: Closure lvalue capture bugfix (#2457)
  feat: Syntax for environment types now works with generics (#2383)
  fix(parser): fixes for the parsing of 'where' clauses (#2430)
  fix: Run `wasm` nodejs tests with no fails (#2387)
  chore: Run `cargo fmt` (#2455)
  chore: Perform formatting changes to integration tests (#2448)
  ...
TomAFrench added a commit that referenced this pull request Aug 30, 2023
* master: (42 commits)
  fix(ssa): Handle right shift with constants (#2481)
  chore(noir): Release 0.10.4 (#2354)
  fix: Divide by zero should fail to satisfy constraints for `Field` and ints (#2475)
  fix(ssa): Remove padding from ToRadix call with constant inputs (#2479)
  fix: Implement handling of array aliasing in the mem2reg optimization pass (#2463)
  chore: resolve `Instruction` inputs fully before checking against cache (#2472)
  chore: Move independent `run_test` function into nargo core (#2468)
  feat: Standard library functions can now be called with closure args  (#2471)
  feat(frontend): aztec syntactic sugar (feature flagged) (#2403)
  chore(ci): enforce compliance with `cargo fmt` (#2467)
  chore(ci): Allow releases to have additional feature flags (#2405)
  feat: Add `assert_eq` keyword (#2137)
  fix(ssa): Do not optimize for allocates in constant folding (#2466)
  feat(ssa): Reuse existing results for duplicated instructions with no side-effects (#2460)
  fix: Closure lvalue capture bugfix (#2457)
  feat: Syntax for environment types now works with generics (#2383)
  fix(parser): fixes for the parsing of 'where' clauses (#2430)
  fix: Run `wasm` nodejs tests with no fails (#2387)
  chore: Run `cargo fmt` (#2455)
  chore: Perform formatting changes to integration tests (#2448)
  ...
superstar0402 added a commit to superstar0402/aztec-nr that referenced this pull request Aug 16, 2024
…(#1735)

## Overview

Now that noir-lang/noir#2403 has been merged
into noir and released under the `aztec` tag. This PR now builds!

This cleans up the syntax for noir programs, making them less verbose
and easier to get started with.
For example what originally was:
```rust
    fn mint(
        inputs: PrivateContextInputs,
        amount: Field, 
        owner: Field
    ) -> distinct pub abi::PrivateCircuitPublicInputs {
        let storage = Storage::init();
        let mut context = PrivateContext::new(inputs, abi::hash_args([amount, owner]));

        // Insert new note to a set of user notes and emit the newly created encrypted note preimage via oracle call.
        let owner_balance = storage.balances.at(owner);
        send_note(&mut context, owner_balance, amount, owner);
        emit_unencrypted_log(&mut context, "Coins minted");

        // Return private circuit public inputs. All private functions need to return this as it is part of the input of the private kernel..
        context.finish()
    }
```
can instead be written as:
```rust
   #[aztec(private)]
    fn mint(
        amount: Field, 
        owner: Field
    )  {
        let storage = Storage::init();

        // Insert new note to a set of user notes and emit the newly created encrypted note preimage via oracle call.
        let owner_balance = storage.balances.at(owner);
        send_note(&mut context, owner_balance, amount, owner);
        emit_unencrypted_log(&mut context, "Coins minted");
    }
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants