diff --git a/math/Cargo.toml b/math/Cargo.toml index 6a8dd7531..ebbb48782 100644 --- a/math/Cargo.toml +++ b/math/Cargo.toml @@ -33,6 +33,7 @@ default = ["std"] std = ["utils/std"] [dependencies] +serde = { version = "1.0", features = [ "derive" ], optional = true, default-features = false } utils = { version = "0.6", path = "../utils/core", package = "winter-utils", default-features = false } [dev-dependencies] diff --git a/math/src/field/extensions/cubic.rs b/math/src/field/extensions/cubic.rs index fbc7573f5..fb5a4b1e5 100644 --- a/math/src/field/extensions/cubic.rs +++ b/math/src/field/extensions/cubic.rs @@ -15,6 +15,9 @@ use utils::{ DeserializationError, Randomizable, Serializable, SliceReader, }; +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; + // QUADRATIC EXTENSION FIELD // ================================================================================================ @@ -25,6 +28,7 @@ use utils::{ /// field elements. #[repr(C)] #[derive(Copy, Clone, Debug, PartialEq, Eq, Default)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct CubeExtension>(B, B, B); impl> CubeExtension { diff --git a/math/src/field/extensions/quadratic.rs b/math/src/field/extensions/quadratic.rs index 3a2dceb52..ad3705f0f 100644 --- a/math/src/field/extensions/quadratic.rs +++ b/math/src/field/extensions/quadratic.rs @@ -15,6 +15,9 @@ use utils::{ DeserializationError, Randomizable, Serializable, SliceReader, }; +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; + // QUADRATIC EXTENSION FIELD // ================================================================================================ @@ -25,6 +28,7 @@ use utils::{ /// elements. #[repr(C)] #[derive(Copy, Clone, Debug, PartialEq, Eq, Default)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] pub struct QuadExtension>(B, B); impl> QuadExtension { diff --git a/math/src/field/f128/mod.rs b/math/src/field/f128/mod.rs index a8aa77481..9348d6993 100644 --- a/math/src/field/f128/mod.rs +++ b/math/src/field/f128/mod.rs @@ -25,6 +25,9 @@ use utils::{ Serializable, }; +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; + #[cfg(test)] mod tests; @@ -48,6 +51,8 @@ const ELEMENT_BYTES: usize = core::mem::size_of::(); /// Internal values are stored in their canonical form in the range [0, M). The backing type is /// `u128`. #[derive(Copy, Clone, Debug, PartialEq, Eq, Default)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] +#[cfg_attr(feature = "serde", serde(transparent))] pub struct BaseElement(u128); impl BaseElement { diff --git a/math/src/field/f62/mod.rs b/math/src/field/f62/mod.rs index 966deb723..d72b8ee40 100644 --- a/math/src/field/f62/mod.rs +++ b/math/src/field/f62/mod.rs @@ -22,6 +22,9 @@ use utils::{ DeserializationError, Randomizable, Serializable, }; +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; + #[cfg(test)] mod tests; @@ -54,6 +57,8 @@ const G: u64 = 4421547261963328785; /// Internal values are stored in Montgomery representation and can be in the range [0; 2M). The /// backing type is `u64`. #[derive(Copy, Clone, Debug, Default)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] +#[cfg_attr(feature = "serde", serde(from = "u64", into = "u64"))] pub struct BaseElement(u64); impl BaseElement { @@ -457,6 +462,18 @@ impl From<[u8; 8]> for BaseElement { } } +impl From for u128 { + fn from(value: BaseElement) -> Self { + value.as_int() as u128 + } +} + +impl From for u64 { + fn from(value: BaseElement) -> Self { + value.as_int() + } +} + impl<'a> TryFrom<&'a [u8]> for BaseElement { type Error = DeserializationError; diff --git a/math/src/field/f64/mod.rs b/math/src/field/f64/mod.rs index 39633c3d2..6a4ad464a 100644 --- a/math/src/field/f64/mod.rs +++ b/math/src/field/f64/mod.rs @@ -27,6 +27,9 @@ use utils::{ DeserializationError, Randomizable, Serializable, }; +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; + #[cfg(test)] mod tests; @@ -50,7 +53,10 @@ const ELEMENT_BYTES: usize = core::mem::size_of::(); /// Internal values represent x * R mod M where R = 2^64 mod M and x in [0, M). /// The backing type is `u64` but the internal values are always in the range [0, M). #[derive(Copy, Clone, Debug, Default)] +#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))] +#[cfg_attr(feature = "serde", serde(from = "u64", into = "u64"))] pub struct BaseElement(u64); + impl BaseElement { /// Creates a new field element from the provided `value`; the value is converted into /// Montgomery representation. @@ -591,6 +597,18 @@ impl<'a> TryFrom<&'a [u8]> for BaseElement { } } +impl From for u128 { + fn from(value: BaseElement) -> Self { + value.as_int() as u128 + } +} + +impl From for u64 { + fn from(value: BaseElement) -> Self { + value.as_int() + } +} + impl AsBytes for BaseElement { fn as_bytes(&self) -> &[u8] { // TODO: take endianness into account