Skip to content

Commit

Permalink
Extract curve25519 crate from zk-token-sdk (solana-labs#951)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
kevinheavey authored Jun 18, 2024
1 parent 4e0afd6 commit b855bd0
Show file tree
Hide file tree
Showing 28 changed files with 141 additions and 113 deletions.
16 changes: 13 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ members = [
"connection-cache",
"core",
"cost-model",
"curves/*",
"dos",
"download-utils",
"entry",
Expand Down Expand Up @@ -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" }
Expand Down Expand Up @@ -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" }
Expand Down
1 change: 1 addition & 0 deletions curves/curve25519/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/farf/
18 changes: 18 additions & 0 deletions curves/curve25519/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"] }
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
},
Expand Down Expand Up @@ -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,
},
Expand Down
25 changes: 25 additions & 0 deletions curves/curve25519/src/errors.rs
Original file line number Diff line number Diff line change
@@ -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,
}
8 changes: 8 additions & 0 deletions curves/curve25519/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
},
Expand Down Expand Up @@ -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,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -21,4 +25,18 @@ mod target_arch {
Scalar::from_canonical_bytes(pod.0).ok_or(Curve25519Error::PodConversion)
}
}

impl From<Scalar> for PodScalar {
fn from(scalar: Scalar) -> Self {
Self(scalar.to_bytes())
}
}

impl TryFrom<PodScalar> for Scalar {
type Error = ElGamalError;

fn try_from(pod: PodScalar) -> Result<Self, Self::Error> {
Scalar::from_canonical_bytes(pod.0).ok_or(ElGamalError::CiphertextDeserialization)
}
}
}
2 changes: 1 addition & 1 deletion programs/bpf_loader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
30 changes: 9 additions & 21 deletions programs/bpf_loader/src/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,7 @@ declare_builtin_function!(
_arg5: u64,
memory_mapping: &mut MemoryMapping,
) -> Result<u64, Error> {
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
Expand Down Expand Up @@ -967,9 +967,7 @@ declare_builtin_function!(
result_point_addr: u64,
memory_mapping: &mut MemoryMapping,
) -> Result<u64, Error> {
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 => {
Expand Down Expand Up @@ -1195,9 +1193,7 @@ declare_builtin_function!(
result_point_addr: u64,
memory_mapping: &mut MemoryMapping,
) -> Result<u64, Error> {
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));
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down
17 changes: 14 additions & 3 deletions programs/sbf/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b855bd0

Please sign in to comment.