Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions crates/keys/src/algorithm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use serde::{Deserialize, Serialize};

#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum CryptoAlgorithm {
Ed25519,
Secp256k1,
Secp256r1,
}
3 changes: 3 additions & 0 deletions crates/keys/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
mod algorithm;
mod payload;
mod signatures;
mod signing_keys;
mod verifying_keys;

pub use algorithm::*;
pub use signatures::*;
pub use signing_keys::*;
pub use verifying_keys::*;
Expand Down
11 changes: 11 additions & 0 deletions crates/keys/src/payload.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use prism_serde::raw_or_b64;
use serde::{Deserialize, Serialize};

use crate::CryptoAlgorithm;

#[derive(Serialize, Deserialize)]
pub struct CryptoPayload {
pub algorithm: CryptoAlgorithm,
#[serde(with = "raw_or_b64")]
pub bytes: Vec<u8>,
}
28 changes: 14 additions & 14 deletions crates/keys/src/signatures.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use anyhow::{bail, Result};
use anyhow::Result;
use ed25519_consensus::Signature as Ed25519Signature;
use p256::ecdsa::Signature as Secp256r1Signature;
use secp256k1::ecdsa::Signature as Secp256k1Signature;

use prism_serde::CryptoPayload;
use serde::{Deserialize, Serialize};
use std::{self};

use crate::{payload::CryptoPayload, CryptoAlgorithm};

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Default)]
#[serde(try_from = "CryptoPayload", into = "CryptoPayload")]
pub enum Signature {
Expand All @@ -27,27 +28,26 @@ impl Signature {
}
}

