Skip to content

Commit

Permalink
Merge pull request #12 from Chandan-M-N/final-tests
Browse files Browse the repository at this point in the history
Test cases
  • Loading branch information
Chandan-M-N authored Nov 24, 2024
2 parents 5756bc6 + cedf721 commit 7e0c7d5
Show file tree
Hide file tree
Showing 4 changed files with 489 additions and 1 deletion.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ ff = "0.9" # Finite field arithmetic
rand = "0.8" # Random number generation
num-bigint = "0.4" # For large integer support (could be useful for mod operations)
log = "0.4"
env_logger = "0.10"
env_logger = "0.10"

[dev-dependencies]
approx = "0.5.1"
29 changes: 29 additions & 0 deletions src/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,33 @@ impl CKKSEncryptor {
reduced_result
}

pub fn homomorphic_divide_with_constant(&self, cipher: &Polynomial, constant: i64) -> Polynomial {
// Gracefully handle the case when the constant is zero
if constant == 0 {
info!("Division by zero is not allowed. Returning the original polynomial unchanged.");
return cipher.clone(); // Return the original polynomial as is
}

// Scale the constant to match the ciphertext's scale
let scaling_factor = 10_000_000; // Assuming 1e7 scaling factor
let scaled_constant = constant * scaling_factor;

// Compute the reciprocal of the scaled constant
let reciprocal = scaling_factor / scaled_constant; // This is effectively 1/constant in scaled form

// Multiply the ciphertext by the reciprocal
let scaled_reciprocal_poly = Polynomial::new(vec![reciprocal]);
let result = self.homomorphic_multiply(cipher, &scaled_reciprocal_poly);

// Perform modular reduction to ensure the result fits within the modulus
let reduced_result = mod_reduce(&result, self.params.modulus);
info!(
"Result after homomorphic division with constant and mod reduction: {:?}",
reduced_result
);

reduced_result
}


}
Loading

0 comments on commit 7e0c7d5

Please sign in to comment.