Repository: aptos-labs/aptos-core
File: keyless/pepper/service/src/dedicated_handlers/pepper_request.rs (Rust)

Code before:
        PepperServiceError::InternalError(format!(
            "Failed to serialize pepper input! Error: {:?}",
            error
        ))
    })?;

    // Generate the pepper base and proof using the VUF
    let (pepper_base, vuf_proof) =
        vuf::bls12381_g1_bls::Bls12381G1Bls::eval(vuf_private_key.deref(), &input_bytes).map_err(
            |error| {
                PepperServiceError::InternalError(format!(
                    "Failed to evaluate bls12381_g1_bls VUF: {}",
                    error
                ))
            },
        )?;

    // Verify that the proof is empty
    if !vuf_proof.is_empty() {
        return Err(PepperServiceError::InternalError(
            "The VUF proof is not empty! This shouldn't happen.".to_string(),
        ));
    }

    // Derive the VUF public key
    let vuf_public_key = vuf::bls12381_g1_bls::Bls12381G1Bls::pk_from_sk(vuf_private_key.deref())
        .map_err(|error| {
        PepperServiceError::InternalError(format!(
            "Failed to get VUF public key from private key: {}",
            error
        ))
    })?;

    // Verify the pepper base output
    vuf::bls12381_g1_bls::Bls12381G1Bls::verify(
        &vuf_public_key,
        &input_bytes,
        &pepper_base,
        &vuf_proof,
    )
    .map_err(|error| {
        PepperServiceError::InternalError(format!("VUF verification failed: {}", error))
    })?;

    Ok(pepper_base)
}

/// Creates the pepper input, and updates the account recovery DB
async fn create_pepper_input(
    claims: &TokenData<Claims>,
    uid_key: String,

Review comment (suggestion):
It would be better to not re-derive the PK here & just pass it as an argument.

It's going to add the **same** unnecessary work for every pepper request: i.e., always re-computing $$\mathsf{sk}\cdot G$$

Code after:
        PepperServiceError::InternalError(format!(
            "Failed to serialize pepper input! Error: {:?}",
            error
        ))
    })?;

    // Generate the pepper base and proof using the VUF
    let (pepper_base, vuf_proof) =
        vuf::bls12381_g1_bls::Bls12381G1Bls::eval(vuf_keypair.vuf_private_key(), &input_bytes)
            .map_err(|error| {
                PepperServiceError::InternalError(format!(
                    "Failed to evaluate bls12381_g1_bls VUF: {}",
                    error
                ))
            })?;

    // Verify that the proof is empty
    if !vuf_proof.is_empty() {
        return Err(PepperServiceError::InternalError(
            "The VUF proof is not empty! This shouldn't happen.".to_string(),
        ));
    }

    // Verify the pepper base output (this ensures we only ever return valid outputs,
    // and protects against various security issues, e.g., fault based side channels).
    vuf::bls12381_g1_bls::Bls12381G1Bls::verify(
        vuf_keypair.vuf_public_key(),
        &input_bytes,
        &pepper_base,
        &vuf_proof,
    )
    .map_err(|error| {
        PepperServiceError::InternalError(format!("VUF verification failed: {}", error))
    })?;

    Ok(pepper_base)
}

/// Creates the pepper input, and updates the account recovery DB
async fn create_pepper_input(
    claims: &TokenData<Claims>,
    uid_key: String,
    uid_val: String,
    account_recovery_managers: Arc<AccountRecoveryManagers>,
    aud_override: Option<String>,
    account_recovery_db: Arc<dyn AccountRecoveryDBInterface + Send + Sync>,
) -> Result<PepperInput, PepperServiceError> {
    let iss = claims.claims.iss.clone();
    let claims_aud = claims.claims.aud.clone();

    // Get the aud for the pepper input. Note: if the request is from an account