-
Notifications
You must be signed in to change notification settings - Fork 186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add basic serde support for Scalar, G1, G2 with human readable encoding #125
Open
joschisan
wants to merge
1
commit into
zkcrypto:main
Choose a base branch
from
joschisan:serde
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
use group::Curve; | ||
use serde::de::Error; | ||
use serde::{Deserialize, Deserializer, Serialize, Serializer}; | ||
|
||
use crate::g1::{G1Affine, G1Projective}; | ||
use crate::g2::{G2Affine, G2Projective}; | ||
use crate::scalar::Scalar; | ||
|
||
impl Serialize for Scalar { | ||
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> { | ||
serdect::array::serialize_hex_lower_or_bin(&self.to_bytes(), s) | ||
} | ||
} | ||
|
||
impl<'d> Deserialize<'d> for Scalar { | ||
fn deserialize<D: Deserializer<'d>>(d: D) -> Result<Self, D::Error> { | ||
let mut byte_array = [0; 32]; | ||
|
||
serdect::array::deserialize_hex_or_bin(&mut byte_array, d)?; | ||
|
||
Option::from(Scalar::from_bytes(&byte_array)) | ||
.ok_or_else(|| D::Error::custom("Could not decode scalar")) | ||
} | ||
} | ||
|
||
impl Serialize for G1Affine { | ||
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> { | ||
serdect::array::serialize_hex_lower_or_bin(&self.to_compressed(), s) | ||
} | ||
} | ||
|
||
impl<'d> Deserialize<'d> for G1Affine { | ||
fn deserialize<D: Deserializer<'d>>(d: D) -> Result<Self, D::Error> { | ||
let mut byte_array = [0; 48]; | ||
|
||
serdect::array::deserialize_hex_or_bin(&mut byte_array, d)?; | ||
|
||
Option::from(G1Affine::from_compressed(&byte_array)) | ||
.ok_or_else(|| D::Error::custom("Could not decode compressed group element")) | ||
} | ||
} | ||
|
||
impl Serialize for G2Affine { | ||
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> { | ||
serdect::array::serialize_hex_lower_or_bin(&self.to_compressed(), s) | ||
} | ||
} | ||
|
||
impl<'d> Deserialize<'d> for G2Affine { | ||
fn deserialize<D: Deserializer<'d>>(d: D) -> Result<Self, D::Error> { | ||
let mut byte_array = [0; 96]; | ||
|
||
serdect::array::deserialize_hex_or_bin(&mut byte_array, d)?; | ||
|
||
Option::from(G2Affine::from_compressed(&byte_array)) | ||
.ok_or_else(|| D::Error::custom("Could not decode compressed group element")) | ||
} | ||
} | ||
|
||
impl Serialize for G1Projective { | ||
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> { | ||
self.to_affine().serialize(s) | ||
} | ||
} | ||
|
||
impl<'d> Deserialize<'d> for G1Projective { | ||
fn deserialize<D: Deserializer<'d>>(d: D) -> Result<Self, D::Error> { | ||
Ok(G1Affine::deserialize(d)?.into()) | ||
} | ||
} | ||
|
||
impl Serialize for G2Projective { | ||
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> { | ||
self.to_affine().serialize(s) | ||
} | ||
} | ||
|
||
impl<'d> Deserialize<'d> for G2Projective { | ||
fn deserialize<D: Deserializer<'d>>(d: D) -> Result<Self, D::Error> { | ||
Ok(G2Affine::deserialize(d)?.into()) | ||
} | ||
} | ||
|
||
#[test] | ||
fn serde_json_scalar_roundtrip() { | ||
let serialized = serde_json::to_string(&Scalar::zero()).unwrap(); | ||
|
||
assert_eq!( | ||
serialized, | ||
"\"0000000000000000000000000000000000000000000000000000000000000000\"" | ||
); | ||
|
||
let deserialized: Scalar = serde_json::from_str(&serialized).unwrap(); | ||
|
||
assert_eq!(deserialized, Scalar::zero()); | ||
} | ||
|
||
#[test] | ||
fn serde_json_g1_roundtrip() { | ||
let serialized = serde_json::to_string(&G1Affine::generator()).unwrap(); | ||
|
||
assert_eq!( | ||
serialized, | ||
"\"97f1d3a73197d7942695638c4fa9ac0fc3688c4f9774b905a14e3a3f171bac586c55e83ff97a1aeffb3af00adb22c6bb\"" | ||
); | ||
|
||
let deserialized: G1Affine = serde_json::from_str(&serialized).unwrap(); | ||
|
||
assert_eq!(deserialized, G1Affine::generator()); | ||
} | ||
|
||
#[test] | ||
fn serde_json_g2_roundtrip() { | ||
let serialized = serde_json::to_string(&G2Affine::generator()).unwrap(); | ||
|
||
assert_eq!( | ||
serialized, | ||
"\"93e02b6052719f607dacd3a088274f65596bd0d09920b61ab5da61bbdc7f5049334cf11213945d57e5ac7d055d042b7e024aa2b2f08f0a91260805272dc51051c6e47ad4fa403b02b4510b647ae3d1770bac0326a805bbefd48056c8c121bdb8\"" | ||
); | ||
|
||
let deserialized: G2Affine = serde_json::from_str(&serialized).unwrap(); | ||
|
||
assert_eq!(deserialized, G2Affine::generator()); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One important caveat on
serdect
is we revised the format in the v0.3.0-pre.0 prereleases, usingserialize_bytes
/deserialize_bytes
which unfortunately adds a length header in the binary encodings even when using fixed-sized arrays but provides more compact serialization on formats like MessagePack which are more likely to be constant-time-ish, which is an unfortunate tradeoff where we opted for broader constant time assuredness over a minimally compact encoding.Barring any complaints about the length header, we just need to fix a bug/regression on v0.3.0 in the slice encoder and we can ship a final release. I would probably suggest using that (or convincing us to roll back) to avoid future breakages in the encoding format.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When would you expect to ship v0.3.0?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This regression needs to be fixed first: RustCrypto/formats#1322
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given this regression has already been fixed (and relevant
serdect
release published), is there a chance to get this PR moving again?