Skip to content

Commit

Permalink
small fixes;
Browse files Browse the repository at this point in the history
  • Loading branch information
punwai committed Jul 18, 2022
1 parent d85ffd0 commit 1d63be3
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 62 deletions.
2 changes: 1 addition & 1 deletion crates/sui-config/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl NodeConfig {
}

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

pub fn sui_address(&self) -> SuiAddress {
Expand Down
6 changes: 2 additions & 4 deletions crates/sui-core/src/epoch/reconfiguration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,8 @@ where
.unwrap();
let client: A = A::recreate(channel);
let name: &[u8] = &validator.name;
let public_key_bytes =
PublicKeyBytes::from_bytes(name).map_err(|e| SuiError::GenericAuthorityError {
error: e.to_string(),
})?;
let public_key_bytes = PublicKeyBytes::from_bytes(name)
.map_err(|e| SuiError::KeyConversionError(e.to_string()))?;
new_clients.insert(public_key_bytes, client);
}

Expand Down
2 changes: 1 addition & 1 deletion crates/sui-core/src/generate_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::{fs::File, io::Write};
use sui_types::{
base_types::{self, ObjectDigest, ObjectID, TransactionDigest, TransactionEffectsDigest},
batch::UpdateItem,
crypto::{get_key_pair, AuthoritySignature, Signature, KeypairTraits, PublicKeyBytes},
crypto::{get_key_pair, AuthoritySignature, KeypairTraits, PublicKeyBytes, Signature},
messages::{
CallArg, EntryArgumentErrorKind, ExecutionFailureStatus, ExecutionStatus, ObjectArg,
ObjectInfoRequestKind, SingleTransactionKind, TransactionKind,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use sui_adapter::genesis;
use sui_config::genesis::Genesis;
use sui_config::ValidatorInfo;
use sui_types::crypto::{get_key_pair, PublicKeyBytes};
use sui_types::crypto::{KeyPair, Signature, KeypairTraits};
use sui_types::crypto::{KeyPair, KeypairTraits, Signature};

use sui_types::messages::Transaction;
use sui_types::object::{Object, GAS_VALUE_FOR_TESTING};
Expand Down
4 changes: 2 additions & 2 deletions crates/sui-core/src/unit_tests/authority_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use sui_adapter::genesis;
use sui_types::object::Data;
use sui_types::{
base_types::dbg_addr,
crypto::{KeyPair, KeypairTraits},
crypto::{get_key_pair, Signature},
crypto::{KeyPair, KeypairTraits},
messages::Transaction,
object::{Owner, OBJECT_START_VERSION},
sui_system_state::SuiSystemState,
Expand Down Expand Up @@ -1581,7 +1581,7 @@ async fn test_store_revert_state_update() {

#[cfg(test)]
fn init_state_parameters() -> (Committee, SuiAddress, KeyPair, Arc<AuthorityStore>) {
use narwhal_crypto::{traits::KeyPair};
use narwhal_crypto::traits::KeyPair;
use sui_types::crypto::PublicKeyBytes;

let (authority_address, authority_key) = get_key_pair();
Expand Down
2 changes: 1 addition & 1 deletion crates/sui-core/src/unit_tests/batch_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

use rand::{prelude::StdRng, SeedableRng};
use sui_types::committee::Committee;
use sui_types::crypto::PublicKeyBytes;
use sui_types::crypto::get_key_pair;
use sui_types::crypto::get_key_pair_from_rng;
use sui_types::crypto::PublicKeyBytes;
use sui_types::crypto::{KeyPair, KeypairTraits};
use sui_types::messages_checkpoint::CheckpointRequest;
use sui_types::messages_checkpoint::CheckpointResponse;
Expand Down
28 changes: 10 additions & 18 deletions crates/sui-types/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ use std::hash::{Hash, Hasher};

// Comment the one you want to use
pub type KeyPair = Ed25519KeyPair; // Associated Types don't work here yet for some reason.
pub type PrivateKey = Ed25519PrivateKey;
pub type PublicKey = Ed25519PublicKey;
pub type PrivateKey = <KeyPair as KeypairTraits>::PrivKey;
pub type PublicKey = <KeyPair as KeypairTraits>::PubKey;
pub type PublicKeyBytes = Ed25519PublicKeyBytes;

// Signatures for Authorities
Expand Down Expand Up @@ -85,16 +85,12 @@ impl signature::Signer<Signature> for KeyPair {
fn try_sign(&self, msg: &[u8]) -> Result<Signature, signature::Error> {
let signature_bytes: <<KeyPair as KeypairTraits>::PrivKey as SigningKey>::Sig =
self.try_sign(msg)?;
println!("signature_bytes: {:?}", signature_bytes);
self.public().verify(msg, &signature_bytes)?;

println!("VERIFIED! {:?}", signature_bytes);
let pk_bytes: PublicKeyBytes = self.public().into();
let public_key_bytes = pk_bytes.as_ref();
let public_key_bytes: PublicKeyBytes = self.public().into();
let mut result_bytes = [0u8; SUI_SIGNATURE_LENGTH];
result_bytes[..<KeyPair as KeypairTraits>::Sig::LENGTH]
.copy_from_slice(signature_bytes.as_ref());
result_bytes[<KeyPair as KeypairTraits>::Sig::LENGTH..].copy_from_slice(public_key_bytes);
result_bytes[<KeyPair as KeypairTraits>::Sig::LENGTH..]
.copy_from_slice(public_key_bytes.as_ref());
Ok(Signature(result_bytes))
}
}
Expand All @@ -108,7 +104,7 @@ impl signature::Verifier<Signature> for PublicKeyBytes {

let public_key =
PublicKey::from_bytes(self.as_ref()).map_err(|_| signature::Error::new())?;

// perform cryptographic signature check
public_key
.verify(message, &signature)
Expand Down Expand Up @@ -407,9 +403,7 @@ impl<const STRONG_THRESHOLD: bool> AuthorityQuorumSignInfo<STRONG_THRESHOLD> {
})
}

pub fn len(
&self
) -> u64 {
pub fn len(&self) -> u64 {
self.signers_map.len()
}

Expand All @@ -428,10 +422,6 @@ impl<const STRONG_THRESHOLD: bool> AuthorityQuorumSignInfo<STRONG_THRESHOLD> {
);

let mut weight = 0;
let pk_vec = obligation
.public_keys
.get_mut(message_index)
.ok_or(SuiError::InvalidAddress)?;

// Create obligations for the committee signatures
obligation
Expand All @@ -453,7 +443,9 @@ impl<const STRONG_THRESHOLD: bool> AuthorityQuorumSignInfo<STRONG_THRESHOLD> {
fp_ensure!(voting_rights > 0, SuiError::UnknownSigner);
weight += voting_rights;

pk_vec.push(committee.public_key(authority)?);
obligation
.public_keys
.push(committee.public_key(authority)?);
}

let threshold = if STRONG_THRESHOLD {
Expand Down
2 changes: 2 additions & 0 deletions crates/sui-types/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,8 @@ pub enum SuiError {
HkdfError(String),
#[error("Signature key generation error: {0}")]
SignatureKeyGenError(String),
#[error("Key Conversion Error: {0}")]
KeyConversionError(String),

// Epoch related errors.
#[error("Validator temporarily stopped processing transactions due to epoch change")]
Expand Down
2 changes: 1 addition & 1 deletion crates/sui-types/src/messages_checkpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,8 +479,8 @@ mod tests {
use narwhal_crypto::traits::KeyPair;
use rand::prelude::StdRng;
use rand::SeedableRng;
use tonic::codegen::http::uri::Authority;
use std::collections::BTreeSet;
use tonic::codegen::http::uri::Authority;

use super::*;
use crate::utils::make_committee_key;
Expand Down
43 changes: 32 additions & 11 deletions crates/sui-types/src/unit_tests/messages_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fn test_signed_values() {
TransactionData::new_transfer(a2, random_object_ref(), a1, random_object_ref(), 10000),
&sec2,
);

let v = SignedTransaction::new(
committee.epoch(),
transaction.clone(),
Expand All @@ -66,7 +66,7 @@ fn test_signed_values() {
committee.epoch(),
transaction,
PublicKeyBytes::from(sec3.public()),
&sec3
&sec3,
);
assert!(v.verify(&committee).is_err());

Expand Down Expand Up @@ -135,11 +135,17 @@ fn test_certificates() {

let mut builder = SignatureAggregator::try_new(transaction.clone(), &committee).unwrap();
assert!(builder
.append(v1.auth_sign_info.authority, v1.auth_sign_info.signature.clone())
.append(
v1.auth_sign_info.authority,
v1.auth_sign_info.signature.clone()
)
.unwrap()
.is_none());
let mut c = builder
.append(v2.auth_sign_info.authority, v2.auth_sign_info.signature.clone())
.append(
v2.auth_sign_info.authority,
v2.auth_sign_info.signature.clone(),
)
.unwrap()
.unwrap();

Expand All @@ -150,7 +156,10 @@ fn test_certificates() {

let mut builder = SignatureAggregator::try_new(transaction, &committee).unwrap();
assert!(builder
.append(v1.auth_sign_info.authority, v1.auth_sign_info.signature.clone())
.append(
v1.auth_sign_info.authority,
v1.auth_sign_info.signature.clone()
)
.unwrap()
.is_none());
assert!(builder
Expand Down Expand Up @@ -200,7 +209,9 @@ fn test_new_with_signatures() {
);
}

fn get_obligation_input<T>(value: &T) -> (VerificationObligation<AggregateAuthoritySignature>, usize)
fn get_obligation_input<T>(
value: &T,
) -> (VerificationObligation<AggregateAuthoritySignature>, usize)
where
T: BcsSignable,
{
Expand All @@ -223,7 +234,9 @@ fn test_handle_reject_malicious_signature() {
let (_, sec) = get_key_pair();
let sig = AuthoritySignature::new(&Foo("some data".to_string()), &sec);
authorities.insert(PublicKeyBytes::from(sec.public()), 1);
if i < 4 { signatures.push((PublicKeyBytes::from(sec.public()), sig)) };
if i < 4 {
signatures.push((PublicKeyBytes::from(sec.public()), sig))
};
}

let committee = Committee::new(0, authorities.clone()).unwrap();
Expand Down Expand Up @@ -281,8 +294,12 @@ fn test_reject_extra_public_key() {

signatures.sort_by_key(|k| k.0);

let used_signatures: Vec<(AuthorityName, AuthoritySignature)> =
vec![signatures[0].clone(), signatures[1].clone(), signatures[2].clone(), signatures[3].clone()];
let used_signatures: Vec<(AuthorityName, AuthoritySignature)> = vec![
signatures[0].clone(),
signatures[1].clone(),
signatures[2].clone(),
signatures[3].clone(),
];

let committee = Committee::new(0, authorities.clone()).unwrap();
let mut quorum =
Expand All @@ -308,8 +325,12 @@ fn test_reject_reuse_signatures() {
signatures.push((PublicKeyBytes::from(sec.public()), sig));
}

let used_signatures: Vec<(AuthorityName, AuthoritySignature)> =
vec![signatures[0].clone(), signatures[1].clone(), signatures[2].clone(), signatures[2].clone()];
let used_signatures: Vec<(AuthorityName, AuthoritySignature)> = vec![
signatures[0].clone(),
signatures[1].clone(),
signatures[2].clone(),
signatures[2].clone(),
];

let committee = Committee::new(0, authorities.clone()).unwrap();
let quorum =
Expand Down
27 changes: 5 additions & 22 deletions crates/sui/src/genesis_ceremony.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ use sui_config::{
use sui_types::{
base_types::{encode_bytes_hex, ObjectID, SuiAddress},
crypto::KeyPair,
crypto::{KeypairTraits, PublicKey, PublicKeyBytes, Signature, ToFromBytes, AuthoritySignature, get_key_pair},
crypto::{
get_key_pair, AuthoritySignature, KeypairTraits, PublicKey, PublicKeyBytes, Signature,
ToFromBytes,
},
object::Object,
};

Expand Down Expand Up @@ -158,21 +161,9 @@ pub fn run(cmd: Ceremony) -> Result<()> {
println!("sig: {:?}", sig);
kp2.public().verify(&built_genesis_bytes, &sig)?;


// Sign the genesis bytes
let signature: Signature = keypair.try_sign(&built_genesis_bytes)?;
let sig = AuthoritySignature::from_bytes(signature.signature_bytes())?;
println!("sig: {:?}", sig);
keypair.public().verify(&built_genesis_bytes, &sig)?;
println!("VERI2");
let pk_bytes: PublicKeyBytes = PublicKeyBytes::from_bytes(signature.public_key_bytes()).unwrap();
println!("{:?}", signature);
pk_bytes.verify(&built_genesis_bytes, &signature)?;
let sig: AuthoritySignature = AuthoritySignature::from_bytes(signature.signature_bytes()).unwrap();

println!("{:?}", signature);
keypair.public().verify(&built_genesis_bytes[..], &sig)?;


let signature_dir = dir.join(GENESIS_BUILDER_SIGNATURE_DIR);
std::fs::create_dir_all(&signature_dir)?;

Expand All @@ -193,15 +184,8 @@ pub fn run(cmd: Ceremony) -> Result<()> {

let path = entry.path();
let signature_bytes = fs::read(path)?;
println!("{:?}", signature_bytes);
let signature: Signature = Signature::from_bytes(&signature_bytes)?;
println!("{:?}", signature);
println!("{:?}", signature.signature_bytes());

let public_key = PublicKeyBytes::from_bytes(signature.public_key_bytes())?;

assert!(public_key.verify(&genesis_bytes, &signature).is_ok());
println!("passing genesis");
signatures.insert(public_key, signature);
}

Expand Down Expand Up @@ -261,7 +245,6 @@ mod test {
(keypair, info)
})
.collect::<Vec<_>>();
let s: Signature = validators[0].0.try_sign(b"123")?;

// Initialize
let command = Ceremony {
Expand Down

0 comments on commit 1d63be3

Please sign in to comment.