-
Notifications
You must be signed in to change notification settings - Fork 3
Shake256 with 33 bytes #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
moudyellaz
wants to merge
5
commits into
main
Choose a base branch
from
shake256-33bytes-demo
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
670092b
shake256 with 33 bytes
moudyellaz 1c6f692
Update shake256-33bytes-demo/methods/guest/src/main.rs
moudyellaz e0fa74c
fixes
moudyellaz 4333bb6
removed conflict paragraph
moudyellaz e2b8bf4
tried some fixes, reverted original
moudyellaz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| /target | ||
| **/target | ||
| /target 2 | ||
| **/target 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| [package] | ||
| name = "shake256-33bytes-demo" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
| build = "build.rs" | ||
|
|
||
| [[bin]] | ||
| name = "shake256-33bytes-demo" | ||
| path = "src/main.rs" | ||
|
|
||
| [dependencies] | ||
| risc0-zkvm = { version = "2.3.1", features = ["std", "prove"] } | ||
| anyhow = "1.0" | ||
| hex = "0.4" | ||
| serde = { version = "1.0", features = ["derive"] } | ||
| sha2 = "0.10" | ||
| hkdf = "0.12" | ||
| sha3 = "0.10" | ||
| serde-big-array = "0.5" | ||
| tiny-keccak = { version = "2", default-features = false, features = ["shake"] } # ADD | ||
|
|
||
| [dev-dependencies] | ||
| rand = "0.8" | ||
| cipher = { version = "0.4", features = ["std"] } | ||
| serde = { version = "1", default-features = false, features = ["derive", "alloc"] } | ||
|
|
||
| [package.metadata.risc0] | ||
| methods = ["methods/guest"] | ||
| # methods = {path = "methods"} | ||
|
|
||
| [build-dependencies] | ||
| risc0-build = "2.3.1" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| fn main() { | ||
| risc0_build::embed_methods(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| [package] | ||
| name = "methods" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
|
|
||
| [build-dependencies] | ||
| risc0-build = { version = "2.3.1" } | ||
|
|
||
| [package.metadata.risc0] | ||
| methods = ["guest"] | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| fn main() { | ||
| risc0_build::embed_methods(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| [package] | ||
| name = "guest" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
|
|
||
| [workspace] | ||
|
|
||
| [dependencies] | ||
| risc0-zkvm = { version = "2.3.1", default-features = false} | ||
| serde = { version = "1", default-features = false, features = ["derive", "alloc"] } | ||
| sha2 = { version = "0.10", default-features = false } | ||
| hkdf = { version = "0.12", default-features = false } | ||
| sha3 = { version = "0.10", default-features = false } | ||
| tiny-keccak = { version = "2", default-features = false, features = ["shake"] } # ADD |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| #![no_std] | ||
| #![no_main] | ||
| extern crate alloc; | ||
|
|
||
| use alloc::vec::Vec; | ||
| use risc0_zkvm::guest::env; | ||
| use serde::{Deserialize, Serialize}; | ||
|
|
||
| // ---------- module 1 ---------- | ||
| mod ser_bytes33 { | ||
| use core::fmt; | ||
| use serde::{Deserialize, Deserializer, Serialize, Serializer}; | ||
| use serde::de::{self, SeqAccess, Visitor}; | ||
|
|
||
| #[derive(Clone, Copy, PartialEq, Eq, Debug)] | ||
| pub struct Bytes33(pub [u8; 33]); | ||
|
|
||
| impl Serialize for Bytes33 { | ||
| fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> { | ||
| s.serialize_bytes(&self.0) | ||
| } | ||
| } | ||
| impl<'de> Deserialize<'de> for Bytes33 { | ||
| fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> { | ||
| struct V; | ||
| impl<'de> Visitor<'de> for V { | ||
| type Value = Bytes33; | ||
| fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
| write!(f, "exactly 33 bytes") | ||
| } | ||
| fn visit_bytes<E: de::Error>(self, v: &[u8]) -> Result<Self::Value, E> { | ||
| if v.len() != 33 { return Err(E::invalid_length(v.len(), &"33 bytes")); } | ||
| let mut a = [0u8; 33]; | ||
| a.copy_from_slice(v); | ||
| Ok(Bytes33(a)) | ||
| } | ||
| fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<Self::Value, A::Error> { | ||
| let mut a = [0u8; 33]; | ||
| for i in 0..33 { | ||
| a[i] = seq.next_element()? | ||
| .ok_or_else(|| de::Error::invalid_length(i, &"33 bytes"))?; | ||
| } | ||
| Ok(Bytes33(a)) | ||
| } | ||
| } | ||
| d.deserialize_bytes(V) | ||
| } | ||
| } | ||
| impl AsRef<[u8; 33]> for Bytes33 { fn as_ref(&self) -> &[u8; 33] { &self.0 } } | ||
| impl AsRef<[u8]> for Bytes33 { fn as_ref(&self) -> &[u8] { &self.0 } } | ||
| } | ||
| use ser_bytes33::Bytes33; // bring into crate scope | ||
| // ---------- end module 1 ---------- | ||
|
|
||
| // ---------- module 2 ---------- | ||
| mod crypto { | ||
| #![allow(clippy::needless_borrows_for_generic_args)] | ||
|
|
||
| extern crate alloc; | ||
| use alloc::vec; | ||
| use alloc::vec::Vec; | ||
| use sha2::{Digest, Sha256}; | ||
| use tiny_keccak::{Hasher, Shake}; | ||
|
|
||
| pub fn nssa_kdf( | ||
| ss_bytes: [u8; 32], | ||
| epk: &[u8; 33], | ||
| ipk: &[u8; 33], | ||
| commitment: &[u8; 32], | ||
| out_index: u32, | ||
| ) -> [u8; 32] { | ||
| let mut hasher = Sha256::new(); | ||
|
|
||
| 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<u8> { | ||
| let mut sh = Shake::v256(); | ||
| 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); | ||
|
|
||
| let mut ct = vec![0u8; pt.len()]; | ||
| for (i, &b) in pt.iter().enumerate() { | ||
| ct[i] = b ^ ks[i]; | ||
| } | ||
| ct | ||
| } | ||
| } | ||
| use crypto::{enc_xor_shake256, nssa_kdf}; | ||
| // ---------- end module 2 ---------- | ||
|
|
||
| // ---------- plain types at crate root ---------- | ||
| #[derive(Debug, Serialize, Deserialize, Clone)] | ||
| pub struct EncInput { | ||
| pub ss_bytes: [u8; 32], | ||
| pub epk_bytes: Bytes33, | ||
| pub ipk_bytes: Bytes33, | ||
| pub commitment: [u8; 32], | ||
| pub out_index: u32, | ||
| } | ||
| // ---------- end types ---------- | ||
|
|
||
| // ---------- entrypoint at crate root ---------- | ||
| risc0_zkvm::guest::entry!(guest_main); | ||
|
|
||
| pub fn guest_main() { | ||
| let EncInput { ss_bytes, epk_bytes, ipk_bytes, commitment, out_index } = env::read(); | ||
|
|
||
| let mut info: Vec<u8> = Vec::new(); | ||
| info.extend_from_slice(epk_bytes.as_ref()); | ||
| info.extend_from_slice(ipk_bytes.as_ref()); | ||
| info.extend_from_slice(&commitment); | ||
|
|
||
| let k_enc = nssa_kdf(ss_bytes, epk_bytes.as_ref(), ipk_bytes.as_ref(), &commitment, out_index); | ||
|
|
||
| let pt: &[u8] = b"hello"; | ||
| let ct = enc_xor_shake256(&k_enc, &info, pt); | ||
|
|
||
| env::commit_slice(&ct); | ||
| } | ||
| // ---------- end entry ---------- | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| include!(concat!(env!("OUT_DIR"), "/methods.rs")); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| pub mod methods { | ||
| include!(concat!(env!("OUT_DIR"), "/methods.rs")); | ||
| } | ||
|
|
||
|
|
||
| use serde::{Deserialize, Serialize}; | ||
| // ---------- 33-byte wrapper (public) ---------- | ||
| pub mod ser_bytes33 { // (public so main.rs can use it) | ||
| use core::fmt; | ||
| use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; | ||
|
|
||
| #[derive(Clone, Copy, PartialEq, Eq, Debug)] | ||
| pub struct Bytes33(pub [u8; 33]); | ||
|
|
||
| impl Serialize for Bytes33 { | ||
| fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> { | ||
| s.serialize_bytes(&self.0) | ||
| } | ||
| } | ||
| impl<'de> Deserialize<'de> for Bytes33 { | ||
| fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> { | ||
| struct V; | ||
| impl<'de> de::Visitor<'de> for V { | ||
| type Value = Bytes33; | ||
| fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
| write!(f, "exactly 33 bytes") | ||
| } | ||
| fn visit_bytes<E: de::Error>(self, v: &[u8]) -> Result<Self::Value, E> { | ||
| if v.len() != 33 { return Err(E::invalid_length(v.len(), &"33 bytes")); } | ||
| let mut a = [0u8; 33]; | ||
| a.copy_from_slice(v); | ||
| Ok(Bytes33(a)) | ||
| } | ||
| } | ||
| d.deserialize_bytes(V) | ||
| } | ||
| } | ||
| impl AsRef<[u8; 33]> for Bytes33 { fn as_ref(&self) -> &[u8; 33] { &self.0 } } | ||
| impl AsRef<[u8]> for Bytes33 { fn as_ref(&self) -> &[u8] { &self.0 } } | ||
|
|
||
| // this is an optional QoL: it allows `epk_bytes.into()` from a plain array | ||
| impl From<[u8; 33]> for Bytes33 { | ||
| fn from(a: [u8; 33]) -> Self { Bytes33(a) } | ||
| } | ||
| } | ||
| pub use ser_bytes33::Bytes33; // re-export | ||
| // -------------------------------------------------- | ||
|
|
||
| // ---------- crypto (public) ---------- | ||
| pub mod crypto { // makes the module public | ||
| use sha2::{Digest, Sha256}; | ||
| use tiny_keccak::{Hasher, Shake}; | ||
|
|
||
| pub fn nssa_kdf( // keeps public | ||
| ss_bytes: [u8; 32], | ||
| epk: &[u8; 33], | ||
| ipk: &[u8; 33], | ||
| commitment: &[u8; 32], | ||
| out_index: u32, | ||
| ) -> [u8; 32] { | ||
| let mut hasher = Sha256::new(); | ||
| 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<u8> { | ||
| let mut sh = Shake::v256(); | ||
| 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); | ||
|
|
||
| let mut ct = vec![0u8; pt.len()]; | ||
| for (i, &b) in pt.iter().enumerate() { | ||
| ct[i] = b ^ ks[i]; | ||
| } | ||
| ct | ||
| } | ||
| } | ||
|
|
||
| // Re-export so callers can `use shake256_demo::{enc_xor_shake256, nssa_kdf};` | ||
| pub use crypto::{enc_xor_shake256, nssa_kdf}; | ||
| // ------------------------------------------------------------ | ||
|
|
||
| // ---------- types ---------- | ||
| #[derive(Debug, Serialize, Deserialize, Clone)] | ||
| pub struct EncInput { | ||
| pub ss_bytes: [u8; 32], | ||
| pub epk_bytes: Bytes33, | ||
| pub ipk_bytes: Bytes33, | ||
| pub commitment: [u8; 32], | ||
| pub out_index: u32, | ||
| } | ||
|
|
||
| // ------------------------------------------------------------ | ||
|
|
||
| pub fn build_info(epk: &Bytes33, ipk: &Bytes33) -> Vec<u8> { | ||
| let mut info = Vec::with_capacity(66); | ||
| info.extend_from_slice(epk.as_ref()); | ||
| info.extend_from_slice(ipk.as_ref()); | ||
| info | ||
| } | ||
|
|
||
| pub fn example_use(input: &EncInput) -> ([u8; 32], Vec<u8>) { | ||
| let info = build_info(&input.epk_bytes, &input.ipk_bytes); | ||
| let k: [u8; 32] = [0u8; 32]; | ||
| (k, info) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a workaround to get rid of the
ser_bytes33module and theBytes33struct.The issues you had with 33 bytes come from trying to derive
SerializeandDeserializeforEncInput. Since you only need a way to convert an instance ofEncInputto bytes, you can make it work by implementing those manually.Then in
src/main.rswhen writing inputs to the guets program you can do the followingInside the guest program, we read it as follows: