Skip to content
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

Implement gate-featured serde for exported structures #38

Closed
wants to merge 9 commits into from

Conversation

CPerezz
Copy link

@CPerezz CPerezz commented Apr 18, 2020

A few weeks ago, I needed serde implementations for the types that this library exports, so I coded them on our fork: https://github.com/dusk-network/bls12_381

Then I saw the suggestion raised in #35 by @hdevalence and thought that it would be nice to convert our impl to a feature-gated behind a serde feature.

So I modified our impl according to the gate-feature suggestion and made the PR.

Adds serde impl for:

  • G1Affine
  • G2Affine
  • G2Prepared which then required Fp, Fp2 & Fp6 serde impls.

All the implementations will make the deserialization fail if any of the items is not canonically-encoded.

Appart from that, I think that implement Write and Read for these types would be also nice, since sometimes, you just want to move the point structures as bytes but without doing any compresions/decompressions, having them "serialized" as bytes (which would be much more performant). See: dusk-network/plonk#190 where a Proof takes a considerable amount of time to be deserialized just because we call decompression for each point we store on it computing the security checks.

I added the serde feature to the default features group, but feel free to remove it from there since Idk if everyone will want the feature enabled by default.

The serde implementation sets the `Fp` as 48 bytes,
then encodes it and does the decoding and decompression
on the deserialization.

The deserialization will fail if the `Fp` was encoded
incorrectly or the bytes are less than 48.

Added tests for G1Affine roundtrip de/serialization.

We implement this serde support for `Fp` since it will
be necessary so later is easier for us to de/serialize
`G2Prepared` which does not carry any `to_bytes` impl.
The serde implementation sets the `Fp2` as 2 - 48 bytes
`Fp`s and then encodes them and does the decoding and decompression
on the deserialization.

The deserialization will fail if any of the `Fp`s was encoded
incorrectly or the bytes are less than 48 for each of them.

Added tests for roundtrip de/serialization.

We implement this serde support for `Fp2` since it will
be necessary so later is easier for us to de/serialize
`G2Prepared` which does not carry any `to_bytes` impl.
The serde implementation sets the `Fp6` struct as a combination
of 3 `Fp2` structs de/serializing them.

The deserialization will fail if any of the `Fp2`s were encoded
incorrectly.

Added tests for roundtrip de/serialization.

Implement Serde for Fp2

We implement this serde support for `Fp6` since it will
be necessary so later is easier for us to de/serialize
`G2Prepared` which does not carry any `to_bytes` impl.
- The serialization is done for `G2Prepared`
as for a struct which fields have Serde implemented.

- The `subtle::Choice` is encoded as an `u8` and built
from it later on the deserialization.

- Added roundtrip tests for G2Prepared.
The implementation compresses the point to 48 bytes,
then encodes it and does the decoding and decompression
on the deserialization.

Added tests for G1Affine roundtrip de/serialization.
- The serialization compresses the `G2Affine` point to
96 bytes which are then serialized.

- The deserialization then, deserializes the 96 bytes
and tries to re-build the point checking it's correctness.
If the point is not cannonically encoded or not valid the
deserialization process will fail.

- Added roundtrip tests for G2Affine.
@narodnik
Copy link

narodnik commented Jun 1, 2020

You should add Scalar serialization as well

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants