Skip to content
Snippets Groups Projects
Commit af2fdc54 authored by asynchronous rob's avatar asynchronous rob
Browse files

fix compilation for native

parent 5a675e9c
No related merge requests found
......@@ -191,6 +191,16 @@ name = "dtoa"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "ed25519"
version = "0.1.0"
dependencies = [
"polkadot-primitives 0.1.0",
"ring 0.12.1 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-hex 2.0.0 (git+https://github.com/rphmeier/rustc-hex.git)",
"untrusted 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "edit-distance"
version = "2.0.1"
......@@ -1008,6 +1018,7 @@ version = "0.1.0"
dependencies = [
"assert_matches 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"byteorder 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"ed25519 0.1.0",
"error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
"native-runtime 0.1.0",
"parity-wasm 0.15.4 (registry+https://github.com/rust-lang/crates.io-index)",
......@@ -1096,16 +1107,15 @@ version = "0.1.0"
name = "polkadot-runtime-std"
version = "0.1.0"
dependencies = [
"ed25519 0.1.0",
"environmental 0.1.0",
"polkadot-primitives 0.1.0",
"polkadot-runtime-codec 0.1.0",
"polkadot-state-machine 0.1.0",
"pwasm-alloc 0.1.0",
"pwasm-libc 0.1.0",
"ring 0.12.1 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc_version 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"triehash 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"untrusted 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
......
......@@ -27,7 +27,7 @@ use primitives::block::{self, HeaderHash};
use blockchain::{self, BlockId, BlockStatus};
fn header_hash(header: &primitives::block::Header) -> primitives::block::HeaderHash {
primitives::hash(&ser::to_vec(header))
primitives::hashing::blake2_256(&ser::to_vec(header)).into()
}
struct PendingBlock {
......
[package]
name = "ed25519"
version = "0.1.0"
authors = ["Parity Technologies <admin@parity.io>"]
[dependencies]
ring = "0.12"
untrusted = "0.5"
polkadot-primitives = { version = "0.1", path = "../primitives" }
rustc-hex = { git = "https://github.com/rphmeier/rustc-hex.git", version = "2.0", default_features = false }
// Copyright 2017 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
//! Simple Ed25519 API.
extern crate ring;
extern crate polkadot_primitives as primitives;
extern crate untrusted;
extern crate rustc_hex;
use ring::{rand, signature};
use primitives::Signature;
use rustc_hex::FromHex;
/// Verify a message without type checking the parameters' types for the right size.
pub fn verify(sig: &[u8], message: &[u8], public: &[u8]) -> bool {
let public_key = untrusted::Input::from(public);
let msg = untrusted::Input::from(message);
let sig = untrusted::Input::from(sig);
match signature::verify(&signature::ED25519, public_key, msg, sig) {
Ok(_) => true,
_ => false,
}
}
/// A public key.
#[derive(PartialEq, Clone, Debug)]
pub struct Public ([u8; 32]);
/// A key pair.
pub struct Pair(signature::Ed25519KeyPair);
impl Public {
/// A new instance from the given 32-byte `data`.
pub fn from(data: [u8; 32]) -> Self {
Public(data)
}
/// A new instance from the given slice that should be 32 bytes long.
pub fn from_slice(data: &[u8]) -> Self {
let mut r = [0u8; 32];
r.copy_from_slice(data);
Public(r)
}
}
impl AsRef<[u8; 32]> for Public {
fn as_ref(&self) -> &[u8; 32] {
&self.0
}
}
impl AsRef<[u8]> for Public {
fn as_ref(&self) -> &[u8] {
&self.0[..]
}
}
impl Pair {
/// Generate new secure (random) key pair.
pub fn new() -> Pair {
let rng = rand::SystemRandom::new();
let pkcs8_bytes = signature::Ed25519KeyPair::generate_pkcs8(&rng).unwrap();
Pair(signature::Ed25519KeyPair::from_pkcs8(untrusted::Input::from(&pkcs8_bytes)).unwrap())
}
/// Make a new key pair from a seed phrase.
pub fn from_seed(seed: &[u8; 32]) -> Pair {
Pair(signature::Ed25519KeyPair::from_seed_unchecked(untrusted::Input::from(&seed[..])).unwrap())
}
/// Make a new key pair from the raw secret.
pub fn from_secret(secret: &[u8; 32]) -> Pair {
let mut pkcs8_bytes: Vec<_> = FromHex::from_hex("302e020100300506032b657004220420").unwrap();
pkcs8_bytes.extend_from_slice(&secret[..]);
Pair(signature::Ed25519KeyPair::from_pkcs8_maybe_unchecked(untrusted::Input::from(&pkcs8_bytes)).unwrap())
}
/// Make a new key pair from the raw secret and public key (it will check to make sure
/// they correspond to each other).
pub fn from_both(secret_public: &[u8; 64]) -> Option<Pair> {
let mut pkcs8_bytes: Vec<_> = FromHex::from_hex("3053020101300506032b657004220420").unwrap();
pkcs8_bytes.extend_from_slice(&secret_public[0..32]);
pkcs8_bytes.extend_from_slice(&[0xa1u8, 0x23, 0x03, 0x21, 0x00]);
pkcs8_bytes.extend_from_slice(&secret_public[32..64]);
signature::Ed25519KeyPair::from_pkcs8_maybe_unchecked(untrusted::Input::from(&pkcs8_bytes)).ok().map(Pair)
}
/// Sign a message.
pub fn sign(&self, message: &[u8]) -> Signature {
let mut r = [0u8; 64];
r.copy_from_slice(self.0.sign(message).as_ref());
Signature::from(r)
}
/// Get the public key.
pub fn public(&self) -> Public {
let mut r = [0u8; 32];
let pk = self.0.public_key_bytes();
r.copy_from_slice(pk);
Public(r)
}
}
/// Verify a signature on a message.
pub fn verify_strong(sig: &Signature, message: &[u8], pubkey: &Public) -> bool {
let public_key = untrusted::Input::from(&pubkey.0[..]);
let msg = untrusted::Input::from(message);
let sig = untrusted::Input::from(&sig.0[..]);
match signature::verify(&signature::ED25519, public_key, msg, sig) {
Ok(_) => true,
_ => false,
}
}
impl From<&'static str> for Public {
fn from(hex: &'static str) -> Self {
let mut r = [0u8; 32];
let v: Vec<_> = FromHex::from_hex(hex).unwrap();
r.copy_from_slice(&v[0..32]);
Public(r)
}
}
impl From<&'static str> for Pair {
fn from(hex: &'static str) -> Self {
let data: Vec<_> = FromHex::from_hex(hex).expect("Key pair given is static so hex should be good.");
match data.len() {
32 => {
let mut r = [0u8; 32];
r.copy_from_slice(&data[0..32]);
Pair::from_secret(&r)
}
64 => {
let mut r = [0u8; 64];
r.copy_from_slice(&data[0..64]);
Pair::from_both(&r).expect("Key pair given is static so should be good.")
}
_ => {
panic!("Key pair given is static so should be correct length.");
}
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_vector_should_work() {
let pair: Pair = "9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60".into();
let public = pair.public();
assert_eq!(public, "d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a".into());
let message = b"";
let signature: Signature = "e5564300c360ac729086e2cc806e828a84877f1eb8e5d974d873e065224901555fb8821590a33bacc61e39701cf9b46bd25bf5f0595bbe24655141438e7a100b".into();
assert!(&pair.sign(&message[..]) == &signature);
assert!(verify_strong(&signature, &message[..], &public));
}
#[test]
fn generated_pair_should_work() {
let pair = Pair::new();
let public = pair.public();
let message = b"Something important";
let signature = pair.sign(&message[..]);
assert!(verify_strong(&signature, &message[..], &public));
}
#[test]
fn seeded_pair_should_work() {
use primitives::hexdisplay::HexDisplay;
let pair = Pair::from_seed(b"12345678901234567890123456789012");
let public = pair.public();
assert_eq!(public, "2f8c6129d816cf51c374bc7f08c3e63ed156cf78aefb4a6550d97b87997977ee".into());
let message: Vec<_> = FromHex::from_hex("2f8c6129d816cf51c374bc7f08c3e63ed156cf78aefb4a6550d97b87997977ee00000000000000002228000000d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a4500000000000000").unwrap();
let signature = pair.sign(&message[..]);
println!("Correct signature: {}", HexDisplay::from(&signature.0));
assert!(verify_strong(&signature, &message[..], &public));
}
}
......@@ -17,6 +17,7 @@ byteorder = "1.1"
rustc-hex = "1.0.0"
native-runtime = { path = "../native-runtime", version = "0.1" }
triehash = "0.1.0"
ed25519 = { path = "../ed25519", version = "0.1" }
[dev-dependencies]
assert_matches = "1.1"
......@@ -32,6 +32,7 @@ extern crate polkadot_runtime_std as runtime_std;
extern crate polkadot_primitives as primitives;
extern crate polkadot_serializer as serializer;
extern crate polkadot_state_machine as state_machine;
extern crate ed25519;
extern crate serde;
extern crate parity_wasm;
......
......@@ -27,7 +27,7 @@ use state_machine::{Externalities, CodeExecutor};
use error::{Error, ErrorKind, Result};
use wasm_utils::{MemoryInstance, UserDefinedElements,
AddModuleWithoutFullDependentInstance};
use primitives::{ed25519, blake2_256, twox_128, twox_256};
use primitives::{blake2_256, twox_128, twox_256};
use primitives::hexdisplay::HexDisplay;
use triehash::ordered_trie_root;
......@@ -217,7 +217,7 @@ impl_function_executor!(this: FunctionExecutor<'e, E>,
this.memory.get_into(pubkey_data, &mut pubkey[..]).map_err(|_| DummyUserError)?;
let msg = this.memory.get(msg_data, msg_len as usize).map_err(|_| DummyUserError)?;
if ed25519::Signature::from(sig).verify(&msg, &ed25519::Public::from(pubkey)) {
if ::ed25519::verify(&sig, &msg, &pubkey) {
0
} else {
5
......@@ -372,7 +372,7 @@ mod tests {
fn ed25519_verify_should_work() {
let mut ext = TestExternalities::default();
let test_code = include_bytes!("../../wasm-runtime/target/wasm32-unknown-unknown/release/runtime_test.compact.wasm");
let key = ed25519::Pair::from_seed(&blake2_256(b"test"));
let key = ::ed25519::Pair::from_seed(&blake2_256(b"test"));
let sig = key.sign(b"all ok!");
let mut calldata = vec![];
calldata.extend_from_slice(key.public().as_ref());
......
......@@ -57,6 +57,6 @@ pub use network::{NonReservedPeerMode, ConnectionFilter, ConnectionDirection, Ne
// TODO: move it elsewhere
fn header_hash(header: &primitives::block::Header) -> primitives::block::HeaderHash {
primitives::hash(&ser::to_vec(header))
primitives::hashing::blake2_256(&ser::to_vec(header)).into()
}
......@@ -129,7 +129,7 @@ pub fn deserialize_check_len<'de, D>(deserializer: D, len: ExpectedLen) -> Resul
#[cfg(feature = "std")]
fn format_err(e: ::rustc_hex::FromHexError) -> String {
format!("invalid hex value: {:?}", e);
format!("invalid hex value: {:?}", e)
}
#[cfg(not(feature = "std"))]
......
......@@ -19,7 +19,7 @@
#![warn(missing_docs)]
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(not(feature = "std"), feature(alloc, lang_items))]
#![cfg_attr(not(feature = "std"), feature(alloc))]
extern crate rustc_hex;
extern crate serde;
......@@ -63,17 +63,6 @@ macro_rules! try_opt {
}
}
#[lang = "panic_fmt"]
#[no_mangle]
pub extern fn panic_fmt(_fmt: ::core::fmt::Arguments, _file: &'static str, _line: u32, _col: u32) {
unsafe {
ext_print_utf8(_file.as_ptr() as *const u8, _file.len() as u32);
ext_print_num(_line as u64);
ext_print_num(_col as u64);
::core::intrinsics::abort()
}
}
mod bytes;
pub mod block;
pub mod contract;
......
......@@ -15,8 +15,7 @@ polkadot-state-machine = { path = "../state-machine", version = "0.1", optional
polkadot-primitives = { path = "../primitives", version = "0.1", default_features = false }
polkadot-runtime-codec = { path = "../runtime-codec", version = "0.1", default_features = false }
triehash = { version = "0.1", optional = true }
ring = { version = "0.12", optional = true }
untrusted = { version = "0.5", optional = true }
ed25519 = { path = "../ed25519", version = "0.1", optional = true }
[features]
default = ["std"]
......@@ -26,8 +25,7 @@ std = [
"triehash",
"polkadot-primitives/std",
"polkadot-runtime-codec/std",
"ring",
"untrusted"
"ed25519",
]
nightly = []
strict = []
......@@ -20,8 +20,7 @@ extern crate environmental;
extern crate polkadot_state_machine;
extern crate polkadot_primitives as primitives;
extern crate triehash;
extern crate ring;
extern crate untrusted;
extern crate ed25519;
pub use std::vec;
pub use std::rc;
......@@ -93,16 +92,7 @@ pub fn enumerated_trie_root(serialised_values: &[&[u8]]) -> [u8; 32] {
/// Verify a ed25519 signature.
pub fn ed25519_verify(sig: &[u8; 64], msg: &[u8], pubkey: &[u8; 32]) -> bool {
use ring::signature;
let public_key = untrusted::Input::from(pubkey);
let msg = untrusted::Input::from(msg);
let sig = untrusted::Input::from(sig);
match signature::verify(&signature::ED25519, public_key, msg, sig) {
Ok(_) => true,
_ => false,
}
ed25519::verify(sig, msg, pubkey)
}
/// Execute the given closure with global function available whose functionality routes into the
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment