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

Add signature serialization function #1378

Merged
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
56 changes: 15 additions & 41 deletions types/src/certificate.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Provides two types of cerrtificates and their accumulators.

use crate::data::serialize_signature;
use crate::vote::ViewSyncData;
use crate::{
data::{fake_commitment, LeafType},
Expand All @@ -18,12 +19,16 @@ use std::marker::PhantomData;
use std::{collections::BTreeMap, fmt::Debug, ops::Deref};

// NOTE Sishan: For signature aggregation
use jf_primitives::signatures::{AggregateableSignatureSchemes, SignatureScheme};
use hotshot_primitives::quorum_certificate::{BitvectorQuorumCertificate, QuorumCertificateValidation, StakeTableEntry};
use jf_primitives::signatures::bls_over_bn254::{BLSOverBN254CurveSignatureScheme, KeyPair as QCKeyPair, VerKey};
use bincode::Options;
use hotshot_utils::bincode::bincode_opts;
use bitvec::prelude::*;
use hotshot_primitives::quorum_certificate::{
BitvectorQuorumCertificate, QuorumCertificateValidation, StakeTableEntry,
};
use hotshot_utils::bincode::bincode_opts;
use jf_primitives::signatures::bls_over_bn254::{
BLSOverBN254CurveSignatureScheme, KeyPair as QCKeyPair, VerKey,
};
use jf_primitives::signatures::{AggregateableSignatureSchemes, SignatureScheme};

/// A `DACertificate` is a threshold signature that some data is available.
/// It is signed by the members of the DA committee, not the entire network. It is used
Expand All @@ -38,7 +43,6 @@ pub struct DACertificate<TYPES: NodeType> {
/// committment to the block
pub block_commitment: Commitment<TYPES::BlockType>,


/// The list of signatures establishing the validity of this Quorum Certifcate
///
/// This is a mapping of the byte encoded public keys provided by the [`crate::traits::node_implementation::NodeImplementation`], to
Expand Down Expand Up @@ -195,43 +199,13 @@ impl<TYPES: NodeType, LEAF: LeafType<NodeType = TYPES>> Committable
for QuorumCertificate<TYPES, LEAF>
{
fn commit(&self) -> Commitment<Self> {
let mut builder = commit::RawCommitmentBuilder::new("Quorum Certificate Commitment");

builder = builder
.field("Leaf commitment", self.leaf_commitment)
.u64_field("View number", *self.view_number.deref());

let signatures: Option<
(<BLSOverBN254CurveSignatureScheme as SignatureScheme>::Signature,
<BitvectorQuorumCertificate<BLSOverBN254CurveSignatureScheme> as
QuorumCertificateValidation<BLSOverBN254CurveSignatureScheme>>::Proof)> = match self.signatures.clone() {
QCYesNoSignature::Yes(signatures) => {
builder = builder.var_size_field("QC Type", "Yes".as_bytes());
Some(signatures)
}
QCYesNoSignature::No(signatures) => {
builder = builder.var_size_field("QC Type", "No".as_bytes());
Some(signatures)
}
QCYesNoSignature::Genesis() => {
builder = builder.var_size_field("QC Type", "Yes".as_bytes());
None
}
};
if signatures != None {
let (sig, proof) = signatures.unwrap();
let proof_bytes = bincode_opts()
.serialize(&proof.as_bitslice())
.expect("This serialization shouldn't be able to fail");
builder = builder.var_size_field("bitvec proof", proof_bytes.as_slice());
let sig_bytes = bincode_opts()
.serialize(&sig)
.expect("This serialization shouldn't be able to fail");
builder = builder.var_size_field("aggregated signature", sig_bytes.as_slice());
}
let signatures_bytes = serialize_signature(&self.signatures);

builder
.u64_field("Is genesis", self.is_genesis.into())
commit::RawCommitmentBuilder::new("Quorum Certificate Commitment")
.field("leaf commitment", self.leaf_commitment)
.u64_field("view number", *self.view_number.deref())
.constant_str("justify_qc signatures")
.var_size_bytes(&signatures_bytes)
.finalize()
}

Expand Down
112 changes: 43 additions & 69 deletions types/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! `HotShot`'s version of a block, and proposals, messages upon which to reach the consensus.

use crate::{
certificate::{DACertificate, QuorumCertificate, YesNoSignature, QCYesNoSignature},
certificate::{DACertificate, QCYesNoSignature, QuorumCertificate, YesNoSignature},
constants::genesis_proposer_id,
traits::{
consensus_type::validating_consensus::ValidatingConsensusType,
Expand All @@ -29,13 +29,17 @@ use std::{
hash::Hash,
};
// NOTE Sishan: For signature aggregation
use jf_primitives::signatures::{AggregateableSignatureSchemes, SignatureScheme};
use hotshot_primitives::quorum_certificate::{BitvectorQuorumCertificate, QuorumCertificateValidation, StakeTableEntry};
use jf_primitives::signatures::bls_over_bn254::{BLSOverBN254CurveSignatureScheme, KeyPair as QCKeyPair, VerKey};
use bincode::Options;
use hotshot_utils::bincode::bincode_opts;
use bitvec::prelude::*;
use bit_vec::BitVec;
use bitvec::prelude::*;
use hotshot_primitives::quorum_certificate::{
BitvectorQuorumCertificate, QuorumCertificateValidation, StakeTableEntry,
};
use hotshot_utils::bincode::bincode_opts;
use jf_primitives::signatures::bls_over_bn254::{
BLSOverBN254CurveSignatureScheme, KeyPair as QCKeyPair, VerKey,
};
use jf_primitives::signatures::{AggregateableSignatureSchemes, SignatureScheme};

/// Type-safe wrapper around `u64` so we know the thing we're talking about is a view number.
#[derive(
Expand Down Expand Up @@ -763,13 +767,12 @@ pub fn random_commitment<S: Committable>(rng: &mut dyn rand::RngCore) -> Commitm
.finalize()
}

impl<TYPES: NodeType> Committable for ValidatingLeaf<TYPES> {
fn commit(&self) -> commit::Commitment<Self> {
let mut signatures_bytes = vec![];
let signatures: Option<
(<BLSOverBN254CurveSignatureScheme as SignatureScheme>::Signature,
<BitvectorQuorumCertificate<BLSOverBN254CurveSignatureScheme> as
QuorumCertificateValidation<BLSOverBN254CurveSignatureScheme>>::Proof)> = match &self.justify_qc.signatures {
pub fn serialize_signature(signature: &QCYesNoSignature) -> Vec<u8> {
let mut signatures_bytes = vec![];
let signatures: Option<
(<BLSOverBN254CurveSignatureScheme as SignatureScheme>::Signature,
<BitvectorQuorumCertificate<BLSOverBN254CurveSignatureScheme> as
QuorumCertificateValidation<BLSOverBN254CurveSignatureScheme>>::Proof)> = match &signature {
QCYesNoSignature::Yes(signatures) => {
signatures_bytes.extend("Yes".as_bytes());
Some(signatures.clone())
Expand All @@ -782,26 +785,31 @@ impl<TYPES: NodeType> Committable for ValidatingLeaf<TYPES> {
None
}
};
// TODO (Keyao) this is the same for both leaf types. It's better to extract the common
// part.
if signatures != None {
let (sig, proof) = signatures.unwrap();
let proof_bytes = bincode_opts()
.serialize(&proof.as_bitslice())
.expect("This serialization shouldn't be able to fail");
signatures_bytes.extend("bitvec proof".as_bytes());
signatures_bytes.extend(proof_bytes.as_slice());
let sig_bytes = bincode_opts()
.serialize(&sig)
.expect("This serialization shouldn't be able to fail");
signatures_bytes.extend("aggregated signature".as_bytes());
signatures_bytes.extend(sig_bytes.as_slice());
} else {
signatures_bytes.extend("genesis".as_bytes());
}
if signatures != None {
let (sig, proof) = signatures.unwrap();
let proof_bytes = bincode_opts()
.serialize(&proof.as_bitslice())
.expect("This serialization shouldn't be able to fail");
signatures_bytes.extend("bitvec proof".as_bytes());
signatures_bytes.extend(proof_bytes.as_slice());
let sig_bytes = bincode_opts()
.serialize(&sig)
.expect("This serialization shouldn't be able to fail");
signatures_bytes.extend("aggregated signature".as_bytes());
signatures_bytes.extend(sig_bytes.as_slice());
} else {
signatures_bytes.extend("genesis".as_bytes());
}

commit::RawCommitmentBuilder::new("Leaf Comm")
.u64_field("view_number", *self.view_number)
signatures_bytes
}

impl<TYPES: NodeType> Committable for ValidatingLeaf<TYPES> {
fn commit(&self) -> commit::Commitment<Self> {
let signatures_bytes = serialize_signature(&self.justify_qc.signatures);

commit::RawCommitmentBuilder::new("leaf commitment")
.u64_field("view number", *self.view_number)
.u64_field("height", self.height)
.field("parent Leaf commitment", self.parent_commitment)
.field("block commitment", self.deltas.commit())
Expand Down Expand Up @@ -830,45 +838,11 @@ impl<TYPES: NodeType> Committable for SequencingLeaf<TYPES> {
Either::Left(block) => block.commit(),
Either::Right(commitment) => *commitment,
};
let mut signatures_bytes = vec![];

let signatures: Option<
(<BLSOverBN254CurveSignatureScheme as SignatureScheme>::Signature,
<BitvectorQuorumCertificate<BLSOverBN254CurveSignatureScheme> as
QuorumCertificateValidation<BLSOverBN254CurveSignatureScheme>>::Proof)> = match &self.justify_qc.signatures {
QCYesNoSignature::Yes(signatures) => {
signatures_bytes.extend("Yes".as_bytes());
Some(signatures.clone())
}
QCYesNoSignature::No(signatures) => {
signatures_bytes.extend("No".as_bytes());
Some(signatures.clone())
}
QCYesNoSignature::Genesis() => {
None
}
};
// TODO (Keyao) this is the same for both leaf types. It's better to extract the common
// part.
if signatures != None {
let (sig, proof) = signatures.unwrap();
let proof_bytes = bincode_opts()
.serialize(&proof.as_bitslice())
.expect("This serialization shouldn't be able to fail");
signatures_bytes.extend("bitvec proof".as_bytes());
signatures_bytes.extend(proof_bytes.as_slice());
let sig_bytes = bincode_opts()
.serialize(&sig)
.expect("This serialization shouldn't be able to fail");
signatures_bytes.extend("aggregated signature".as_bytes());
signatures_bytes.extend(sig_bytes.as_slice());
} else {
signatures_bytes.extend("genesis".as_bytes());
}

let signatures_bytes = serialize_signature(&self.justify_qc.signatures);

commit::RawCommitmentBuilder::new("Leaf Comm")
.u64_field("view_number", *self.view_number)
commit::RawCommitmentBuilder::new("leaf commitment")
.u64_field("view number", *self.view_number)
.u64_field("height", self.height)
.field("parent Leaf commitment", self.parent_commitment)
.field("block commitment", block_commitment)
Expand Down
Loading