pub fn secret_key_from_components(
    sk_raw_bytes: &[u8],
    pk: &types::PublicKeyBytes,
) -> CryptoResult<types::SecretKeyBytes> {
    let group = EcGroup::from_curve_name(CURVE_NAME)
        .map_err(|e| wrap_openssl_err(e, "unable to create EC group"))?;
    let private_number = BigNum::from_slice(sk_raw_bytes)
        .map_err(|e| wrap_openssl_err(e, "unable to parse big integer"))?;
    let mut ctx =
        BigNumContext::new().map_err(|e| wrap_openssl_err(e, "unable to create BigNumContext"))?;
    let public_point = EcPoint::from_bytes(&group, &pk.0, &mut ctx).map_err(|e| {
        CryptoError::MalformedPublicKey {
            algorithm: AlgorithmId::EcdsaSecp256k1,
            key_bytes: Some(pk.0.to_vec()),
            internal_error: e.to_string(),
        }
    })?;
    let ec_key =
        EcKey::from_private_components(&group, &private_number, &public_point).map_err(|_| {
            CryptoError::MalformedSecretKey {
                algorithm: AlgorithmId::EcdsaSecp256k1,
                internal_error: "OpenSSL error".to_string(), // don't leak sensitive information
            }
        })?;
    let mut sk_der =
        ec_key
            .private_key_to_der()
            .map_err(|e| CryptoError::AlgorithmNotSupported {
                algorithm: AlgorithmId::EcdsaSecp256k1,
                reason: format!("OpenSSL failed with error {}", e.to_string()),
            })?;
    Ok(types::SecretKeyBytes(SecretVec::new_and_zeroize_argument(
        &mut sk_der,
    )))
}
/// Create a secp256k1 secret key from raw bytes
///
/// # Arguments
/// * `sk_raw_bytes` is the big-endian encoding of unsigned integer
/// * `pk` is the public key associated with this secret key
/// # Errors
/// * `AlgorithmNotSupported` if an error occured while invoking OpenSSL
/// * `MalformedPublicKey` if the public key could not be parsed
/// * `MalformedSecretKey` if the secret key does not coorespond with the public
///   key
Create a secp256k1 secret key from raw bytes
Create a secp256k1 secret key from raw bytes
pub fn secret_key_from_components(
    sk_raw_bytes: &[u8],
    pk: &types::PublicKeyBytes,
) -> CryptoResult<types::SecretKeyBytes> {
    let group = EcGroup::from_curve_name(CURVE_NAME)
        .map_err(|e| wrap_openssl_err(e, "unable to create EC group"))?;
    let private_number = BigNum::from_slice(sk_raw_bytes)
        .map_err(|e| wrap_openssl_err(e, "unable to parse big integer"))?;
    let mut ctx =
        BigNumContext::new().map_err(|e| wrap_openssl_err(e, "unable to create BigNumContext"))?;
    let public_point = EcPoint::from_bytes(&group, &pk.0, &mut ctx).map_err(|e| {
        CryptoError::MalformedPublicKey {
            algorithm: AlgorithmId::EcdsaSecp256k1,
            key_bytes: Some(pk.0.to_vec()),
            internal_error: e.to_string(),
        }
    })?;
    let ec_key =
        EcKey::from_private_components(&group, &private_number, &public_point).map_err(|_| {
            CryptoError::MalformedSecretKey {
                algorithm: AlgorithmId::EcdsaSecp256k1,
                internal_error: "OpenSSL error".to_string(), // don't leak sensitive information
            }
        })?;
    let mut sk_der =
        ec_key
            .private_key_to_der()
            .map_err(|e| CryptoError::AlgorithmNotSupported {
                algorithm: AlgorithmId::EcdsaSecp256k1,
                reason: format!("OpenSSL failed with error {}", e.to_string()),
            })?;
    Ok(types::SecretKeyBytes(SecretVec::new_and_zeroize_argument(
        &mut sk_der,
    )))
}
pub fn secret_key_from_components ( sk_raw_bytes : & [ u8 ] , pk : & types :: PublicKeyBytes , ) -> CryptoResult < types :: SecretKeyBytes > { let group = EcGroup :: from_curve_name ( CURVE_NAME ) . map_err ( | e | wrap_openssl_err ( e , " " ) ) ? ; let private_number = BigNum :: from_slice ( sk_raw_bytes ) . map_err ( | e | wrap_openssl_err ( e , " " ) ) ? ; let mut ctx = BigNumContext :: new ( ) . map_err ( | e | wrap_openssl_err ( e , " " ) ) ? ; let public_point = EcPoint :: from_bytes ( & group , & pk . 0 , & mut ctx ) . map_err ( | e | { CryptoError :: MalformedPublicKey { algorithm : AlgorithmId :: EcdsaSecp256k1 , key_bytes : Some ( pk . 0 . to_vec ( ) ) , internal_error : e . to_string ( ) , } } ) ? ; let ec_key = EcKey :: from_private_components ( & group , & private_number , & public_point ) . map_err ( | _ | { CryptoError :: MalformedSecretKey { algorithm : AlgorithmId :: EcdsaSecp256k1 , internal_error : " " . to_string ( ) , // don't leak sensitive information } } ) ? ; let mut sk_der = ec_key . private_key_to_der ( ) . map_err ( | e | CryptoError :: AlgorithmNotSupported { algorithm : AlgorithmId :: EcdsaSecp256k1 , reason : format ! ( " " e to_string ( ) ) , } ) ? ; Ok ( types :: SecretKeyBytes ( SecretVec :: new_and_zeroize_argument ( & mut sk_der , ) ) ) }
Create a secp256k1 secret key from raw bytes
Create a secp256k1 secret key from raw bytes
sk_raw_bytes
&[u8]
pk
&types::PublicKeyBytes
sk_raw_bytes
&[u8]
pk
&types::PublicKeyBytes
errors
`AlgorithmNotSupported` if an error occured while invoking OpenSSL
`MalformedPublicKey` if the public key could not be parsed
`MalformedSecretKey` if the secret key does not coorespond with the public
key
` AlgorithmNotSupported ` if an error occured while invoking OpenSSL ` MalformedPublicKey ` if the public key could not be parsed ` MalformedSecretKey ` if the secret key does not coorespond with the public key