From b855bd0c5491d21d2f45f312be0e29647970045f Mon Sep 17 00:00:00 2001 From: Kevin Heavey Date: Tue, 18 Jun 2024 14:08:57 +0900 Subject: [PATCH] Extract curve25519 crate from zk-token-sdk (#951) * extract curve25519 crate * remove obsolete comment * fix Cargo.toml files * fix imports * update lock file * remove unused deps from zk-token-sdk * fmt * add solana-curve25519 patch * add missing override to programs/sbf/Cargo.toml * copy over an allow() * move new crate to curves dir * use workspace version * add back missing dev dep * add missing dependencies to programs/sbf * fmt * move dep to the correct dependency table * remove #[cfg(not(target_os = "solana"))] above errors mod --- Cargo.lock | 16 ++++++++-- Cargo.toml | 3 ++ curves/curve25519/.gitignore | 1 + curves/curve25519/Cargo.toml | 18 +++++++++++ .../curve25519/src}/curve_syscall_traits.rs | 0 .../curve25519/src}/edwards.rs | 6 ++-- curves/curve25519/src/errors.rs | 25 ++++++++++++++++ curves/curve25519/src/lib.rs | 8 +++++ .../curve25519/src}/ristretto.rs | 6 ++-- .../curve25519/src}/scalar.rs | 20 ++++++++++++- programs/bpf_loader/Cargo.toml | 2 +- programs/bpf_loader/src/syscalls/mod.rs | 30 ++++++------------- programs/sbf/Cargo.lock | 17 +++++++++-- programs/sbf/Cargo.toml | 2 ++ programs/sbf/rust/curve25519/Cargo.toml | 1 + programs/sbf/rust/curve25519/src/lib.rs | 2 +- zk-token-sdk/Cargo.toml | 2 +- zk-token-sdk/src/curve25519/errors.rs | 7 ----- zk-token-sdk/src/curve25519/mod.rs | 11 ------- zk-token-sdk/src/encryption/elgamal.rs | 2 +- zk-token-sdk/src/errors.rs | 19 +----------- zk-token-sdk/src/lib.rs | 3 +- zk-token-sdk/src/zk_token_elgamal/convert.rs | 23 ++------------ zk-token-sdk/src/zk_token_elgamal/ops.rs | 6 ++-- .../src/zk_token_elgamal/pod/elgamal.rs | 6 ++-- .../zk_token_elgamal/pod/grouped_elgamal.rs | 12 ++++---- .../src/zk_token_elgamal/pod/instruction.rs | 2 +- .../src/zk_token_elgamal/pod/pedersen.rs | 4 +-- 28 files changed, 141 insertions(+), 113 deletions(-) create mode 100644 curves/curve25519/.gitignore create mode 100644 curves/curve25519/Cargo.toml rename {zk-token-sdk/src/curve25519 => curves/curve25519/src}/curve_syscall_traits.rs (100%) rename {zk-token-sdk/src/curve25519 => curves/curve25519/src}/edwards.rs (99%) create mode 100644 curves/curve25519/src/errors.rs create mode 100644 curves/curve25519/src/lib.rs rename {zk-token-sdk/src/curve25519 => curves/curve25519/src}/ristretto.rs (99%) rename {zk-token-sdk/src/curve25519 => curves/curve25519/src}/scalar.rs (53%) delete mode 100644 zk-token-sdk/src/curve25519/errors.rs delete mode 100644 zk-token-sdk/src/curve25519/mod.rs diff --git a/Cargo.lock b/Cargo.lock index dbb41adcf32a60..05c5b9d28f8f26 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -978,7 +978,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4114279215a005bc675e386011e594e1d9b800918cea18fcadadcce864a2046b" dependencies = [ "borsh-derive 0.10.3", - "hashbrown 0.13.2", + "hashbrown 0.12.3", ] [[package]] @@ -5841,13 +5841,13 @@ dependencies = [ "rand 0.8.5", "scopeguard", "solana-compute-budget", + "solana-curve25519", "solana-measure", "solana-poseidon", "solana-program-runtime", "solana-sdk", "solana-type-overrides", "solana-vote", - "solana-zk-token-sdk", "solana_rbpf", "test-case", "thiserror", @@ -6292,6 +6292,16 @@ dependencies = [ "test-case", ] +[[package]] +name = "solana-curve25519" +version = "2.0.0" +dependencies = [ + "bytemuck", + "curve25519-dalek", + "solana-program", + "thiserror", +] + [[package]] name = "solana-dos" version = "2.0.0" @@ -8067,7 +8077,6 @@ dependencies = [ "bytemuck", "byteorder", "curve25519-dalek", - "getrandom 0.1.16", "itertools 0.12.1", "lazy_static", "merlin", @@ -8078,6 +8087,7 @@ dependencies = [ "serde_derive", "serde_json", "sha3 0.9.1", + "solana-curve25519", "solana-program", "solana-sdk", "subtle", diff --git a/Cargo.toml b/Cargo.toml index 50347b0958ea7b..eafd6cf328ad6f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,6 +31,7 @@ members = [ "connection-cache", "core", "cost-model", + "curves/*", "dos", "download-utils", "entry", @@ -340,6 +341,7 @@ solana-config-program = { path = "programs/config", version = "=2.0.0" } solana-connection-cache = { path = "connection-cache", version = "=2.0.0", default-features = false } solana-core = { path = "core", version = "=2.0.0" } solana-cost-model = { path = "cost-model", version = "=2.0.0" } +solana-curve25519 = { path = "curves/curve25519", version = "=2.0.0" } solana-download-utils = { path = "download-utils", version = "=2.0.0" } solana-entry = { path = "entry", version = "=2.0.0" } solana-faucet = { path = "faucet", version = "=2.0.0" } @@ -490,6 +492,7 @@ crossbeam-epoch = { git = "https://github.com/anza-xyz/crossbeam", rev = "fd279d # # There is a similar override in `programs/sbf/Cargo.toml`. Please keep both # comments and the overrides in sync. +solana-curve25519 = { path = "curves/curve25519" } solana-program = { path = "sdk/program" } solana-zk-sdk = { path = "zk-sdk" } solana-zk-token-sdk = { path = "zk-token-sdk" } diff --git a/curves/curve25519/.gitignore b/curves/curve25519/.gitignore new file mode 100644 index 00000000000000..b645148aa9118c --- /dev/null +++ b/curves/curve25519/.gitignore @@ -0,0 +1 @@ +/farf/ diff --git a/curves/curve25519/Cargo.toml b/curves/curve25519/Cargo.toml new file mode 100644 index 00000000000000..e16d753578afd1 --- /dev/null +++ b/curves/curve25519/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "solana-curve25519" +description = "Solana Curve25519 Syscalls" +documentation = "https://docs.rs/solana-curve25519" +version = { workspace = true } +authors = { workspace = true } +repository = { workspace = true } +homepage = { workspace = true } +license = { workspace = true } +edition = { workspace = true } + +[dependencies] +bytemuck = { workspace = true, features = ["derive"] } +solana-program = { workspace = true } +thiserror = { workspace = true } + +[target.'cfg(not(target_os = "solana"))'.dependencies] +curve25519-dalek = { workspace = true, features = ["serde"] } diff --git a/zk-token-sdk/src/curve25519/curve_syscall_traits.rs b/curves/curve25519/src/curve_syscall_traits.rs similarity index 100% rename from zk-token-sdk/src/curve25519/curve_syscall_traits.rs rename to curves/curve25519/src/curve_syscall_traits.rs diff --git a/zk-token-sdk/src/curve25519/edwards.rs b/curves/curve25519/src/edwards.rs similarity index 99% rename from zk-token-sdk/src/curve25519/edwards.rs rename to curves/curve25519/src/edwards.rs index 0dd019b1910d0a..c77204564bacd1 100644 --- a/zk-token-sdk/src/curve25519/edwards.rs +++ b/curves/curve25519/src/edwards.rs @@ -9,7 +9,7 @@ pub struct PodEdwardsPoint(pub [u8; 32]); mod target_arch { use { super::*, - crate::curve25519::{ + crate::{ curve_syscall_traits::{GroupOperations, MultiScalarMultiplication, PointValidation}, errors::Curve25519Error, scalar::PodScalar, @@ -134,7 +134,7 @@ mod target_arch { mod target_arch { use { super::*, - crate::curve25519::{ + crate::{ curve_syscall_traits::{ADD, CURVE25519_EDWARDS, MUL, SUB}, scalar::PodScalar, }, @@ -245,7 +245,7 @@ mod target_arch { mod tests { use { super::*, - crate::curve25519::scalar::PodScalar, + crate::scalar::PodScalar, curve25519_dalek::{ constants::ED25519_BASEPOINT_POINT as G, edwards::EdwardsPoint, traits::Identity, }, diff --git a/curves/curve25519/src/errors.rs b/curves/curve25519/src/errors.rs new file mode 100644 index 00000000000000..2b43a12145ab42 --- /dev/null +++ b/curves/curve25519/src/errors.rs @@ -0,0 +1,25 @@ +use thiserror::Error; + +#[derive(Error, Clone, Debug, Eq, PartialEq)] +pub enum Curve25519Error { + #[error("pod conversion failed")] + PodConversion, +} + +#[derive(Error, Clone, Debug, Eq, PartialEq)] +pub enum ElGamalError { + #[error("key derivation method not supported")] + DerivationMethodNotSupported, + #[error("seed length too short for derivation")] + SeedLengthTooShort, + #[error("seed length too long for derivation")] + SeedLengthTooLong, + #[error("failed to deserialize ciphertext")] + CiphertextDeserialization, + #[error("failed to deserialize public key")] + PubkeyDeserialization, + #[error("failed to deserialize keypair")] + KeypairDeserialization, + #[error("failed to deserialize secret key")] + SecretKeyDeserialization, +} diff --git a/curves/curve25519/src/lib.rs b/curves/curve25519/src/lib.rs new file mode 100644 index 00000000000000..d0ab9d4709da11 --- /dev/null +++ b/curves/curve25519/src/lib.rs @@ -0,0 +1,8 @@ +#![allow(clippy::arithmetic_side_effects, clippy::op_ref)] +//! Syscall operations for curve25519 + +pub mod curve_syscall_traits; +pub mod edwards; +pub mod errors; +pub mod ristretto; +pub mod scalar; diff --git a/zk-token-sdk/src/curve25519/ristretto.rs b/curves/curve25519/src/ristretto.rs similarity index 99% rename from zk-token-sdk/src/curve25519/ristretto.rs rename to curves/curve25519/src/ristretto.rs index 772441a32aa65f..f2af775fc481a1 100644 --- a/zk-token-sdk/src/curve25519/ristretto.rs +++ b/curves/curve25519/src/ristretto.rs @@ -9,7 +9,7 @@ pub struct PodRistrettoPoint(pub [u8; 32]); mod target_arch { use { super::*, - crate::curve25519::{ + crate::{ curve_syscall_traits::{GroupOperations, MultiScalarMultiplication, PointValidation}, errors::Curve25519Error, scalar::PodScalar, @@ -135,7 +135,7 @@ mod target_arch { mod target_arch { use { super::*, - crate::curve25519::{ + crate::{ curve_syscall_traits::{ADD, CURVE25519_RISTRETTO, MUL, SUB}, scalar::PodScalar, }, @@ -247,7 +247,7 @@ mod target_arch { mod tests { use { super::*, - crate::curve25519::scalar::PodScalar, + crate::scalar::PodScalar, curve25519_dalek::{ constants::RISTRETTO_BASEPOINT_POINT as G, ristretto::RistrettoPoint, traits::Identity, }, diff --git a/zk-token-sdk/src/curve25519/scalar.rs b/curves/curve25519/src/scalar.rs similarity index 53% rename from zk-token-sdk/src/curve25519/scalar.rs rename to curves/curve25519/src/scalar.rs index e154851902a043..bb0cfcf1c90fcb 100644 --- a/zk-token-sdk/src/curve25519/scalar.rs +++ b/curves/curve25519/src/scalar.rs @@ -6,7 +6,11 @@ pub struct PodScalar(pub [u8; 32]); #[cfg(not(target_os = "solana"))] mod target_arch { - use {super::*, crate::curve25519::errors::Curve25519Error, curve25519_dalek::scalar::Scalar}; + use { + super::*, + crate::errors::{Curve25519Error, ElGamalError}, + curve25519_dalek::scalar::Scalar, + }; impl From<&Scalar> for PodScalar { fn from(scalar: &Scalar) -> Self { @@ -21,4 +25,18 @@ mod target_arch { Scalar::from_canonical_bytes(pod.0).ok_or(Curve25519Error::PodConversion) } } + + impl From for PodScalar { + fn from(scalar: Scalar) -> Self { + Self(scalar.to_bytes()) + } + } + + impl TryFrom for Scalar { + type Error = ElGamalError; + + fn try_from(pod: PodScalar) -> Result { + Scalar::from_canonical_bytes(pod.0).ok_or(ElGamalError::CiphertextDeserialization) + } + } } diff --git a/programs/bpf_loader/Cargo.toml b/programs/bpf_loader/Cargo.toml index bf1f6315729972..148c0c92333995 100644 --- a/programs/bpf_loader/Cargo.toml +++ b/programs/bpf_loader/Cargo.toml @@ -16,12 +16,12 @@ libsecp256k1 = { workspace = true } log = { workspace = true } scopeguard = { workspace = true } solana-compute-budget = { workspace = true } +solana-curve25519 = { workspace = true } solana-measure = { workspace = true } solana-poseidon = { workspace = true } solana-program-runtime = { workspace = true } solana-sdk = { workspace = true } solana-type-overrides = { workspace = true } -solana-zk-token-sdk = { workspace = true } solana_rbpf = { workspace = true } thiserror = { workspace = true } diff --git a/programs/bpf_loader/src/syscalls/mod.rs b/programs/bpf_loader/src/syscalls/mod.rs index b0e7fa8ad84b53..304359a67cf895 100644 --- a/programs/bpf_loader/src/syscalls/mod.rs +++ b/programs/bpf_loader/src/syscalls/mod.rs @@ -901,7 +901,7 @@ declare_builtin_function!( _arg5: u64, memory_mapping: &mut MemoryMapping, ) -> Result { - use solana_zk_token_sdk::curve25519::{curve_syscall_traits::*, edwards, ristretto}; + use solana_curve25519::{curve_syscall_traits::*, edwards, ristretto}; match curve_id { CURVE25519_EDWARDS => { let cost = invoke_context @@ -967,9 +967,7 @@ declare_builtin_function!( result_point_addr: u64, memory_mapping: &mut MemoryMapping, ) -> Result { - use solana_zk_token_sdk::curve25519::{ - curve_syscall_traits::*, edwards, ristretto, scalar, - }; + use solana_curve25519::{curve_syscall_traits::*, edwards, ristretto, scalar}; match curve_id { CURVE25519_EDWARDS => match group_op { ADD => { @@ -1195,9 +1193,7 @@ declare_builtin_function!( result_point_addr: u64, memory_mapping: &mut MemoryMapping, ) -> Result { - use solana_zk_token_sdk::curve25519::{ - curve_syscall_traits::*, edwards, ristretto, scalar, - }; + use solana_curve25519::{curve_syscall_traits::*, edwards, ristretto, scalar}; if points_len > 512 { return Err(Box::new(SyscallError::InvalidLength)); @@ -2765,7 +2761,7 @@ mod tests { #[test] fn test_syscall_edwards_curve_point_validation() { - use solana_zk_token_sdk::curve25519::curve_syscall_traits::CURVE25519_EDWARDS; + use solana_curve25519::curve_syscall_traits::CURVE25519_EDWARDS; let config = Config::default(); prepare_mockup!(invoke_context, program_id, bpf_loader::id()); @@ -2838,7 +2834,7 @@ mod tests { #[test] fn test_syscall_ristretto_curve_point_validation() { - use solana_zk_token_sdk::curve25519::curve_syscall_traits::CURVE25519_RISTRETTO; + use solana_curve25519::curve_syscall_traits::CURVE25519_RISTRETTO; let config = Config::default(); prepare_mockup!(invoke_context, program_id, bpf_loader::id()); @@ -2911,9 +2907,7 @@ mod tests { #[test] fn test_syscall_edwards_curve_group_ops() { - use solana_zk_token_sdk::curve25519::curve_syscall_traits::{ - ADD, CURVE25519_EDWARDS, MUL, SUB, - }; + use solana_curve25519::curve_syscall_traits::{ADD, CURVE25519_EDWARDS, MUL, SUB}; let config = Config::default(); prepare_mockup!(invoke_context, program_id, bpf_loader::id()); @@ -3068,9 +3062,7 @@ mod tests { #[test] fn test_syscall_ristretto_curve_group_ops() { - use solana_zk_token_sdk::curve25519::curve_syscall_traits::{ - ADD, CURVE25519_RISTRETTO, MUL, SUB, - }; + use solana_curve25519::curve_syscall_traits::{ADD, CURVE25519_RISTRETTO, MUL, SUB}; let config = Config::default(); prepare_mockup!(invoke_context, program_id, bpf_loader::id()); @@ -3227,9 +3219,7 @@ mod tests { #[test] fn test_syscall_multiscalar_multiplication() { - use solana_zk_token_sdk::curve25519::curve_syscall_traits::{ - CURVE25519_EDWARDS, CURVE25519_RISTRETTO, - }; + use solana_curve25519::curve_syscall_traits::{CURVE25519_EDWARDS, CURVE25519_RISTRETTO}; let config = Config::default(); prepare_mockup!(invoke_context, program_id, bpf_loader::id()); @@ -3335,9 +3325,7 @@ mod tests { #[test] fn test_syscall_multiscalar_multiplication_maximum_length_exceeded() { - use solana_zk_token_sdk::curve25519::curve_syscall_traits::{ - CURVE25519_EDWARDS, CURVE25519_RISTRETTO, - }; + use solana_curve25519::curve_syscall_traits::{CURVE25519_EDWARDS, CURVE25519_RISTRETTO}; let config = Config::default(); prepare_mockup!(invoke_context, program_id, bpf_loader::id()); diff --git a/programs/sbf/Cargo.lock b/programs/sbf/Cargo.lock index c653807660a06a..20f9e83d575a0c 100644 --- a/programs/sbf/Cargo.lock +++ b/programs/sbf/Cargo.lock @@ -727,7 +727,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4114279215a005bc675e386011e594e1d9b800918cea18fcadadcce864a2046b" dependencies = [ "borsh-derive 0.10.3", - "hashbrown 0.13.2", + "hashbrown 0.12.3", ] [[package]] @@ -4793,12 +4793,12 @@ dependencies = [ "log", "scopeguard", "solana-compute-budget", + "solana-curve25519", "solana-measure", "solana-poseidon", "solana-program-runtime", "solana-sdk", "solana-type-overrides", - "solana-zk-token-sdk", "solana_rbpf", "thiserror", ] @@ -5047,6 +5047,16 @@ dependencies = [ "solana-vote-program", ] +[[package]] +name = "solana-curve25519" +version = "2.0.0" +dependencies = [ + "bytemuck", + "curve25519-dalek", + "solana-program", + "thiserror", +] + [[package]] name = "solana-download-utils" version = "2.0.0" @@ -5859,6 +5869,7 @@ dependencies = [ name = "solana-sbf-rust-curve25519" version = "2.0.0" dependencies = [ + "solana-curve25519", "solana-program", "solana-zk-token-sdk", ] @@ -6737,7 +6748,6 @@ dependencies = [ "bytemuck", "byteorder 1.5.0", "curve25519-dalek", - "getrandom 0.1.14", "itertools 0.12.1", "lazy_static", "merlin", @@ -6748,6 +6758,7 @@ dependencies = [ "serde_derive", "serde_json", "sha3 0.9.1", + "solana-curve25519", "solana-program", "solana-sdk", "subtle", diff --git a/programs/sbf/Cargo.toml b/programs/sbf/Cargo.toml index defd20f4716402..555940ef40a900 100644 --- a/programs/sbf/Cargo.toml +++ b/programs/sbf/Cargo.toml @@ -31,6 +31,7 @@ solana-accounts-db = { path = "../../accounts-db", version = "=2.0.0" } solana-bpf-loader-program = { path = "../bpf_loader", version = "=2.0.0" } solana-cli-output = { path = "../../cli-output", version = "=2.0.0" } solana-compute-budget = { path = "../../compute-budget", version = "=2.0.0" } +solana-curve25519 = { path = "../../curves/curve25519", version = "=2.0.0" } solana-ledger = { path = "../../ledger", version = "=2.0.0" } solana-logger = { path = "../../logger", version = "=2.0.0" } solana-measure = { path = "../../measure", version = "=2.0.0" } @@ -218,5 +219,6 @@ members = [ # # There is a similar override in `../../Cargo.toml`. Please keep both comments # and the overrides in sync. +solana-curve25519 = { path = "../../curves/curve25519" } solana-program = { path = "../../sdk/program" } solana-zk-token-sdk = { path = "../../zk-token-sdk" } diff --git a/programs/sbf/rust/curve25519/Cargo.toml b/programs/sbf/rust/curve25519/Cargo.toml index c75477788e0dca..ad555810ff203e 100644 --- a/programs/sbf/rust/curve25519/Cargo.toml +++ b/programs/sbf/rust/curve25519/Cargo.toml @@ -9,6 +9,7 @@ license = { workspace = true } edition = { workspace = true } [dependencies] +solana-curve25519 = { workspace = true } solana-program = { workspace = true } solana-zk-token-sdk = { workspace = true } diff --git a/programs/sbf/rust/curve25519/src/lib.rs b/programs/sbf/rust/curve25519/src/lib.rs index a8096d65b34710..42718278c2c685 100644 --- a/programs/sbf/rust/curve25519/src/lib.rs +++ b/programs/sbf/rust/curve25519/src/lib.rs @@ -2,8 +2,8 @@ extern crate solana_program; use { + solana_curve25519::{edwards, ristretto, scalar}, solana_program::{custom_heap_default, custom_panic_default, msg}, - solana_zk_token_sdk::curve25519::{edwards, ristretto, scalar}, }; #[no_mangle] diff --git a/zk-token-sdk/Cargo.toml b/zk-token-sdk/Cargo.toml index fb3dc25649b5a8..2cc9b4bcb5d15f 100644 --- a/zk-token-sdk/Cargo.toml +++ b/zk-token-sdk/Cargo.toml @@ -14,6 +14,7 @@ base64 = { workspace = true } bytemuck = { workspace = true, features = ["derive"] } num-derive = { workspace = true } num-traits = { workspace = true } +solana-curve25519 = { workspace = true } solana-program = { workspace = true } thiserror = { workspace = true } @@ -25,7 +26,6 @@ aes-gcm-siv = { workspace = true } bincode = { workspace = true } byteorder = { workspace = true } curve25519-dalek = { workspace = true, features = ["serde"] } -getrandom = { version = "0.1", features = ["dummy"] } itertools = { workspace = true } lazy_static = { workspace = true } merlin = { workspace = true } diff --git a/zk-token-sdk/src/curve25519/errors.rs b/zk-token-sdk/src/curve25519/errors.rs deleted file mode 100644 index 2aabc732a39006..00000000000000 --- a/zk-token-sdk/src/curve25519/errors.rs +++ /dev/null @@ -1,7 +0,0 @@ -use thiserror::Error; - -#[derive(Error, Clone, Debug, Eq, PartialEq)] -pub enum Curve25519Error { - #[error("pod conversion failed")] - PodConversion, -} diff --git a/zk-token-sdk/src/curve25519/mod.rs b/zk-token-sdk/src/curve25519/mod.rs deleted file mode 100644 index 19c4aa1388aa9a..00000000000000 --- a/zk-token-sdk/src/curve25519/mod.rs +++ /dev/null @@ -1,11 +0,0 @@ -//! Syscall operations for curve25519 -//! -//! This module lives inside the zk-token-sdk for now, but should move to a general location since -//! it is independent of zk-tokens. - -pub mod curve_syscall_traits; -pub mod edwards; -#[cfg(not(target_os = "solana"))] -pub mod errors; -pub mod ristretto; -pub mod scalar; diff --git a/zk-token-sdk/src/encryption/elgamal.rs b/zk-token-sdk/src/encryption/elgamal.rs index 7f0a48820a6f35..696bedd369153d 100644 --- a/zk-token-sdk/src/encryption/elgamal.rs +++ b/zk-token-sdk/src/encryption/elgamal.rs @@ -21,7 +21,6 @@ use { Pedersen, PedersenCommitment, PedersenOpening, G, H, PEDERSEN_COMMITMENT_LEN, }, }, - errors::ElGamalError, RISTRETTO_POINT_LEN, SCALAR_LEN, }, base64::{prelude::BASE64_STANDARD, Engine}, @@ -32,6 +31,7 @@ use { traits::Identity, }, serde::{Deserialize, Serialize}, + solana_curve25519::errors::ElGamalError, solana_sdk::{ derivation_path::DerivationPath, signature::Signature, diff --git a/zk-token-sdk/src/errors.rs b/zk-token-sdk/src/errors.rs index 2d3adb74635574..98a36c585ba045 100644 --- a/zk-token-sdk/src/errors.rs +++ b/zk-token-sdk/src/errors.rs @@ -3,6 +3,7 @@ use crate::range_proof::errors::RangeProofGenerationError; use { crate::{range_proof::errors::RangeProofVerificationError, sigma_proofs::errors::*}, + solana_curve25519::errors::ElGamalError, thiserror::Error, }; @@ -18,24 +19,6 @@ pub enum AuthenticatedEncryptionError { Deserialization, } -#[derive(Error, Clone, Debug, Eq, PartialEq)] -pub enum ElGamalError { - #[error("key derivation method not supported")] - DerivationMethodNotSupported, - #[error("seed length too short for derivation")] - SeedLengthTooShort, - #[error("seed length too long for derivation")] - SeedLengthTooLong, - #[error("failed to deserialize ciphertext")] - CiphertextDeserialization, - #[error("failed to deserialize public key")] - PubkeyDeserialization, - #[error("failed to deserialize keypair")] - KeypairDeserialization, - #[error("failed to deserialize secret key")] - SecretKeyDeserialization, -} - #[cfg(not(target_os = "solana"))] #[derive(Error, Clone, Debug, Eq, PartialEq)] pub enum ProofGenerationError { diff --git a/zk-token-sdk/src/lib.rs b/zk-token-sdk/src/lib.rs index 2946e177358adc..83d8b188366e7d 100644 --- a/zk-token-sdk/src/lib.rs +++ b/zk-token-sdk/src/lib.rs @@ -17,6 +17,8 @@ // // `clippy::op_ref` is turned off to prevent clippy from warning that this is not idiomatic code. +pub use solana_curve25519 as curve25519; + #[cfg(not(target_os = "solana"))] #[macro_use] pub(crate) mod macros; @@ -27,7 +29,6 @@ mod sigma_proofs; #[cfg(not(target_os = "solana"))] mod transcript; -pub mod curve25519; pub mod errors; pub mod instruction; pub mod zk_token_elgamal; diff --git a/zk-token-sdk/src/zk_token_elgamal/convert.rs b/zk-token-sdk/src/zk_token_elgamal/convert.rs index a437c817b41e72..286383cc1cd98b 100644 --- a/zk-token-sdk/src/zk_token_elgamal/convert.rs +++ b/zk-token-sdk/src/zk_token_elgamal/convert.rs @@ -1,4 +1,4 @@ -use {super::pod, crate::curve25519::ristretto::PodRistrettoPoint}; +use {super::pod, solana_curve25519::ristretto::PodRistrettoPoint}; impl From<(pod::PedersenCommitment, pod::DecryptHandle)> for pod::ElGamalCiphertext { fn from((commitment, handle): (pod::PedersenCommitment, pod::DecryptHandle)) -> Self { @@ -47,26 +47,7 @@ impl From for pod::DecryptHandle { #[cfg(not(target_os = "solana"))] mod target_arch { - use { - super::pod, - crate::{curve25519::scalar::PodScalar, errors::ElGamalError}, - curve25519_dalek::{ristretto::CompressedRistretto, scalar::Scalar}, - std::convert::TryFrom, - }; - - impl From for PodScalar { - fn from(scalar: Scalar) -> Self { - Self(scalar.to_bytes()) - } - } - - impl TryFrom for Scalar { - type Error = ElGamalError; - - fn try_from(pod: PodScalar) -> Result { - Scalar::from_canonical_bytes(pod.0).ok_or(ElGamalError::CiphertextDeserialization) - } - } + use {super::pod, curve25519_dalek::ristretto::CompressedRistretto}; impl From for pod::CompressedRistretto { fn from(cr: CompressedRistretto) -> Self { diff --git a/zk-token-sdk/src/zk_token_elgamal/ops.rs b/zk-token-sdk/src/zk_token_elgamal/ops.rs index 38da19c1c2e7f1..d0cd41cc799e02 100644 --- a/zk-token-sdk/src/zk_token_elgamal/ops.rs +++ b/zk-token-sdk/src/zk_token_elgamal/ops.rs @@ -1,9 +1,9 @@ -use crate::{ - curve25519::{ +use { + crate::zk_token_elgamal::pod, + solana_curve25519::{ ristretto::{add_ristretto, multiply_ristretto, subtract_ristretto, PodRistrettoPoint}, scalar::PodScalar, }, - zk_token_elgamal::pod, }; const SHIFT_BITS: usize = 16; diff --git a/zk-token-sdk/src/zk_token_elgamal/pod/elgamal.rs b/zk-token-sdk/src/zk_token_elgamal/pod/elgamal.rs index 64c3e794b4816b..8a329949a984d5 100644 --- a/zk-token-sdk/src/zk_token_elgamal/pod/elgamal.rs +++ b/zk-token-sdk/src/zk_token_elgamal/pod/elgamal.rs @@ -2,11 +2,9 @@ #[cfg(not(target_os = "solana"))] use { - crate::{ - encryption::elgamal::{self as decoded}, - errors::ElGamalError, - }, + crate::encryption::elgamal::{self as decoded}, curve25519_dalek::ristretto::CompressedRistretto, + solana_curve25519::errors::ElGamalError, }; use { crate::{ diff --git a/zk-token-sdk/src/zk_token_elgamal/pod/grouped_elgamal.rs b/zk-token-sdk/src/zk_token_elgamal/pod/grouped_elgamal.rs index c7e820fcd04508..8f7e6540acd543 100644 --- a/zk-token-sdk/src/zk_token_elgamal/pod/grouped_elgamal.rs +++ b/zk-token-sdk/src/zk_token_elgamal/pod/grouped_elgamal.rs @@ -3,14 +3,12 @@ #[cfg(not(target_os = "solana"))] use crate::encryption::grouped_elgamal::GroupedElGamalCiphertext; use { - crate::{ - errors::ElGamalError, - zk_token_elgamal::pod::{ - elgamal::{ElGamalCiphertext, DECRYPT_HANDLE_LEN, ELGAMAL_CIPHERTEXT_LEN}, - pedersen::{PedersenCommitment, PEDERSEN_COMMITMENT_LEN}, - Pod, Zeroable, - }, + crate::zk_token_elgamal::pod::{ + elgamal::{ElGamalCiphertext, DECRYPT_HANDLE_LEN, ELGAMAL_CIPHERTEXT_LEN}, + pedersen::{PedersenCommitment, PEDERSEN_COMMITMENT_LEN}, + Pod, Zeroable, }, + solana_curve25519::errors::ElGamalError, std::fmt, }; diff --git a/zk-token-sdk/src/zk_token_elgamal/pod/instruction.rs b/zk-token-sdk/src/zk_token_elgamal/pod/instruction.rs index e29e3a500551ee..6a763633b3eb13 100644 --- a/zk-token-sdk/src/zk_token_elgamal/pod/instruction.rs +++ b/zk-token-sdk/src/zk_token_elgamal/pod/instruction.rs @@ -3,7 +3,7 @@ use crate::zk_token_elgamal::pod::{ Zeroable, }; #[cfg(not(target_os = "solana"))] -use crate::{errors::ElGamalError, instruction::transfer as decoded}; +use {crate::instruction::transfer as decoded, solana_curve25519::errors::ElGamalError}; #[derive(Clone, Copy, Pod, Zeroable)] #[repr(C)] diff --git a/zk-token-sdk/src/zk_token_elgamal/pod/pedersen.rs b/zk-token-sdk/src/zk_token_elgamal/pod/pedersen.rs index d27f307f43df2c..831bb7ce75ea95 100644 --- a/zk-token-sdk/src/zk_token_elgamal/pod/pedersen.rs +++ b/zk-token-sdk/src/zk_token_elgamal/pod/pedersen.rs @@ -2,8 +2,8 @@ #[cfg(not(target_os = "solana"))] use { - crate::{encryption::pedersen as decoded, errors::ElGamalError}, - curve25519_dalek::ristretto::CompressedRistretto, + crate::encryption::pedersen as decoded, curve25519_dalek::ristretto::CompressedRistretto, + solana_curve25519::errors::ElGamalError, }; use { crate::{