Skip to content

Commit

Permalink
chore: removing noir bug workaround (#10535)
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan authored Jan 13, 2025
1 parent e494f6b commit 8be882f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ where
compute_payload(context, note, recipient, sender)
}

// This function seems to be affected by the following Noir bug:
// https://github.com/noir-lang/noir/issues/5771
// If you get weird behavior it might be because of it.
pub fn encode_and_encrypt_note<Note, let N: u32>(
context: &mut PrivateContext,
recipient: AztecAddress,
Expand Down
3 changes: 3 additions & 0 deletions noir-projects/aztec-nr/aztec/src/encrypted_logs/payload.nr
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ fn compute_encrypted_log<let P: u32, let M: u32>(
// Then we fill in the rest as the incoming body ciphertext
let size = M - offset;
assert_eq(size, incoming_body_ciphertext.len(), "ciphertext length mismatch");
// Nargo seems to struggle with realizing that `offset` is a constant at this point.
// We then redefine it in terms of the assertion above to give nargo a hint.
let offset = M - incoming_body_ciphertext.len();
for i in 0..size {
encrypted_bytes[offset + i] = incoming_body_ciphertext[i];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ use dep::aztec::macros::aztec;
contract TokenBlacklist {
// Libs
use dep::aztec::{
encrypted_logs::encrypted_note_emission::encode_and_encrypt_note_unconstrained,
encrypted_logs::encrypted_note_emission::{
encode_and_encrypt_note, encode_and_encrypt_note_unconstrained,
},
hash::compute_secret_hash,
macros::{functions::{initializer, internal, private, public, view}, storage::storage},
prelude::{AztecAddress, Map, NoteGetterOptions, PrivateSet, PublicMutable, SharedMutable},
Expand Down Expand Up @@ -186,11 +188,11 @@ contract TokenBlacklist {
assert(notes.len() == 1, "note not popped");

// Add the token note to user's balances set
// TODO: constrain encryption below - we are using unconstrained here only because of the following Noir issue
// https://github.com/noir-lang/noir/issues/5771
storage.balances.add(to, U128::from_integer(amount)).emit(
encode_and_encrypt_note_unconstrained(&mut context, to, context.msg_sender()),
);
storage.balances.add(to, U128::from_integer(amount)).emit(encode_and_encrypt_note(
&mut context,
to,
context.msg_sender(),
));
}

#[private]
Expand All @@ -206,11 +208,11 @@ contract TokenBlacklist {
assert(nonce == 0, "invalid nonce");
}

// TODO: constrain encryption below - we are using unconstrained here only becuase of the following Noir issue
// https://github.com/noir-lang/noir/issues/5771
storage.balances.sub(from, U128::from_integer(amount)).emit(
encode_and_encrypt_note_unconstrained(&mut context, from, from),
);
storage.balances.sub(from, U128::from_integer(amount)).emit(encode_and_encrypt_note(
&mut context,
from,
from,
));

TokenBlacklist::at(context.this_address())._increase_public_balance(to, amount).enqueue(
&mut context,
Expand Down Expand Up @@ -255,11 +257,11 @@ contract TokenBlacklist {
assert(nonce == 0, "invalid nonce");
}

// TODO: constrain encryption below - we are using unconstrained here only because of the following Noir issue
// https://github.com/noir-lang/noir/issues/5771
storage.balances.sub(from, U128::from_integer(amount)).emit(
encode_and_encrypt_note_unconstrained(&mut context, from, from),
);
storage.balances.sub(from, U128::from_integer(amount)).emit(encode_and_encrypt_note(
&mut context,
from,
from,
));

TokenBlacklist::at(context.this_address())._reduce_total_supply(amount).enqueue(&mut context);
}
Expand Down
24 changes: 7 additions & 17 deletions noir-projects/noir-contracts/contracts/token_contract/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ contract Token {
context::{PrivateCallInterface, PrivateContext},
encrypted_logs::{
encrypted_event_emission::encode_and_encrypt_event_unconstrained,
encrypted_note_emission::encode_and_encrypt_note_unconstrained,
encrypted_note_emission::{
encode_and_encrypt_note, encode_and_encrypt_note_unconstrained,
},
},
macros::{
events::event,
Expand Down Expand Up @@ -250,10 +252,8 @@ contract Token {
assert(nonce == 0, "invalid nonce");
}

// TODO: constrain encryption below - we are using unconstrained here only because of the following Noir issue
// https://github.com/noir-lang/noir/issues/5771
storage.balances.at(from).sub(from, U128::from_integer(amount)).emit(
encode_and_encrypt_note_unconstrained(&mut context, from, from),
encode_and_encrypt_note(&mut context, from, from),
);
Token::at(context.this_address())._increase_public_balance(to, amount).enqueue(&mut context);
}
Expand Down Expand Up @@ -376,22 +376,14 @@ contract Token {
let amount = U128::from_integer(amount);
// docs:start:increase_private_balance
// docs:start:encrypted
// TODO: constrain encryption below - we are using unconstrained here only becuase of the following Noir issue
// https://github.com/noir-lang/noir/issues/5771
storage.balances.at(from).sub(from, amount).emit(encode_and_encrypt_note_unconstrained(
storage.balances.at(from).sub(from, amount).emit(encode_and_encrypt_note(
&mut context,
from,
from,
));
// docs:end:encrypted
// docs:end:increase_private_balance
// TODO: constrain encryption below - we are using unconstrained here only becuase of the following Noir issue
// https://github.com/noir-lang/noir/issues/5771
storage.balances.at(to).add(to, amount).emit(encode_and_encrypt_note_unconstrained(
&mut context,
to,
from,
));
storage.balances.at(to).add(to, amount).emit(encode_and_encrypt_note(&mut context, to, from));
}
// docs:end:transfer_in_private

Expand All @@ -403,10 +395,8 @@ contract Token {
} else {
assert(nonce == 0, "invalid nonce");
}
// TODO: constrain encryption below - we are using unconstrained here only becuase of the following Noir issue
// https://github.com/noir-lang/noir/issues/5771
storage.balances.at(from).sub(from, U128::from_integer(amount)).emit(
encode_and_encrypt_note_unconstrained(&mut context, from, from),
encode_and_encrypt_note(&mut context, from, from),
);
Token::at(context.this_address())._reduce_total_supply(amount).enqueue(&mut context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,7 @@ describe('e2e_token_contract transfer private', () => {
.withWallet(wallets[1])
.methods.transfer_in_private(badAccount.address, accounts[1].address, 0, nonce)
.send();
await expect(txCancelledAuthwit.wait()).rejects.toThrow(
"Assertion failed: Message not authorized by account 'result == IS_VALID_SELECTOR'",
);
await expect(txCancelledAuthwit.wait()).rejects.toThrow('Assertion failed: Message not authorized by account');
});
});
});

0 comments on commit 8be882f

Please sign in to comment.