pub struct PublicKeySet { /* private fields */ }Expand description
A public key and an associated set of public key shares.
Implementations§
Source§impl PublicKeySet
impl PublicKeySet
Sourcepub fn threshold(&self) -> usize
pub fn threshold(&self) -> usize
Returns the threshold t: any set of t + 1 signature shares can be combined into a full
signature.
Sourcepub fn public_key(&self) -> PublicKey
pub fn public_key(&self) -> PublicKey
Returns the public key.
Returns the i-th public key share.
§Errors
Returns Error::IndexMapsToZero if i maps to the zero scalar after the internal
index + 1 offset (e.g. -1i32). This prevents a caller from accidentally
retrieving the master public key through an out-of-range signed index.
Sourcepub fn combine_signatures<'a, T, I>(&self, shares: I) -> Result<Signature>
pub fn combine_signatures<'a, T, I>(&self, shares: I) -> Result<Signature>
Combines the shares into a signature that can be verified with the main public key.
The validity of the shares is not checked: If one of them is invalid, the resulting signature also is. Only returns an error if there is a duplicate index or too few shares.
Validity of signature shares should be checked beforehand, or validity of the result afterwards.
§Errors
Error::NotEnoughShares: Fewer thanthreshold + 1shares were providedError::DuplicateEntry: Two shares have the same index
§Example
let sk_set = SecretKeySet::random(3, &mut rand::thread_rng());
let sk_shares: Vec<_> = (0..6usize)
.map(|i| sk_set.secret_key_share(i).expect("valid index"))
.collect();
let pk_set = sk_set.public_keys();
let msg = "Happy birthday! If this is signed, at least four people remembered!";
// Create four signature shares for the message.
let sig_shares: BTreeMap<_, _> = (0..4).map(|i| (i, sk_shares[i].sign(msg))).collect();
// Validate the signature shares.
for (i, sig_share) in &sig_shares {
assert!(pk_set.public_key_share(*i).expect("valid index").verify(sig_share, msg));
}
// Combine them to produce the main signature.
let sig = pk_set.combine_signatures(&sig_shares).expect("not enough shares");
// Validate the main signature. If the shares were valid, this can't fail.
assert!(pk_set.public_key().verify(&sig, msg));§Security
Signature share validity is not checked by this method. A single invalid or
maliciously crafted share silently produces an invalid combined signature. In
adversarial settings, callers must verify each share against its corresponding
PublicKeyShare before combining:
for (i, sig_share) in &sig_shares {
assert!(pk_set.public_key_share(*i).expect("valid index").verify(sig_share, msg));
}Validity of the result can also be checked afterwards by verifying the combined
signature against PublicKeySet::public_key.
Sourcepub fn decrypt<'a, T, I>(&self, shares: I, ct: &Ciphertext) -> Result<Vec<u8>>
pub fn decrypt<'a, T, I>(&self, shares: I, ct: &Ciphertext) -> Result<Vec<u8>>
Combines the shares to decrypt the ciphertext.
The ciphertext is verified before interpolation. Decryption share validity is
not checked; use decrypt_with_verification
if you need per-share verification in adversarial settings.
§Errors
Error::InvalidCiphertext: The ciphertext failed integrity verificationError::NotEnoughShares: Fewer thanthreshold + 1shares were providedError::DuplicateEntry: Two shares have the same index
§Example
use std::collections::BTreeMap;
use threshold_pairing::SecretKeySet;
let mut rng = rand::thread_rng();
let sk_set = SecretKeySet::random(2, &mut rng);
let pk_set = sk_set.public_keys();
let message = b"secret message";
let ciphertext = pk_set.public_key().encrypt(message);
// Collect 3 decryption shares
let shares: BTreeMap<_, _> = (0..3usize)
.map(|i| {
let share = sk_set.secret_key_share(i).unwrap()
.decrypt_share(&ciphertext).unwrap();
(i, share)
})
.collect();
let decrypted = pk_set.decrypt(&shares, &ciphertext).expect("valid shares");
assert_eq!(message.to_vec(), decrypted);Sourcepub fn decrypt_with_verification<'a, T, I>(
&self,
shares: I,
ct: &Ciphertext,
) -> Result<Vec<u8>>
pub fn decrypt_with_verification<'a, T, I>( &self, shares: I, ct: &Ciphertext, ) -> Result<Vec<u8>>
Combines the shares to decrypt the ciphertext, verifying each decryption share against its corresponding public key share before interpolation.
This is the recommended method in adversarial settings where individual participants may submit invalid or malicious decryption shares.
§Errors
Error::InvalidCiphertext: The ciphertext failed integrity verificationError::NotEnoughShares: Fewer thanthreshold + 1shares were providedError::DuplicateEntry: Two shares have the same indexError::InvalidShare: A decryption share failed verification against its public key share
§Example
use std::collections::BTreeMap;
use threshold_pairing::SecretKeySet;
let mut rng = rand::thread_rng();
let sk_set = SecretKeySet::random(2, &mut rng);
let pk_set = sk_set.public_keys();
let message = b"secret message";
let ciphertext = pk_set.public_key().encrypt(message);
// Collect 3 decryption shares together with their public key shares
let shares: BTreeMap<_, _> = (0..3usize)
.map(|i| {
let sk_share = sk_set.secret_key_share(i).unwrap();
let pk_share = pk_set.public_key_share(i).unwrap();
let dec_share = sk_share.decrypt_share(&ciphertext).unwrap();
(i, (dec_share, pk_share))
})
.collect();
let decrypted = pk_set
.decrypt_with_verification(
shares.iter().map(|(i, (d, p))| (*i, d, p)),
&ciphertext,
)
.expect("valid shares");
assert_eq!(message.to_vec(), decrypted);Trait Implementations§
Source§impl Clone for PublicKeySet
impl Clone for PublicKeySet
Source§fn clone(&self) -> PublicKeySet
fn clone(&self) -> PublicKeySet
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for PublicKeySet
impl Debug for PublicKeySet
Source§impl<'de> Deserialize<'de> for PublicKeySet
impl<'de> Deserialize<'de> for PublicKeySet
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl From<Commitment> for PublicKeySet
impl From<Commitment> for PublicKeySet
Source§fn from(commit: Commitment) -> PublicKeySet
fn from(commit: Commitment) -> PublicKeySet
Source§impl Hash for PublicKeySet
impl Hash for PublicKeySet
Source§impl Ord for PublicKeySet
impl Ord for PublicKeySet
Source§fn cmp(&self, other: &PublicKeySet) -> Ordering
fn cmp(&self, other: &PublicKeySet) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialEq for PublicKeySet
impl PartialEq for PublicKeySet
Source§impl PartialOrd for PublicKeySet
impl PartialOrd for PublicKeySet
Source§impl Serialize for PublicKeySet
impl Serialize for PublicKeySet
impl Eq for PublicKeySet
impl StructuralPartialEq for PublicKeySet
Auto Trait Implementations§
impl Freeze for PublicKeySet
impl RefUnwindSafe for PublicKeySet
impl Send for PublicKeySet
impl Sync for PublicKeySet
impl Unpin for PublicKeySet
impl UnsafeUnpin for PublicKeySet
impl UnwindSafe for PublicKeySet
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§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.