facebookresearch/Cupcake

Implement `std::ops::{Add, Sub, AddAssign, SubAssign}` for ring elements and for ciphertexts

Pratyush opened this issue · 1 comments

Rust enables overloading of the +, +=, -, and -= operators for user-defined types; this could be used to enable writing something like &a + &b, where a and b are ciphertexts, or even where b is a plaintext message. For ring-ring and ciphertext-ciphertext addition, the impl is fairly straightforward, along the lines of

impl<'a, T> Add<&'a RqPoly<T>> for &'a RqPoly<T>
where
    T: Clone + ArithUtils<T> + PartialEq,
{
	type Output = RqPoly<T>;
	
	fn add(self, other: &RqPoly<T>) -> RqPoly<T> {
		let mut result = self.clone();
		result.add_inplace(other);
		result
	}
}

impl<'a, T> Add<&'a FVCiphertext<T>> for &'a FVCiphertext<T>
where
    T: Clone + ArithUtils<T> + PartialEq,
{
	type Output = FVCiphertext<T>;
	
    fn add(self, other: &FVCiphertext<T>, ct2: &FVCiphertext<T>) -> FVCiphertext<T> {
    	let mut result = self.clone();
		result.0.add_inplace(&other.0);
		result.1.add_inplace(&other.1);
		result
    }
}

For plaintext-ciphertext addition, it's a little more complicated as we need to pass in some additional data (namely, self.delta here), but if we include this data in the ciphertext or plaintext then overloading should be possible still.

Let me know if a PR for some of these changes would be appreciated!

Definitely! Contributions are welcome. Just make sure you have submitted the CLA in CONTRIBUTING.md. Thanks:)