Skip to content

Commit

Permalink
Temp commit to backup
Browse files Browse the repository at this point in the history
  • Loading branch information
Gary Benattar authored and Gary Benattar committed Jun 1, 2018
1 parent 393a870 commit 37d5fdc
Show file tree
Hide file tree
Showing 11 changed files with 291 additions and 323 deletions.
5 changes: 2 additions & 3 deletions examples/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@ fn main() {
fn main() {

use paillier::*;
use paillier::core::*;

// generate a fresh keypair
let keypair = Paillier::keypair();

// choose type of encryption
let ek = standard::EncryptionKey::from(&keypair);
let ek = core::EncryptionKey::from(&keypair);

// choose type of decryption
let dk = standard::DecryptionKey::from(&keypair);
let dk = core::DecryptionKey::from(&keypair);
// let dk = crt::DecryptionKey::from(&keypair);

// pair keys with integral coding
Expand Down
8 changes: 4 additions & 4 deletions src/arithimpl/gmpimpl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ impl ModPow for Mpz {
}

impl ModMul for Mpz {
fn modmul(base: &Self, exponent: &Self, modulus: &Self) -> Self {
let base_mod_modulus = base.mod_floor(modulus);
let exponent_mod_modulus = exponent.mod_floor(modulus);
fn modmul(a: &Self, b: &Self, modulus: &Self) -> Self {
let base_mod_modulus = a.mod_floor(modulus);
let exponent_mod_modulus = b.mod_floor(modulus);
let mul_res = base_mod_modulus * exponent_mod_modulus;

mul_res.mod_floor(modulus)
Expand All @@ -71,7 +71,7 @@ impl ToString for Mpz {
}

impl FromString<Mpz> for Mpz {
fn from_hex_str(a: String) -> Mpz { Mpz::from_str_radix(&a, 16).unwrap() }
fn from_hex_str(a: &str) -> Mpz { Mpz::from_str_radix(a, 16).unwrap() }
}

impl ConvertFrom<Mpz> for u64 {
Expand Down
2 changes: 1 addition & 1 deletion src/arithimpl/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub trait ToString {
}

pub trait FromString<I> {
fn from_hex_str(a: String) -> I;
fn from_hex_str(a: &str) -> I;
}

use std::ops::{Add, Sub, Mul, Div, Rem, Shr, Neg};
Expand Down
116 changes: 1 addition & 115 deletions src/core/crt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,7 @@
//! Faster decryption using the Chinese Remainder Theorem.
use super::*;
use crypto::sha2::Sha256;
use crypto::digest::Digest;
use std::error::Error;

/// Decryption key that should be kept private.
#[derive(Debug,Clone)]
pub struct DecryptionKey<I> {
p: I, // first prime
q: I, // second prime
n: I, // the modulus (also in public key)
pp: I,
pminusone: I,
qq: I,
qminusone: I,
pinvq: I,
hp: I,
hq: I,
}

use core::DecryptionKey;

impl<I> ::traits::DecryptionKey for DecryptionKey<I> {}

Expand Down Expand Up @@ -91,102 +73,6 @@ where
}
}

impl fmt::Display for ZKProverError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "ZKProverError")
}
}

impl Error for ZKProverError {
fn description(&self) -> &str {
"Error while proving"
}
}

impl<I> ZKProver<I> for DecryptionKey<I>
where
I : Samplable,
I : Eq,
I : One,
I : ModPow,
I : ModInv,
I : ModMul,
I : EGCD,
I : ToString,
I : FromString<I>,
for<'a> &'a I: Add<I, Output=I>,
for<'b> I: Add<&'b I, Output=I>,
for<'a,'b> &'a I: Sub<&'b I, Output=I>,
for<'a> I: Rem<&'a I, Output=I>,
for<'a,'b> &'a I: Rem<&'b I, Output=I>,
for<'a> &'a I: Mul<I, Output=I>
{

fn generate_proof(&self, challenge: &Vec<I>, e: &I, z: &Vec<I>) -> Result<I, ZKProverError> {
let phi = (&self.p - &I::one()) * (&self.q - &I::one());

let mut a : Vec<I> = Vec::new();
let mut i : usize = 0;
while i < ZK_SECURITY_FACTOR {
if I::egcd(&self.n, &z[i]).0 != I::one() ||
I::egcd(&self.n, &challenge[i]).0 != I::one() {
return Err(ZKProverError);
}

let zn = I::modpow(&z[i], &self.n, &self.n);
let cphi = I::modpow(&challenge[i], &phi, &self.n);
let cminphi = I::modinv(
&I::modpow(&challenge[i], &e, &self.n), &self.n);

a.push((zn * cphi * cminphi) % &self.n);

if I::egcd(&self.n, &z[i]).0 != I::one(){
return Err(ZKProverError);
}

i += 1;
}

let mut a_x_hash = Sha256::new();
a_x_hash.input_str(&I::to_hex_str(&self.n));

let mut j : usize = 0;
while j < ZK_SECURITY_FACTOR {
a_x_hash.input_str(&I::to_hex_str(&challenge[j]));
a_x_hash.input_str(&I::to_hex_str(&a[j]));
j += 1;
}

if &I::from_hex_str(a_x_hash.result_str()) != e {
return Err(ZKProverError);
}

let dn = I::modinv(&self.n, &phi);
let dp = &dn % &(&self.p - &I::one());
let dq = &dn % &(&self.q - &I::one());

let mut y_tag_hash = Sha256::new();

let mut k : usize = 0;
while k < ZK_SECURITY_FACTOR {
let cp = &challenge[k] % &self.p;
let mp = I::modpow(&cp, &dp, &self.p);

let cq = &challenge[k] % &self.q;
let mq = I::modpow(&cq, &dq, &self.q);

let qinvp = I::modinv(&self.q, &self.p);
let mtag = &mq + (&self.q * I::modmul(&qinvp, &(&mp - &mq), &self.p));

y_tag_hash.input_str(&I::to_hex_str(&mtag));

k += 1;
}

Ok(I::from_hex_str(y_tag_hash.result_str()))
}
}

fn h<I>(p: &I, pp: &I, n: &I) -> I
where
I: One,
Expand Down
2 changes: 1 addition & 1 deletion src/core/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ mod tests {
fn test_correct_encryption_decryption() {
let ref keypair = test_keypair();
let ek: generic::EncryptionKey<_> = generic::EncryptionKey::from(keypair);
let dk: crt::DecryptionKey<_> = crt::DecryptionKey::from(keypair);
let dk: standard::DecryptionKey<_> = standard::DecryptionKey::from(keypair);
let code = integral::Code::default();

let m = code.encode(&10_u64);
Expand Down
32 changes: 27 additions & 5 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,27 @@ use std::ops::{Add, Sub, Mul, Div, Rem};
use num_traits::{One};
use arithimpl::traits::*;

/// Encryption key that may be shared publicly.
#[derive(Debug,Clone)]
pub struct EncryptionKey<I> {
pub n: I, // the modulus
nn: I, // the modulus squared
}

/// Decryption key that should be kept private.
#[derive(Debug,Clone)]
pub struct DecryptionKey<I> {
p: I, // first prime
q: I, // second prime
n: I, // the modulus (also in public key)
pp: I,
pminusone: I,
qq: I,
qminusone: I,
pinvq: I,
hp: I,
hq: I,
}

/// Representation of a keypair from which encryption and decryption keys can be derived.
pub struct Keypair<I> {
Expand Down Expand Up @@ -56,15 +77,15 @@ where // TODO clean up bounds
for<'a> I: Rem<&'a I, Output=I>,
for<'a,'b> &'a I: Rem<&'b I, Output=I>,
{
type EK = standard::EncryptionKey<I>;
type DK = crt::DecryptionKey<I>;
type EK = EncryptionKey<I>;
type DK = DecryptionKey<I>;

fn encryption_key(&self) -> Self::EK {
standard::EncryptionKey::from(self)
EncryptionKey::from(self)
}

fn decryption_key(&self) -> Self::DK {
crt::DecryptionKey::from(self)
DecryptionKey::from(self)
}
}

Expand Down Expand Up @@ -124,6 +145,7 @@ where
pub mod generic;
pub mod standard;
pub mod crt;
pub mod zkproof;

#[cfg(feature="keygen")]
pub mod keygen;
Expand Down Expand Up @@ -191,7 +213,7 @@ mod tests {
#[cfg(feature="keygen")]
#[test]
fn test_correct_keygen() {
let (ek, dk): (standard::EncryptionKey<I>, _) = AbstractPaillier::keypair_with_modulus_size(2048).keys();
let (ek, dk): (EncryptionKey<I>, _) = AbstractPaillier::keypair_with_modulus_size(2048).keys();

let m = Plaintext::from(10);
let c = AbstractPaillier::encrypt(&ek, &m);
Expand Down
11 changes: 1 addition & 10 deletions src/core/standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,10 @@
//! Standard encryption and decryption.
use super::*;


/// Encryption key that may be shared publicly.
#[derive(Debug,Clone)]
pub struct EncryptionKey<I> {
pub n: I, // the modulus
nn: I, // the modulus squared
}

use core::EncryptionKey;

impl<I> ::traits::EncryptionKey for EncryptionKey<I> {}


impl<'kp, I> From<&'kp Keypair<I>> for EncryptionKey<I>
where
I: Clone,
Expand Down
Loading

0 comments on commit 37d5fdc

Please sign in to comment.