Skip to content

Commit fc903d6

Browse files
committed
field: require TryInto<Base> for ExtensionField
1 parent 4dfe325 commit fc903d6

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/primitives/field.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
//! Generic Field Traits
44
5+
use core::convert::TryInto;
56
use core::iter::{Skip, Take};
67
use core::{fmt, hash, iter, ops};
78

@@ -153,7 +154,7 @@ pub trait Field:
153154

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

src/primitives/gf32_ext.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@ impl<const DEG: usize> From<Fe32> for Fe32Ext<DEG> {
3737
}
3838
}
3939

40+
impl<const DEG: usize> core::convert::TryFrom<Fe32Ext<DEG>> for Fe32 {
41+
type Error = ();
42+
43+
fn try_from(ext: Fe32Ext<DEG>) -> Result<Self, Self::Error> {
44+
for elem in &ext.inner[1..] {
45+
if *elem != Fe32::Q {
46+
return Err(());
47+
}
48+
}
49+
Ok(ext.inner[0])
50+
}
51+
}
52+
4053
impl<const DEG: usize> fmt::Debug for Fe32Ext<DEG> {
4154
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Display::fmt(self, f) }
4255
}

0 commit comments

Comments
 (0)