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

[token-2022] Confidential transfer parallel split proofs #5054

Merged

Conversation

samkim-crypto
Copy link
Contributor

@samkim-crypto samkim-crypto commented Aug 20, 2023

Summary of changes

  • 838e6be: I removed the split proofs from the Transfer instruction and added a separate TransferWithSplitProofs that support transfer with split proof contexts.
  • cda6078: Updated the main program logic for split proof contexts to support no-op if the account is not initialized and also auto close on full transfer execution. Since CPI into the proof program is not currently supported, I currently disabled auto close on transfer execution with an invalid instruction data error. The notable change in this PR is to the result type of verify_transfer_proof in verify_proof.rs. I made it so that the function returns an Option that is wrapped inside another Result. I considered adding a custom type for the return type, but I thought this could make things even more complicated.
  • 98b171e: I updated the token client to support the TransferWithSplitProofs instruction. This PR adds the following functions:
    • confidential_transfer_create_equality_and_ciphertext_validity_proofs_for_transfer: This function creates context state accounts for the equality and ciphertext validity proofs that are needed for a transfer
    • confidential_transfer_create_range_proof_for_transfer: This function creates context state account for the range proof that is needed for a transfer
    • confidential_transfer_transfer_with_split_proofs: This submits the TransferWithSplitProofs instruction assuming that all the context states have been previously created.
    • confidential_transfer_close_context_state: This function closes a context state account
    • confidential_transfer_transfer_with_split_proofs_in_parallel: This function submits two transactions in parallel. The first transaction consist of instructions needed to create the relevant equality and ciphertext validity proof with the transfer instruction attached. The second transaction consists of instructions to needed to create range proof also with the transfer instruction attached. Whichever instruction lands later will fully execute the transfer.
  • 8589746: Updated tests for the TransferWithSplitProofs instruction.

@samkim-crypto samkim-crypto added the WIP Work in progress label Aug 20, 2023
@samkim-crypto samkim-crypto removed the WIP Work in progress label Aug 25, 2023
joncinque
joncinque previously approved these changes Aug 25, 2023
Copy link
Contributor

@joncinque joncinque left a comment

Choose a reason for hiding this comment

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

Looks really good! Only some minor comments, R+ after that

Comment on lines +1282 to +1284
source_token_account: &Pubkey,
destination_token_account: &Pubkey,
mint: &Pubkey,
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: I'm just noticing this now, so you don't need to fix it in this PR, but all of the confidential transfer functions order these as "source, destination, mint", whereas the normal transfer functions order it as "source, mint, destination". We should make that consistent at some point

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, thanks for the catch. I'll reorder the inputs in the confidential transfer functions in a follow-up PR.

source_decrypt_handles,
)?;
// If `maybe_proof_context` is `None`, then this means that
// `close_split_context_state_on_execution` is true and a required context state account is
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be no_op_on_uninitialized_split_context_state, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the catch! Fixed.


if maybe_proof_context.is_none() {
msg!("Context states not fully initialized: return with no op");
msg!("WARNING: transfer is NOT yet executed");
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Maybe "WARNING" is a bit strong, since it is expected operation. Let's just say "transfer is NOT yet executed" and combine it with the earlier msg

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are right. I removed the warning and merged the "transfer is NOT yet executed" to the previous message.

@@ -648,7 +782,7 @@ fn process_source_for_transfer(
source_transfer_amount_lo,
source_transfer_amount_hi,
)
.ok_or(TokenError::CiphertextArithmeticFailed)?;
.ok_or(ProgramError::InvalidInstructionData)?;
Copy link
Contributor

Choose a reason for hiding this comment

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

Did you mean to revert all of these errors? Unless there's a good reason, let's stick with the more specific ones

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This must have been introduced when I was refactoring. I reverted these changes.

/// If an optional transfer instruction is provided, then the transfer instruction is attached
/// to the same transaction.
#[allow(clippy::too_many_arguments)]
pub async fn confidential_transfer_create_equality_and_ciphertext_validity_proofs_for_transfer<
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: instead of proofs can you say context_state since that's what's being done? And rather than overloading this public function to optionally add a transfer, how about have three functions:

  • confidential_transfer_create_equality_and_ciphertext_validity_context_states_for_transfer -- just the creation of the context state accounts
  • confidential_transfer_create_equality_and_ciphertext_validity_context_states_and_transfer_parallel -- specifically for parallel execution, requires the transfer instruction
  • some internal helper function that they both call into to create the proofs, up to you for naming!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, that is a great idea!

///
/// If an optional transfer instruction is provided, then the transfer instruction is attached
/// to the same transaction.
pub async fn confidential_transfer_create_range_proof_for_transfer<S: Signer>(
Copy link
Contributor

Choose a reason for hiding this comment

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

Same thing here, can we split it into three functions?

@mergify mergify bot dismissed joncinque’s stale review August 25, 2023 20:08

Pull request has been modified.

@samkim-crypto samkim-crypto merged commit f9fa519 into solana-labs:master Aug 25, 2023
30 checks passed
serbangv pushed a commit to serbangv/solana-program-library that referenced this pull request Aug 29, 2023
…#5054)

* add `TransferWithSplitProofs` instruction

* add program logic for `TransferWithSplitProofs` instruction

* update token client for `TransferWithSplitProofs` instruction

* update tests for `TransferWithSplitProofs` instruction

* clippy

* fix comments and modify warning message from no-op

* fix ciphertext arithmetic error type

* refactor token client proof context state functions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Closed
Development

Successfully merging this pull request may close these issues.

2 participants