Repository: aptos-labs/aptos-core
File: consensus/src/pipeline/decryption_pipeline_builder.rs (Rust)

Code before:
                metadata.clone(),
                derived_key_share,
            )))
            .is_err()
        {
            return Err(anyhow!(
                "derived_self_key_share_tx receiver dropped, pipeline likely aborted"
            )
            .into());
        }

        // TODO(ibalajiarun): improve perf
        let proofs = FPTXWeighted::eval_proofs_compute_all(
            &proofs_promise,
            secret_share_config.digest_key(),
        );

        let maybe_decryption_key = secret_shared_key_rx
            .await
            .map_err(|_| anyhow!("secret_shared_key_rx dropped"))?;
        // TODO(ibalajiarun): account for the case where decryption key is not available
        let decryption_key = maybe_decryption_key.expect("decryption key should be available");

        info!(
            "Successfully received decryption key for block {}: metadata={:?}",
            block.round(),
            decryption_key.metadata
        );
        let decrypted_txns: Vec<_> = encrypted_txns
            .into_par_iter()
            .zip(txn_ciphertexts)
            .map(|(mut txn, ciphertext)| {
                let eval_proof = proofs.get(&ciphertext.id()).expect("must exist");
                match FPTXWeighted::decrypt_individual::<DecryptedPayload>(
                    &decryption_key.key,
                    &ciphertext,
                    &digest,
                    &eval_proof,
                ) {
                    Ok(payload) => {
                        let (executable, nonce) = payload.unwrap();
                        txn.payload_mut()
                            .as_encrypted_payload_mut()
                            .map(|p| {
                                p.into_decrypted(eval_proof, executable, nonce)
                                    .expect("must happen")
                            })
                            .expect("must exist");
                    },
                    Err(e) => {
                        error!(

Review comment (performance):
The `decrypt_individual` fn is not efficient and was meant only for verifying correct decryption during state sync. It should be quite a bit (maybe 2x) more efficient to call `prepare_cts` after computing eval proofs, which will return return `Vec<PreparedCiphertext>`,  and then once the decryption key is received, to call `decrypt` on this vec.

Code after:
            .into());
        }

        // TODO(ibalajiarun): improve perf
        let proofs = FPTXWeighted::eval_proofs_compute_all(
            &proofs_promise,
            secret_share_config.digest_key(),
        );

        let maybe_decryption_key = secret_shared_key_rx
            .await
            .map_err(|_| anyhow!("secret_shared_key_rx dropped"))?;
        // TODO(ibalajiarun): account for the case where decryption key is not available
        let decryption_key = maybe_decryption_key.expect("decryption key should be available");

        info!(
            "Successfully received decryption key for block {}: metadata={:?}",
            block.round(),
            decryption_key.metadata
        );
        let decrypted_txns: Vec<_> = encrypted_txns
            .into_par_iter()
            .zip(txn_ciphertexts)
            .map(|(mut txn, ciphertext)| {
                let eval_proof = proofs.get(&ciphertext.id()).expect("must exist");
                match FPTXWeighted::decrypt_individual::<DecryptedPayload>(
                    &decryption_key.key,
                    &ciphertext,
                    &digest,
                    &eval_proof,
                ) {
                    Ok(payload) => {
                        let (executable, nonce) = payload.unwrap();
                        txn.payload_mut()
                            .as_encrypted_payload_mut()
                            .map(|p| {
                                p.into_decrypted(eval_proof, executable, nonce)
                                    .expect("must happen")
                            })
                            .expect("must exist");
                    },
                    Err(e) => {
                        error!(
                            "Failed to decrypt transaction with ciphertext id {:?}: {:?}",
                            ciphertext.id(),
                            e
                        );
                        txn.payload_mut()
                            .as_encrypted_payload_mut()
                            .map(|p| p.into_failed_decryption(eval_proof).expect("must happen"))
                            .expect("must exist");