pub fn from_algorithm_and_bytes(algorithm: &str, bytes: &[u8]) -> Result<Self> {
pub fn from_algorithm_and_bytes(algorithm: CryptoAlgorithm, bytes: &[u8]) -> Result<Self> {
match algorithm {
"ed25519" => {
CryptoAlgorithm::Ed25519 => {
Ed25519Signature::try_from(bytes).map(Signature::Ed25519).map_err(|e| e.into())
}
"secp256k1" => {
CryptoAlgorithm::Secp256k1 => {
Secp256k1Signature::from_der(bytes).map(Signature::Secp256k1).map_err(|e| e.into())
}
"secp256r1" => {
CryptoAlgorithm::Secp256r1 => {
Secp256r1Signature::from_der(bytes).map(Signature::Secp256r1).map_err(|e| e.into())
}
_ => bail!("Unexpected algorithm for Signature"),
}
}

pub fn algorithm(&self) -> &'static str {
pub fn algorithm(&self) -> CryptoAlgorithm {
match self {
Signature::Ed25519(_) => "ed25519",
Signature::Secp256k1(_) => "secp256k1",
Signature::Secp256r1(_) => "secp256r1",
Signature::Placeholder => "placeholder",
Signature::Ed25519(_) => CryptoAlgorithm::Ed25519,
Signature::Secp256k1(_) => CryptoAlgorithm::Secp256k1,
Signature::Secp256r1(_) => CryptoAlgorithm::Secp256r1,
Signature::Placeholder => CryptoAlgorithm::Ed25519,
}
}
}
Expand All @@ -56,14 +56,14 @@ impl TryFrom<CryptoPayload> for Signature {
type Error = anyhow::Error;

fn try_from(value: CryptoPayload) -> std::result::Result<Self, Self::Error> {
Signature::from_algorithm_and_bytes(&value.algorithm, &value.bytes)
Signature::from_algorithm_and_bytes(value.algorithm, &value.bytes)
}
}

impl From<Signature> for CryptoPayload {
fn from(signature: Signature) -> Self {
CryptoPayload {
algorithm: signature.algorithm().to_string(),
algorithm: signature.algorithm(),
bytes: signature.to_bytes(),
}
}
Expand Down
26 changes: 12 additions & 14 deletions crates/keys/src/signing_keys.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::{bail, Result};
use anyhow::Result;
use ed25519_consensus::SigningKey as Ed25519SigningKey;
use p256::ecdsa::{
signature::DigestSigner, Signature as Secp256r1Signature, SigningKey as Secp256r1SigningKey,
Expand All @@ -8,8 +8,7 @@ use secp256k1::{Message as Secp256k1Message, SecretKey as Secp256k1SigningKey, S

use sha2::Digest as _;

use crate::{Signature, VerifyingKey};
use prism_serde::CryptoPayload;
use crate::{payload::CryptoPayload, CryptoAlgorithm, Signature, VerifyingKey};

#[derive(Clone, Debug)]
pub enum SigningKey {
Expand Down Expand Up @@ -43,26 +42,25 @@ impl SigningKey {
}
}

pub fn from_algorithm_and_bytes(algorithm: &str, bytes: &[u8]) -> Result<Self> {
pub fn from_algorithm_and_bytes(algorithm: CryptoAlgorithm, bytes: &[u8]) -> Result<Self> {
match algorithm {
"ed25519" => Ed25519SigningKey::try_from(bytes)
CryptoAlgorithm::Ed25519 => Ed25519SigningKey::try_from(bytes)
.map(|sk| SigningKey::Ed25519(Box::new(sk)))
.map_err(|e| e.into()),
"secp256k1" => Secp256k1SigningKey::from_slice(bytes)
CryptoAlgorithm::Secp256k1 => Secp256k1SigningKey::from_slice(bytes)
.map(SigningKey::Secp256k1)
.map_err(|e| e.into()),
"secp256r1" => Secp256r1SigningKey::from_slice(bytes)
CryptoAlgorithm::Secp256r1 => Secp256r1SigningKey::from_slice(bytes)
.map(SigningKey::Secp256r1)
.map_err(|e| e.into()),
_ => bail!("Unexpected algorithm for VerifyingKey"),
}
}

pub fn algorithm(&self) -> &'static str {
pub fn algorithm(&self) -> CryptoAlgorithm {
match self {
SigningKey::Ed25519(_) => "ed25519",
SigningKey::Secp256k1(_) => "secp256k1",
SigningKey::Secp256r1(_) => "secp256r1",
SigningKey::Ed25519(_) => CryptoAlgorithm::Ed25519,
SigningKey::Secp256k1(_) => CryptoAlgorithm::Secp256k1,
SigningKey::Secp256r1(_) => CryptoAlgorithm::Secp256r1,
}
}

Expand Down Expand Up @@ -100,14 +98,14 @@ impl TryFrom<CryptoPayload> for SigningKey {
type Error = anyhow::Error;

fn try_from(value: CryptoPayload) -> std::result::Result<Self, Self::Error> {
SigningKey::from_algorithm_and_bytes(&value.algorithm, &value.bytes)
SigningKey::from_algorithm_and_bytes(value.algorithm, &value.bytes)
}
}

impl From<SigningKey> for CryptoPayload {
fn from(signing_key: SigningKey) -> Self {
CryptoPayload {
algorithm: signing_key.algorithm().to_string(),
algorithm: signing_key.algorithm(),
bytes: signing_key.to_bytes(),
}
}
Expand Down
32 changes: 14 additions & 18 deletions crates/keys/src/verifying_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@ use std::{
hash::{Hash, Hasher},
};

use crate::{Signature, SigningKey};
use prism_serde::{
base64::{FromBase64, ToBase64},
CryptoPayload,
};
use crate::{payload::CryptoPayload, CryptoAlgorithm, Signature, SigningKey};
use prism_serde::base64::{FromBase64, ToBase64};

#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
#[serde(try_from = "CryptoPayload", into = "CryptoPayload")]
Expand Down Expand Up @@ -63,26 +60,25 @@ impl VerifyingKey {
}
}

pub fn from_algorithm_and_bytes(algorithm: &str, bytes: &[u8]) -> Result<Self> {
pub fn from_algorithm_and_bytes(algorithm: CryptoAlgorithm, bytes: &[u8]) -> Result<Self> {
match algorithm {
"ed25519" => Ed25519VerifyingKey::try_from(bytes)
CryptoAlgorithm::Ed25519 => Ed25519VerifyingKey::try_from(bytes)
.map(VerifyingKey::Ed25519)
.map_err(|e| e.into()),
"secp256k1" => Secp256k1VerifyingKey::from_slice(bytes)
CryptoAlgorithm::Secp256k1 => Secp256k1VerifyingKey::from_slice(bytes)
.map(VerifyingKey::Secp256k1)
.map_err(|e| e.into()),
"secp256r1" => Secp256r1VerifyingKey::from_sec1_bytes(bytes)
CryptoAlgorithm::Secp256r1 => Secp256r1VerifyingKey::from_sec1_bytes(bytes)
.map(VerifyingKey::Secp256r1)
.map_err(|e| e.into()),
_ => bail!("Unexpected algorithm for VerifyingKey"),
}
}

pub fn algorithm(&self) -> &'static str {
pub fn algorithm(&self) -> CryptoAlgorithm {
match self {
VerifyingKey::Ed25519(_) => "ed25519",
VerifyingKey::Secp256k1(_) => "secp256k1",
VerifyingKey::Secp256r1(_) => "secp256r1",
VerifyingKey::Ed25519(_) => CryptoAlgorithm::Ed25519,
VerifyingKey::Secp256k1(_) => CryptoAlgorithm::Secp256k1,
VerifyingKey::Secp256r1(_) => CryptoAlgorithm::Secp256r1,
}
}

Expand Down Expand Up @@ -125,15 +121,15 @@ impl TryFrom<CryptoPayload> for VerifyingKey {
type Error = anyhow::Error;

fn try_from(value: CryptoPayload) -> std::result::Result<Self, Self::Error> {
VerifyingKey::from_algorithm_and_bytes(&value.algorithm, &value.bytes)
VerifyingKey::from_algorithm_and_bytes(value.algorithm, &value.bytes)
}
}

impl From<VerifyingKey> for CryptoPayload {
fn from(signature: VerifyingKey) -> Self {
fn from(verifying_key: VerifyingKey) -> Self {
CryptoPayload {
algorithm: signature.algorithm().to_string(),
bytes: signature.to_bytes(),
algorithm: verifying_key.algorithm(),
bytes: verifying_key.to_bytes(),
}
}
}
Expand Down
9 changes: 0 additions & 9 deletions crates/serde/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,6 @@ pub mod base64;
pub mod binary;
pub mod hex;

use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
pub struct CryptoPayload {
pub algorithm: String,
#[serde(with = "raw_or_b64")]
pub bytes: Vec<u8>,
}

pub mod raw_or_hex {
use std::fmt::Display;

Expand Down
Binary file modified elf/riscv32im-succinct-zkvm-elf
Binary file not shown.
Loading