Skip to content

Commit

Permalink
chore: noir bug workaround (#9443)
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan authored Oct 27, 2024
1 parent 2ebe361 commit f619687
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,16 @@ contract TokenBlacklist {
// Add the token note to user's balances set
let msg_sender_keys = get_public_keys(context.msg_sender());
let to_keys = get_public_keys(to);
storage.balances.add(to, U128::from_integer(amount)).emit(encode_and_encrypt_note(
&mut context,
msg_sender_keys.ovpk_m,
to_keys.ivpk_m,
to,
));
// 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.add(to, U128::from_integer(amount)).emit(
encode_and_encrypt_note_unconstrained(
&mut context,
msg_sender_keys.ovpk_m,
to_keys.ivpk_m,
to,
),
);
}

#[private]
Expand All @@ -213,12 +217,16 @@ contract TokenBlacklist {
}

let from_keys = get_public_keys(from);
storage.balances.sub(from, U128::from_integer(amount)).emit(encode_and_encrypt_note(
&mut context,
from_keys.ovpk_m,
from_keys.ivpk_m,
from,
));
// 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_keys.ovpk_m,
from_keys.ivpk_m,
from,
),
);

TokenBlacklist::at(context.this_address())._increase_public_balance(to, amount).enqueue(
&mut context,
Expand Down Expand Up @@ -269,12 +277,16 @@ contract TokenBlacklist {
}

let from_keys = get_public_keys(from);
storage.balances.sub(from, U128::from_integer(amount)).emit(encode_and_encrypt_note(
&mut context,
from_keys.ovpk_m,
from_keys.ivpk_m,
from,
));
// 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_keys.ovpk_m,
from_keys.ivpk_m,
from,
),
);

TokenBlacklist::at(context.this_address())._reduce_total_supply(amount).enqueue(&mut context);
}
Expand Down
41 changes: 28 additions & 13 deletions noir-projects/noir-contracts/contracts/token_contract/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -463,20 +463,28 @@ contract Token {
let amount = U128::from_integer(amount);
// docs:start:increase_private_balance
// docs:start:encrypted
storage.balances.at(from).sub(from_keys.npk_m, amount).emit(encode_and_encrypt_note(
&mut context,
from_keys.ovpk_m,
from_keys.ivpk_m,
from,
));
// 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_keys.npk_m, amount).emit(
encode_and_encrypt_note_unconstrained(
&mut context,
from_keys.ovpk_m,
from_keys.ivpk_m,
from,
),
);
// docs:end:encrypted
// docs:end:increase_private_balance
storage.balances.at(to).add(to_keys.npk_m, amount).emit(encode_and_encrypt_note(
&mut context,
from_keys.ovpk_m,
to_keys.ivpk_m,
to,
));
// 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_keys.npk_m, amount).emit(
encode_and_encrypt_note_unconstrained(
&mut context,
from_keys.ovpk_m,
to_keys.ivpk_m,
to,
),
);
}
// docs:end:transfer_from
// docs:start:burn
Expand All @@ -488,8 +496,15 @@ contract Token {
assert(nonce == 0, "invalid nonce");
}
let from_keys = get_public_keys(from);
// 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_keys.npk_m, U128::from_integer(amount)).emit(
encode_and_encrypt_note(&mut context, from_keys.ovpk_m, from_keys.ivpk_m, from),
encode_and_encrypt_note_unconstrained(
&mut context,
from_keys.ovpk_m,
from_keys.ivpk_m,
from,
),
);
Token::at(context.this_address())._reduce_total_supply(amount).enqueue(&mut context);
}
Expand Down

0 comments on commit f619687

Please sign in to comment.