[][src]Struct elgamal_ristretto::PublicKey

pub struct 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]

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]

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 verify_correct_decryption(
    self,
    proof: CompactProof,
    ciphertext: Ciphertext,
    plaintext: RistrettoPoint
) -> bool
[src]

Verify correct decryption

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);
   let ciphertext = pk.encrypt(plaintext);

   let decryption = sk.decrypt(ciphertext);
   let proof = sk.prove_correct_decryption(ciphertext, decryption);

   assert!(pk.verify_correct_decryption(proof, ciphertext, decryption));

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]

impl<'de> Deserialize<'de> for PublicKey[src]

Auto Trait Implementations

Blanket Implementations

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = !

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> DeserializeOwned for T where
    T: Deserialize<'de>, 
[src]

impl<T> Same<T> for T

type Output = T

Should always be Self