-
Notifications
You must be signed in to change notification settings - Fork 18
[zk-sdk-pod] Split out pod types in the zk-sdk into a separate zk-sdk-pod crate #135
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
Draft
samkim-crypto
wants to merge
7
commits into
solana-program:main
Choose a base branch
from
samkim-crypto:zk-sdk-pod
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.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3e50b16
create `solana-zk-sdk-pod` and migrate encryption pod types
samkim-crypto 6752548
clean up pod encryption types in zk-sdk
samkim-crypto 835bc7e
migrate sigma proof pod types
samkim-crypto 53994e0
migrate range proof pod types
samkim-crypto 03a0521
migrate proof data types
samkim-crypto d39b621
cargo fmt and clippy
samkim-crypto bbb0546
fix sbf visibililty
samkim-crypto 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| [workspace] | ||
| resolver = "2" | ||
| members = ["zk-sdk", "zk-sdk-wasm-js"] | ||
| members = ["zk-sdk", "zk-sdk-pod", "zk-sdk-wasm-js"] | ||
|
|
||
| [workspace.package] | ||
| authors = ["Anza Maintainers <[email protected]>"] | ||
|
|
@@ -53,6 +53,7 @@ solana-seed-phrase = "3.0.0" | |
| solana-signature = { version = "3.0.0", default-features = false } | ||
| solana-signer = "3.0.0" | ||
| solana-zk-sdk = { path = "zk-sdk", version = "4.0.0" } | ||
| solana-zk-sdk-pod = { path = "zk-sdk-pod", version = "0.1.0" } | ||
| solana-zk-sdk-wasm-js = { path = "zk-sdk-wasm-js", version = "0.1.0" } | ||
| subtle = "2.6.1" | ||
| thiserror = "2.0.17" | ||
|
|
||
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,25 @@ | ||
| [package] | ||
| name = "solana-zk-sdk-pod" | ||
| description = "Solana ZK SDK Plain Old Data (Pod)" | ||
| documentation = "https://docs.rs/solana-zk-sdk-pod" | ||
| version = "0.1.0" | ||
| authors = { workspace = true } | ||
| repository = { workspace = true } | ||
| homepage = { workspace = true } | ||
| license = { workspace = true } | ||
| edition = { workspace = true } | ||
|
|
||
| [dependencies] | ||
| base64 = { workspace = true } | ||
| bytemuck = { workspace = true } | ||
| bytemuck_derive = { workspace = true } | ||
| thiserror = { workspace = true } | ||
|
|
||
| [lib] | ||
| crate-type = ["rlib"] | ||
|
|
||
| [package.metadata.docs.rs] | ||
| targets = ["x86_64-unknown-linux-gnu"] | ||
|
|
||
| [lints] | ||
| workspace = true |
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
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,104 @@ | ||
| //! Plain Old Data types for the ElGamal encryption scheme. | ||
|
|
||
| use { | ||
| crate::{ | ||
| encryption::{DECRYPT_HANDLE_LEN, ELGAMAL_CIPHERTEXT_LEN, ELGAMAL_PUBKEY_LEN}, | ||
| macros::{impl_from_bytes, impl_from_str}, | ||
| }, | ||
| base64::{prelude::BASE64_STANDARD, Engine}, | ||
| bytemuck::Zeroable, | ||
| std::fmt, | ||
| }; | ||
|
|
||
| /// Maximum length of a base64 encoded ElGamal public key | ||
| const ELGAMAL_PUBKEY_MAX_BASE64_LEN: usize = 44; | ||
|
|
||
| /// Maximum length of a base64 encoded ElGamal ciphertext | ||
| const ELGAMAL_CIPHERTEXT_MAX_BASE64_LEN: usize = 88; | ||
|
|
||
| /// Maximum length of a base64 encoded ElGamal decrypt handle | ||
| const DECRYPT_HANDLE_MAX_BASE64_LEN: usize = 44; | ||
|
|
||
| /// The `ElGamalCiphertext` type as a `Pod`. | ||
| #[derive(Clone, Copy, bytemuck_derive::Pod, bytemuck_derive::Zeroable, PartialEq, Eq)] | ||
| #[repr(transparent)] | ||
| pub struct PodElGamalCiphertext(pub [u8; ELGAMAL_CIPHERTEXT_LEN]); | ||
|
|
||
| impl fmt::Debug for PodElGamalCiphertext { | ||
| fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
| write!(f, "{:?}", self.0) | ||
| } | ||
| } | ||
|
|
||
| impl fmt::Display for PodElGamalCiphertext { | ||
| fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
| write!(f, "{}", BASE64_STANDARD.encode(self.0)) | ||
| } | ||
| } | ||
|
|
||
| impl Default for PodElGamalCiphertext { | ||
| fn default() -> Self { | ||
| Self::zeroed() | ||
| } | ||
| } | ||
|
|
||
| impl_from_str!( | ||
| TYPE = PodElGamalCiphertext, | ||
| BYTES_LEN = ELGAMAL_CIPHERTEXT_LEN, | ||
| BASE64_LEN = ELGAMAL_CIPHERTEXT_MAX_BASE64_LEN | ||
| ); | ||
|
|
||
| impl_from_bytes!( | ||
| TYPE = PodElGamalCiphertext, | ||
| BYTES_LEN = ELGAMAL_CIPHERTEXT_LEN | ||
| ); | ||
|
|
||
| /// The `ElGamalPubkey` type as a `Pod`. | ||
| #[derive(Clone, Copy, Default, bytemuck_derive::Pod, bytemuck_derive::Zeroable, PartialEq, Eq)] | ||
| #[repr(transparent)] | ||
| pub struct PodElGamalPubkey(pub [u8; ELGAMAL_PUBKEY_LEN]); | ||
|
|
||
| impl fmt::Debug for PodElGamalPubkey { | ||
| fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
| write!(f, "{:?}", self.0) | ||
| } | ||
| } | ||
|
|
||
| impl fmt::Display for PodElGamalPubkey { | ||
| fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
| write!(f, "{}", BASE64_STANDARD.encode(self.0)) | ||
| } | ||
| } | ||
|
|
||
| impl_from_str!( | ||
| TYPE = PodElGamalPubkey, | ||
| BYTES_LEN = ELGAMAL_PUBKEY_LEN, | ||
| BASE64_LEN = ELGAMAL_PUBKEY_MAX_BASE64_LEN | ||
| ); | ||
|
|
||
| impl_from_bytes!(TYPE = PodElGamalPubkey, BYTES_LEN = ELGAMAL_PUBKEY_LEN); | ||
|
|
||
| /// The `DecryptHandle` type as a `Pod`. | ||
| #[derive(Clone, Copy, Default, bytemuck_derive::Pod, bytemuck_derive::Zeroable, PartialEq, Eq)] | ||
| #[repr(transparent)] | ||
| pub struct PodDecryptHandle(pub [u8; DECRYPT_HANDLE_LEN]); | ||
|
|
||
| impl fmt::Debug for PodDecryptHandle { | ||
| fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
| write!(f, "{:?}", self.0) | ||
| } | ||
| } | ||
|
|
||
| impl fmt::Display for PodDecryptHandle { | ||
| fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
| write!(f, "{}", BASE64_STANDARD.encode(self.0)) | ||
| } | ||
| } | ||
|
|
||
| impl_from_str!( | ||
| TYPE = PodDecryptHandle, | ||
| BYTES_LEN = DECRYPT_HANDLE_LEN, | ||
| BASE64_LEN = DECRYPT_HANDLE_MAX_BASE64_LEN | ||
| ); | ||
|
|
||
| impl_from_bytes!(TYPE = PodDecryptHandle, BYTES_LEN = DECRYPT_HANDLE_LEN); |
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,138 @@ | ||
| //! Plain Old Data types for the Grouped ElGamal encryption scheme. | ||
|
|
||
| use { | ||
| crate::{ | ||
| encryption::{ | ||
| elgamal::PodElGamalCiphertext, pedersen::PodPedersenCommitment, DECRYPT_HANDLE_LEN, | ||
| ELGAMAL_CIPHERTEXT_LEN, PEDERSEN_COMMITMENT_LEN, | ||
| }, | ||
| macros::{impl_from_bytes, impl_from_str}, | ||
| }, | ||
| base64::{prelude::BASE64_STANDARD, Engine}, | ||
| bytemuck::Zeroable, | ||
| std::fmt, | ||
| }; | ||
|
|
||
| /// Maximum length of a base64 encoded grouped ElGamal ciphertext with 2 handles | ||
| const GROUPED_ELGAMAL_CIPHERTEXT_2_HANDLES_MAX_BASE64_LEN: usize = 132; | ||
|
|
||
| /// Maximum length of a base64 encoded grouped ElGamal ciphertext with 3 handles | ||
| const GROUPED_ELGAMAL_CIPHERTEXT_3_HANDLES_MAX_BASE64_LEN: usize = 176; | ||
|
|
||
| macro_rules! impl_extract { | ||
| (TYPE = $type:ident) => { | ||
| impl $type { | ||
| /// Extract the commitment component from a grouped ciphertext | ||
| pub fn extract_commitment(&self) -> PodPedersenCommitment { | ||
| // `GROUPED_ELGAMAL_CIPHERTEXT_2_HANDLES` guaranteed to be at least `PEDERSEN_COMMITMENT_LEN` | ||
| let commitment = self.0[..PEDERSEN_COMMITMENT_LEN].try_into().unwrap(); | ||
| PodPedersenCommitment(commitment) | ||
| } | ||
|
|
||
| /// Extract a regular ElGamal ciphertext using the decrypt handle at a specified index. | ||
| pub fn try_extract_ciphertext( | ||
| &self, | ||
| index: usize, | ||
| ) -> Result<PodElGamalCiphertext, crate::errors::PodParseError> { | ||
| let mut ciphertext_bytes = [0u8; ELGAMAL_CIPHERTEXT_LEN]; | ||
| ciphertext_bytes[..PEDERSEN_COMMITMENT_LEN] | ||
| .copy_from_slice(&self.0[..PEDERSEN_COMMITMENT_LEN]); | ||
|
|
||
| let handle_start = DECRYPT_HANDLE_LEN | ||
| .checked_mul(index) | ||
| .and_then(|n| n.checked_add(PEDERSEN_COMMITMENT_LEN)) | ||
| .ok_or(crate::errors::PodParseError::GroupedCiphertextIndexOutOfBounds)?; | ||
| let handle_end = handle_start | ||
| .checked_add(DECRYPT_HANDLE_LEN) | ||
| .ok_or(crate::errors::PodParseError::GroupedCiphertextIndexOutOfBounds)?; | ||
| ciphertext_bytes[PEDERSEN_COMMITMENT_LEN..].copy_from_slice( | ||
| self.0 | ||
| .get(handle_start..handle_end) | ||
| .ok_or(crate::errors::PodParseError::GroupedCiphertextIndexOutOfBounds)?, | ||
| ); | ||
|
|
||
| Ok(PodElGamalCiphertext(ciphertext_bytes)) | ||
| } | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| /// Byte length of a grouped ElGamal ciphertext with 2 handles | ||
| const GROUPED_ELGAMAL_CIPHERTEXT_2_HANDLES: usize = | ||
| PEDERSEN_COMMITMENT_LEN + DECRYPT_HANDLE_LEN + DECRYPT_HANDLE_LEN; | ||
|
|
||
| /// Byte length of a grouped ElGamal ciphertext with 3 handles | ||
| const GROUPED_ELGAMAL_CIPHERTEXT_3_HANDLES: usize = | ||
| PEDERSEN_COMMITMENT_LEN + DECRYPT_HANDLE_LEN + DECRYPT_HANDLE_LEN + DECRYPT_HANDLE_LEN; | ||
|
|
||
| /// The `GroupedElGamalCiphertext` type with two decryption handles as a `Pod` | ||
| #[derive(Clone, Copy, bytemuck_derive::Pod, bytemuck_derive::Zeroable, PartialEq, Eq)] | ||
| #[repr(transparent)] | ||
| pub struct PodGroupedElGamalCiphertext2Handles(pub [u8; GROUPED_ELGAMAL_CIPHERTEXT_2_HANDLES]); | ||
|
|
||
| impl fmt::Debug for PodGroupedElGamalCiphertext2Handles { | ||
| fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
| write!(f, "{:?}", self.0) | ||
| } | ||
| } | ||
|
|
||
| impl Default for PodGroupedElGamalCiphertext2Handles { | ||
| fn default() -> Self { | ||
| Self::zeroed() | ||
| } | ||
| } | ||
|
|
||
| impl fmt::Display for PodGroupedElGamalCiphertext2Handles { | ||
| fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
| write!(f, "{}", BASE64_STANDARD.encode(self.0)) | ||
| } | ||
| } | ||
|
|
||
| impl_from_str!( | ||
| TYPE = PodGroupedElGamalCiphertext2Handles, | ||
| BYTES_LEN = GROUPED_ELGAMAL_CIPHERTEXT_2_HANDLES, | ||
| BASE64_LEN = GROUPED_ELGAMAL_CIPHERTEXT_2_HANDLES_MAX_BASE64_LEN | ||
| ); | ||
|
|
||
| impl_from_bytes!( | ||
| TYPE = PodGroupedElGamalCiphertext2Handles, | ||
| BYTES_LEN = GROUPED_ELGAMAL_CIPHERTEXT_2_HANDLES | ||
| ); | ||
|
|
||
| impl_extract!(TYPE = PodGroupedElGamalCiphertext2Handles); | ||
|
|
||
| /// The `GroupedElGamalCiphertext` type with three decryption handles as a `Pod` | ||
| #[derive(Clone, Copy, bytemuck_derive::Pod, bytemuck_derive::Zeroable, PartialEq, Eq)] | ||
| #[repr(transparent)] | ||
| pub struct PodGroupedElGamalCiphertext3Handles(pub [u8; GROUPED_ELGAMAL_CIPHERTEXT_3_HANDLES]); | ||
|
|
||
| impl fmt::Debug for PodGroupedElGamalCiphertext3Handles { | ||
| fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
| write!(f, "{:?}", self.0) | ||
| } | ||
| } | ||
|
|
||
| impl Default for PodGroupedElGamalCiphertext3Handles { | ||
| fn default() -> Self { | ||
| Self::zeroed() | ||
| } | ||
| } | ||
|
|
||
| impl fmt::Display for PodGroupedElGamalCiphertext3Handles { | ||
| fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
| write!(f, "{}", BASE64_STANDARD.encode(self.0)) | ||
| } | ||
| } | ||
|
|
||
| impl_from_str!( | ||
| TYPE = PodGroupedElGamalCiphertext3Handles, | ||
| BYTES_LEN = GROUPED_ELGAMAL_CIPHERTEXT_3_HANDLES, | ||
| BASE64_LEN = GROUPED_ELGAMAL_CIPHERTEXT_3_HANDLES_MAX_BASE64_LEN | ||
| ); | ||
|
|
||
| impl_from_bytes!( | ||
| TYPE = PodGroupedElGamalCiphertext3Handles, | ||
| BYTES_LEN = GROUPED_ELGAMAL_CIPHERTEXT_3_HANDLES | ||
| ); | ||
|
|
||
| impl_extract!(TYPE = PodGroupedElGamalCiphertext3Handles); |
Oops, something went wrong.
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.
The code in
zk-sdk-podis mostly a copy of the original pod logic in thezk-sdk. The only main difference is that the enclosed byte array has apubvisibility while it used to havepub(crate)visibility inside thezk-sdk.