Skip to content

A pure-Rust implementation of the Paillier encryption scheme

License

Notifications You must be signed in to change notification settings

snipsco/rust-paillier

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

80 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Paillier

Build Status Latest version License: MIT/Apache2

Efficient pure-Rust library for the Paillier partially homomorphic encryption scheme, offering encoding of both scalars and vectors (for encrypting several values together). Supports several underlying arbitrary precision libraries, including RAMP (default), GMP, and num.

Important: while we have followed recommendations regarding the scheme itself, this library should currently be seen as an experimental implementation. In particular, no particular efforts have so far been made to harden it against non-cryptographic attacks, including side-channel attacks.

extern crate paillier;
use paillier::*;

fn main() {

  // generate a fresh keypair and extract encryption and decryption keys
  let (ek, dk) = Paillier::keypair().keys();

  // select integral coding
  let code = integral::Code::default();

  // pair keys with coding
  let eek = ek.with_code(&code);
  let ddk = dk.with_code(&code);

  // encrypt four values
  let c1 = Paillier::encrypt(&eek, &10);
  let c2 = Paillier::encrypt(&eek, &20);
  let c3 = Paillier::encrypt(&eek, &30);
  let c4 = Paillier::encrypt(&eek, &40);

  // add all of them together
  let c = Paillier::add(&eek,
    &Paillier::add(&eek, &c1, &c2),
    &Paillier::add(&eek, &c3, &c4)
  );

  // multiply the sum by 2
  let d = Paillier::mul(&eek, &c, &2);

  // decrypt final result
  let m: u64 = Paillier::decrypt(&ddk, &d);
  println!("decrypted total sum is {}", m);

}

Installation

Note that some functionality is not included by default; see the Building section for more details.

GitHub

git clone https://github.com/snipsco/rust-paillier
cd rust-paillier
cargo build --release

Cargo

[dependencies]
paillier = { version="0.1" }

Building

The nightly toolchain is currently needed in order to build the library. For performance reasons we strongly encourage building and testing in release mode.

Arithmetic

The library supports the use of different arithmetic libraries, currently defaulting to RAMP for portability with good performance.

For RAMP-only compilation use cargo parameters

--no-default-features --features "inclramp defaultramp keygen"

For num-only compilation use

--no-default-features --features "inclnum defaultnum"

For GMP-only compilation use

--no-default-features --features "inclgmp defaultgmp keygen"

Finally, use

--no-default-features --features "inclramp inclnum inclgmp defaultramp"

to have one or more available, using one of them as the default (useful for e.g. performance tests).

Key generation

Key generation is optional as it is currently only implemented when using RAMP or GMP as the underlying arithmetic library.

While included by default it may be excluded using parameter

--no-default-features

in which case one or more arithmetic libraries must be specified as well as a default one, e.g.

--features "inclramp inclgmp defaultramp"

as shown in above .

Performance

Several benches are included, testing both the underlying arithmetic libraries as well as the operations of the scheme. All may be run using

cargo bench

and including either several arithmetic libraries and key generation as discussed above.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.