use std::sync::Arc;
use crate::ext::ExtSpace;
use crate::family::CompiledModuleFamily;
use crate::field::Fp;
use super::{
InterfaceFamilyHom, InterfaceFamilyHomError, InterfaceFamilyHomPlan, InterfaceFamilyHomWork,
InterfacePartition,
};
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum InterfaceFamilyHereditaryError {
Hom(InterfaceFamilyHomError),
NonzeroIdeal { basis_relations: usize },
}
display_error! { InterfaceFamilyHereditaryError {
Self::Hom(error) => "fixed-interior Hom plan rejected: {error}";
Self::NonzeroIdeal { basis_relations } => "hereditary interface plan needs the zero ideal, but the reduced basis has {basis_relations} relations";
} }
error_source! { InterfaceFamilyHereditaryError {
Self::Hom(error) => Some(error),
_ => None,
} }
impl From<InterfaceFamilyHomError> for InterfaceFamilyHereditaryError {
fn from(error: InterfaceFamilyHomError) -> Self {
Self::Hom(error)
}
}
#[derive(Clone)]
pub struct InterfaceFamilyHereditaryPlan {
hom: InterfaceFamilyHomPlan,
arrow_cochains: usize,
}
impl InterfaceFamilyHereditaryPlan {
pub fn compile(
family: &CompiledModuleFamily,
partition: &InterfacePartition,
anchor_values: &[Fp],
) -> Result<Self, InterfaceFamilyHereditaryError> {
if !Arc::ptr_eq(family.algebra(), partition.algebra()) {
return Err(InterfaceFamilyHereditaryError::Hom(
InterfaceFamilyHomError::DifferentAlgebra,
));
}
let basis_relations = family.algebra().certificate().basis.len();
if basis_relations != 0 {
return Err(InterfaceFamilyHereditaryError::NonzeroIdeal { basis_relations });
}
let hom = InterfaceFamilyHomPlan::compile(family, partition, anchor_values)?;
Ok(Self {
arrow_cochains: family.hom_equations().len(),
hom,
})
}
accessor_methods! {
pub hom_plan() -> &InterfaceFamilyHomPlan = |this| &this.hom;
pub arrow_cochains() -> usize = |this| this.arrow_cochains;
}
pub fn compute(
&self,
source_values: &[Fp],
target_values: &[Fp],
) -> Result<InterfaceFamilyHereditaryPair, InterfaceFamilyHereditaryError> {
let hom = self.hom.compute(source_values, target_values)?;
Ok(self.finish(hom))
}
pub fn verify(&self) -> bool {
self.hom.verify()
&& self.hom.family().algebra().certificate().basis.is_empty()
&& self.arrow_cochains == self.hom.family().hom_equations().len()
}
fn finish(&self, hom: InterfaceFamilyHom) -> InterfaceFamilyHereditaryPair {
let hom_work = hom.work();
let constraint_rank = hom_work
.fixed_rank
.checked_add(hom_work.interface_rank)
.expect("two ranks bounded by one equation matrix cannot overflow");
let ext1_dimension = self
.arrow_cochains
.checked_sub(constraint_rank)
.expect("the constraint rank cannot exceed its arrow-cochain rows");
InterfaceFamilyHereditaryPair {
plan: self.clone(),
hom,
ext1_dimension,
work: InterfaceFamilyHereditaryWork {
hom: hom_work,
arrow_cochains: self.arrow_cochains,
constraint_rank,
},
}
}
}
debug_fields! { InterfaceFamilyHereditaryPlan |this| {
"hom_plan" => this.hom;
"arrow_cochains" => this.arrow_cochains;
} }
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct InterfaceFamilyHereditaryWork {
pub hom: InterfaceFamilyHomWork,
pub arrow_cochains: usize,
pub constraint_rank: usize,
}
#[derive(Clone)]
pub struct InterfaceFamilyHereditaryPair {
plan: InterfaceFamilyHereditaryPlan,
hom: InterfaceFamilyHom,
ext1_dimension: usize,
work: InterfaceFamilyHereditaryWork,
}
impl InterfaceFamilyHereditaryPair {
accessor_methods! {
pub plan() -> &InterfaceFamilyHereditaryPlan = |this| &this.plan;
pub hom() -> &InterfaceFamilyHom = |this| &this.hom;
pub hom_dimension() -> usize = |this| this.hom.dim();
pub ext1_dimension() -> usize = |this| this.ext1_dimension;
pub work() -> InterfaceFamilyHereditaryWork = |this| this.work;
}
pub fn ext_dimension(&self, degree: usize) -> usize {
match degree {
0 => self.hom_dimension(),
1 => self.ext1_dimension,
_ => 0,
}
}
pub fn verify(&self) -> bool {
if !self.plan.verify() || !self.hom.verify() {
return false;
}
let source = self.hom.source();
let target = self.hom.target();
let Ok(ext1) = ExtSpace::new(source, target, 1) else {
return false;
};
let Ok(ext2) = ExtSpace::new(source, target, 2) else {
return false;
};
ext1.dim() == self.ext1_dimension && ext2.dim() == 0
}
}
debug_fields! { InterfaceFamilyHereditaryPair |this| {
"source_dimensions" => this.hom.source().dim_vector();
"target_dimensions" => this.hom.target().dim_vector();
"hom_dimension" => this.hom_dimension();
"ext1_dimension" => this.ext1_dimension;
"work" => this.work;
} }