use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum GenerationError {
InvalidGenerator {
generator: i64,
reason: &'static str,
},
DuplicateGenerator { generator: i64 },
InvalidSplitPrime { split_prime: i64 },
PrimeNotSplit { split_prime: i64, generator: i64 },
InvalidFieldElementDimension { expected: usize, actual: usize },
InvalidSearchParameter {
parameter: &'static str,
reason: &'static str,
},
PrimeElementNotFound { prime: i64, max_exponent: usize },
MissingImaginaryGenerator,
InsufficientPoints { requested: usize, found: usize },
}
impl fmt::Display for GenerationError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
GenerationError::InvalidGenerator { generator, reason } => {
write!(f, "invalid generator {generator}: {reason}")
}
GenerationError::DuplicateGenerator { generator } => {
write!(f, "duplicate generator {generator}")
}
GenerationError::InvalidSplitPrime { split_prime } => {
write!(f, "split prime {split_prime} is not an odd prime")
}
GenerationError::PrimeNotSplit {
split_prime,
generator,
} => write!(
f,
"prime {split_prime} is not split by generator {generator}"
),
GenerationError::InvalidFieldElementDimension { expected, actual } => write!(
f,
"field element has {actual} coefficients, but field degree is {expected}"
),
GenerationError::InvalidSearchParameter { parameter, reason } => {
write!(f, "invalid search parameter {parameter}: {reason}")
}
GenerationError::PrimeElementNotFound {
prime,
max_exponent,
} => write!(
f,
"could not find a suitable prime element theta for p = {prime} up to exponent {max_exponent}"
),
GenerationError::MissingImaginaryGenerator => write!(
f,
"field is missing the imaginary generator -1 required by the construction"
),
GenerationError::InsufficientPoints { requested, found } => write!(
f,
"could not generate enough points: requested {requested}, found {found}"
),
}
}
}
impl std::error::Error for GenerationError {}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum VerificationError {
PointCountMismatch { expected: usize, actual: usize },
EdgeIndexOutOfBounds {
edge: (usize, usize),
point_count: usize,
},
SelfEdge { index: usize },
EdgeSetMismatch {
expected: Vec<(usize, usize)>,
actual: Vec<(usize, usize)>,
},
PointMismatch { index: usize },
QuantizedProjectionMismatch {
index: usize,
expected: [i64; 2],
actual: [i64; 2],
},
AlgebraicKeyDimensionMismatch { expected: usize, actual: usize },
InvalidAlgebraicKeyDenominator { denominator: i64 },
TranslationNotNormOne { index: usize },
CandidateOutsideWindow {
index: usize,
radius: String,
max_embedding_norm: String,
},
ProjectionMismatch { index: usize },
CandidatePathMismatch { index: usize },
CandidatePathTranslationOutOfBounds {
candidate_index: usize,
translation_index: usize,
translation_count: usize,
},
EdgeProvenanceMismatch { edge_index: usize },
InvalidConstruction { reason: String },
CertificateSchemaMismatch { reason: String },
}
impl fmt::Display for VerificationError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
VerificationError::PointCountMismatch { expected, actual } => write!(
f,
"certificate has {actual} points, but expected {expected}"
),
VerificationError::EdgeIndexOutOfBounds { edge, point_count } => write!(
f,
"certificate edge ({}, {}) is outside point count {point_count}",
edge.0, edge.1
),
VerificationError::SelfEdge { index } => {
write!(f, "certificate contains self-edge at point {index}")
}
VerificationError::EdgeSetMismatch { expected, actual } => write!(
f,
"certificate edge set mismatch: expected {} edges, got {}",
expected.len(),
actual.len()
),
VerificationError::PointMismatch { index } => {
write!(f, "certificate point {index} does not match construction")
}
VerificationError::QuantizedProjectionMismatch {
index,
expected,
actual,
} => write!(
f,
"projection key {index} mismatch: expected {:?}, got {:?}",
expected, actual
),
VerificationError::AlgebraicKeyDimensionMismatch { expected, actual } => write!(
f,
"algebraic key has {actual} coefficients, but field degree is {expected}"
),
VerificationError::InvalidAlgebraicKeyDenominator { denominator } => write!(
f,
"algebraic key denominator must be positive, got {denominator}"
),
VerificationError::TranslationNotNormOne { index } => {
write!(f, "translation {index} is not certified as norm-one")
}
VerificationError::CandidateOutsideWindow {
index,
radius,
max_embedding_norm,
} => write!(
f,
"candidate {index} is outside polydisk radius {radius}; max embedding norm is {max_embedding_norm}"
),
VerificationError::ProjectionMismatch { index } => {
write!(f, "projection {index} does not match algebraic candidate")
}
VerificationError::CandidatePathMismatch { index } => {
write!(f, "candidate path {index} does not replay to stored key")
}
VerificationError::CandidatePathTranslationOutOfBounds {
candidate_index,
translation_index,
translation_count,
} => write!(
f,
"candidate {candidate_index} path references translation {translation_index}, but only {translation_count} translations exist"
),
VerificationError::EdgeProvenanceMismatch { edge_index } => {
write!(
f,
"construction edge {edge_index} is not explained by its translation provenance"
)
}
VerificationError::InvalidConstruction { reason } => {
write!(f, "invalid certificate construction: {reason}")
}
VerificationError::CertificateSchemaMismatch { reason } => {
write!(f, "certificate schema mismatch: {reason}")
}
}
}
}
impl std::error::Error for VerificationError {}