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

core: Add sender public key to note costructors #218

Merged
merged 2 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions circuits/src/sender_enc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub(crate) fn gadget(
sender_pk: PublicKey,
signatures: (SchnorrSignature, SchnorrSignature),
output_npk: [JubJubAffine; OUTPUT_NOTES],
sender_blinder: [(JubJubScalar, JubJubScalar); OUTPUT_NOTES],
sender_blinder: [[JubJubScalar; 2]; OUTPUT_NOTES],
// [enc_A, enc_B] for note 0
sender_enc_out0: [(JubJubAffine, JubJubAffine); 2],
// [enc_A, enc_B] for note 1
Expand Down Expand Up @@ -55,11 +55,11 @@ pub(crate) fn gadget(
let note_pk_0 = composer.append_public_point(output_npk[0]);
let note_pk_1 = composer.append_public_point(output_npk[1]);

let blinder_A_0 = composer.append_witness(sender_blinder[0].0);
let blinder_B_0 = composer.append_witness(sender_blinder[0].1);
let blinder_A_0 = composer.append_witness(sender_blinder[0][0]);
let blinder_B_0 = composer.append_witness(sender_blinder[0][1]);

let blinder_A_1 = composer.append_witness(sender_blinder[1].0);
let blinder_B_1 = composer.append_witness(sender_blinder[1].1);
let blinder_A_1 = composer.append_witness(sender_blinder[1][0]);
let blinder_B_1 = composer.append_witness(sender_blinder[1][1]);

// assert that the sender encryption of the first note is correct
// appends the values of sender_enc_out0 as public input
Expand Down
6 changes: 3 additions & 3 deletions circuits/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ pub struct TxCircuit<const H: usize, const I: usize> {
max_fee: u64,
sender_pk: PublicKey,
signatures: (SchnorrSignature, SchnorrSignature),
sender_blinder: [(JubJubScalar, JubJubScalar); OUTPUT_NOTES],
sender_blinder: [[JubJubScalar; 2]; OUTPUT_NOTES],
}

impl<const H: usize, const I: usize> Default for TxCircuit<H, I> {
Expand Down Expand Up @@ -367,7 +367,7 @@ impl<const H: usize, const I: usize> Default for TxCircuit<H, I> {
let signatures =
(SchnorrSignature::default(), SchnorrSignature::default());
let sender_blinder =
[(JubJubScalar::default(), JubJubScalar::default()); OUTPUT_NOTES];
[[JubJubScalar::default(), JubJubScalar::default()]; OUTPUT_NOTES];

Self {
tx_input_notes: tx_input_notes.try_into().unwrap(),
Expand All @@ -394,7 +394,7 @@ impl<const H: usize, const I: usize> TxCircuit<H, I> {
max_fee: u64,
sender_pk: PublicKey,
signatures: (SchnorrSignature, SchnorrSignature),
sender_blinder: [(JubJubScalar, JubJubScalar); OUTPUT_NOTES],
sender_blinder: [[JubJubScalar; 2]; OUTPUT_NOTES],
) -> Self {
Self {
tx_input_notes,
Expand Down
46 changes: 27 additions & 19 deletions circuits/tests/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ struct TestingParameters {
sender_pk: PublicKey,
output_npk: [JubJubAffine; OUTPUT_NOTES],
signatures: (SchnorrSignature, SchnorrSignature),
sender_blinder: [(JubJubScalar, JubJubScalar); OUTPUT_NOTES],
sender_blinder: [[JubJubScalar; 2]; OUTPUT_NOTES],
}

lazy_static! {
Expand All @@ -49,6 +49,8 @@ lazy_static! {

let pp = PublicParameters::setup(1 << CAPACITY, &mut rng).unwrap();
let sender_sk = SecretKey::random(&mut rng);
let sender_pk = PublicKey::from(&sender_sk);
let receiver_pk = PublicKey::from(&SecretKey::random(&mut rng));

let mut tree = Tree::<(), HEIGHT>::new();
let payload_hash = BlsScalar::from(1234u64);
Expand All @@ -67,8 +69,6 @@ lazy_static! {
let deposit = 5;
let max_fee = 5;

let sender_pk = PublicKey::from(&sender_sk);
let receiver_pk = PublicKey::from(&SecretKey::random(&mut rng));

// generate both ouput note public keys
let receiver_npk = *receiver_pk.gen_stealth_address(
Expand All @@ -88,14 +88,15 @@ lazy_static! {
let schnorr_sk_b = SchnorrSecretKey::from(sender_sk.b());
let sig_b = schnorr_sk_b.sign(&mut rng, payload_hash);

let sender_blinder_0 = (
// sender blinder for the output notes
let sender_blinder_0 = [
JubJubScalar::random(&mut rng),
JubJubScalar::random(&mut rng),
);
let sender_blinder_1 = (
];
let sender_blinder_1 = [
JubJubScalar::random(&mut rng),
JubJubScalar::random(&mut rng),
);
];

TestingParameters {
pp,
Expand All @@ -115,15 +116,18 @@ lazy_static! {
fn create_and_insert_test_note(
rng: &mut (impl RngCore + CryptoRng),
tree: &mut Tree<(), HEIGHT>,
pk: &PublicKey,
sender_pk: &PublicKey,
pos: u64,
value: u64,
) -> Note {
let sender_blinder = [
JubJubScalar::random(&mut *rng),
JubJubScalar::random(&mut *rng),
];
let mut note = Note::transparent(rng, pk, value, sender_blinder);

// create a note that belongs to the sender
let mut note =
Note::transparent(rng, sender_pk, sender_pk, value, sender_blinder);
note.set_pos(pos);

let item = Item {
Expand All @@ -138,17 +142,17 @@ fn create_and_insert_test_note(
fn create_test_tx_input_notes<const I: usize>(
rng: &mut (impl RngCore + CryptoRng),
tree: &mut Tree<(), HEIGHT>,
sk: &SecretKey,
sender_sk: &SecretKey,
payload_hash: BlsScalar,
) -> [TxInputNote<HEIGHT>; I] {
let pk = PublicKey::from(sk);
let sender_pk = PublicKey::from(sender_sk);

let mut notes = Vec::new();
for i in 0..I {
notes.push(create_and_insert_test_note(
rng,
tree,
&pk,
&sender_pk,
i.try_into().unwrap(),
25,
));
Expand All @@ -157,9 +161,14 @@ fn create_test_tx_input_notes<const I: usize>(
let mut input_notes = Vec::new();
for i in 0..I {
let merkle_opening = tree.opening(*notes[i].pos()).expect("Tree read.");
let input_note =
TxInputNote::new(rng, &notes[i], merkle_opening, &sk, payload_hash)
.expect("Note created properly.");
let input_note = TxInputNote::new(
rng,
&notes[i],
merkle_opening,
sender_sk,
payload_hash,
)
.expect("Note created properly.");

input_notes.push(input_note);
}
Expand All @@ -171,17 +180,16 @@ fn create_tx_output_note(
rng: &mut (impl RngCore + CryptoRng),
value: u64,
note_pk: JubJubAffine,
// (blinder_A, blinder_B)
sender_blinder: (JubJubScalar, JubJubScalar),
sender_blinder: [JubJubScalar; 2],
) -> TxOutputNote {
let value_blinder = JubJubScalar::random(&mut *rng);
let value_commitment = value_commitment(value, value_blinder);

let sender_blinder_a = sender_blinder.0;
let sender_blinder_a = sender_blinder[0];
let sender_enc_a =
elgamal::encrypt(&note_pk.into(), TP.sender_pk.A(), &sender_blinder_a);

let sender_blinder_b = sender_blinder.1;
let sender_blinder_b = sender_blinder[1];
let sender_enc_b =
elgamal::encrypt(&note_pk.into(), TP.sender_pk.B(), &sender_blinder_b);

Expand Down
4 changes: 4 additions & 0 deletions core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Add `encrypt_sender` function to encrypt the sender with the npk [#214]
- Add `decrypt_sender` method to the `Note` [#214]
- Add `elgamal::encrypt` and `elgamal::decrypt`
- Add `stealth_address` function directly to note [#208]
- Add function `value_commitment` [#201]
Expand All @@ -20,6 +22,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Rename `tx_max_fee` to `max_fee` [#214]
- Add `sender_enc` field to the `Note` [#214]
- Add `sender_blinder` parameter for `Note` contructors [#214]
- Add `sender_pk` parameter for `Note` contructors [#214]
- Add `sender_enc` parameter for `Note::transparent_stealth` [#214]
- Rename `encryption_blinder` to `value_blinder` [#214]
- Rename `NOTE_ENCRYPTION_SIZE` to `NOTE_VALUE_ENC_SIZE` [#214]
- Move `OUTPUT_NOTES` to crate root
Expand Down
6 changes: 4 additions & 2 deletions core/src/encryption/elgamal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ pub fn encrypt(
/// Returns a JubJubExtended plaintext.
pub fn decrypt(
secret_key: &JubJubScalar,
ciphertext_1: &JubJubExtended,
ciphertext_2: &JubJubExtended,
ciphertext: &(JubJubExtended, JubJubExtended),
xevisalle marked this conversation as resolved.
Show resolved Hide resolved
) -> JubJubExtended {
let ciphertext_1 = ciphertext.0;
let ciphertext_2 = ciphertext.1;

// return the plaintext
ciphertext_2 - ciphertext_1 * secret_key
}
4 changes: 3 additions & 1 deletion core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ pub use keys::hash;
pub use keys::public::PublicKey;
pub use keys::secret::SecretKey;
pub use keys::view::ViewKey;
pub use note::{Note, NoteType, VALUE_ENC_SIZE as NOTE_VAL_ENC_SIZE};
pub use note::{
encrypt_sender, Note, NoteType, VALUE_ENC_SIZE as NOTE_VAL_ENC_SIZE,
};
pub use stealth_address::StealthAddress;

#[cfg(feature = "alloc")]
Expand Down
Loading