[−][src]Struct elgamal_ristretto::PublicKey
The PublicKey
struct represents an ElGamal public key.
Methods
impl PublicKey
[src]
pub fn encrypt(self, message: RistrettoPoint) -> Ciphertext
[src]
Encrypts a message in the Ristretto group. It has the additive homomorphic property, allowing addition (and subtraction) by another ciphertext and multiplication (and division) by scalars.
#Example
extern crate rand; extern crate curve25519_dalek; extern crate elgamal_ristretto; use rand::rngs::OsRng; use elgamal_ristretto::{PublicKey, SecretKey}; use curve25519_dalek::ristretto::{RistrettoPoint, }; use curve25519_dalek::scalar::{Scalar, }; let mut csprng = OsRng::new().unwrap(); // Generate key pair let sk = SecretKey::new(&mut csprng); let pk = PublicKey::from(&sk); // Generate random messages let ptxt1 = RistrettoPoint::random(&mut csprng); let ptxt2 = RistrettoPoint::random(&mut csprng); // Encrypt messages let ctxt1 = pk.encrypt(ptxt1); let ctxt2 = pk.encrypt(ptxt2); // Add ciphertexts and check that addition is maintained in the plaintexts let encrypted_addition = ctxt1 + ctxt2; let decrypted_addition = sk.decrypt(encrypted_addition); assert_eq!(ptxt1 + ptxt2, decrypted_addition); // Multiply by scalar and check that multiplication is maintained in the plaintext let scalar_mult = Scalar::random(&mut csprng); assert_eq!(sk.decrypt(ctxt1 * scalar_mult), scalar_mult * ptxt1);
pub fn encrypt_and_prove(
self,
message: RistrettoPoint
) -> (Ciphertext, CompactProof)
[src]
self,
message: RistrettoPoint
) -> (Ciphertext, CompactProof)
Encrypts a message in the Ristretto group and generates a proof of correct encryption
#Example
extern crate rand; extern crate curve25519_dalek; extern crate elgamal_ristretto; use rand::rngs::OsRng; use elgamal_ristretto::{PublicKey, SecretKey}; use curve25519_dalek::ristretto::{RistrettoPoint, }; let mut csprng = OsRng::new().unwrap(); let sk = SecretKey::new(&mut csprng); let pk = PublicKey::from(&sk); let plaintext = RistrettoPoint::random(&mut csprng); // Encrypt plaintext and generate proof let (enc_plaintext, proof) = pk.encrypt_and_prove(plaintext); // Verify proof assert!(enc_plaintext.verify_correct_encryption(&plaintext, proof));
pub fn get_point(&self) -> RistrettoPoint
[src]
Get the public key as a RistrettoPoint
pub fn verify_signature(
self,
message: &RistrettoPoint,
signature: (Scalar, RistrettoPoint)
) -> bool
[src]
self,
message: &RistrettoPoint,
signature: (Scalar, RistrettoPoint)
) -> bool
Verify EdDSA signature
#Example
extern crate rand; extern crate curve25519_dalek; extern crate elgamal_ristretto; use rand::rngs::OsRng; use elgamal_ristretto::{PublicKey, SecretKey}; use curve25519_dalek::ristretto::RistrettoPoint; // Generate key-pair let mut csprng = OsRng::new().unwrap(); let sk = SecretKey::new(&mut csprng); let pk = PublicKey::from(&sk); // Sign message let msg = RistrettoPoint::random(&mut csprng); let signature = sk.sign(msg); // Verify signature assert!(pk.verify_signature(&msg, signature)); // Verify signature against incorrect message assert!(!pk.verify_signature(&RistrettoPoint::random(&mut csprng), signature))
pub fn verify_proof_knowledge(self, proof: CompactProof) -> bool
[src]
Verify proof of knowledege of private key related to a public key
Example
extern crate rand; extern crate curve25519_dalek; extern crate elgamal_ristretto; use rand::rngs::OsRng; use elgamal_ristretto::{PublicKey, SecretKey}; use curve25519_dalek::ristretto::RistrettoPoint; let mut csprng = OsRng::new().unwrap(); let sk = SecretKey::new(&mut csprng); let pk = PublicKey::from(&sk); let proof = sk.prove_knowledge(); assert!(pk.verify_proof_knowledge(proof));
pub fn to_bytes(&self) -> [u8; 32]
[src]
Convert to bytes
pub fn from_bytes(bytes: &[u8]) -> PublicKey
[src]
Generate public key from bytes
Trait Implementations
impl From<RistrettoPoint> for PublicKey
[src]
fn from(point: RistrettoPoint) -> PublicKey
[src]
Given a secret key, compute its corresponding Public key
impl<'a> From<&'a SecretKey> for PublicKey
[src]
fn from(secret: &'a SecretKey) -> PublicKey
[src]
Given a secret key, compute its corresponding Public key
impl Clone for PublicKey
[src]
impl Copy for PublicKey
[src]
impl PartialEq<PublicKey> for PublicKey
[src]
impl Debug for PublicKey
[src]
impl Serialize for PublicKey
[src]
fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error> where
__S: Serializer,
[src]
__S: Serializer,
impl<'de> Deserialize<'de> for PublicKey
[src]
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
[src]
__D: Deserializer<'de>,
Auto Trait Implementations
impl Send for PublicKey
impl Sync for PublicKey
impl Unpin for PublicKey
impl UnwindSafe for PublicKey
impl RefUnwindSafe for PublicKey
Blanket Implementations
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> From<T> for T
[src]
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> DeserializeOwned for T where
T: Deserialize<'de>,
[src]
T: Deserialize<'de>,
impl<T> Same<T> for T
type Output = T
Should always be Self