[zk-token-sdk] Add encryption error variant `SeedLengthTooShort` and remove variant `PubkeyDoesNotExist` (#31840)
This commit is contained in:
parent
e58ad81b64
commit
93efc13bbf
|
@ -36,9 +36,8 @@ use {
|
||||||
pub enum AuthenticatedEncryptionError {
|
pub enum AuthenticatedEncryptionError {
|
||||||
#[error("key derivation method not supported")]
|
#[error("key derivation method not supported")]
|
||||||
DerivationMethodNotSupported,
|
DerivationMethodNotSupported,
|
||||||
|
#[error("seed length too short for derivation")]
|
||||||
#[error("pubkey does not exist")]
|
SeedLengthTooShort,
|
||||||
PubkeyDoesNotExist,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct AuthenticatedEncryption;
|
struct AuthenticatedEncryption;
|
||||||
|
@ -163,7 +162,7 @@ impl SeedDerivable for AeKey {
|
||||||
const MINIMUM_SEED_LEN: usize = 16;
|
const MINIMUM_SEED_LEN: usize = 16;
|
||||||
|
|
||||||
if seed.len() < MINIMUM_SEED_LEN {
|
if seed.len() < MINIMUM_SEED_LEN {
|
||||||
return Err("Seed is too short".into());
|
return Err(AuthenticatedEncryptionError::SeedLengthTooShort.into());
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut hasher = Sha3_512::new();
|
let mut hasher = Sha3_512::new();
|
||||||
|
|
|
@ -54,6 +54,8 @@ use {
|
||||||
pub enum ElGamalError {
|
pub enum ElGamalError {
|
||||||
#[error("key derivation method not supported")]
|
#[error("key derivation method not supported")]
|
||||||
DerivationMethodNotSupported,
|
DerivationMethodNotSupported,
|
||||||
|
#[error("seed length too short for derivation")]
|
||||||
|
SeedLengthTooShort,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Algorithm handle for the twisted ElGamal encryption scheme
|
/// Algorithm handle for the twisted ElGamal encryption scheme
|
||||||
|
@ -373,7 +375,8 @@ impl ElGamalSecretKey {
|
||||||
public_seed: &[u8],
|
public_seed: &[u8],
|
||||||
) -> Result<Self, Box<dyn error::Error>> {
|
) -> Result<Self, Box<dyn error::Error>> {
|
||||||
let seed = Self::seed_from_signer(signer, public_seed)?;
|
let seed = Self::seed_from_signer(signer, public_seed)?;
|
||||||
Self::from_seed(&seed)
|
let key = Self::from_seed(&seed)?;
|
||||||
|
Ok(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Derive a seed from a Solana signer used to generate an ElGamal secret key.
|
/// Derive a seed from a Solana signer used to generate an ElGamal secret key.
|
||||||
|
@ -407,11 +410,11 @@ impl ElGamalSecretKey {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Derive an ElGamal secret key from an entropy seed.
|
/// Derive an ElGamal secret key from an entropy seed.
|
||||||
pub fn from_seed(seed: &[u8]) -> Result<Self, Box<dyn error::Error>> {
|
pub fn from_seed(seed: &[u8]) -> Result<Self, ElGamalError> {
|
||||||
const MINIMUM_SEED_LEN: usize = 32;
|
const MINIMUM_SEED_LEN: usize = 32;
|
||||||
|
|
||||||
if seed.len() < MINIMUM_SEED_LEN {
|
if seed.len() < MINIMUM_SEED_LEN {
|
||||||
return Err("Seed is too short".into());
|
return Err(ElGamalError::SeedLengthTooShort);
|
||||||
}
|
}
|
||||||
Ok(ElGamalSecretKey(Scalar::hash_from_bytes::<Sha3_512>(seed)))
|
Ok(ElGamalSecretKey(Scalar::hash_from_bytes::<Sha3_512>(seed)))
|
||||||
}
|
}
|
||||||
|
@ -467,7 +470,8 @@ impl EncodableKey for ElGamalSecretKey {
|
||||||
|
|
||||||
impl SeedDerivable for ElGamalSecretKey {
|
impl SeedDerivable for ElGamalSecretKey {
|
||||||
fn from_seed(seed: &[u8]) -> Result<Self, Box<dyn error::Error>> {
|
fn from_seed(seed: &[u8]) -> Result<Self, Box<dyn error::Error>> {
|
||||||
Self::from_seed(seed)
|
let key = Self::from_seed(seed)?;
|
||||||
|
Ok(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_seed_and_derivation_path(
|
fn from_seed_and_derivation_path(
|
||||||
|
@ -481,10 +485,11 @@ impl SeedDerivable for ElGamalSecretKey {
|
||||||
seed_phrase: &str,
|
seed_phrase: &str,
|
||||||
passphrase: &str,
|
passphrase: &str,
|
||||||
) -> Result<Self, Box<dyn error::Error>> {
|
) -> Result<Self, Box<dyn error::Error>> {
|
||||||
Self::from_seed(&generate_seed_from_seed_phrase_and_passphrase(
|
let key = Self::from_seed(&generate_seed_from_seed_phrase_and_passphrase(
|
||||||
seed_phrase,
|
seed_phrase,
|
||||||
passphrase,
|
passphrase,
|
||||||
))
|
))?;
|
||||||
|
Ok(key)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue