Skip to content

Commit

Permalink
Refactor implementation + docs (Argyle-Software#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hidden committed Jan 2, 2024
1 parent 2044542 commit 11305d1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@ assert!(keys.public.len() == PUBLICKEYBYTES);
assert!(keys.expose_secret().len() == SECRETKEYBYTES);
```

### Restoring a Keypair
```rust
use pqc_dilithium::*;
use crate::params::{PUBLICKEYBYTES, SECRETKEYBYTES};
use std::convert::TryInto;

// Assuming you have public and secret key bytes
let public_bytes: Vec<u8> = vec![0u8; PUBLICKEYBYTES]; // Example byte vectors
let secret_bytes: Vec<u8> = vec![0u8; SECRETKEYBYTES];

// Restore the keypair
let restored_keypair = Keypair::new(public_bytes, secret_bytes);
assert!(restored_keypair.is_ok());
```

### Signing
```rust
let msg = "Hello".as_bytes();
Expand Down
35 changes: 28 additions & 7 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,38 @@ impl std::fmt::Debug for Keypair {

pub enum SignError {
Input,
ConversionFailed,
Verify,
}

impl Keypair {
/// Constructs a new `Keypair` from public and secret key bytes.
///
/// # Errors
/// Returns `SignError::ConversionFailed` if the byte vectors are not of the expected length.
///
/// # Example
/// ```
/// # use pqc_dilithium::*;
/// # use crate::params::{PUBLICKEYBYTES, SECRETKEYBYTES};
/// let public = vec![0u8; PUBLICKEYBYTES];
/// let secret = vec![0u8; SECRETKEYBYTES];
/// let keypair = Keypair::new(public, secret);
/// assert!(keypair.is_ok());
/// ```
pub fn new(
pub_bytes: Vec<u8>,
sec_bytes: Vec<u8>,
) -> Result<Self, SignError> {
let public = pub_bytes
.try_into()
.map_err(|_| SignError::ConversionFailed)?;
let secret = sec_bytes
.try_into()
.map_err(|_| SignError::ConversionFailed)?;
Ok(Self { public, secret })
}

/// Explicitly expose secret key
/// ```
/// # use pqc_dilithium::*;
Expand All @@ -32,13 +60,6 @@ impl Keypair {
pub fn expose_secret(&self) -> &[u8] {
&self.secret
}

pub fn restore(pub_bytes: Vec<u8>, sec_bytes: Vec<u8>) -> Self {
Self {
public: pub_bytes.try_into().unwrap(),
secret: sec_bytes.try_into().unwrap(),
}
}

/// Generates a keypair for signing and verification
///
Expand Down

0 comments on commit 11305d1

Please sign in to comment.