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

Refactor such that we can derive Encodable and Decodable for crypto crates APIs #4512

Merged
merged 3 commits into from
Mar 27, 2024

Conversation

joschisan
Copy link
Contributor

@joschisan joschisan commented Mar 9, 2024

Previously we derived encodable and decodable by hand for all the public types of tpe. However, with new crypto also built on bls12_381 like lightning-ng and ecash-ng that has more complex public structs this becomes increasingly cumbersome and repetitious. Hence, we refactor the code to be able to derive Encodable and Decodable for all future crypto crates built on bls12_381.

In a next step we want to do the same with serde Serialize and Deserialize by upstreaming an implementation to bls12_381 such that we can derive the traits as well. Notice that we duplicate the serde implementation for Scalar, G1, G2 as we copy it from tbs to core were we need it as well. We leave it in tbs though as the copy in core is supposed to be removed after we upstreamed it.

@joschisan joschisan marked this pull request as ready for review March 9, 2024 08:24
@joschisan joschisan requested review from a team as code owners March 9, 2024 08:24

let message = b"Hello world!";
let ciphertext = pk.encrypt(message);
let decryption_share = sks.secret_key_share(0).decrypt_share(&ciphertext).unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: use expect instead of unwrap

let point = G1Affine::from_compressed(&bytes);

if point.is_some().unwrap_u8() == 1 {
Ok(point.unwrap())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: use expect

let scalar = Scalar::from_bytes(&byte_array);
// FIXME: probably safe with public data, but doesn't look nice
if scalar.is_some().into() {
Ok(scalar.unwrap())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: expect

m1sterc001guy
m1sterc001guy previously approved these changes Mar 10, 2024
Copy link
Contributor

@m1sterc001guy m1sterc001guy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Someone with more context than me should also probably look, but LGTM

@bradleystachurski bradleystachurski self-requested a review March 11, 2024 16:43
@dpc
Copy link
Contributor

dpc commented Mar 15, 2024

@elsirion

Copy link
Contributor

@dpc dpc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On a lower level (mechanicaly what's goin on) it look OK, but on the higher level I'm lost.

The legacy vs new encoding seems like a complication, but for a good reason.

I also don't understand what's the status of "upstream serde support" or what exactly it means here.

@elsirion is probably best person to review

@elsirion
Copy link
Contributor

Need to take a look, it's less of a priority than other things we might want to get into 0.3 since it's mostly useful for LN and e-cash NG, which are both longer-term research projects.

@elsirion elsirion self-requested a review March 17, 2024 09:36
@@ -31,8 32,6 @@ serde_json = { workspace = true }
strum = { workspace = true }
strum_macros = { workspace = true }
hex = { version = "0.4.3", features = [ "serde"] }
sha3 = "0.10.8"
tbs = { package = "fedimint-tbs", version = "0.3.0-alpha", path = "../crypto/tbs" }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uff, that was dirty anyway. Much better to just depend on the crypto primitive.

@@ -15,6 16,9 @@ use rand_chacha::ChaChaRng;
use serde::{Deserialize, Serialize};
use sha3::Digest;

// this is a legacy encoding that we need to keep for tbs; everywhere else we
// want to upstream serde support for bls12_381 such das non-human-readable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/such das/such that/?

fedimint-core/src/bls12_381_serde.rs Show resolved Hide resolved
fedimint-core/src/bls12_381_serde.rs Show resolved Hide resolved
fedimint-core/src/bls12_381_serde.rs Outdated Show resolved Hide resolved
fedimint-core/src/encoding/bls12_381.rs Outdated Show resolved Hide resolved
fedimint-core/src/encoding/bls12_381.rs Show resolved Hide resolved
fedimint-core/src/encoding/bls12_381.rs Show resolved Hide resolved
use crate::encoding::{Decodable, DecodeError, Encodable};
use crate::module::registry::ModuleDecoderRegistry;

impl Encodable for threshold_crypto::PublicKeySet {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we just derive all this now? Care would need to be taken not to break anything, but worth looking into imo (follow-up, not this PR).

dpc
dpc previously approved these changes Mar 27, 2024
@joschisan joschisan added this pull request to the merge queue Mar 27, 2024
Merged via the queue into fedimint:master with commit 9b61f79 Mar 27, 2024
21 checks passed
@joschisan joschisan deleted the encode_crypto branch March 27, 2024 21:10
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.

4 participants