From 0bad1ad7550fcd7aeb6868763c8b5dbcf787f8b5 Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 29 Jun 2021 00:10:46 +0800 Subject: [PATCH] isolate a potential compiler bug --- Cargo.lock | 37 +++++++- Cargo.toml | 13 ++- buildpush.bat | 3 +- services/shellchat/Cargo.toml | 7 +- services/shellchat/src/cmds/engine.rs | 6 +- services/test-stub/Cargo.toml | 22 +++++ services/test-stub/README.md | 2 + services/test-stub/src/main.rs | 126 ++++++++++++++++++++++++++ xtask/src/main.rs | 5 +- 9 files changed, 204 insertions(+), 17 deletions(-) create mode 100644 services/test-stub/Cargo.toml create mode 100644 services/test-stub/README.md create mode 100644 services/test-stub/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 4aa93808a..1f0fc654c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -508,14 +508,25 @@ version = "3.1.0" dependencies = [ "byteorder", "digest", - "engine-25519", - "engine25519-as", "log", "rand_core 0.5.1", "subtle", "zeroize", ] +[[package]] +name = "curve25519-dalek" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "639891fde0dbea823fc3d798a0fdf9d2f9440a42d64a78ab3488b0ca025117b3" +dependencies = [ + "byteorder", + "digest", + "rand_core 0.5.1", + "subtle", + "zeroize", +] + [[package]] name = "digest" version = "0.9.0" @@ -544,7 +555,7 @@ dependencies = [ name = "ed25519-dalek" version = "1.0.1" dependencies = [ - "curve25519-dalek", + "curve25519-dalek 3.1.0", "ed25519", "log", "rand 0.7.3", @@ -1616,7 +1627,7 @@ dependencies = [ "codec", "com", "content-plugin-api", - "curve25519-dalek", + "curve25519-dalek 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "digest", "ed25519-dalek", "engine-25519", @@ -1636,6 +1647,7 @@ dependencies = [ "rand_core 0.5.1", "rkyv 0.4.3", "rtc", + "sha2", "susres", "ticktimer-server", "trng", @@ -1785,6 +1797,21 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "test-stub" +version = "0.1.0" +dependencies = [ + "log", + "log-server", + "num-derive", + "num-traits", + "susres", + "ticktimer-server", + "utralib", + "xous", + "xous-names", +] + [[package]] name = "textwrap" version = "0.11.0" @@ -2085,7 +2112,7 @@ version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5a0c105152107e3b96f6a00a65e86ce82d9b125230e1c4302940eca58ff71f4f" dependencies = [ - "curve25519-dalek", + "curve25519-dalek 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "rand_core 0.5.1", "zeroize", ] diff --git a/Cargo.toml b/Cargo.toml index e0d042b10..eeb0c4a5c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,7 @@ default-members = [ "services/engine-sha512", "services/engine-25519", "services/aes", + "services/test-stub", ] members = [ "xous-ipc", @@ -67,6 +68,7 @@ members = [ "services/engine-sha512", "services/engine-25519", "services/aes", + "services/test-stub", ] resolver = "2" @@ -91,13 +93,14 @@ lto = "fat" xous = {path = "xous-rs"} xous-macros = {path = "macros"} # prefer hardware-accelerated versions of services -sha2 = {path = "services/engine-sha512", package = "engine-sha512"} -[patch.crates-io.curve25519-dalek] +#sha2 = {path = "services/engine-sha512", package = "engine-sha512"} + +#[patch.crates-io.curve25519-dalek] #git="https://github.com/betrusted-io/curve25519-dalek.git" #branch="main" -path = "../curve25519-dalek" -default-features = false -features = ["u32_backend", "betrusted"] +#path = "../curve25519-dalek" +#default-features = false +#features = ["u32_backend", "betrusted"] [patch."https://github.com/betrusted-io/xous-engine-25519.git"] engine-25519 = {path = "services/engine-25519"} \ No newline at end of file diff --git a/buildpush.bat b/buildpush.bat index 06a38480a..de568eacd 100644 --- a/buildpush.bat +++ b/buildpush.bat @@ -1,6 +1,7 @@ REM a crappy starter script to help with builds under Windows environments. ymmv. -cargo xtask hw-image precursors/soc.svd +REM cargo xtask hw-image precursors/soc.svd +cargo xtask minimal precursors/soc.svd CertUtil -hashfile precursors/soc_csr.bin MD5 CertUtil -hashfile target/riscv32imac-unknown-none-elf/release/loader.bin MD5 diff --git a/services/shellchat/Cargo.toml b/services/shellchat/Cargo.toml index 45acc44d3..b1ae8681d 100644 --- a/services/shellchat/Cargo.toml +++ b/services/shellchat/Cargo.toml @@ -40,13 +40,16 @@ rkyv = {version = "0.4.3", default-features = false, features = ["const_generics # hardware acceleration adaptations are inserted into a fork of the main branch. rand_core = "0.5" hex = {version = "0.4.3", default-features = false, features = []} +sha2 = {version = "0.9.5", default-features = false, features = []} [dependencies.curve25519-dalek] # don't forget the Cargo.toml patch.crates.io file to patch over third-party dependencies as well! #git="https://github.com/betrusted-io/curve25519-dalek.git" #branch="main" -path = "../../../curve25519-dalek" +#path = "../../../curve25519-dalek" +version = "3.1.0" default-features = false -features = ["u32_backend", "betrusted"] +#features = ["u32_backend", "betrusted"] +features = ["u32_backend"] [dependencies.x25519-dalek] version = "1.1.1" diff --git a/services/shellchat/src/cmds/engine.rs b/services/shellchat/src/cmds/engine.rs index fa806e9f3..dbae78d45 100644 --- a/services/shellchat/src/cmds/engine.rs +++ b/services/shellchat/src/cmds/engine.rs @@ -419,8 +419,10 @@ impl<'a> ShellCmdApi<'a> for Engine { let keypair: Keypair = Keypair{ secret: secret, public: public }; let sig1: Signature = Signature::from_bytes(&sig_bytes[..]).unwrap(); - let mut prehash_for_signing = engine_sha512::Sha512::default(); // this defaults to Hw then Sw strategy - let mut prehash_for_verifying = engine_sha512::Sha512::default(); + //let mut prehash_for_signing = engine_sha512::Sha512::default(); // this defaults to Hw then Sw strategy + //let mut prehash_for_verifying = engine_sha512::Sha512::default(); + let mut prehash_for_signing = sha2::Sha512::default(); // this defaults to Hw then Sw strategy + let mut prehash_for_verifying = sha2::Sha512::default(); prehash_for_signing.update(&msg_bytes[..]); prehash_for_verifying.update(&msg_bytes[..]); diff --git a/services/test-stub/Cargo.toml b/services/test-stub/Cargo.toml new file mode 100644 index 000000000..407d7f4d8 --- /dev/null +++ b/services/test-stub/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "test-stub" +version = "0.1.0" +authors = ["bunnie "] +edition = "2018" +description = "Test Stub Program" + +[dependencies] +xous = { path = "../../xous-rs" } +log-server = { path = "../log-server" } +ticktimer-server = { path = "../ticktimer-server" } +xous-names = { path = "../xous-names" } +log = "0.4" +num-derive = {version = "0.3", default-features = false} +num-traits = {version = "0.2", default-features = false} +susres = {path = "../susres"} + +[target.'cfg(not(any(windows,unix)))'.dependencies] +utralib = { path = "../../utralib"} + +[features] +default = [] diff --git a/services/test-stub/README.md b/services/test-stub/README.md new file mode 100644 index 000000000..cb839c6db --- /dev/null +++ b/services/test-stub/README.md @@ -0,0 +1,2 @@ +A test stub. Various small routines and tests get thrown here over time +for isolating parts of hairy bugs. diff --git a/services/test-stub/src/main.rs b/services/test-stub/src/main.rs new file mode 100644 index 000000000..e9f145c7b --- /dev/null +++ b/services/test-stub/src/main.rs @@ -0,0 +1,126 @@ +#![cfg_attr(target_os = "none", no_std)] +#![cfg_attr(target_os = "none", no_main)] + +use core::{ + fmt::Debug, hash::Hash, ptr::copy_nonoverlapping, +}; + +macro_rules! unsafe_read_slice { + ($src:expr, $dst:expr, $size:expr, $which:ident) => {{ + assert_eq!($src.len(), $size * $dst.len()); + + unsafe { + copy_nonoverlapping( + $src.as_ptr(), + $dst.as_mut_ptr() as *mut u8, + $src.len(), + ); + } + for v in $dst.iter_mut() { + *v = v.$which(); + } + }}; +} +pub trait ByteOrder: +{ + fn read_u64_into(src: &[u8], dst: &mut [u64]); +} +#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] +pub enum LittleEndian {} + +impl Default for LittleEndian { + fn default() -> LittleEndian { + panic!("LittleEndian default") + } +} +impl ByteOrder for LittleEndian { + #[inline] + fn read_u64_into(src: &[u8], dst: &mut [u64]) { + unsafe_read_slice!(src, dst, 8, to_le); + } +} + +#[derive(Copy, Clone, Hash)] +pub struct Scalar { + pub bytes: [u8; 32], +} +impl Scalar { + pub fn non_adjacent_form(&self, w: usize) -> [i8; 256] { + + let mut naf = [0i8; 256]; + + let mut x_u64 = [0u64; 5]; + LittleEndian::read_u64_into(&self.bytes, &mut x_u64[0..4]); + + let width = 1 << w; + let window_mask = width - 1; + + let mut pos = 0; + let mut carry = 0; + while pos < 256 { + //log::info!("naf[{}]: {:?}", pos, naf); + + // Construct a buffer of bits of the scalar, starting at bit `pos` + let u64_idx = pos / 64; + let bit_idx = pos % 64; + let bit_buf: u64; + if bit_idx < 64 - w { + // This window's bits are contained in a single u64 + bit_buf = x_u64[u64_idx] >> bit_idx; + } else { + // Combine the current u64's bits with the bits from the next u64 + bit_buf = (x_u64[u64_idx] >> bit_idx) | (x_u64[1+u64_idx] << (64 - bit_idx)); + } + + // Add the carry into the current window + let window = carry + (bit_buf & window_mask); + + if window & 1 == 0 { + // If the window value is even, preserve the carry and continue. + // Why is the carry preserved? + // If carry == 0 and window & 1 == 0, then the next carry should be 0 + // If carry == 1 and window & 1 == 0, then bit_buf & 1 == 1 so the next carry should be 1 + pos += 1; + continue; + } + + if window < width/2 { + carry = 0; + naf[pos] = window as i8; + } else { + carry = 1; + naf[pos] = (window as i8).wrapping_sub(width as i8); + } + + pos += w; + } + + naf + } +} + +#[xous::xous_main] +fn xmain() -> ! { + log_server::init_wait().unwrap(); + log::set_max_level(log::LevelFilter::Info); + log::info!("my PID is {}", xous::process::id()); + + let scalar = Scalar { + bytes: [189, 59, 214, 8, 77, 86, 240, 50, 111, 170, 86, 37, 124, 154, 209, 79, 102, 72, 93, 53, 130, 157, 102, 200, 60, 240, 215, 104, 246, 58, 214, 13], + }; + let a_naf = scalar.non_adjacent_form(5); + + let expected_result = [-3, 0, 0, 0, 0, 0, 15, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 11, 0, 0, 0, 0, 3, 0, 0, 0, 0, 1, 0, 0, 0, 0, 13, 0, 0, 0, 0, 0, -7, 0, 0, 0, 0, 11, 0, 0, 0, 0, 0, 0, 0, 0, 15, 0, 0, 0, 0, -7, 0, 0, 0, 0, -3, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, -11, 0, 0, 0, 0, -5, 0, 0, 0, 0, 11, 0, 0, 0, 0, 5, 0, 0, 0, 0, 1, 0, 0, 0, 0, -1, 0, 0, 0, 0, -11, 0, 0, 0, 0, 0, 13, 0, 0, 0, 0, 0, 0, -3, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, -13, 0, 0, 0, 0, 0, -15, 0, 0, 0, 0, -11, 0, 0, 0, 0, 15, 0, 0, 0, 0, -11, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, -5, 0, 0, 0, 0, 0, -11, 0, 0, 0, 0, 0, 13, 0, 0, 0, 0, 0, 0, 0, -7, 0, 0, 0, 0, -3, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, -5, 0, 0, 0, 0, 9, 0, 0, 0, 0, -13, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, -5, 0, 0, 0, 0, 0, -7, 0, 0, 0, 0, -5, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0]; + + log::info!("a_naf: {:?}", a_naf); + log::info!("expected_result: {:?}", expected_result); + assert!(a_naf == expected_result, "mismatch!"); + + loop { + xous::yield_slice(); + } + /* + log::trace!("quitting"); + xous::terminate_process(0) + */ +} diff --git a/xtask/src/main.rs b/xtask/src/main.rs index fdd4bed82..930f15dcf 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -74,8 +74,9 @@ fn try_main() -> Result<(), DynError> { "xous-names", "trng", "llio", - "rkyv-test-client", - "rkyv-test-server", +// "rkyv-test-client", +// "rkyv-test-server", + "test-stub", "susres", "com", ];