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

Made tagged_blob be able to accept constants #72

Merged
merged 3 commits into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- Derive `Debug`, `Snafu` on `enum TaggedBlobError`
- Updated `tagged-base64` reference url to reflect the Espresso Systems name change
- Add `HashToGroup` support for both SW and TE curves
- `#[tagged_blob(...)]` macro now supports `const` variables in addition to string literals

### Bugfixes

Expand Down
1 change: 1 addition & 0 deletions plonk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ derivative = { version = "2", features = ["use_core"] }
num-bigint = { version = "0.4", default-features = false}
rand_chacha = { version = "0.3.1" }
sha3 = "^0.10"
espresso-systems-common = { git = "https://github.com/espressosystems/espresso-systems-common", tag = "0.1.1" }


[dependencies.ark-poly-commit]
Expand Down
5 changes: 3 additions & 2 deletions plonk/src/proof_system/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use ark_std::{
vec,
vec::Vec,
};
use espresso_systems_common::jellyfish as tag;
use jf_rescue::RescueParameter;
use jf_utils::{field_switching, fq_to_fr, fr_to_fq, tagged_blob};

Expand All @@ -57,7 +58,7 @@ pub(crate) type CommitKey<'a, E> = Powers<'a, E>;
pub type OpenKey<E> = VerifierKey<E>;

/// A Plonk SNARK proof.
#[tagged_blob("PROOF")]
#[tagged_blob(tag::PROOF)]
#[derive(Debug, Clone, PartialEq, Eq, CanonicalSerialize, CanonicalDeserialize, Derivative)]
#[derivative(Hash(bound = "E:PairingEngine"))]
pub struct Proof<E: PairingEngine> {
Expand Down Expand Up @@ -232,7 +233,7 @@ pub struct PlookupProof<E: PairingEngine> {
}

/// An aggregated SNARK proof that batchly proving multiple instances.
#[tagged_blob("BATCHPROOF")]
#[tagged_blob(tag::BATCHPROOF)]
#[derive(Debug, Clone, PartialEq, Eq, CanonicalSerialize, CanonicalDeserialize, Derivative)]
#[derivative(Hash(bound = "E:PairingEngine"))]
pub struct BatchProof<E: PairingEngine> {
Expand Down
1 change: 1 addition & 0 deletions primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ derivative = { version = "2", features = ["use_core"] }
rand_chacha = { version = "0.3.1", default-features = false }
sha2 = { version = "0.10.1", default-features = false }
digest = { version = "0.10.1", default-features = false }
espresso-systems-common = { git = "https://github.com/espressosystems/espresso-systems-common", tag = "0.1.1" }

[dev-dependencies]
rand_chacha = "^0.3"
Expand Down
5 changes: 3 additions & 2 deletions primitives/src/merkle_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use ark_std::{
vec::Vec,
};
use core::{convert::TryFrom, fmt::Debug};
use espresso_systems_common::jellyfish as tag;
use jf_rescue::{Permutation, RescueParameter};
use jf_utils::tagged_blob;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -189,7 +190,7 @@ impl<F: PrimeField> MerklePath<F> {
}

/// Represents the value for a node in the merkle tree.
#[tagged_blob("NODE")]
#[tagged_blob(tag::NODE)]
#[derive(
Clone, Debug, PartialEq, Eq, Hash, Default, CanonicalSerialize, CanonicalDeserialize, Copy,
)]
Expand Down Expand Up @@ -817,7 +818,7 @@ where
}

/// Data struct for a merkle leaf.
#[tagged_blob("LEAF")]
#[tagged_blob(tag::LEAF)]
#[derive(
Clone, Debug, PartialEq, Eq, Hash, Default, CanonicalSerialize, CanonicalDeserialize, Copy,
)]
Expand Down
7 changes: 4 additions & 3 deletions primitives/src/signatures/bls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use ark_std::{
One, UniformRand,
};
use core::marker::PhantomData;
use espresso_systems_common::jellyfish as tag;
use jf_utils::{multi_pairing, tagged_blob};

/// BLS signature scheme.
Expand All @@ -28,14 +29,14 @@ pub struct BLSSignatureScheme<P: Bls12Parameters> {
}

/// BLS public verification key
#[tagged_blob("BLSVERKEY")]
#[tagged_blob(tag::BLSVERKEY)]
#[derive(CanonicalSerialize, CanonicalDeserialize, Derivative)]
#[derivative(Clone(bound = "P: Bls12Parameters"))]
#[derivative(Default(bound = "P: Bls12Parameters"))]
pub struct BLSVerKey<P: Bls12Parameters>(pub(crate) GroupAffine<P::G2Parameters>);

/// Signing key for BLS signature.
#[tagged_blob("BLSSIGNINGKEY")]
#[tagged_blob(tag::BLSSIGNINGKEY)]
#[derive(CanonicalSerialize, CanonicalDeserialize, Derivative)]
#[derivative(Clone(bound = "P: Bls12Parameters"))]
#[derivative(Default(bound = "P: Bls12Parameters"))]
Expand All @@ -44,7 +45,7 @@ pub struct BLSSignKey<P: Bls12Parameters>(
);

/// Signing key for BLS signature.
#[tagged_blob("BLSSIG")]
#[tagged_blob(tag::BLSSIG)]
#[derive(CanonicalSerialize, CanonicalDeserialize, Derivative)]
#[derivative(Clone(bound = "P: Bls12Parameters"))]
#[derivative(Default(bound = "P: Bls12Parameters"))]
Expand Down
7 changes: 4 additions & 3 deletions primitives/src/signatures/schnorr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use ark_std::{
string::ToString,
vec,
};
use espresso_systems_common::jellyfish as tag;
use jf_rescue::{Permutation, RescueParameter};
use jf_utils::{fq_to_fr, fq_to_fr_with_mask, fr_to_fq, tagged_blob};
use zeroize::Zeroize;
Expand Down Expand Up @@ -120,7 +121,7 @@ impl<F: PrimeField> SignKey<F> {

/// Signature public verification key
// derive zeroize here so that keypair can be zeroized
#[tagged_blob("SCHNORRVERKEY")]
#[tagged_blob(tag::SCHNORRVERKEY)]
#[derive(Clone, CanonicalSerialize, CanonicalDeserialize, Derivative)]
#[derivative(Debug(bound = "P: Parameters"))]
#[derivative(Default(bound = "P: Parameters"))]
Expand Down Expand Up @@ -186,7 +187,7 @@ impl<P: Parameters + Clone> VerKey<P> {

/// Signature secret key pair used to sign messages
// make sure sk can be zeroized
#[tagged_blob("SIGNKEYPAIR")]
#[tagged_blob(tag::SIGNKEYPAIR)]
#[derive(Clone, Default, CanonicalSerialize, CanonicalDeserialize, PartialEq, Derivative)]
#[derivative(Debug(bound = "P: Parameters"))]
pub struct KeyPair<P>
Expand All @@ -202,7 +203,7 @@ where
// =====================================================

/// The signature of Schnorr signature scheme
#[tagged_blob("SIG")]
#[tagged_blob(tag::SIG)]
#[derive(Clone, Eq, CanonicalSerialize, CanonicalDeserialize, Derivative)]
#[derivative(Debug(bound = "P: Parameters"))]
#[derivative(Default(bound = "P: Parameters"))]
Expand Down
2 changes: 2 additions & 0 deletions utilities/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ ark-ed-on-bls12-381-bandersnatch = { git = "https://github.com/arkworks-rs/curve
ark-bn254 = { version = "0.3.0", default-features = false, features = ["curve"] }
ark-bls12-377 = { git = "https://github.com/arkworks-rs/curves", rev = "677b4ae751a274037880ede86e9b6f30f62635af" }
ark-bls12-381 = { version = "0.3.0", default-features = false, features = ["curve"] }
ark-serialize = { version = "0.3.0", default-features = false, features = ["derive"] }
serde_json = "1.0"

[features]
std = []
2 changes: 1 addition & 1 deletion utilities/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// You should have received a copy of the MIT License
// along with the Jellyfish library. If not, see <https://mit-license.org/>.

#![no_std]
#![cfg_attr(not(test), no_std)]

mod conversion;
mod macros;
Expand Down
33 changes: 33 additions & 0 deletions utilities/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,39 @@ pub mod tagged_blob {
let bytes = deserialize_with_tag(T::tag().as_str(), deserializer)?;
Ok((bytes, Default::default()))
}

#[cfg(test)]
mod test {
use crate as jf_utils;
use ark_serialize::*;
use jf_utils::tagged_blob;
use std::vec::Vec;

#[tagged_blob("A")]
#[derive(Debug, Clone, PartialEq, Eq, CanonicalDeserialize, CanonicalSerialize)]
pub struct A(Vec<u8>);

#[test]
fn test_tagged_blob_static_str() {
let a = A(Vec::new());
let str = serde_json::to_string(&a).unwrap();
assert_eq!(str, r#""A~AAAAAAAAAABr""#);
Copy link
Contributor

@alxiong alxiong Jun 21, 2022

Choose a reason for hiding this comment

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

question: can you help me recall why this would print to this string? Why AAAAAAAAAABr ?

cc @jbearer

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe AAAAAAAAAAB is the length of the buffer (0u64) and r is the checksum

}

mod tags {
pub const B: &str = "B";
}
#[tagged_blob(tags::B)]
#[derive(Debug, Clone, PartialEq, Eq, CanonicalDeserialize, CanonicalSerialize)]
pub struct B(Vec<u8>);

#[test]
fn test_tagged_blob_const_str() {
let b = B(Vec::new());
let str = serde_json::to_string(&b).unwrap();
assert_eq!(str, r#""B~AAAAAAAAAADg""#);
}
}
}

/// Serializers for finite field elements.
Expand Down
7 changes: 4 additions & 3 deletions utilities_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ extern crate proc_macro;
use ark_std::format;
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, AttributeArgs, Item, NestedMeta};
use syn::{parse_macro_input, AttributeArgs, Item, Meta, NestedMeta};

/// Derive serdes for a type which serializes as a binary blob.
///
Expand Down Expand Up @@ -89,9 +89,10 @@ pub fn tagged_blob(args: TokenStream, input: TokenStream) -> TokenStream {
_ => panic!("expected struct or enum"),
};
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
let tag = match args.as_slice() {
let tag: &dyn quote::ToTokens = match args.as_slice() {
[NestedMeta::Lit(tag)] => tag,
_ => panic!("tagged_blob takes one argument, the tag, as a string literal"),
[NestedMeta::Meta(Meta::Path(path))] => path,
x => panic!("tagged_blob takes one argument, the tag, as a string literal or expression, found {:?}", x),
};
let serde_str = format!("jf_utils::TaggedBlob<{}>", quote!(#name #ty_generics));
let output = quote! {
Expand Down