Skip to content

Commit

Permalink
Merge pull request #209 from hackaugusto/hacka-conditional-support-fo…
Browse files Browse the repository at this point in the history
…r-serde

feature: add conditional support for serde
  • Loading branch information
irakliyk authored Aug 9, 2023
2 parents 42c2226 + d2c2d6d commit d97dc57
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions math/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 4 additions & 0 deletions math/src/field/extensions/cubic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ use utils::{
DeserializationError, Randomizable, Serializable, SliceReader,
};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

// QUADRATIC EXTENSION FIELD
// ================================================================================================

Expand All @@ -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: ExtensibleField<3>>(B, B, B);

impl<B: ExtensibleField<3>> CubeExtension<B> {
Expand Down
4 changes: 4 additions & 0 deletions math/src/field/extensions/quadratic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ use utils::{
DeserializationError, Randomizable, Serializable, SliceReader,
};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

// QUADRATIC EXTENSION FIELD
// ================================================================================================

Expand All @@ -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: ExtensibleField<2>>(B, B);

impl<B: ExtensibleField<2>> QuadExtension<B> {
Expand Down
5 changes: 5 additions & 0 deletions math/src/field/f128/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ use utils::{
Serializable,
};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

#[cfg(test)]
mod tests;

Expand All @@ -48,6 +51,8 @@ const ELEMENT_BYTES: usize = core::mem::size_of::<u128>();
/// 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 {
Expand Down
17 changes: 17 additions & 0 deletions math/src/field/f62/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ use utils::{
DeserializationError, Randomizable, Serializable,
};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

#[cfg(test)]
mod tests;

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -457,6 +462,18 @@ impl From<[u8; 8]> for BaseElement {
}
}

impl From<BaseElement> for u128 {
fn from(value: BaseElement) -> Self {
value.as_int() as u128
}
}

impl From<BaseElement> for u64 {
fn from(value: BaseElement) -> Self {
value.as_int()
}
}

impl<'a> TryFrom<&'a [u8]> for BaseElement {
type Error = DeserializationError;

Expand Down
18 changes: 18 additions & 0 deletions math/src/field/f64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ use utils::{
DeserializationError, Randomizable, Serializable,
};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

#[cfg(test)]
mod tests;

Expand All @@ -50,7 +53,10 @@ const ELEMENT_BYTES: usize = core::mem::size_of::<u64>();
/// 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.
Expand Down Expand Up @@ -591,6 +597,18 @@ impl<'a> TryFrom<&'a [u8]> for BaseElement {
}
}

impl From<BaseElement> for u128 {
fn from(value: BaseElement) -> Self {
value.as_int() as u128
}
}

impl From<BaseElement> for u64 {
fn from(value: BaseElement) -> Self {
value.as_int()
}
}

impl AsBytes for BaseElement {
fn as_bytes(&self) -> &[u8] {
// TODO: take endianness into account
Expand Down

0 comments on commit d97dc57

Please sign in to comment.