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

Parallelize signature deserialization #2258

Merged
merged 4 commits into from
Dec 19, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions console/account/src/compute_key/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ mod bytes;
mod from_bits;
mod serialize;
mod size_in_bits;
mod size_in_bytes;
mod to_address;
mod to_bits;
mod to_fields;
Expand Down
23 changes: 23 additions & 0 deletions console/account/src/compute_key/size_in_bytes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (C) 2019-2023 Aleo Systems Inc.
// This file is part of the snarkVM library.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use super::*;

impl<N: Network> SizeInBytes for ComputeKey<N> {
/// Returns the compute key size in bytes.
#[inline]
fn size_in_bytes() -> usize {
Group::<N>::size_in_bytes() + Group::<N>::size_in_bytes()
}
}
1 change: 1 addition & 0 deletions console/account/src/signature/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ mod from_bits;
mod parse;
mod serialize;
mod size_in_bits;
mod size_in_bytes;
mod to_bits;
mod to_fields;
mod verify;
Expand Down
23 changes: 23 additions & 0 deletions console/account/src/signature/size_in_bytes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (C) 2019-2023 Aleo Systems Inc.
// This file is part of the snarkVM library.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use super::*;

impl<N: Network> SizeInBytes for Signature<N> {
/// Returns the signature size in bytes.
#[inline]
fn size_in_bytes() -> usize {
Scalar::<N>::size_in_bytes() + Scalar::<N>::size_in_bytes() + ComputeKey::<N>::size_in_bytes()
}
}
1 change: 1 addition & 0 deletions console/network/environment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub mod prelude {
pub use snarkvm_curves::{AffineCurve, MontgomeryParameters, ProjectiveCurve, TwistedEdwardsParameters};
pub use snarkvm_fields::{Field as _, PrimeField as _, SquareRootField as _, Zero as _};
pub use snarkvm_utilities::{
cfg_chunks,
cfg_find,
cfg_find_map,
cfg_into_iter,
Expand Down
6 changes: 5 additions & 1 deletion ledger/narwhal/batch-certificate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ license = "Apache-2.0"
edition = "2021"

[features]
default = [ ]
default = [ "rayon" ]
serial = [ "console/serial" ]
wasm = [ "console/wasm" ]
test-helpers = [ "narwhal-batch-header/test-helpers" ]
Expand All @@ -48,6 +48,10 @@ version = "=0.16.13"
version = "2.0"
features = [ "serde" ]

[dependencies.rayon]
version = "1"
optional = true

[dependencies.serde_json]
version = "1.0"
features = [ "preserve_order" ]
Expand Down
13 changes: 6 additions & 7 deletions ledger/narwhal/batch-certificate/src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,13 @@ impl<N: Network> FromBytes for BatchCertificate<N> {
Self::MAX_SIGNATURES
)));
}
// Read the signature bytes.
let mut signature_bytes = vec![0u8; num_signatures as usize * Signature::<N>::size_in_bytes()];
reader.read_exact(&mut signature_bytes)?;
// Read the signatures.
let mut signatures = IndexSet::with_capacity(num_signatures as usize);
for _ in 0..num_signatures {
// Read the signature.
let signature = Signature::read_le(&mut reader)?;
// Insert the signature.
signatures.insert(signature);
}
let signatures = cfg_chunks!(signature_bytes, Signature::<N>::size_in_bytes())
.map(Signature::read_le)
.collect::<Result<IndexSet<_>, _>>()?;
howardwu marked this conversation as resolved.
Show resolved Hide resolved
// Return the batch certificate.
Self::from(batch_header, signatures).map_err(error)
} else {
Expand Down
3 changes: 3 additions & 0 deletions ledger/narwhal/batch-certificate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ use narwhal_transmission_id::TransmissionID;
use core::hash::{Hash, Hasher};
use indexmap::{IndexMap, IndexSet};

#[cfg(not(feature = "serial"))]
use rayon::prelude::*;

#[derive(Clone)]
pub enum BatchCertificate<N: Network> {
// TODO (howardwu): For mainnet - Delete V1 and switch everyone to V2 as the default.
Expand Down