diff --git a/chacha20-demo/.DS_Store b/chacha20-demo/.DS_Store new file mode 100644 index 0000000..f7b9cb6 Binary files /dev/null and b/chacha20-demo/.DS_Store differ diff --git a/chacha20-demo/methods/.DS_Store b/chacha20-demo/methods/.DS_Store new file mode 100644 index 0000000..247a0f5 Binary files /dev/null and b/chacha20-demo/methods/.DS_Store differ diff --git a/shake256-33bytes-demo/.gitignore b/shake256-33bytes-demo/.gitignore new file mode 100644 index 0000000..9e5faa4 --- /dev/null +++ b/shake256-33bytes-demo/.gitignore @@ -0,0 +1,4 @@ +/target +**/target +/target 2 +**/target 2 diff --git a/shake256-33bytes-demo/methods/build.rs b/shake256-33bytes-demo/methods/build.rs index 08a8a4e..f23f0a5 100644 --- a/shake256-33bytes-demo/methods/build.rs +++ b/shake256-33bytes-demo/methods/build.rs @@ -1,3 +1,3 @@ fn main() { risc0_build::embed_methods(); -} +} \ No newline at end of file diff --git a/shake256-33bytes-demo/methods/guest/src/main.rs b/shake256-33bytes-demo/methods/guest/src/main.rs index e92b286..c46f762 100644 --- a/shake256-33bytes-demo/methods/guest/src/main.rs +++ b/shake256-33bytes-demo/methods/guest/src/main.rs @@ -5,7 +5,7 @@ extern crate alloc; use alloc::vec::Vec; use risc0_zkvm::guest::env; use serde::{Deserialize, Serialize}; - + // ---------- module 1 ---------- mod ser_bytes33 { use core::fmt; @@ -70,20 +70,22 @@ mod crypto { out_index: u32, ) -> [u8; 32] { let mut hasher = Sha256::new(); - sha2::Digest::update(&mut hasher, b"NSSA/v0.1/KDF-SHA256"); - sha2::Digest::update(&mut hasher, &ss_bytes); - sha2::Digest::update(&mut hasher, &epk[..]); - sha2::Digest::update(&mut hasher, &ipk[..]); - sha2::Digest::update(&mut hasher, &commitment[..]); - sha2::Digest::update(&mut hasher, &out_index.to_le_bytes()); + + hasher.update(b"NSSA/v0.1/KDF-SHA256"); + hasher.update(&ss_bytes); + hasher.update(&epk[..]); + hasher.update(&ipk[..]); + hasher.update(&commitment[..]); + hasher.update(&out_index.to_le_bytes()); + hasher.finalize().into() } pub fn enc_xor_shake256(key: &[u8; 32], info: &[u8], pt: &[u8]) -> Vec { let mut sh = Shake::v256(); - tiny_keccak::Hasher::update(&mut sh, b"NSSA/v0.1/shake-ks"); - tiny_keccak::Hasher::update(&mut sh, &key[..]); - tiny_keccak::Hasher::update(&mut sh, info); + sh.update(b"NSSA/v0.1/shake-ks"); + sh.update(&key[..]); + sh.update(info); let mut ks = vec![0u8; pt.len()]; sh.finalize(&mut ks); diff --git a/shake256-33bytes-demo/methods/src/main.rs b/shake256-33bytes-demo/methods/src/main.rs deleted file mode 100644 index af9351d..0000000 --- a/shake256-33bytes-demo/methods/src/main.rs +++ /dev/null @@ -1,83 +0,0 @@ -#![no_std] -#![no_main] - -extern crate alloc; - -use alloc::vec::Vec; -use hkdf::Hkdf; -use risc0_zkvm::guest::env; -use risc0_zkvm::guest::entry; -use serde::{Deserialize, Serialize}; -use sha2::{Digest, Sha256}; -use sha3::{digest::{ExtendableOutput, Update, XofReader}, Shake256}; - -entry!(main); - - -#[derive(Debug, Serialize, Deserialize)] -pub struct EncInput { - pub plaintext: Vec, - pub ss_bytes: [u8; 32], - pub epk_bytes: Vec, - pub ipk_bytes: Vec, - pub commitment: [u8; 32], - pub out_index: u32, -} - -fn nssa_kdf( - ss_bytes: &[u8; 32], - epk: &[u8; 32], - ipk: &[u8; 32], - commitment: &[u8; 32], - out_index: u32, -) -> ([u8; 32], Vec) { - // salt = SHA256("NSSA/v0.1/KDF-SHA256") - let mut hasher = Sha256::new(); - hasher.update(b"NSSA/v0.1/KDF-SHA256"); - let salt = hasher.finalize(); - - let hk = Hkdf::::new(Some(&salt), ss_bytes); - - // info = "NSSA/v0.1/enc" || Epk || Ipk || commitment || le(out_index) - let mut info = Vec::with_capacity(3 + 33 + 33 + 32 + 4 + 16); - info.extend_from_slice(b"NSSA/v0.1/enc"); - info.extend_from_slice(epk); - info.extend_from_slice(ipk); - info.extend_from_slice(commitment); - info.extend_from_slice(&out_index.to_le_bytes()); - - let mut k_enc = [0u8; 32]; - hk.expand(&info, &mut k_enc).unwrap(); - - (k_enc, info) -} - -fn enc_xor_shake256(k_enc: &[u8; 32], ad: &[u8], pt: &[u8]) -> Vec { - let mut shake = Shake256::default(); - shake.update(b"NSSA/v0.1/ENC/SHAKE256"); - shake.update(k_enc); - shake.update(ad); - let mut xof = shake.finalize_xof(); - - let mut ks = vec![0u8; pt.len()]; - xof.read(&mut ks); - - pt.iter().zip(ks).map(|(p, k)| p ^ k).collect() -} - -pub fn main() { - let input: EncInput = env::read(); - - let (k_enc, info) = nssa_kdf( - &input.ss_bytes, - &input.epk_compressed, - &input.ipk_compressed, - &input.commitment, - input.out_index, - ); - - let ct = enc_xor_shake256(&k_enc, &info, &input.plaintext); - - // Commit ciphertext to the journal - env::commit_slice(&ct); -} \ No newline at end of file diff --git a/shake256-33bytes-demo/src/lib.rs b/shake256-33bytes-demo/src/lib.rs index 9ff6af8..b246a12 100644 --- a/shake256-33bytes-demo/src/lib.rs +++ b/shake256-33bytes-demo/src/lib.rs @@ -1,6 +1,5 @@ use serde::{Deserialize, Serialize}; - // ---------- 33-byte wrapper (public) ---------- pub mod ser_bytes33 { // (public so main.rs can use it) use core::fmt; @@ -56,20 +55,20 @@ pub mod crypto { // makes the module public out_index: u32, ) -> [u8; 32] { let mut hasher = Sha256::new(); - sha2::Digest::update(&mut hasher, b"NSSA/v0.1/KDF-SHA256"); - sha2::Digest::update(&mut hasher, &ss_bytes); - sha2::Digest::update(&mut hasher, &epk[..]); - sha2::Digest::update(&mut hasher, &ipk[..]); - sha2::Digest::update(&mut hasher, &commitment[..]); - sha2::Digest::update(&mut hasher, &out_index.to_le_bytes()); + hasher.update(b"NSSA/v0.1/KDF-SHA256"); + hasher.update(&ss_bytes); + hasher.update(&epk[..]); + hasher.update(&ipk[..]); + hasher.update(&commitment[..]); + hasher.update(&out_index.to_le_bytes()); hasher.finalize().into() } pub fn enc_xor_shake256(key: &[u8; 32], info: &[u8], pt: &[u8]) -> Vec { let mut sh = Shake::v256(); - tiny_keccak::Hasher::update(&mut sh, b"NSSA/v0.1/shake-ks"); - tiny_keccak::Hasher::update(&mut sh, &key[..]); - tiny_keccak::Hasher::update(&mut sh, info); + sh.update(b"NSSA/v0.1/shake-ks"); + sh.update(&key[..]); + sh.update(info); let mut ks = vec![0u8; pt.len()]; sh.finalize(&mut ks); @@ -95,6 +94,7 @@ pub struct EncInput { pub commitment: [u8; 32], pub out_index: u32, } + // ------------------------------------------------------------ pub fn build_info(epk: &Bytes33, ipk: &Bytes33) -> Vec { diff --git a/shake256-33bytes-demo/src/main.rs b/shake256-33bytes-demo/src/main.rs index 0e47728..244d12e 100644 --- a/shake256-33bytes-demo/src/main.rs +++ b/shake256-33bytes-demo/src/main.rs @@ -35,8 +35,7 @@ fn main() -> anyhow::Result<()> { 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38, 0x39,0x3A,0x3B,0x3C,0x3D,0x3E,0x3F,0x40, ]; - - let input = EncInput { +let input = EncInput { ss_bytes, epk_bytes: Bytes33::from(epk_raw), ipk_bytes: Bytes33::from(ipk_raw),