Skip to content

Commit

Permalink
add range proof
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Lodder <[email protected]>
  • Loading branch information
mikelodder7 committed Aug 21, 2024
1 parent 626bcdb commit a407061
Show file tree
Hide file tree
Showing 10 changed files with 481 additions and 46 deletions.
9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ crypto = ["unknown_order/crypto"]
gmp = ["unknown_order/gmp"]
openssl = ["unknown_order/openssl"]
rust = ["unknown_order/rust"]
wasm = ["getrandom", "rand", "wasm-bindgen", "serde-wasm-bindgen"]
wasm = ["getrandom", "wasm-bindgen", "serde-wasm-bindgen"]

[dependencies]
bit-vec = "0.8"
digest = "0.10"
getrandom = { version = "0.2", features = ["js"], optional = true }
rand = { version = "0.8", optional = true }
rand = "0.8"
postcard = { version = "1.0.9", features = ["use-std"] }
serde = { version = "1.0", features = ["serde_derive"] }
serde-wasm-bindgen = { version = "0.6", optional = true }
sha2 = "0.10"
thiserror = "1.0"
unknown_order = { version = "0.10", default-features = false }
wasm-bindgen = { version = "0.2", default-features = false, features = ["serde-serialize"], optional = true }
Expand All @@ -36,9 +38,8 @@ zeroize = { version = "1.8", features = ["zeroize_derive"] }
elliptic-curve = "0.13"
hex = "0.4"
k256 = { version = "0.13", features = ["arithmetic"] }
rand_chacha = "0.3.1"
wasm-bindgen-test = "0.3"
rand = "0.8"
multibase = "0.9"
sha2 = "0.10"
serde_json = "1.0"
unicase = "2.6"
5 changes: 2 additions & 3 deletions src/decryptionkey.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{mod_in, Ciphertext, EncryptionKey, error::*};
use crate::{error::*, mod_in, Ciphertext, EncryptionKey};
use serde::{Deserialize, Serialize};
use unknown_order::BigNumber;
use zeroize::Zeroize;
Expand Down Expand Up @@ -107,8 +107,7 @@ impl DecryptionKey {
/// Convert a byte representation to a decryption key
pub fn from_bytes<B: AsRef<[u8]>>(data: B) -> PaillierResult<Self> {
let data = data.as_ref();
let bytes =
postcard::from_bytes::<DecryptionKeyBytes>(data)?;
let bytes = postcard::from_bytes::<DecryptionKeyBytes>(data)?;
let pk = EncryptionKey::from_bytes(bytes.n.as_slice())?;
Ok(Self {
pk,
Expand Down
22 changes: 15 additions & 7 deletions src/encryptionkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use unknown_order::BigNumber;
use zeroize::Zeroize;

/// A Paillier encryption key
#[derive(Clone, Debug, Zeroize)]
#[derive(Clone, Debug, Default, Zeroize)]
pub struct EncryptionKey {
pub(crate) n: BigNumber, // N = p * q, where p,q are primes
pub(crate) nn: BigNumber, // N^2
Expand Down Expand Up @@ -66,23 +66,31 @@ impl EncryptionKey {
M: AsRef<[u8]>,
{
let xx = BigNumber::from_slice(x);
if !mod_in(&xx, &self.n) {
let r = r.unwrap_or_else(|| Nonce::random(&self.n));

let c = self.encrypt_num_with_nonce(&xx, &r)?;

Ok((c, r))
}

/// Encrypt a number with the encryption key and given nonce
#[allow(clippy::many_single_char_names)]
pub fn encrypt_num_with_nonce(&self, x: &BigNumber, r: &Nonce) -> PaillierResult<Ciphertext> {
if !mod_in(x, &self.n) {
return Err(PaillierError::InvalidEncryptionInputs);
}

let r = r.unwrap_or_else(|| Nonce::random(&self.n));

if !mod_in(&r, &self.n) {
if !mod_in(r, &self.n) {
return Err(PaillierError::InvalidEncryptionInputs);
}

// a = (N+1)^m mod N^2
let a = (&self.n + BigNumber::one()).modpow(&xx, &self.nn);
let a = (&self.n + BigNumber::one()).modpow(x, &self.nn);
// b = r^N mod N^2
let b = &r.modpow(&self.n, &self.nn);

let c = a.modmul(b, &self.nn);
Ok((c, r))
Ok(c)
}

/// Combines two Paillier ciphertexts
Expand Down
9 changes: 9 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ pub enum PaillierError {
/// Invalid ciphertext
#[error("Invalid ciphertext, unable to decrypt")]
InvalidCiphertext,
/// Invalid range proof error factor number
#[error("Invalid range proof error factor number")]
InvalidRangeProofErrorFactor,
/// Invalid verifier commitment
#[error("Invalid verifier commitment")]
InvalidVerifierCommitment,
/// Invalid range proof
#[error("Invalid range proof")]
InvalidRangeProof,
}

/// Paillier results
Expand Down
17 changes: 6 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,11 @@ mod macros;
mod decryptionkey;
mod encryptionkey;
mod error;
mod proof_psf;

pub use error::*;
pub use unknown_order;
mod proof;
mod utils;

use unknown_order::BigNumber;

pub(crate) fn mod_in(a: &BigNumber, n: &BigNumber) -> bool {
let lhs = &BigNumber::one() <= a;
let rhs = a < n;
lhs & rhs
}
use utils::*;

/// A Paillier Ciphertext
pub type Ciphertext = BigNumber;
Expand All @@ -41,4 +34,6 @@ pub type Nonce = BigNumber;

pub use decryptionkey::*;
pub use encryptionkey::*;
pub use proof_psf::*;
pub use error::*;
pub use proof::*;
pub use unknown_order;
5 changes: 5 additions & 0 deletions src/proof.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod range;
mod square_free;

pub use range::*;
pub use square_free::*;
Loading

0 comments on commit a407061

Please sign in to comment.