Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove ring and fix clippy warnings #68

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,27 @@ travis-ci = { repository = "mortendahl/rust-paillier" }
[features]
default = ["usegmp", "keygen", "proofs"]
keygen = []
proofs = ["ring"]
proofs = ["rust-crypto", "hex"]
useramp = ["ramp"]
useframp = ["framp"]
usegmp = ["rust-gmp"]
gmp_nonsec = []

[dependencies]
num-traits = "0.1"
rand = "0.5"
rayon = "1.0"
num-traits = "0.2"
rand = "0.6"
rayon = "1.1"
serde = "1.0"
serde_derive = "1.0"
ramp = { version="0.5", optional=true }
framp = { version="0.3", optional=true }
rust-gmp = { version="0.5", optional=true }
ring = { version="0.13", optional=true }

bit-vec = "0.5"
ramp = { version = "0.5", optional = true }
framp = { version = "0.3", optional = true }
rust-gmp = { version = "0.5", optional = true }
bit-vec = "0.6"
rust-crypto = { version = "^0.2", optional = true }
hex = { version = "^0.3", optional = true }

[dev-dependencies]
bencher = "0.1"
# bencher = { git="https://github.com/snipsco-forks/bencher" }
serde_json = "1.0"

[[bench]]
Expand Down
16 changes: 8 additions & 8 deletions benches/arith.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,28 @@ mod helpers;
use helpers::*;

pub fn bench_mul(b: &mut Bencher) {
let ref p: BigInt = str::parse(P2048).unwrap();
let ref q: BigInt = str::parse(Q2048).unwrap();
let p: &BigInt = &str::parse(P2048).unwrap();
let q: &BigInt = &str::parse(Q2048).unwrap();

b.iter(|| {
let _ = p * q;
});
}

pub fn bench_mulrem(b: &mut Bencher) {
let ref p: BigInt = str::parse(P2048).unwrap();
let ref q: BigInt = str::parse(Q2048).unwrap();
let ref n: BigInt = str::parse(N2048).unwrap();
let p: &BigInt = &str::parse(P2048).unwrap();
let q: &BigInt = &str::parse(Q2048).unwrap();
let n: &BigInt = &str::parse(N2048).unwrap();

b.iter(|| {
let _ = (p * q) % n;
});
}

