-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
47 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,53 @@ | ||
//! Module for private nonces | ||
use std::fmt::Debug; | ||
use crate::common::scalar_from_bytes; | ||
use curve25519_dalek::scalar::Scalar; | ||
use serde::{Deserialize, Serialize}; | ||
use zeroize::Zeroize; | ||
use crate::public_partial_nonces::{PublicPartialNonces, PublicPartialNoncesBytes}; | ||
|
||
#[derive(Debug, PartialEq, Eq)] | ||
|
||
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] | ||
/// Private Partial Nonces, they should be kept until partially signing a message and then they should be discarded. | ||
/// | ||
/// SECURITY: Reusing them across signatures will cause the private key to leak | ||
pub struct PrivatePartialNonces(pub(crate) [Scalar; 2]); | ||
|
||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] | ||
pub struct PrivatePartialNoncesBytes(pub(crate) Vec<u8>); | ||
|
||
impl Into<PrivatePartialNonces> for PrivatePartialNoncesBytes { | ||
fn into(self) -> PrivatePartialNonces { | ||
PrivatePartialNonces::deserialize(self.0).unwrap() | ||
} | ||
pub struct PrivatePartialNonces { | ||
pub(crate) scalar1: Scalar, | ||
pub(crate) scalar2: Scalar | ||
} | ||
|
||
impl From<PrivatePartialNonces> for PrivatePartialNoncesBytes { | ||
fn from(value: PrivatePartialNonces) -> Self { | ||
Self(value.serialize()) | ||
} | ||
} | ||
impl PrivatePartialNonces { | ||
/// Serialize the private partial nonces for storage. | ||
/// | ||
/// SECURITY: Do not reuse the nonces across signing instances. reusing the nonces will leak the private key. | ||
pub fn serialize(&self) -> Vec<u8> { | ||
pub fn serialize(&self) -> [u8; 64] { | ||
let mut output = [0u8; 64]; | ||
output[..32].copy_from_slice(&self.0[0].to_bytes()); | ||
output[32..64].copy_from_slice(&self.0[1].to_bytes()); | ||
output.to_vec() | ||
output[..32].copy_from_slice(&self.scalar1.to_bytes()); | ||
output[32..64].copy_from_slice(&self.scalar2.to_bytes()); | ||
output | ||
} | ||
|
||
/// Deserialize the private nonces, | ||
/// Will return `None` if they're invalid. | ||
pub fn deserialize(bytes: Vec<u8>) -> Option<Self> { | ||
if bytes.len() != 64 { | ||
return None | ||
} | ||
|
||
Some(Self([ | ||
scalar_from_bytes(&bytes[..32])?, | ||
scalar_from_bytes(&bytes[32..64])?, | ||
])) | ||
pub fn deserialize(bytes: [u8; 64]) -> Option<Self> { | ||
Some(Self{ | ||
scalar1: scalar_from_bytes(&bytes[..32])?, | ||
scalar2: scalar_from_bytes(&bytes[32..64])?, | ||
}) | ||
} | ||
} | ||
|
||
impl zeroize::ZeroizeOnDrop for PrivatePartialNonces {} | ||
|
||
impl zeroize::Zeroize for PrivatePartialNonces { | ||
fn zeroize(&mut self) { | ||
self.0.zeroize() | ||
self.scalar1.zeroize(); | ||
self.scalar2.zeroize(); | ||
} | ||
} | ||
|
||
impl Drop for PrivatePartialNonces { | ||
fn drop(&mut self) { | ||
self.0.zeroize(); | ||
self.scalar1.zeroize(); | ||
self.scalar2.zeroize(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters