Skip to content

Commit

Permalink
field: require TryInto<Base> for ExtensionField
Browse files Browse the repository at this point in the history
  • Loading branch information
apoelstra committed Sep 30, 2024
1 parent 4dfe325 commit fc903d6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/primitives/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

//! Generic Field Traits
use core::convert::TryInto;
use core::iter::{Skip, Take};
use core::{fmt, hash, iter, ops};

Expand Down Expand Up @@ -153,7 +154,7 @@ pub trait Field:

/// Trait describing a simple extension field (field obtained from another by
/// adjoining one element).
pub trait ExtensionField: Field + From<Self::BaseField> {
pub trait ExtensionField: Field + From<Self::BaseField> + TryInto<Self::BaseField> {
/// The type of the base field.
type BaseField: Field;

Expand Down
13 changes: 13 additions & 0 deletions src/primitives/gf32_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ impl<const DEG: usize> From<Fe32> for Fe32Ext<DEG> {
}
}

impl<const DEG: usize> core::convert::TryFrom<Fe32Ext<DEG>> for Fe32 {
type Error = ();

fn try_from(ext: Fe32Ext<DEG>) -> Result<Self, Self::Error> {
for elem in &ext.inner[1..] {
if *elem != Fe32::Q {
return Err(());
}
}
Ok(ext.inner[0])
}
}

impl<const DEG: usize> fmt::Debug for Fe32Ext<DEG> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Display::fmt(self, f) }
}
Expand Down

0 comments on commit fc903d6

Please sign in to comment.