Skip to content

Commit

Permalink
Performance Optimizations (#5)
Browse files Browse the repository at this point in the history
Enabling Lazy NTT, giving a speedup of about 2x for encryption/decryption/rerandomization. 

* Updated version to 0.2.0

* added poly operation benchmarks

* added butterfly mod and tests

* cleaned up interfaces.

* basic lazy butterfly tests work

* lazy butterfly and tests

* added trait and tests for lazy transforms

* swap

* switch to use split_at

* added lazy (i)ntt; tests pass.

* conditional compilation

* benchmarking

* optimized performance by pure u64 impl

* added benchmark for integer ops and gaussian sampling

* sample ternary

* optimized uniform sampling

* clean up

* enabled lazy ntt by default

* fixing reviewer comments

* fix underflow

* further addressing comments

* updating version to 0.2.1

* remove unused dependencies

Co-authored-by: Hao Chen <sxxach@gmail.com>
  • Loading branch information
haochenuw and haochenCrypto committed Aug 30, 2021
1 parent c8ee75a commit df18280
Show file tree
Hide file tree
Showing 22 changed files with 1,086 additions and 532 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
Cargo.lock
src/*.md
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## 0.2.1 (August 30, 2021)

* Performance optimizations: Faster encryption/decryption based on lazy NTT

## 0.2.0 (June 1, 2021)

* Added serailization support
* Added ability to customize plaintext modulus
20 changes: 18 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "Cupcake"
version = "0.1.1"
version = "0.2.1"
authors = ["Hao Chen <haoche@fb.com>"]
license = "MIT"
edition = "2018"
Expand All @@ -20,12 +20,28 @@ bencher = "0.1.5"
name = "cupcake"
path = "src/lib.rs"

[features]
bench = []

[[example]]
name = "basic"

[[example]]
name = "serialization"

[[bench]]
name = "example"
name = "scheme"
harness = false

[[bench]]
name = "polyops"
harness = false
required-features = ["bench"]

[[bench]]
name = "butterfly"
harness = false

[[bench]]
name = "integerops"
harness = false
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Cupcake requires or works with
## Installation
Add the following line to the dependencies of your Cargo.toml:
```
Cupcake = "0.1.1"
Cupcake = "0.2.1"
```

## Building from source
Expand Down
73 changes: 73 additions & 0 deletions benches/butterfly.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright (c) Facebook, Inc. and its affiliates.
//
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.
#[macro_use]
extern crate bencher;
use bencher::Bencher;
use cupcake::integer_arith::butterfly::{
butterfly, inverse_butterfly, lazy_butterfly, lazy_butterfly_u64,
};
use cupcake::integer_arith::scalar::Scalar;
use cupcake::integer_arith::ArithUtils;

#[allow(non_snake_case)]
fn bench_butterfly(bench: &mut Bencher) {
let q = Scalar::new_modulus(18014398492704769u64);
let x = rand::random::<u64>();
let y = rand::random::<u64>();
let w = rand::random::<u64>();

let mut X = Scalar::from(x);
let mut Y = Scalar::from(y);
let W = Scalar::from(w);

bench.iter(|| {
let _ = butterfly(&mut X, &mut Y, &W, &q);
})
}

#[allow(non_snake_case)]
fn bench_inverse_butterfly(bench: &mut Bencher) {
let q = Scalar::new_modulus(18014398492704769u64);
let x = rand::random::<u64>();
let y = rand::random::<u64>();
let w = rand::random::<u64>();

let mut X = Scalar::from(x);
let mut Y = Scalar::from(y);
let W = Scalar::from(w);

bench.iter(|| {
let _ = inverse_butterfly(&mut X, &mut Y, &W, &q);
})
}

#[allow(non_snake_case)]
fn bench_lazy_butterfly(bench: &mut Bencher) {
let q = Scalar::new_modulus(18014398492704769u64);
let x = rand::random::<u64>();
let y = rand::random::<u64>();
let w = rand::random::<u64>();

let mut X = Scalar::from(x);
let mut Y = Scalar::from(y);
let W = Scalar::from(w);

let Wprime: u64 = cupcake::integer_arith::util::compute_harvey_ratio(W.rep(), q.rep());

let twoq: u64 = q.rep() << 1;

bench.iter(|| {
let _ = lazy_butterfly_u64(x, y, W.rep(), Wprime, q.rep(), twoq);
})
}

benchmark_group!(
butterfly_group,
bench_butterfly,
bench_inverse_butterfly,
bench_lazy_butterfly
);

benchmark_main!(butterfly_group);
Loading

0 comments on commit df18280

Please sign in to comment.