Skip to content
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

feat: test all available curves #186

Merged
merged 34 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
141ebdd
feat: test all curves
smuu Dec 19, 2024
9aa437b
feat: config checks
smuu Dec 19, 2024
9c63742
feat: build
smuu Dec 19, 2024
a9f608d
fix: remove debug message
smuu Dec 19, 2024
9e31143
fix: intentation
smuu Dec 19, 2024
bacc54e
fix: test output as before
smuu Dec 19, 2024
ef9b950
Merge branch 'main' into smuu/20241213-test-all-curves
smuu Dec 19, 2024
8223b70
fix: wrong usage of function
smuu Dec 19, 2024
db1b9e8
feat: apply coderabbit review 1st round
smuu Dec 19, 2024
efb146c
fix: errors
smuu Dec 19, 2024
c2740af
feat: apply coderabbit review 2nd round
smuu Dec 19, 2024
06c3c14
feat: apply review from jns-ps
smuu Dec 19, 2024
4f1fce2
Merge branch 'main' into smuu/20241213-test-all-curves
smuu Dec 19, 2024
313ff7e
feat: build
smuu Dec 19, 2024
fec1480
feat: apply clippy changes
smuu Dec 19, 2024
a02f267
fix: unit tests
smuu Dec 19, 2024
706bb76
feat: apply coderabbit review 3rd round
smuu Dec 19, 2024
4fca18a
fix: funding
smuu Dec 19, 2024
51a4efd
refactor(keys): algorithm as enum
jns-ps Dec 19, 2024
7fbf5c5
chore: Incorporate changes in zkvm elf
jns-ps Dec 19, 2024
15fc919
fix: fund all in sequence
smuu Dec 19, 2024
64e68f3
feat: integration test all curves
smuu Dec 19, 2024
58a0978
fix: clippy
smuu Dec 19, 2024
a37a8fb
fix: remove unused dep
smuu Dec 19, 2024
0403811
Merge branch 'crypto-alg-as-enum' into smuu/20241213-test-all-curves
smuu Dec 29, 2024
bf06889
feat: apply coderabbit review 4th round
smuu Dec 29, 2024
3608504
Merge branch 'main' into smuu/20241213-test-all-curves
smuu Jan 8, 2025
0a36317
feat: applied review
smuu Jan 8, 2025
90c6b60
feat: apply review
smuu Jan 9, 2025
81c5ed8
revert unused dep
smuu Jan 9, 2025
cae20ba
fix: apply review
smuu Jan 9, 2025
a381432
fix: remove debugging message
smuu Jan 9, 2025
0c529b4
feat: add rust comment
smuu Jan 9, 2025
68c9579
fix: remove logging key
smuu Jan 9, 2025
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
7 changes: 3 additions & 4 deletions Cargo.lock

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

7 changes: 7 additions & 0 deletions crates/cli/src/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ pub struct CommandArgs {
#[arg(long)]
verifying_key: Option<String>,

/// Can be one of: ed25519, secp256k1, secp256r1
smuu marked this conversation as resolved.
Show resolved Hide resolved
#[arg(long, default_value = "ed25519")]
verifying_key_algorithm: Option<String>,

#[arg(long)]
config_path: Option<String>,

Expand Down Expand Up @@ -97,6 +101,7 @@ pub struct Config {
pub da_layer: DALayerOption,
pub redis_config: Option<RedisConfig>,
pub verifying_key: Option<String>,
pub verifying_key_algorithm: String,
}

impl Default for Config {
Expand All @@ -107,6 +112,7 @@ impl Default for Config {
da_layer: DALayerOption::default(),
redis_config: Some(RedisConfig::default()),
verifying_key: None,
verifying_key_algorithm: "ed25519".to_string(),
}
}
}
Expand Down Expand Up @@ -201,6 +207,7 @@ fn apply_command_line_args(config: Config, args: CommandArgs) -> Config {
}),
da_layer: config.da_layer,
verifying_key: args.verifying_key.or(config.verifying_key),
verifying_key_algorithm: args.verifying_key_algorithm.unwrap_or(config.verifying_key_algorithm),
}
}

