Skip to content

Commit

Permalink
Add more documentation for serializing
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-zw committed Sep 5, 2022
1 parent 5c1dde5 commit b4ffbe1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ec/src/models/short_weierstrass/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ pub trait SWCurveConfig: super::CurveConfig {
res
}

/// If uncompressed, serializes both x and y coordinates as well as a bit for whether it is
/// infinity. If compressed, serializes x coordinate with two bits to encode whether y is
/// positive, negative, or infinity.
#[inline]
fn serialize_with_mode<W: Write>(
item: &Affine<Self>,
Expand All @@ -129,6 +132,7 @@ pub trait SWCurveConfig: super::CurveConfig {
}
}

/// If `validate` is `Yes`, calls `check()` to make sure the element is valid.
fn deserialize_with_mode<R: Read>(
mut reader: R,
compress: Compress,
Expand Down
5 changes: 5 additions & 0 deletions ec/src/models/twisted_edwards/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ pub trait TECurveConfig: super::CurveConfig {
res
}

/// If uncompressed, serializes both x and y coordinates.
/// If compressed, serializes y coordinate with a bit to encode whether x is positive.
#[inline]
fn serialize_with_mode<W: Write>(
item: &Affine<Self>,
Expand All @@ -101,6 +103,9 @@ pub trait TECurveConfig: super::CurveConfig {
}
}

/// If `validate` is `Yes`, calls `check()` to make sure the element is valid.
///
/// Uses `Affine::get_xs_from_y_unchecked()` for the compressed version.
fn deserialize_with_mode<R: Read>(
mut reader: R,
compress: Compress,
Expand Down
6 changes: 6 additions & 0 deletions serialize/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@ pub use ark_serialize_derive::*;

use digest::{generic_array::GenericArray, Digest, OutputSizeUser};

/// Whether to use a compressed version of the serialization algorithm. Specific behavior depends
/// on implementation. If no compressed version exists (e.g. on `Fp`), mode is ignored.
#[derive(Copy, Clone, PartialEq, Eq)]
pub enum Compress {
Yes,
No,
}

/// Whether to validate the element after deserializing it. Specific behavior depends on
/// implementation. If no validation algorithm exists (e.g. on `Fp`), mode is ignored.
#[derive(Copy, Clone, PartialEq, Eq)]
pub enum Validate {
Yes,
Expand Down Expand Up @@ -76,6 +80,7 @@ pub trait Valid: Sized + Sync {
/// }
/// ```
pub trait CanonicalSerialize {
/// The general serialize method that takes in customization flags.
fn serialize_with_mode<W: Write>(
&self,
writer: W,
Expand Down Expand Up @@ -118,6 +123,7 @@ pub trait CanonicalSerialize {
/// }
/// ```
pub trait CanonicalDeserialize: Valid {
/// The general deserialize method that takes in customization flags.
fn deserialize_with_mode<R: Read>(
reader: R,
compress: Compress,
Expand Down

0 comments on commit b4ffbe1

Please sign in to comment.