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

Signer module - kairos-crypto #35

Merged
merged 14 commits into from
Mar 28, 2024
Merged

Signer module - kairos-crypto #35

merged 14 commits into from
Mar 28, 2024

Conversation

koxu1996
Copy link
Contributor

@koxu1996 koxu1996 commented Mar 11, 2024

This PR moves cryptography from kairos-cli - introduced temporarily in Base CLI parsing #12 - into a separate package named kairos-crypto.

There are few major improvements, mainly:

  1. Introduced CryptoSigner trait to provide implementation-agnostic interface.
  2. Casper specific implementation is available via feature flag crypto-casper (enabled by default).
  3. Introduced sign() and verify() methods - it will be used for signature checking, once Deterministic payload format: ASN.1 schema with DER encoding #30 is merged.
  4. Serialization error was split into distinct CryptoError::Serialization and CryptoError::Deserialization.

@koxu1996 koxu1996 self-assigned this Mar 11, 2024
@koxu1996 koxu1996 changed the title Implement kairos-crypto. Signer module - kairos-crypto Mar 11, 2024
@koxu1996 koxu1996 mentioned this pull request Mar 15, 2024
@koxu1996 koxu1996 force-pushed the feature/generic-crypto-module branch 2 times, most recently from c9f3997 to 9ba8cdc Compare March 17, 2024 16:30
@koxu1996 koxu1996 marked this pull request as ready for review March 18, 2024 12:56
@koxu1996 koxu1996 force-pushed the feature/generic-crypto-module branch from 9ba8cdc to d12f91e Compare March 18, 2024 12:58
@koxu1996
Copy link
Contributor Author

Rebased and ready for review.

Comment on lines 12 to 17
fn from_public_key(bytes: &[u8]) -> Result<Self, CryptoError>
where
Self: Sized;

fn sign(&self, data: &[u8]) -> Result<Vec<u8>, CryptoError>;
fn verify(&self, data: &[u8], signature_bytes: &[u8]) -> Result<(), CryptoError>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer AsRef<[u8]> instead of &[u8].

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 9792ee3.

Copy link
Contributor

@Avi-D-coder Avi-D-coder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine.
I don't love traits with a single impl, but since we are planing on adding more it's good to merge.

.private_key
.as_ref()
.ok_or(CryptoError::MissingPrivateKey)?;
let signature = crypto::sign(data, private_key, &self.public_key);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mentioned elsewhere, casper_types::crypto::sign has an unused public_key.

Here is the crypto signing code that litmus uses (which has been unit tested and exhaustively tested against casper_types for compatibility).

https://github.com/cspr-rad/litmus/blob/673f8182560f7f1ff6fee04d775070a6b3e380c3/src/crypto.rs#L24-L40

// Signs the given message using the given key.
pub fn sign<T: AsRef<[u8]>>(secret_key: &SecretKey, message: T) -> Signature {
    match secret_key {
        SecretKey::System => Signature::System,
        SecretKey::Ed25519(secret_key) => {
            let signature = secret_key.sign(message.as_ref());
            Signature::Ed25519(signature)
        }
        SecretKey::Secp256k1(secret_key) => {
            let signature = secret_key
                .try_sign(message.as_ref())
                .expect("should create signature");
            Signature::Secp256k1(signature)
        }
        _ => panic!("SecretKey is marked as non-exhaustive, but this should never happen"),
    }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am aware that public key is not used internally, but I see no point in duplicating the Casper implementation just to get rid of it:

image

Additionally, public key is always available in Signer, so there is no extra code necessary.

@marijanp marijanp added the demo Required for our first demo label Mar 21, 2024
Copy link
Contributor

@Rom3dius Rom3dius left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to go

@koxu1996 koxu1996 merged commit b9fdf2c into main Mar 28, 2024
6 checks passed
@koxu1996 koxu1996 deleted the feature/generic-crypto-module branch March 28, 2024 00:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
demo Required for our first demo
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants