Skip to content

Commit

Permalink
fix: naming inconsistencies
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Jul 5, 2024
1 parent 41d8580 commit 6e84801
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ contract PrivateToken {
funded_amount: Field,
randomness: Field // A randomness to mix in with the generated notes.
) {
// 1. This function is called by fee paying contract (FPC) when setting up a refund so we need to support
// the authwit flow here and check that the user really permitted FPC to set up a refund on their behalf.
// 1. This function is called by fee paying contract (fee_payer) when setting up a refund so we need to support
// the authwit flow here and check that the user really permitted fee_payer to set up a refund on their behalf.
assert_current_call_valid_authwit(&mut context, sponsored_user);

// 2. Get all the relevant user keys
Expand All @@ -179,7 +179,7 @@ contract PrivateToken {
storage.balances.sub(sponsored_user_npk_m_hash, U128::from_integer(funded_amount)).emit(encode_and_encrypt_note_with_keys(&mut context, sponsored_user_ovpk, sponsored_user_ivpk));

// 4. We generate the refund points.
let (fpc_point, user_point) = TokenNote::generate_refund_points(
let (fee_payer_point, user_point) = TokenNote::generate_refund_points(
fee_payer_npk_m_hash,
sponsored_user_npk_m_hash,
funded_amount,
Expand All @@ -191,30 +191,33 @@ contract PrivateToken {
context.set_public_teardown_function(
context.this_address(),
FunctionSelector::from_signature("complete_refund(Field,Field,Field,Field)"),
[fpc_point.x, fpc_point.y, user_point.x, user_point.y]
[fee_payer_point.x, fee_payer_point.y, user_point.x, user_point.y]
);
}

#[aztec(public)]
#[aztec(internal)]
fn complete_refund(
fpc_point_x: Field,
fpc_point_y: Field,
fee_payer_point_x: Field,
fee_payer_point_y: Field,
user_point_x: Field,
user_point_y: Field
) {
// 1. We get the final note content hashes by calling the `complete_refund` on the note.
let fpc_point = EmbeddedCurvePoint { x: fpc_point_x, y: fpc_point_y, is_infinite: false };
let fee_payer_point = EmbeddedCurvePoint { x: fee_payer_point_x, y: fee_payer_point_y, is_infinite: false };
let user_point = EmbeddedCurvePoint { x: user_point_x, y: user_point_y, is_infinite: false };
let tx_fee = context.transaction_fee();
let (fpc_note_content_hash, user_note_content_hash) = TokenNote::complete_refund(fpc_point, user_point, tx_fee);
let (fee_payer_note_content_hash, user_note_content_hash) = TokenNote::complete_refund(fee_payer_point, user_point, tx_fee);

// 2. Now we "manually" compute the inner note hashes.
let fpc_inner_note_hash = compute_inner_note_hash_from_preimage(PrivateToken::storage().balances.slot, fpc_note_content_hash);
let fee_payer_inner_note_hash = compute_inner_note_hash_from_preimage(
PrivateToken::storage().balances.slot,
fee_payer_note_content_hash
);
let user_inner_note_hash = compute_inner_note_hash_from_preimage(PrivateToken::storage().balances.slot, user_note_content_hash);

// 3. At last we emit the note hashes.
context.push_note_hash(fpc_inner_note_hash);
context.push_note_hash(fee_payer_inner_note_hash);
context.push_note_hash(user_inner_note_hash);
// --> Once the tx is settled user and fee recipient can add the notes to their pixies.
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ trait PrivatelyRefundable {
) -> (EmbeddedCurvePoint, EmbeddedCurvePoint);

fn complete_refund(
fee_payer_point: EmbeddedCurvePoint,
sponsored_user_point: EmbeddedCurvePoint,
Incomplete_fee_payer_point: EmbeddedCurvePoint,
incomplete_sponsored_user_point: EmbeddedCurvePoint,
transaction_fee: Field
) -> (Field, Field);
}
Expand Down Expand Up @@ -123,7 +123,7 @@ impl PrivatelyRefundable for TokenNote {
let (fee_payer_npk_m_hash_lo, fee_payer_npk_m_hash_hi) = decompose(fee_payer_npk_m_hash);

// 2. Now that we have correct representationsn of fee payer and randomness we can compute `G ^ (fee_payer_npk + randomness)`
let fee_payer_point = multi_scalar_mul(
let Incomplete_fee_payer_point = multi_scalar_mul(
[G1, G1],
[EmbeddedCurveScalar {
lo: fee_payer_npk_m_hash_lo,
Expand All @@ -141,7 +141,7 @@ impl PrivatelyRefundable for TokenNote {
let (funded_amount_lo, funded_amount_hi) = decompose(funded_amount);

// 4. We compute `G ^ (sponsored_user_npk_m_hash + funded_amount + randomness)`
let sponsored_user_point = multi_scalar_mul(
let incomplete_sponsored_user_point = multi_scalar_mul(
[G1, G1, G1],
[EmbeddedCurveScalar {
lo: sponsored_user_lo,
Expand All @@ -159,17 +159,17 @@ impl PrivatelyRefundable for TokenNote {

// 5. At last we represent the points as EmbeddedCurvePoints and return them.
(EmbeddedCurvePoint {
x: fee_payer_point[0],
y: fee_payer_point[1],
is_infinite: fee_payer_point[2] == 1
x: Incomplete_fee_payer_point[0],
y: Incomplete_fee_payer_point[1],
is_infinite: Incomplete_fee_payer_point[2] == 1
}, EmbeddedCurvePoint {
x: sponsored_user_point[0],
y: sponsored_user_point[1],
is_infinite: sponsored_user_point[2] == 1
x: incomplete_sponsored_user_point[0],
y: incomplete_sponsored_user_point[1],
is_infinite: incomplete_sponsored_user_point[2] == 1
})
}

fn complete_refund(fee_payer_point: EmbeddedCurvePoint, sponsored_user_point: EmbeddedCurvePoint, transaction_fee: Field) -> (Field, Field) {
fn complete_refund(Incomplete_fee_payer_point: EmbeddedCurvePoint, incomplete_sponsored_user_point: EmbeddedCurvePoint, transaction_fee: Field) -> (Field, Field) {
// 1. We convert the transaction fee to high and low limbs to be able to use BB API.
let (transaction_fee_lo, transaction_fee_hi) = decompose(transaction_fee);

Expand Down Expand Up @@ -224,8 +224,8 @@ impl PrivatelyRefundable for TokenNote {
deduce what n is. This is the discrete log problem.
However we can still perform addition/subtraction on points! That is why we generate those two points, which are:
fee_payer_point := (fee_payer_npk + randomness) * G
sponsored_user_point := (sponsored_user_npk + funded_amount + randomness) * G
Incomplete_fee_payer_point := (fee_payer_npk + randomness) * G
incomplete_sponsored_user_point := (sponsored_user_npk + funded_amount + randomness) * G
where `funded_amount` is the total amount in tokens that the sponsored user initially supplied, from which the transaction fee will be subtracted.
Expand All @@ -235,11 +235,11 @@ impl PrivatelyRefundable for TokenNote {
Then we arrive at the final points via addition/subtraction of that transaction fee point:
completed_fpc_point := fee_payer_point + fee_point
fee_payer_point := Incomplete_fee_payer_point + fee_point
= (fee_payer_npk + randomness) * G + transaction_fee * G
= (fee_payer_npk + randomness + transaction_fee) * G
completed_user_point := sponsored_user_point - fee_point
sponsored_user_point := incomplete_sponsored_user_point - fee_point
= (sponsored_user_npk + funded_amount + randomness) * G - transaction_fee * G
= (sponsored_user_npk + randomness + (funded_amount - transaction_fee)) * G
Expand All @@ -251,11 +251,14 @@ impl PrivatelyRefundable for TokenNote {
}
*/

let completed_fpc_point = fee_payer_point + fee_point;
// 3. Now we leverage homomorphism to privately add the fee to fee payer point and subtract it from
// the sponsored user point in public.
let fee_payer_point = Incomplete_fee_payer_point + fee_point;
let sponsored_user_point = incomplete_sponsored_user_point - fee_point;

let completed_user_point = sponsored_user_point - fee_point;
assert_eq(completed_user_point.is_infinite, false);
assert_eq(sponsored_user_point.is_infinite, false);

(completed_fpc_point.x, completed_user_point.x)
// Finally we return the x-coordinates of the points which are the note content hashes.
(fee_payer_point.x, sponsored_user_point.x)
}
}

0 comments on commit 6e84801

Please sign in to comment.