2018-03-30 10:43:38 -07:00
|
|
|
//! The `signature` module provides functionality for public, and private keys.
|
2018-03-06 12:48:26 -07:00
|
|
|
|
2018-07-31 11:26:26 -06:00
|
|
|
use bs58;
|
2018-09-26 17:55:36 -06:00
|
|
|
use generic_array::typenum::U64;
|
2018-05-29 21:20:28 -06:00
|
|
|
use generic_array::GenericArray;
|
2018-05-09 00:03:05 -04:00
|
|
|
use rand::{ChaChaRng, Rng, SeedableRng};
|
2018-05-12 15:42:27 -04:00
|
|
|
use rayon::prelude::*;
|
2018-03-06 12:48:26 -07:00
|
|
|
use ring::signature::Ed25519KeyPair;
|
|
|
|
use ring::{rand, signature};
|
2018-07-12 15:42:01 -06:00
|
|
|
use serde_json;
|
2018-09-27 07:49:26 -07:00
|
|
|
use solana_program_interface::pubkey::Pubkey;
|
2018-07-12 15:42:01 -06:00
|
|
|
use std::error;
|
2018-07-31 11:26:26 -06:00
|
|
|
use std::fmt;
|
2018-07-12 15:42:01 -06:00
|
|
|
use std::fs::File;
|
|
|
|
use untrusted::Input;
|
2018-03-06 12:48:26 -07:00
|
|
|
|
2018-08-09 08:56:04 -06:00
|
|
|
pub type Keypair = Ed25519KeyPair;
|
2018-07-31 11:26:26 -06:00
|
|
|
|
2018-08-01 11:28:27 -06:00
|
|
|
#[derive(Serialize, Deserialize, Clone, Copy, Default, Eq, PartialEq, Ord, PartialOrd, Hash)]
|
|
|
|
pub struct Signature(GenericArray<u8, U64>);
|
|
|
|
|
|
|
|
impl Signature {
|
|
|
|
pub fn new(signature_slice: &[u8]) -> Self {
|
|
|
|
Signature(GenericArray::clone_from_slice(&signature_slice))
|
|
|
|
}
|
2018-08-09 09:26:21 -06:00
|
|
|
pub fn verify(&self, pubkey_bytes: &[u8], message_bytes: &[u8]) -> bool {
|
2018-08-09 09:03:08 -06:00
|
|
|
let pubkey = Input::from(pubkey_bytes);
|
2018-08-09 09:26:21 -06:00
|
|
|
let message = Input::from(message_bytes);
|
|
|
|
let signature = Input::from(self.0.as_slice());
|
|
|
|
signature::verify(&signature::ED25519, pubkey, message, signature).is_ok()
|
2018-08-01 11:28:27 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsRef<[u8]> for Signature {
|
|
|
|
fn as_ref(&self) -> &[u8] {
|
|
|
|
&self.0[..]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for Signature {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{}", bs58::encode(self.0).into_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Signature {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{}", bs58::encode(self.0).into_string())
|
|
|
|
}
|
|
|
|
}
|
2018-03-06 12:48:26 -07:00
|
|
|
|
2018-08-09 08:56:04 -06:00
|
|
|
pub trait KeypairUtil {
|
2018-03-07 15:32:22 -07:00
|
|
|
fn new() -> Self;
|
2018-08-09 09:13:57 -06:00
|
|
|
fn pubkey(&self) -> Pubkey;
|
2018-03-06 12:48:26 -07:00
|
|
|
}
|
|
|
|
|
2018-08-09 08:56:04 -06:00
|
|
|
impl KeypairUtil for Ed25519KeyPair {
|
2018-03-07 15:32:22 -07:00
|
|
|
/// Return a new ED25519 keypair
|
|
|
|
fn new() -> Self {
|
|
|
|
let rng = rand::SystemRandom::new();
|
2018-07-12 15:42:01 -06:00
|
|
|
let pkcs8_bytes = Ed25519KeyPair::generate_pkcs8(&rng).expect("generate_pkcs8");
|
|
|
|
Ed25519KeyPair::from_pkcs8(Input::from(&pkcs8_bytes)).expect("from_pcks8")
|
2018-03-07 15:32:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the public key for the given keypair
|
2018-08-09 09:13:57 -06:00
|
|
|
fn pubkey(&self) -> Pubkey {
|
2018-09-26 17:55:36 -06:00
|
|
|
Pubkey::new(self.public_key_bytes())
|
2018-03-07 15:32:22 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-09 00:03:05 -04:00
|
|
|
pub struct GenKeys {
|
2018-07-31 17:41:42 -06:00
|
|
|
generator: ChaChaRng,
|
2018-05-09 00:03:05 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl GenKeys {
|
2018-06-11 14:04:51 -06:00
|
|
|
pub fn new(seed: [u8; 32]) -> GenKeys {
|
2018-07-31 17:41:42 -06:00
|
|
|
let generator = ChaChaRng::from_seed(seed);
|
|
|
|
GenKeys { generator }
|
2018-05-09 00:03:05 -04:00
|
|
|
}
|
|
|
|
|
2018-07-31 17:41:42 -06:00
|
|
|
fn gen_seed(&mut self) -> [u8; 32] {
|
|
|
|
let mut seed = [0u8; 32];
|
|
|
|
self.generator.fill(&mut seed);
|
|
|
|
seed
|
2018-05-09 00:03:05 -04:00
|
|
|
}
|
2018-05-11 12:55:05 -04:00
|
|
|
|
2018-07-31 16:49:58 -06:00
|
|
|
fn gen_n_seeds(&mut self, n: i64) -> Vec<[u8; 32]> {
|
2018-07-31 17:41:42 -06:00
|
|
|
(0..n).map(|_| self.gen_seed()).collect()
|
2018-05-12 15:42:27 -04:00
|
|
|
}
|
|
|
|
|
2018-08-09 08:56:04 -06:00
|
|
|
pub fn gen_n_keypairs(&mut self, n: i64) -> Vec<Keypair> {
|
2018-05-23 14:04:07 -06:00
|
|
|
self.gen_n_seeds(n)
|
2018-05-23 12:23:39 -06:00
|
|
|
.into_par_iter()
|
2018-08-09 08:56:04 -06:00
|
|
|
.map(|seed| Keypair::from_seed_unchecked(Input::from(&seed)).unwrap())
|
2018-05-23 12:23:39 -06:00
|
|
|
.collect()
|
2018-05-11 12:55:05 -04:00
|
|
|
}
|
2018-05-09 00:03:05 -04:00
|
|
|
}
|
|
|
|
|
2018-07-12 17:26:56 -06:00
|
|
|
pub fn read_pkcs8(path: &str) -> Result<Vec<u8>, Box<error::Error>> {
|
2018-07-12 15:42:01 -06:00
|
|
|
let file = File::open(path.to_string())?;
|
|
|
|
let pkcs8: Vec<u8> = serde_json::from_reader(file)?;
|
2018-07-12 17:26:56 -06:00
|
|
|
Ok(pkcs8)
|
|
|
|
}
|
|
|
|
|
2018-08-09 08:56:04 -06:00
|
|
|
pub fn read_keypair(path: &str) -> Result<Keypair, Box<error::Error>> {
|
2018-07-12 17:26:56 -06:00
|
|
|
let pkcs8 = read_pkcs8(path)?;
|
2018-07-12 15:42:01 -06:00
|
|
|
let keypair = Ed25519KeyPair::from_pkcs8(Input::from(&pkcs8))?;
|
|
|
|
Ok(keypair)
|
|
|
|
}
|
|
|
|
|
2018-05-23 12:23:39 -06:00
|
|
|
#[cfg(test)]
|
2018-05-11 12:55:05 -04:00
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use std::collections::HashSet;
|
|
|
|
|
|
|
|
#[test]
|
2018-05-23 14:04:07 -06:00
|
|
|
fn test_new_key_is_deterministic() {
|
2018-06-11 14:04:51 -06:00
|
|
|
let seed = [0u8; 32];
|
2018-07-31 16:49:58 -06:00
|
|
|
let mut gen0 = GenKeys::new(seed);
|
|
|
|
let mut gen1 = GenKeys::new(seed);
|
2018-05-11 12:55:05 -04:00
|
|
|
|
|
|
|
for _ in 0..100 {
|
2018-07-31 17:41:42 -06:00
|
|
|
assert_eq!(gen0.gen_seed().to_vec(), gen1.gen_seed().to_vec());
|
2018-05-11 12:55:05 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-09 09:13:57 -06:00
|
|
|
fn gen_n_pubkeys(seed: [u8; 32], n: i64) -> HashSet<Pubkey> {
|
2018-06-11 14:04:51 -06:00
|
|
|
GenKeys::new(seed)
|
2018-05-23 14:04:07 -06:00
|
|
|
.gen_n_keypairs(n)
|
|
|
|
.into_iter()
|
|
|
|
.map(|x| x.pubkey())
|
|
|
|
.collect()
|
|
|
|
}
|
2018-05-11 12:55:05 -04:00
|
|
|
|
2018-05-23 14:04:07 -06:00
|
|
|
#[test]
|
|
|
|
fn test_gen_n_pubkeys_deterministic() {
|
2018-06-11 14:04:51 -06:00
|
|
|
let seed = [0u8; 32];
|
|
|
|
assert_eq!(gen_n_pubkeys(seed, 50), gen_n_pubkeys(seed, 50));
|
2018-05-11 12:55:05 -04:00
|
|
|
}
|
|
|
|
}
|