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

Add restore (#11) #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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
30 changes: 30 additions & 0 deletions src/api.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::convert::TryInto;

use crate::params::{PUBLICKEYBYTES, SECRETKEYBYTES, SIGNBYTES};
use crate::sign::*;

Expand All @@ -16,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 Down