Expand Down
60 changes: 42 additions & 18 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod node_types;
use cfg::{initialize_da_layer, load_config, Cli, Commands};
use clap::Parser;
use keystore_rs::{KeyChain, KeyStore, KeyStoreType};
use prism_keys::VerifyingKey;
use prism_keys::{SigningKey, VerifyingKey};

use node_types::NodeType;
use prism_lightclient::LightClient;
Expand Down Expand Up @@ -37,14 +37,16 @@ async fn main() -> std::io::Result<()> {
)
})?;

let prover_vk = config.verifying_key.and_then(|s| s.try_into().ok()).and_then(
|vk: VerifyingKey| match vk {
VerifyingKey::Ed25519(key) => Some(key),
_ => None,
},
);
let verifying_key_algorithm = validate_algorithm(&config.verifying_key_algorithm)?;

let prover_vk = VerifyingKey::from_algorithm_and_bytes(
verifying_key_algorithm,
config.verifying_key.unwrap().as_bytes(),
smuu marked this conversation as resolved.
Show resolved Hide resolved
).map_err(|e| std::io::Error::new(
std::io::ErrorKind::InvalidData, format!("invalid prover verifying key: {}", e),
))?;

Arc::new(LightClient::new(da, celestia_config, prover_vk))
Arc::new(LightClient::new(da, celestia_config, Some(prover_vk)))
}
Commands::Prover(args) => {
let config = load_config(args.clone())
Expand All @@ -63,22 +65,27 @@ async fn main() -> std::io::Result<()> {
let redis_connections = RedisConnection::new(&redis_config)
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?;

let signing_key = KeyStoreType::KeyChain(KeyChain)
let signing_key_chain = KeyStoreType::KeyChain(KeyChain)
.get_signing_key()
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?;

let verifying_key_algorithm = validate_algorithm(&config.verifying_key_algorithm)?;

let signing_key = SigningKey::from_algorithm_and_bytes(verifying_key_algorithm, signing_key_chain.as_bytes()).unwrap();
let verifying_key = signing_key.verifying_key();
smuu marked this conversation as resolved.
Show resolved Hide resolved

let prover_cfg = prism_prover::Config {
prover: true,
batcher: true,
webserver: config.webserver.unwrap_or_default(),
signing_key: signing_key.clone(),
verifying_key: signing_key.verification_key(),
verifying_key: verifying_key.clone(),
start_height: config.celestia_config.unwrap_or_default().start_height,
};

info!(
"prover verifying key: {}",
VerifyingKey::from(prover_cfg.verifying_key)
VerifyingKey::from(prover_cfg.verifying_key.clone())
);

Arc::new(
Expand Down Expand Up @@ -107,23 +114,28 @@ async fn main() -> std::io::Result<()> {
let redis_connections = RedisConnection::new(&redis_config)
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?;

let signing_key = KeyStoreType::KeyChain(KeyChain)
let signing_key_chain = KeyStoreType::KeyChain(KeyChain)
.get_signing_key()
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?;

let verifying_key_algorithm = validate_algorithm(&config.verifying_key_algorithm)?;

let signing_key = SigningKey::from_algorithm_and_bytes(verifying_key_algorithm, signing_key_chain.as_bytes()).unwrap();

let prover_vk = config
.verifying_key
.and_then(|s| s.try_into().ok())
.and_then(|vk: VerifyingKey| match vk {
VerifyingKey::Ed25519(key) => Some(key),
_ => None,
})
.ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::NotFound,
"prover verifying key not found",
)
})?;
})
.and_then(|vk| VerifyingKey::from_algorithm_and_bytes(verifying_key_algorithm, vk.as_bytes()).map_err(|e| {
std::io::Error::new(
std::io::ErrorKind::InvalidData,
format!("invalid prover verifying key: {}", e),
)
}))?;

let prover_cfg = prism_prover::Config {
prover: false,
Expand All @@ -147,3 +159,15 @@ async fn main() -> std::io::Result<()> {

node.start().await.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))
}

fn validate_algorithm(algorithm: &str) -> Result<&str, std::io::Error> {
smuu marked this conversation as resolved.
Show resolved Hide resolved
if algorithm.is_empty() {
return Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, "verifying key algorithm is required"));
}

if !["ed25519", "secp256k1", "secp256r1"].contains(&algorithm) {
return Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, "invalid verifying key algorithm"));
}

Ok(algorithm)
}
smuu marked this conversation as resolved.
Show resolved Hide resolved
29 changes: 19 additions & 10 deletions crates/common/src/transaction_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,14 @@
self.hashchains.get(id)
}

pub fn register_service_with_random_keys(&mut self, id: &str) -> UncommittedTransaction {
let random_service_challenge_key = SigningKey::new_ed25519();
let random_service_signing_key = SigningKey::new_ed25519();
pub fn register_service_with_random_keys(
&mut self,
algorithm: &str,
id: &str,
) -> UncommittedTransaction {
let random_service_challenge_key = SigningKey::new_with_algorithm(algorithm);
let random_service_signing_key = SigningKey::new_with_algorithm(algorithm);
self.register_service(id, random_service_challenge_key, random_service_signing_key)

Check failure on line 94 in crates/common/src/transaction_builder.rs

View workflow job for this annotation

GitHub Actions / integration-test

arguments to this method are incorrect

Check failure on line 94 in crates/common/src/transaction_builder.rs

View workflow job for this annotation

GitHub Actions / clippy

arguments to this method are incorrect

Check failure on line 94 in crates/common/src/transaction_builder.rs

View workflow job for this annotation

GitHub Actions / unit-test

arguments to this method are incorrect

Check failure on line 94 in crates/common/src/transaction_builder.rs

View workflow job for this annotation

GitHub Actions / unused dependencies

arguments to this method are incorrect
}

pub fn register_service(
Expand Down Expand Up @@ -116,11 +120,12 @@

pub fn create_account_with_random_key_signed(
&mut self,
algorithm: &str,
id: &str,
service_id: &str,
) -> UncommittedTransaction {
let account_signing_key = SigningKey::new_ed25519();
let account_signing_key = SigningKey::new_with_algorithm(algorithm);
self.create_account_signed(id, service_id, account_signing_key)

Check failure on line 128 in crates/common/src/transaction_builder.rs

View workflow job for this annotation

GitHub Actions / integration-test

mismatched types

Check failure on line 128 in crates/common/src/transaction_builder.rs

View workflow job for this annotation

GitHub Actions / clippy

mismatched types

Check failure on line 128 in crates/common/src/transaction_builder.rs

View workflow job for this annotation

GitHub Actions / unit-test

mismatched types

Check failure on line 128 in crates/common/src/transaction_builder.rs

View workflow job for this annotation

GitHub Actions / unused dependencies

mismatched types
}

pub fn create_account_signed(
Expand All @@ -138,12 +143,13 @@

pub fn create_account_with_random_key(
&mut self,
algorithm: &str,
id: &str,
service_id: &str,
service_signing_key: &SigningKey,
) -> UncommittedTransaction {
let account_signing_key = SigningKey::new_ed25519();
let account_signing_key = SigningKey::new_with_algorithm(algorithm);
self.create_account(id, service_id, service_signing_key, account_signing_key)

Check failure on line 152 in crates/common/src/transaction_builder.rs

View workflow job for this annotation

GitHub Actions / integration-test

mismatched types

Check failure on line 152 in crates/common/src/transaction_builder.rs

View workflow job for this annotation

GitHub Actions / clippy

mismatched types

Check failure on line 152 in crates/common/src/transaction_builder.rs

View workflow job for this annotation

GitHub Actions / unit-test

mismatched types

Check failure on line 152 in crates/common/src/transaction_builder.rs

View workflow job for this annotation

GitHub Actions / unused dependencies

mismatched types
}

pub fn create_account(
Expand Down Expand Up @@ -176,21 +182,22 @@
}
}

pub fn add_random_key_verified_with_root(&mut self, id: &str) -> UncommittedTransaction {
pub fn add_random_key_verified_with_root(&mut self, algorithm: &str, id: &str) -> UncommittedTransaction {
let Some(account_signing_key) = self.account_keys.get(id).cloned() else {
panic!("No existing account key for {}", id)
};

self.add_random_key(id, &account_signing_key, 0)
self.add_random_key(algorithm, id, &account_signing_key, 0)
}

pub fn add_random_key(
&mut self,
algorithm: &str,
id: &str,
signing_key: &SigningKey,
key_idx: usize,
) -> UncommittedTransaction {
let random_key = SigningKey::new_ed25519().into();
let random_key = SigningKey::new_with_algorithm(algorithm).into();

Check failure on line 200 in crates/common/src/transaction_builder.rs

View workflow job for this annotation

GitHub Actions / integration-test

the trait bound `VerifyingKey: From<Result<SigningKey, anyhow::Error>>` is not satisfied

Check failure on line 200 in crates/common/src/transaction_builder.rs

View workflow job for this annotation

GitHub Actions / clippy

the trait bound `prism_keys::VerifyingKey: std::convert::From<std::result::Result<prism_keys::SigningKey, anyhow::Error>>` is not satisfied

Check failure on line 200 in crates/common/src/transaction_builder.rs

View workflow job for this annotation

GitHub Actions / unit-test

the trait bound `VerifyingKey: From<Result<SigningKey, anyhow::Error>>` is not satisfied

Check failure on line 200 in crates/common/src/transaction_builder.rs

View workflow job for this annotation

GitHub Actions / unused dependencies

the trait bound `VerifyingKey: From<Result<SigningKey, anyhow::Error>>` is not satisfied
self.add_key(id, random_key, signing_key, key_idx)
}

Expand Down Expand Up @@ -262,22 +269,24 @@

pub fn add_randomly_signed_data(
&mut self,
algorithm: &str,
id: &str,
value: Vec<u8>,
signing_key: &SigningKey,
key_idx: usize,
) -> UncommittedTransaction {
let value_signing_key = SigningKey::new_ed25519();
let value_signing_key = SigningKey::new_with_algorithm(algorithm);
self.add_signed_data(id, value, &value_signing_key, signing_key, key_idx)

Check failure on line 279 in crates/common/src/transaction_builder.rs

View workflow job for this annotation

GitHub Actions / integration-test

mismatched types

Check failure on line 279 in crates/common/src/transaction_builder.rs

View workflow job for this annotation

GitHub Actions / clippy

mismatched types

Check failure on line 279 in crates/common/src/transaction_builder.rs

View workflow job for this annotation

GitHub Actions / unit-test

mismatched types

Check failure on line 279 in crates/common/src/transaction_builder.rs

View workflow job for this annotation

GitHub Actions / unused dependencies

mismatched types
}

pub fn add_randomly_signed_data_verified_with_root(
&mut self,
algorithm: &str,
id: &str,
value: Vec<u8>,
) -> UncommittedTransaction {
let value_signing_key = SigningKey::new_ed25519();
let value_signing_key = SigningKey::new_with_algorithm(algorithm);
self.add_signed_data_verified_with_root(id, value, &value_signing_key)

Check failure on line 289 in crates/common/src/transaction_builder.rs

View workflow job for this annotation

GitHub Actions / integration-test

mismatched types

Check failure on line 289 in crates/common/src/transaction_builder.rs

View workflow job for this annotation

GitHub Actions / clippy

mismatched types

Check failure on line 289 in crates/common/src/transaction_builder.rs

View workflow job for this annotation

GitHub Actions / unit-test

mismatched types

Check failure on line 289 in crates/common/src/transaction_builder.rs

View workflow job for this annotation

GitHub Actions / unused dependencies

mismatched types
}

pub fn add_signed_data(
Expand Down
2 changes: 1 addition & 1 deletion crates/da/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ readme.workspace = true
[dependencies]
async-trait = { workspace = true }
serde = { workspace = true }
ed25519-consensus = { workspace = true }
tokio = { workspace = true }
log = { workspace = true }
celestia-rpc = { workspace = true }
Expand All @@ -22,4 +21,5 @@ anyhow = { workspace = true }
prism-common = { workspace = true }
prism-errors = { workspace = true }
prism-serde = { workspace = true }
prism-keys = { workspace = true }
sp1-sdk = { workspace = true }
12 changes: 3 additions & 9 deletions crates/da/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Result;
use async_trait::async_trait;
use ed25519_consensus::{Signature, SigningKey, VerificationKey as VerifyingKey};
use prism_keys::{SigningKey, VerifyingKey, Signature};
use prism_common::{digest::Digest, transaction::Transaction};
use prism_serde::{
binary::ToBinary,
Expand Down Expand Up @@ -50,16 +50,10 @@ impl FinalizedEpoch {
let signature_bytes = Vec::<u8>::from_hex(signature)
.map_err(|e| anyhow::anyhow!("Failed to decode signature: {}", e))?;

if signature_bytes.len() != 64 {
return Err(anyhow::anyhow!("Invalid signature length"));
}

let signature: Signature = signature_bytes
.as_slice()
.try_into()
let signature: Signature = Signature::from_algorithm_and_bytes(vk.algorithm(), signature_bytes.as_slice())
.map_err(|_| anyhow::anyhow!("Invalid signature length"))?;

vk.verify(&signature, &message)
vk.verify_signature(&message, &signature)
.map_err(|e| anyhow::anyhow!("Signature verification failed: {}", e))?;
Ok(())
}
Expand Down
15 changes: 14 additions & 1 deletion crates/keys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mod tests {
use prism_serde::base64::ToBase64;
use rand::rngs::OsRng;
use secp256k1::SecretKey as Secp256k1SigningKey;

use p256::ecdsa::SigningKey as Secp256r1SigningKey;
#[test]
fn test_reparsed_verifying_keys_are_equal_to_original() {
let verifying_key_ed25519 = SigningKey::new_ed25519().verifying_key();
Expand Down Expand Up @@ -123,6 +123,19 @@ mod tests {
assert_eq!(decoded_key.to_bytes(), original_key.to_bytes());
}

#[test]
fn test_verifying_key_from_string_secp256r1() {
let original_key: VerifyingKey =
SigningKey::Secp256r1(Secp256r1SigningKey::random(&mut OsRng)).into();
let encoded = original_key.to_bytes().to_base64();

let result = VerifyingKey::try_from(encoded);
assert!(result.is_ok());

let decoded_key = result.unwrap();
assert_eq!(decoded_key.to_bytes(), original_key.to_bytes());
}

#[test]
fn test_verifying_key_from_string_invalid_length() {
let invalid_bytes: [u8; 31] = [1; 31];
Expand Down
2 changes: 1 addition & 1 deletion crates/keys/src/signatures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl Signature {
"secp256r1" => {
Secp256r1Signature::from_der(bytes).map(Signature::Secp256r1).map_err(|e| e.into())
}
_ => bail!("Unexpected algorithm for Signature"),
_ => bail!("Unexpected algorithm for Signature: {}", algorithm),
}
}

Expand Down
11 changes: 10 additions & 1 deletion crates/keys/src/signing_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ impl SigningKey {
SigningKey::Secp256r1(Secp256r1SigningKey::random(&mut OsRng))
}

pub fn new_with_algorithm(algorithm: &str) -> Result<Self> {
match algorithm {
"ed25519" => Ok(SigningKey::Ed25519(Box::new(Ed25519SigningKey::new(OsRng)))),
"secp256k1" => Ok(SigningKey::Secp256k1(Secp256k1SigningKey::new(&mut OsRng))),
"secp256r1" => Ok(SigningKey::Secp256r1(Secp256r1SigningKey::random(&mut OsRng))),
_ => bail!("Unexpected key algorithm for SigningKey: '{}'. Expected one of: ed25519, secp256k1, secp256r1", algorithm),
}
}

pub fn verifying_key(&self) -> VerifyingKey {
self.clone().into()
}
Expand All @@ -54,7 +63,7 @@ impl SigningKey {
"secp256r1" => Secp256r1SigningKey::from_slice(bytes)
.map(SigningKey::Secp256r1)
.map_err(|e| e.into()),
_ => bail!("Unexpected algorithm for VerifyingKey"),
_ => bail!("Unexpected algorithm for SigningKey: {}", algorithm),
}
}

Expand Down
Loading
Loading