Skip to content

Commit

Permalink
[crypto] Unify Narwhal and Sui Crypto (MystenLabs#2994)
Browse files Browse the repository at this point in the history
* Use narwhal library
* [crypto] Changes discussed in review (MystenLabs#3293)

Co-authored-by: François Garillot <[email protected]>
  • Loading branch information
punwai and huitseeker authored Jul 21, 2022
1 parent 6904576 commit 4e5bb18
Show file tree
Hide file tree
Showing 52 changed files with 1,491 additions and 1,449 deletions.
381 changes: 257 additions & 124 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion crates/sui-benchmark/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ sui-config = { path = "../sui-config" }
sui-types = { path = "../sui-types" }

move-core-types = { git = "https://github.com/move-language/move", rev = "7733658048a8bc80e9ba415b8c99aed9234eaa5f", features = ["address20"] }
narwhal-node = { git = "https://github.com/MystenLabs/narwhal", rev = "ae032178da474f74d5ec49f775a5a675460906aa", package = "node" }
narwhal-node = { git = "https://github.com/MystenLabs/narwhal", rev = "f50af9eb40130fb404a678e38d0671fc33f60318", package = "node" }
sui-quorum-driver = { path = "../sui-quorum-driver" }
sui-node = { path = "../sui-node" }
workspace-hack = { path = "../workspace-hack"}
Expand Down
23 changes: 13 additions & 10 deletions crates/sui-benchmark/src/benchmark/transaction_creator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use rayon::prelude::*;
use sui_config::NetworkConfig;
use sui_types::{
base_types::*,
crypto::{get_key_pair, AuthoritySignature, KeyPair, Signature},
crypto::{
get_key_pair, AuthoritySignature, KeyPair, KeypairTraits, PublicKeyBytes, Signature,
SuiAuthoritySignature,
},
messages::*,
object::Object,
SUI_FRAMEWORK_ADDRESS,
Expand Down Expand Up @@ -57,22 +60,22 @@ fn create_gas_object(object_id: ObjectID, owner: SuiAddress) -> Object {
fn make_cert(network_config: &NetworkConfig, tx: &Transaction) -> CertifiedTransaction {
// Make certificate
let committee = network_config.committee();
let mut certificate = CertifiedTransaction::new(committee.epoch(), tx.clone());
certificate.auth_sign_info.epoch = committee.epoch();
let mut sigs: Vec<(AuthorityName, AuthoritySignature)> = Vec::new();
// TODO: Why iterating from 0 to quorum_threshold??
for i in 0..committee.quorum_threshold() {
let secx = network_config
.validator_configs()
.get(i as usize)
.unwrap()
.key_pair();
let pubx = secx.public_key_bytes();
let sig = AuthoritySignature::new(&certificate.data, secx);
certificate
.auth_sign_info
.add_signature(sig, *pubx, &committee)
.unwrap();
let pubx: PublicKeyBytes = secx.public().into();
let sig = AuthoritySignature::new(&tx.data, secx);
sigs.push((pubx, sig));
}
let mut certificate =
CertifiedTransaction::new_with_signatures(committee.epoch(), tx.clone(), sigs, &committee)
.unwrap();
certificate.auth_sign_info.epoch = committee.epoch();
certificate
}

Expand Down Expand Up @@ -155,7 +158,7 @@ impl TransactionCreator {
validator_preparer: &mut ValidatorPreparer,
) -> Vec<(Transaction, CertifiedTransaction)> {
let (address, keypair) = if let Some(a) = sender {
(SuiAddress::from(a.public_key_bytes()), a.copy())
(a.public().into(), a.copy())
} else {
get_key_pair()
};
Expand Down
2 changes: 1 addition & 1 deletion crates/sui-benchmark/src/benchmark/validator_preparer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use sui_core::authority::*;
use sui_types::{
base_types::{SuiAddress, *},
committee::*,
crypto::{KeyPair, PublicKeyBytes},
crypto::{KeyPair, KeypairTraits, PublicKeyBytes},
gas_coin::GasCoin,
object::Object,
};
Expand Down
4 changes: 2 additions & 2 deletions crates/sui-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ multiaddr = "0.14.0"
once_cell = "1.11.0"
tracing = "0.1.35"

narwhal-config = { git = "https://github.com/MystenLabs/narwhal", rev = "ae032178da474f74d5ec49f775a5a675460906aa", package = "config" }
narwhal-crypto = { git = "https://github.com/MystenLabs/narwhal", rev = "ae032178da474f74d5ec49f775a5a675460906aa", package = "crypto" }
narwhal-config = { git = "https://github.com/MystenLabs/narwhal", rev = "f50af9eb40130fb404a678e38d0671fc33f60318", package = "config" }
narwhal-crypto = { git = "https://github.com/MystenLabs/narwhal", rev = "f50af9eb40130fb404a678e38d0671fc33f60318", package = "crypto" }
move-binary-format = { git = "https://github.com/move-language/move", rev = "7733658048a8bc80e9ba415b8c99aed9234eaa5f" }
move-package = { git = "https://github.com/move-language/move", rev = "7733658048a8bc80e9ba415b8c99aed9234eaa5f" }
move-core-types = { git = "https://github.com/move-language/move", rev = "7733658048a8bc80e9ba415b8c99aed9234eaa5f", features = ["address20"] }
Expand Down
13 changes: 8 additions & 5 deletions crates/sui-config/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ use std::{
path::{Path, PathBuf},
sync::Arc,
};
use sui_types::{base_types::encode_bytes_hex, crypto::get_key_pair_from_rng};
use sui_types::{
base_types::encode_bytes_hex,
crypto::{get_key_pair_from_rng, KeypairTraits, PublicKeyBytes},
};

pub struct ConfigBuilder<R = OsRng> {
rng: R,
Expand Down Expand Up @@ -88,7 +91,7 @@ impl<R: ::rand::RngCore + ::rand::CryptoRng> ConfigBuilder<R> {
.enumerate()
.map(|(i, validator)| {
let name = format!("validator-{i}");
let public_key = *validator.key_pair.public_key_bytes();
let public_key: PublicKeyBytes = validator.key_pair.public().into();
let stake = validator.stake;
let network_address = validator.network_address.clone();

Expand Down Expand Up @@ -127,17 +130,17 @@ impl<R: ::rand::RngCore + ::rand::CryptoRng> ConfigBuilder<R> {
let validator_configs = validators
.into_iter()
.map(|validator| {
let public_key = validator.key_pair.public_key_bytes();
let public_key: PublicKeyBytes = validator.key_pair.public().into();
let db_path = self
.config_directory
.join(AUTHORITIES_DB_NAME)
.join(encode_bytes_hex(public_key));
.join(encode_bytes_hex(&public_key));
let network_address = validator.network_address;
let consensus_address = validator.narwhal_consensus_address;
let consensus_db_path = self
.config_directory
.join(CONSENSUS_DB_NAME)
.join(encode_bytes_hex(public_key));
.join(encode_bytes_hex(&public_key));
let consensus_config = ConsensusConfig {
consensus_address,
consensus_db_path,
Expand Down
12 changes: 6 additions & 6 deletions crates/sui-config/src/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use sui_adapter::in_memory_storage::InMemoryStorage;
use sui_adapter::temporary_store::TemporaryStore;
use sui_types::base_types::ObjectID;
use sui_types::base_types::TransactionDigest;
use sui_types::crypto::PublicKey;
use sui_types::crypto::PublicKeyBytes;
use sui_types::gas::SuiGasStatus;
use sui_types::messages::CallArg;
Expand Down Expand Up @@ -60,16 +61,14 @@ impl Genesis {
)
}

pub fn narwhal_committee(
&self,
) -> narwhal_config::SharedCommittee<narwhal_crypto::ed25519::Ed25519PublicKey> {
pub fn narwhal_committee(&self) -> narwhal_config::SharedCommittee<PublicKey> {
let narwhal_committee = self
.validator_set
.iter()
.map(|validator| {
let name = validator
.public_key()
.make_narwhal_public_key()
.try_into()
.expect("Can't get narwhal public key");
let primary = narwhal_config::PrimaryAddresses {
primary_to_primary: validator.narwhal_primary_to_primary.clone(),
Expand Down Expand Up @@ -274,7 +273,7 @@ impl Builder {
onchain_validator.metadata.sui_address.to_vec(),
);
assert_eq!(
validator.public_key().to_vec(),
validator.public_key().as_ref().to_vec(),
onchain_validator.metadata.pubkey_bytes,
);
assert_eq!(validator.name().as_bytes(), onchain_validator.metadata.name);
Expand Down Expand Up @@ -514,6 +513,7 @@ const GENESIS_BUILDER_COMMITTEE_DIR: &str = "committee";
mod test {
use super::Builder;
use crate::{genesis_config::GenesisConfig, utils, ValidatorInfo};
use narwhal_crypto::traits::KeyPair;
use sui_types::crypto::get_key_pair_from_rng;

#[test]
Expand All @@ -539,7 +539,7 @@ mod test {
let key = get_key_pair_from_rng(&mut rand::rngs::OsRng).1;
let validator = ValidatorInfo {
name: "0".into(),
public_key: *key.public_key_bytes(),
public_key: key.public().into(),
stake: 1,
delegation: 0,
network_address: utils::new_network_address(),
Expand Down
7 changes: 4 additions & 3 deletions crates/sui-config/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::path::{Path, PathBuf};
use std::sync::Arc;
use sui_types::base_types::SuiAddress;
use sui_types::committee::StakeUnit;
use sui_types::crypto::KeypairTraits;
use sui_types::crypto::{KeyPair, PublicKeyBytes};

#[derive(Clone, Debug, Deserialize, Serialize)]
Expand Down Expand Up @@ -84,11 +85,11 @@ impl NodeConfig {
}

pub fn public_key(&self) -> PublicKeyBytes {
*self.key_pair.public_key_bytes()
self.key_pair.public().into()
}

pub fn sui_address(&self) -> SuiAddress {
SuiAddress::from(self.public_key())
(&self.public_key()).into()
}

pub fn db_path(&self) -> &Path {
Expand Down Expand Up @@ -156,7 +157,7 @@ impl ValidatorInfo {
}

pub fn sui_address(&self) -> SuiAddress {
SuiAddress::from(self.public_key())
(&self.public_key()).into()
}

pub fn public_key(&self) -> PublicKeyBytes {
Expand Down
8 changes: 4 additions & 4 deletions crates/sui-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ move-vm-runtime = { git = "https://github.com/move-language/move", rev = "773365
typed-store = { git = "https://github.com/MystenLabs/mysten-infra", rev = "123c9e40b529315e1c1d91a54fb717111c3e349c"}
mysten-network = { git = "https://github.com/MystenLabs/mysten-infra", rev = "123c9e40b529315e1c1d91a54fb717111c3e349c" }

narwhal-crypto = { git = "https://github.com/MystenLabs/narwhal", rev = "ae032178da474f74d5ec49f775a5a675460906aa", package = "crypto" }
narwhal-executor = { git = "https://github.com/MystenLabs/narwhal", rev = "ae032178da474f74d5ec49f775a5a675460906aa", package = "executor" }
narwhal-types = { git = "https://github.com/MystenLabs/narwhal", rev = "ae032178da474f74d5ec49f775a5a675460906aa", package = "types" }
narwhal-node = { git = "https://github.com/MystenLabs/narwhal", rev = "ae032178da474f74d5ec49f775a5a675460906aa", package = "node" }
narwhal-crypto = { git = "https://github.com/MystenLabs/narwhal", rev = "f50af9eb40130fb404a678e38d0671fc33f60318", package = "crypto", features=["copy_key"]}
narwhal-executor = { git = "https://github.com/MystenLabs/narwhal", rev = "f50af9eb40130fb404a678e38d0671fc33f60318", package = "executor" }
narwhal-types = { git = "https://github.com/MystenLabs/narwhal", rev = "f50af9eb40130fb404a678e38d0671fc33f60318", package = "types" }
narwhal-node = { git = "https://github.com/MystenLabs/narwhal", rev = "f50af9eb40130fb404a678e38d0671fc33f60318", package = "node" }
workspace-hack = { path = "../workspace-hack"}

[dev-dependencies]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ pub async fn init_configurable_authorities(
Vec<Arc<AuthorityState>>,
Vec<ExecutionDigests>,
) {
use narwhal_crypto::traits::KeyPair;

let authority_count = 4;
let (addr1, key1) = get_key_pair();
let mut gas_objects = Vec::new();
Expand All @@ -229,7 +231,7 @@ pub async fn init_configurable_authorities(
let mut voting_rights = BTreeMap::new();
for _ in 0..authority_count {
let (_, key_pair) = get_key_pair();
let authority_name = *key_pair.public_key_bytes();
let authority_name = key_pair.public().into();
voting_rights.insert(authority_name, 1);
key_pairs.push((authority_name, key_pair));
}
Expand Down
2 changes: 1 addition & 1 deletion crates/sui-core/src/authority_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ impl ValidatorService {
let consensus_config = config
.consensus_config()
.ok_or_else(|| anyhow!("Validator is missing consensus config"))?;
let consensus_keypair = config.key_pair().make_narwhal_keypair();
let consensus_keypair = config.key_pair().copy();
let consensus_name = consensus_keypair.public().clone();
let consensus_store = narwhal_node::NodeStorage::reopen(consensus_config.db_path());
narwhal_node::Node::spawn_primary(
Expand Down
3 changes: 2 additions & 1 deletion crates/sui-core/src/checkpoints/causal_order_effects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ mod tests {

use crate::checkpoints::causal_order_effects::EffectsStore;
use crate::checkpoints::CheckpointStore;
use narwhal_crypto::traits::KeyPair;
use rand::{prelude::StdRng, SeedableRng};
use sui_types::{
base_types::{ExecutionDigests, ObjectDigest, ObjectID, SequenceNumber, TransactionDigest},
Expand Down Expand Up @@ -296,7 +297,7 @@ mod tests {
path,
None,
committee.epoch,
*k.public_key_bytes(),
k.public().into(),
Arc::pin(k.copy()),
)
.unwrap();
Expand Down
24 changes: 12 additions & 12 deletions crates/sui-core/src/checkpoints/tests/checkpoint_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::{collections::HashSet, env, fs, path::PathBuf, sync::Arc, time::Duratio
use sui_types::{
base_types::{AuthorityName, ObjectID, TransactionDigest},
batch::UpdateItem,
crypto::get_key_pair_from_rng,
crypto::{get_key_pair_from_rng, KeypairTraits},
messages::{CertifiedTransaction, ExecutionStatus},
object::Object,
utils::{make_committee_key, make_committee_key_num},
Expand Down Expand Up @@ -70,7 +70,7 @@ fn random_ckpoint_store_num(
path.clone(),
None,
committee.epoch,
*k.public_key_bytes(),
k.public().into(),
Arc::pin(k.copy()),
)
.unwrap();
Expand Down Expand Up @@ -99,7 +99,7 @@ fn crash_recovery() {
path.clone(),
None,
committee.epoch,
*k.public_key_bytes(),
k.public().into(),
Arc::pin(k.copy()),
)
.unwrap();
Expand Down Expand Up @@ -143,7 +143,7 @@ fn crash_recovery() {
path,
None,
committee.epoch,
*k.public_key_bytes(),
k.public().into(),
Arc::pin(k.copy()),
)
.unwrap();
Expand Down Expand Up @@ -779,7 +779,7 @@ fn checkpoint_integration() {
path,
None,
committee.epoch,
*k.public_key_bytes(),
k.public().into(),
Arc::pin(k.copy()),
)
.unwrap();
Expand Down Expand Up @@ -907,15 +907,15 @@ async fn test_batch_to_checkpointing() {
&checkpoints_path,
None,
committee.epoch,
*secret.public_key_bytes(),
secret.public().into(),
secret.clone(),
)
.unwrap(),
));

let state = AuthorityState::new(
committee,
*secret.public_key_bytes(),
secret.public().into(),
secret,
store.clone(),
None,
Expand Down Expand Up @@ -1005,7 +1005,7 @@ async fn test_batch_to_checkpointing_init_crash() {

let state = AuthorityState::new(
committee.clone(),
*secret.public_key_bytes(),
secret.public().into(),
secret.clone(),
store.clone(),
None,
Expand Down Expand Up @@ -1077,7 +1077,7 @@ async fn test_batch_to_checkpointing_init_crash() {
&checkpoints_path,
None,
committee.epoch,
*secret.public_key_bytes(),
secret.public().into(),
secret.clone(),
)
.unwrap(),
Expand All @@ -1088,7 +1088,7 @@ async fn test_batch_to_checkpointing_init_crash() {

let state = AuthorityState::new(
committee,
*secret.public_key_bytes(),
secret.public().into(),
secret,
store.clone(),
None,
Expand Down Expand Up @@ -1597,7 +1597,7 @@ pub async fn checkpoint_tests_setup(
&checkpoints_path,
None,
committee.epoch,
*secret.public_key_bytes(),
secret.public().into(),
secret.clone(),
)
.unwrap();
Expand All @@ -1608,7 +1608,7 @@ pub async fn checkpoint_tests_setup(
let checkpoint = Arc::new(Mutex::new(checkpoint));
let authority = AuthorityState::new(
committee.clone(),
*secret.public_key_bytes(),
secret.public().into(),
secret,
store.clone(),
None,
Expand Down
Loading

0 comments on commit 4e5bb18

Please sign in to comment.