Struct schnorrkel::keys::SecretKey
source · pub struct SecretKey { /* private fields */ }
Expand description
A secret key for use with Ristretto Schnorr signatures.
Internally, these consist of a scalar mod l along with a seed for nonce generation. In this way, we ensure all scalar arithmetic works smoothly in operations like threshold or multi-signatures, or hierarchical deterministic key derivations.
We keep our secret key serializaion “almost” compatable with EdDSA “expanded” secret key serializaion by multiplying the scalar by the cofactor 8, as integers, and dividing on deserializaion. We do not however attempt to keep the scalar’s high bit set, especially not during hierarchical deterministic key derivations, so some Ed25519 libraries might compute the public key incorrectly from our secret key.
Implementations§
source§impl SecretKey
impl SecretKey
sourcepub fn to_bytes(&self) -> [u8; 64]
pub fn to_bytes(&self) -> [u8; 64]
Convert this SecretKey
into an array of 64 bytes with.
Returns an array of 64 bytes, with the first 32 bytes being the secret scalar represented canonically, and the last 32 bytes being the seed for nonces.
§Examples
use schnorrkel::{MiniSecretKey, SecretKey};
let mini_secret_key: MiniSecretKey = MiniSecretKey::generate();
let secret_key: SecretKey = mini_secret_key.expand(MiniSecretKey::UNIFORM_MODE);
let secret_key_bytes: [u8; 64] = secret_key.to_bytes();
let bytes: [u8; 64] = secret_key.to_bytes();
let secret_key_again: SecretKey = SecretKey::from_bytes(&bytes[..]).unwrap();
assert_eq!(&bytes[..], & secret_key_again.to_bytes()[..]);
sourcepub fn from_bytes(bytes: &[u8]) -> SignatureResult<SecretKey>
pub fn from_bytes(bytes: &[u8]) -> SignatureResult<SecretKey>
Construct an SecretKey
from a slice of bytes.
§Examples
use schnorrkel::{MiniSecretKey, SecretKey, ExpansionMode, SignatureError};
let mini_secret_key: MiniSecretKey = MiniSecretKey::generate();
let secret_key: SecretKey = mini_secret_key.expand(MiniSecretKey::ED25519_MODE);
let bytes: [u8; 64] = secret_key.to_bytes();
let secret_key_again: SecretKey = SecretKey::from_bytes(&bytes[..]).unwrap();
assert_eq!(secret_key_again, secret_key);
sourcepub fn to_ed25519_bytes(&self) -> [u8; 64]
pub fn to_ed25519_bytes(&self) -> [u8; 64]
Convert this SecretKey
into an array of 64 bytes, corresponding to
an Ed25519 expanded secret key.
Returns an array of 64 bytes, with the first 32 bytes being the secret scalar shifted ed25519 style, and the last 32 bytes being the seed for nonces.
sourcepub fn from_ed25519_bytes(bytes: &[u8]) -> SignatureResult<SecretKey>
pub fn from_ed25519_bytes(bytes: &[u8]) -> SignatureResult<SecretKey>
Construct an SecretKey
from a slice of bytes, corresponding to
an Ed25519 expanded secret key.
§Example
use schnorrkel::{SecretKey, SECRET_KEY_LENGTH};
use hex_literal::hex;
let secret = hex!("28b0ae221c6bb06856b287f60d7ea0d98552ea5a16db16956849aa371db3eb51fd190cce74df356432b410bd64682309d6dedb27c76845daf388557cbac3ca34");
let public = hex!("46ebddef8cd9bb167dc30878d7113b7e168e6f0646beffd77d69d39bad76b47a");
let secret_key = SecretKey::from_ed25519_bytes(&secret[..]).unwrap();
assert_eq!(secret_key.to_public().to_bytes(), public);
sourcepub fn generate_with<R>(csprng: R) -> SecretKey
pub fn generate_with<R>(csprng: R) -> SecretKey
Generate an “unbiased” SecretKey
directly from a user
suplied csprng
uniformly, bypassing the MiniSecretKey
layer.
sourcepub fn generate() -> SecretKey
pub fn generate() -> SecretKey
Generate an “unbiased” SecretKey
directly,
bypassing the MiniSecretKey
layer.
sourcepub fn to_keypair(self) -> Keypair
pub fn to_keypair(self) -> Keypair
Derive the PublicKey
corresponding to this SecretKey
.
source§impl SecretKey
impl SecretKey
sourcepub fn sign<T: SigningTranscript>(
&self,
t: T,
public_key: &PublicKey,
) -> Signature
pub fn sign<T: SigningTranscript>( &self, t: T, public_key: &PublicKey, ) -> Signature
Sign a transcript with this SecretKey
.
Requires a SigningTranscript
, normally created from a
SigningContext
and a message, as well as the public key
corresponding to self
. Returns a Schnorr signature.
We employ a randomized nonce here, but also incorporate the transcript like in a derandomized scheme, but only after first extending the transcript by the public key. As a result, there should be no attacks even if both the random number generator fails and the function gets called with the wrong public key.
sourcepub fn sign_doublecheck<T>(
&self,
t: T,
public_key: &PublicKey,
) -> SignatureResult<Signature>where
T: SigningTranscript + Clone,
pub fn sign_doublecheck<T>(
&self,
t: T,
public_key: &PublicKey,
) -> SignatureResult<Signature>where
T: SigningTranscript + Clone,
Sign a message with this SecretKey
, but doublecheck the result.
sourcepub fn sign_simple(
&self,
ctx: &[u8],
msg: &[u8],
public_key: &PublicKey,
) -> Signature
pub fn sign_simple( &self, ctx: &[u8], msg: &[u8], public_key: &PublicKey, ) -> Signature
Sign a message with this SecretKey
.
sourcepub fn sign_simple_doublecheck(
&self,
ctx: &[u8],
msg: &[u8],
public_key: &PublicKey,
) -> SignatureResult<Signature>
pub fn sign_simple_doublecheck( &self, ctx: &[u8], msg: &[u8], public_key: &PublicKey, ) -> SignatureResult<Signature>
Sign a message with this SecretKey
, but doublecheck the result.
source§impl SecretKey
impl SecretKey
sourcepub fn vrf_create_from_point(&self, input: RistrettoBoth) -> VRFInOut
pub fn vrf_create_from_point(&self, input: RistrettoBoth) -> VRFInOut
Evaluate the VRF-like multiplication on an uncompressed point, probably not useful in this form.
sourcepub fn vrf_create_from_compressed_point(
&self,
input: &VRFPreOut,
) -> SignatureResult<VRFInOut>
pub fn vrf_create_from_compressed_point( &self, input: &VRFPreOut, ) -> SignatureResult<VRFInOut>
Evaluate the VRF-like multiplication on a compressed point, useful for proving key exchanges, OPRFs, or sequential VRFs.
We caution that such protocols could provide signing oracles
and note that vrf_create_from_point
cannot check for
problematic inputs like attach_input_hash
does.
source§impl SecretKey
impl SecretKey
sourcepub fn hard_derive_mini_secret_key<B: AsRef<[u8]>>(
&self,
cc: Option<ChainCode>,
i: B,
) -> (MiniSecretKey, ChainCode)
pub fn hard_derive_mini_secret_key<B: AsRef<[u8]>>( &self, cc: Option<ChainCode>, i: B, ) -> (MiniSecretKey, ChainCode)
Vaguely BIP32-like “hard” derivation of a MiniSecretKey
from a SecretKey
We do not envision any “good reasons” why these “hard”
derivations should ever be used after the soft Derivation
trait. We similarly do not believe hard derivations
make any sense for ChainCode
s or ExtendedKey
s types.
Yet, some existing BIP32 workflows might do these things,
due to BIP32’s de facto standardization and poor design.
In consequence, we provide this method to do “hard” derivations
in a way that should work with all BIP32 workflows and any
permissible mutations of SecretKey
. This means only that
we hash the SecretKey
’s scalar, but not its nonce because
the secret key remains valid if the nonce is changed.
Trait Implementations§
source§impl ConstantTimeEq for SecretKey
impl ConstantTimeEq for SecretKey
source§impl Derivation for SecretKey
impl Derivation for SecretKey
source§fn derived_key<T>(&self, t: T, cc: ChainCode) -> (SecretKey, ChainCode)where
T: SigningTranscript,
fn derived_key<T>(&self, t: T, cc: ChainCode) -> (SecretKey, ChainCode)where
T: SigningTranscript,
SigningTranscript
, and a chain code.source§impl PartialEq for SecretKey
impl PartialEq for SecretKey
impl Eq for SecretKey
Auto Trait Implementations§
impl Freeze for SecretKey
impl RefUnwindSafe for SecretKey
impl Send for SecretKey
impl Sync for SecretKey
impl Unpin for SecretKey
impl UnwindSafe for SecretKey
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> FmtForward for T
impl<T> FmtForward for T
source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self
to use its Display
implementation when
Debug
-formatted.source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moresource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moresource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self
, then passes self.as_ref()
into the pipe function.source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self
, then passes self.as_mut()
into the pipe
function.source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.source§impl<T> Tap for T
impl<T> Tap for T
source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read moresource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read moresource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read moresource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read moresource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read moresource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read moresource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow()
only in debug builds, and is erased in release
builds.source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref()
only in debug builds, and is erased in release
builds.source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut()
only in debug builds, and is erased in release
builds.source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.