pub fn bench_modarith(b: &mut Bencher) {
let ref p: BigInt = str::parse(P2048).unwrap();
let ref q: BigInt = str::parse(Q2048).unwrap();
let ref n: BigInt = str::parse(N2048).unwrap();
let p: &BigInt = &str::parse(P2048).unwrap();
let q: &BigInt = &str::parse(Q2048).unwrap();
let n: &BigInt = &str::parse(N2048).unwrap();

b.iter(|| {
let _ = BigInt::modpow(p, q, n);
Expand Down
13 changes: 6 additions & 7 deletions benches/encryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ extern crate num_traits;
extern crate paillier;

use bencher::Bencher;
use paillier::encoding::*;
use paillier::*;

mod helpers;
use helpers::*;

pub fn bench_encryption_ek<KS: KeySize>(b: &mut Bencher) {
let ref keypair = KS::keypair();
let keypair = &KS::keypair();
let ek = EncryptionKey::from(keypair);

b.iter(|| {
Expand All @@ -20,7 +19,7 @@ pub fn bench_encryption_ek<KS: KeySize>(b: &mut Bencher) {
}

pub fn bench_encryption_dk<KS: KeySize>(b: &mut Bencher) {
let ref keypair = KS::keypair();
let keypair = &KS::keypair();
let dk = DecryptionKey::from(keypair);

b.iter(|| {
Expand All @@ -29,7 +28,7 @@ pub fn bench_encryption_dk<KS: KeySize>(b: &mut Bencher) {
}

pub fn bench_decryption<KS: KeySize>(b: &mut Bencher) {
let ref keypair = KS::keypair();
let keypair = &KS::keypair();
let (ek, dk) = keypair.keys();

let c = Paillier::encrypt(&ek, 10);
Expand All @@ -40,7 +39,7 @@ pub fn bench_decryption<KS: KeySize>(b: &mut Bencher) {
}

pub fn bench_rerandomisation<KS: KeySize>(b: &mut Bencher) {
let ref keypair = KS::keypair();
let keypair = &KS::keypair();
let ek = EncryptionKey::from(keypair);

let c = Paillier::encrypt(&ek, 10);
Expand All @@ -51,7 +50,7 @@ pub fn bench_rerandomisation<KS: KeySize>(b: &mut Bencher) {
}

pub fn bench_addition<KS: KeySize>(b: &mut Bencher) {
let ref keypair = KS::keypair();
let keypair = &KS::keypair();
let ek = EncryptionKey::from(keypair);

let c1 = Paillier::encrypt(&ek, 10);
Expand All @@ -63,7 +62,7 @@ pub fn bench_addition<KS: KeySize>(b: &mut Bencher) {
}

pub fn bench_multiplication<KS: KeySize>(b: &mut Bencher) {
let ref keypair = KS::keypair();
let keypair = &KS::keypair();
let ek = EncryptionKey::from(keypair);

let c = Paillier::encrypt(&ek, 10);
Expand Down
12 changes: 6 additions & 6 deletions benches/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ extern crate paillier;
use paillier::*;

// 1024 bit primes => 2048 bit modulus
pub static P2048: &'static str = "148677972634832330983979593310074301486537017973460461278300587514468301043894574906886127642530475786889672304776052879927627556769456140664043088700743909632312483413393134504352834240399191134336344285483935856491230340093391784574980688823380828143810804684752914935441384845195613674104960646037368551517";
pub static Q2048: &'static str = "158741574437007245654463598139927898730476924736461654463975966787719309357536545869203069369466212089132653564188443272208127277664424448947476335413293018778018615899291704693105620242763173357203898195318179150836424196645745308205164116144020613415407736216097185962171301808761138424668335445923774195463";
pub static N2048: &'static str = "23601375460155562757123678360900229644381030159964965932095920363097284825175029196457022864038449469086188985762066259059164844287276915193108505099612427967057134520230945630209577834878763915645946525724125804370016991193585261991964913084246563304755455418791629494251095184144084978275430600444710605147457044597210354635288909909182640243950968376955162386281524128586829759108414295175173359174297599533960370415928328418610692822180389889327103292184546896322100484378149887147731744901289563127581082141485046742100147976163228583170704180024449958168221243717383276594270459874555884125566472776234343167371";
pub static P2048: &str = "148677972634832330983979593310074301486537017973460461278300587514468301043894574906886127642530475786889672304776052879927627556769456140664043088700743909632312483413393134504352834240399191134336344285483935856491230340093391784574980688823380828143810804684752914935441384845195613674104960646037368551517";
pub static Q2048: &str = "158741574437007245654463598139927898730476924736461654463975966787719309357536545869203069369466212089132653564188443272208127277664424448947476335413293018778018615899291704693105620242763173357203898195318179150836424196645745308205164116144020613415407736216097185962171301808761138424668335445923774195463";
pub static N2048: &str = "23601375460155562757123678360900229644381030159964965932095920363097284825175029196457022864038449469086188985762066259059164844287276915193108505099612427967057134520230945630209577834878763915645946525724125804370016991193585261991964913084246563304755455418791629494251095184144084978275430600444710605147457044597210354635288909909182640243950968376955162386281524128586829759108414295175173359174297599533960370415928328418610692822180389889327103292184546896322100484378149887147731744901289563127581082141485046742100147976163228583170704180024449958168221243717383276594270459874555884125566472776234343167371";

// 2048 bit primes => 4096 bit modulus
pub static P4096: &'static str = "54012895487015803837782421918841304863093162502146915827099238255626761389465957752056702693431430972436786355954646022466841435632094385265559627938436498972714352765471698566168945062965812056432412175521672036039582393637684261505269548649599691053041645072024278713283987472744964393377089048380212183701013564638897218456903964669359622810875460724326972855594957135344351009076932272355015777958742805494234839710255927334289902051693131165245513596331706022111667560809760947628509288759753593140967096047486612859680010875340619186313770693509235798857494768621913543203586903819461926872265770592622637080247";
pub static Q4096: &'static str = "60110804761482905184172241999095064083721568391310132372880785562823040626081548259976195239057024762128798436684644401019565227508680839629752481384744855648596664223620474562582585419094571730852126918991494749938349375651158144545334949768160783962056913632707282062013023732986998195594940491859337992015569093391582644730733764652146222141495874869085082992832080902317418308778550853362446428222413647016439326663338175383509775221151568910938769471308411320393345489705012051577672571014388700476797545130036524629098427518061068575727892423981365405385986469525296662636940291427883820330312960173766723887143";
pub static N4096: &'static str = "3246758615222388102257247104619985257592790129095589210285276009429248256483846762934600391064503048539903536673803710898604266821127692553307361753316149607744596533947638369976896670599527959946456949729058671997201321029364087175491520869992605813032138070666142912786334578770232410719158199903915219886365963038477353646170462629197320969461918509765448690461526595960295577353920421639783555592907467785122476992591305198715822048909651296920289129580964452643808772386398216489780200158235271114140320078333479463828730923289630749950844692411371115829797899202089704002350025399751552212048387273162551252449279900043300405231911911403088435999645178423690062241837444313757921133439123090595809089406205378183174668004730796885645012612585689200392985339436110097924364054047371753194447028031925597558713228181086781152399656291395019275676908809117433906584203865571607578128934025711654282559310109420295931262272083976366943491672319050567929651567558548285963107610847891996140722185310234362659096832657024506723511060949620779357547927351440604423321590228598130693198375381347497839507868423146129670757985484273179950113558103417825488895000061485634292528378356202449380174380166345045052714420243023833347862564321";
pub static P4096: &str = "54012895487015803837782421918841304863093162502146915827099238255626761389465957752056702693431430972436786355954646022466841435632094385265559627938436498972714352765471698566168945062965812056432412175521672036039582393637684261505269548649599691053041645072024278713283987472744964393377089048380212183701013564638897218456903964669359622810875460724326972855594957135344351009076932272355015777958742805494234839710255927334289902051693131165245513596331706022111667560809760947628509288759753593140967096047486612859680010875340619186313770693509235798857494768621913543203586903819461926872265770592622637080247";
pub static Q4096: &str = "60110804761482905184172241999095064083721568391310132372880785562823040626081548259976195239057024762128798436684644401019565227508680839629752481384744855648596664223620474562582585419094571730852126918991494749938349375651158144545334949768160783962056913632707282062013023732986998195594940491859337992015569093391582644730733764652146222141495874869085082992832080902317418308778550853362446428222413647016439326663338175383509775221151568910938769471308411320393345489705012051577672571014388700476797545130036524629098427518061068575727892423981365405385986469525296662636940291427883820330312960173766723887143";
pub static N4096: &str = "3246758615222388102257247104619985257592790129095589210285276009429248256483846762934600391064503048539903536673803710898604266821127692553307361753316149607744596533947638369976896670599527959946456949729058671997201321029364087175491520869992605813032138070666142912786334578770232410719158199903915219886365963038477353646170462629197320969461918509765448690461526595960295577353920421639783555592907467785122476992591305198715822048909651296920289129580964452643808772386398216489780200158235271114140320078333479463828730923289630749950844692411371115829797899202089704002350025399751552212048387273162551252449279900043300405231911911403088435999645178423690062241837444313757921133439123090595809089406205378183174668004730796885645012612585689200392985339436110097924364054047371753194447028031925597558713228181086781152399656291395019275676908809117433906584203865571607578128934025711654282559310109420295931262272083976366943491672319050567929651567558548285963107610847891996140722185310234362659096832657024506723511060949620779357547927351440604423321590228598130693198375381347497839507868423146129670757985484273179950113558103417825488895000061485634292528378356202449380174380166345045052714420243023833347862564321";

pub trait KeySize {
fn size() -> usize;
Expand Down
36 changes: 18 additions & 18 deletions src/arithimpl/gmpimpl.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg(feature="usegmp")]
#![cfg(feature = "usegmp")]

extern crate gmp;

Expand Down Expand Up @@ -38,7 +38,7 @@ impl NumberTests for Mpz {
me.is_multiple_of(&Mpz::from(2))
}
fn is_negative(me: &Self) -> bool {
me < &Mpz::from(0)
*me < Mpz::from(0)
}
}

Expand Down Expand Up @@ -78,57 +78,57 @@ impl EGCD for Mpz {

impl ConvertFrom<Mpz> for u8 {
fn _from(x: &Mpz) -> u8 {
let foo: Option<u64> = x.into();
foo.unwrap() as u8
let opt_x: Option<u64> = x.into();
opt_x.unwrap() as u8
}
}

impl ConvertFrom<Mpz> for u16 {
fn _from(x: &Mpz) -> u16 {
let foo: Option<u64> = x.into();
foo.unwrap() as u16
let opt_x: Option<u64> = x.into();
opt_x.unwrap() as u16
}
}

impl ConvertFrom<Mpz> for u32 {
fn _from(x: &Mpz) -> u32 {
let foo: Option<u64> = x.into();
foo.unwrap() as u32
let opt_x: Option<u64> = x.into();
opt_x.unwrap() as u32
}
}

impl ConvertFrom<Mpz> for u64 {
fn _from(x: &Mpz) -> u64 {
let foo: Option<u64> = x.into();
foo.unwrap()
let opt_x: Option<u64> = x.into();
opt_x.unwrap()
}
}

impl ConvertFrom<Mpz> for i8 {
fn _from(x: &Mpz) -> i8 {
let foo: Option<u64> = x.into();
foo.unwrap() as i8
let opt_x: Option<u64> = x.into();
opt_x.unwrap() as i8
}
}

impl ConvertFrom<Mpz> for i16 {
fn _from(x: &Mpz) -> i16 {
let foo: Option<u64> = x.into();
foo.unwrap() as i16
let opt_x: Option<u64> = x.into();
opt_x.unwrap() as i16
}
}

impl ConvertFrom<Mpz> for i32 {
fn _from(x: &Mpz) -> i32 {
let foo: Option<u64> = x.into();
foo.unwrap() as i32
let opt_x: Option<u64> = x.into();
opt_x.unwrap() as i32
}
}

impl ConvertFrom<Mpz> for i64 {
fn _from(x: &Mpz) -> i64 {
let foo: Option<u64> = x.into();
foo.unwrap() as i64
let opt_x: Option<u64> = x.into();
opt_x.unwrap() as i64
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/encoding/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ where
{
let mut packed = BigInt::from(components[0]);
for component in &components[1..] {
packed = packed << component_bitsize;
packed = packed + BigInt::from(*component);
packed <<= component_bitsize;
packed += BigInt::from(*component);
}
packed
}
Expand Down Expand Up @@ -62,9 +62,9 @@ fn test_pack() {
let packed = pack(&*v, component_bitsize);
assert_eq!(
packed,
BigInt::from(1) * (BigInt::from(1) << 2 * component_bitsize)
+ BigInt::from(2) * (BigInt::from(1) << 1 * component_bitsize)
+ BigInt::from(3) * (BigInt::from(1) << 0 * component_bitsize)
BigInt::from(1) * (BigInt::from(1) << (2 * component_bitsize))
+ BigInt::from(2) * (BigInt::from(1) << component_bitsize)
+ BigInt::from(3) * (BigInt::from(1))
);

let unpacked: Vec<u64> = unpack(packed, component_bitsize, 3);
Expand Down
10 changes: 5 additions & 5 deletions src/keygen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl PrimeSampable for BigInt {
if is_prime(&candidate) {
return candidate;
}
candidate = candidate + &two;
candidate += &two;
}
}
}
Expand All @@ -52,7 +52,7 @@ impl PrimeSampable for BigInt {
// 3. Run five rounds of the Miller-Rabin test on the candidate.
fn is_prime(candidate: &BigInt) -> bool {
// First, simple trial divide
for p in SMALL_PRIMES.into_iter() {
for p in SMALL_PRIMES.iter() {
let prime = BigInt::from(*p);
let r = candidate % &prime;
if !NumberTests::is_zero(&r) {
Expand Down Expand Up @@ -109,7 +109,7 @@ fn miller_rabin(candidate: &BigInt, limit: usize) -> bool {
} else if y == candidate - &one {
break;
}
counter = counter + BigInt::one();
counter += BigInt::one();
}
return false;
}
Expand All @@ -125,7 +125,7 @@ fn rewrite(n: &BigInt) -> (BigInt, BigInt) {
let one = BigInt::one();

while BigInt::is_even(&d) {
d = d >> 1_usize;
d >>= 1_usize;
s = &s + &one;
}

Expand All @@ -134,7 +134,7 @@ fn rewrite(n: &BigInt) -> (BigInt, BigInt) {

// BoringSSL's table.
// https://boringssl.googlesource.com/boringssl/+/master/crypto/bn/prime.c
#[cfg_attr(rustfmt, rustfmt_skip)]
#[rustfmt::skip]
static SMALL_PRIMES: [u32; 2048] = [
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31,
37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79,
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#![feature(test)]
#![feature(specialization)]

extern crate crypto;
extern crate hex;

extern crate bit_vec;
extern crate num_traits;
extern crate rand;
extern crate rayon;
#[cfg(feature = "proofs")]
extern crate ring;
extern crate serde;
extern crate test;
#[macro_use]
Expand Down
27 changes: 4 additions & 23 deletions src/proof/correct_key.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use std::borrow::Borrow;
use std::error::Error;
use std::fmt;
use std::iter;

use rayon::prelude::*;
use ring::digest::{Context, SHA256};

use arithimpl::traits::*;
use core::extract_nroot;
use rayon::prelude::*;
use {BigInt, DecryptionKey, EncryptionKey, Paillier};

use super::compute_digest;

const STATISTICAL_ERROR_FACTOR: usize = 40;

// TODO: generalize the error string and move the struct to a common location where all other proofs can use it as well
Expand Down Expand Up @@ -162,10 +161,7 @@ impl CorrectKey<EncryptionKey, DecryptionKey> for Paillier {
}

// compute proof in the form of a hash of the recovered roots
let s_digest = compute_digest(challenge.sn.iter().map(|sni| {
let si = extract_nroot(dk, sni);
si
}));
let s_digest = compute_digest(challenge.sn.iter().map(|sni| extract_nroot(dk, sni)));

Ok(CorrectKeyProof { s_digest })
}
Expand All @@ -180,20 +176,6 @@ impl CorrectKey<EncryptionKey, DecryptionKey> for Paillier {
}
}

// TODO[Morten] generalise and move to super
fn compute_digest<IT>(values: IT) -> BigInt
where
IT: Iterator,
IT::Item: Borrow<BigInt>,
{
let mut digest = Context::new(&SHA256);
for value in values {
let bytes: Vec<u8> = value.borrow().into();
digest.update(&bytes);
}
BigInt::from(digest.finish().as_ref())
}

#[cfg(test)]
mod tests {

Expand Down Expand Up @@ -242,5 +224,4 @@ mod tests {
let result = Paillier::verify(&proof_results.unwrap(), &verification_aid);
assert!(result.is_err()); // ERROR expected because of manipulated aid
}

}
Loading