Struct seal_fhe::BFVEncoder
source · [−]pub struct BFVEncoder { /* private fields */ }
Expand description
Provides functionality for CRT batching. If the polynomial modulus degree is N, and the plaintext modulus is a prime number T such that T is congruent to 1 modulo 2N, then BatchEncoder allows the plaintext elements to be viewed as 2-by-(N/2) matrices of integers modulo T. Homomorphic operations performed on such encrypted matrices are applied coefficient (slot) wise, enabling powerful Batched functionality for computations that are vectorizable. This functionality is often called “batching” in the homomorphic encryption literature.
Mathematical Background
Mathematically speaking, if the polynomial modulus is X^N+1
, N
is a power of two, and
PlainModulus is a prime number T
such that 2N
divides T-1
, then integers modulo T
contain a primitive 2N
-th root of unity and the polynomial X^N+1
splits into n
distinct
linear factors as X^N+1 = (X-a_1)*...*(X-a_N) mod T
, where the constants a_1, ..., a_n
are all the distinct primitive 2N
-th roots of unity in integers modulo T
. The Chinese
Remainder Theorem (CRT) states that the plaintext space Z_T[X]/(X^N+1)
in this case is
isomorphic (as an algebra) to the N-fold direct product of fields Z_T
. The isomorphism
is easy to compute explicitly in both directions, which is what this class does.
Furthermore, the Galois group of the extension is (Z/2NZ)* ~= Z/2Z x Z/(N/2)
whose
action on the primitive roots of unity is easy to describe. Since the batching slots
correspond 1-to-1 to the primitive roots of unity, applying Galois automorphisms on the
plaintext act by permuting the slots. By applying generators of the two cyclic
subgroups of the Galois group, we can effectively view the plaintext as a 2
-by-(N/2)
matrix, and enable cyclic row rotations, and column rotations (row swaps).
Valid Parameters
Whether batching can be used depends on whether the plaintext modulus has been chosen appropriately. Thus, to construct a BatchEncoder the user must provide an instance of SEALContext such that its associated EncryptionParameterQualifiers object has the flags ParametersSet and EnableBatching set to true.
Implementations
sourceimpl BFVEncoder
impl BFVEncoder
sourcepub fn new(ctx: &Context) -> Result<Self>
pub fn new(ctx: &Context) -> Result<Self>
Creates a BatchEncoder. It is necessary that the encryption parameters given through the SEALContext object support batching. This means you used PlainModulus::batching when you created your encryption_parameters.
ctx
- The Context
sourcepub fn encode_unsigned(&self, data: &[u64]) -> Result<Plaintext>
pub fn encode_unsigned(&self, data: &[u64]) -> Result<Plaintext>
Creates a plaintext from a given matrix. This function “batches” a given matrix of integers modulo the plaintext modulus into a plaintext element, and stores the result in the destination parameter. The input vector must have size at most equal to the degree of the polynomial modulus. The first half of the elements represent the first row of the matrix, and the second half represent the second row. The numbers in the matrix can be at most equal to the plaintext modulus for it to represent a valid plaintext.
The matrix’s elements are of type u64
.
data
- The 2xN
matrix of integers modulo plaintext modulus to batch
sourcepub fn encode_signed(&self, data: &[i64]) -> Result<Plaintext>
pub fn encode_signed(&self, data: &[i64]) -> Result<Plaintext>
Creates a plaintext from a given matrix. This function “batches” a given matrix of integers modulo the plaintext modulus into a plaintext element, and stores the result in the destination parameter. The input vector must have size at most equal to the degree of the polynomial modulus. The first half of the elements represent the first row of the matrix, and the second half represent the second row. The numbers in the matrix can be at most equal to the plaintext modulus for it to represent a valid plaintext.
The matrix’s elements are of type i64
.
data
- The 2xN
matrix of integers modulo plaintext modulus to batch
sourcepub fn decode_unsigned(&self, plaintext: &Plaintext) -> Result<Vec<u64>>
pub fn decode_unsigned(&self, plaintext: &Plaintext) -> Result<Vec<u64>>
Inverse of encode. This function “unbatches” a given plaintext into a matrix of integers modulo the plaintext modulus, and stores the result in the destination parameter. The input plaintext must have degrees less than the polynomial modulus, and coefficients less than the plaintext modulus, i.e. it must be a valid plaintext for the encryption parameters. Dynamic memory allocations in the process are allocated from the memory pool pointed to by the given MemoryPoolHandle.
The input plaintext matrix should be known to contain u64
elements.
plain
- The plaintext polynomial to unbatch
sourcepub fn decode_signed(&self, plaintext: &Plaintext) -> Result<Vec<i64>>
pub fn decode_signed(&self, plaintext: &Plaintext) -> Result<Vec<i64>>
Inverse of encode. This function “unbatches” a given plaintext into a matrix of integers modulo the plaintext modulus, and stores the result in the destination parameter. The input plaintext must have degrees less than the polynomial modulus, and coefficients less than the plaintext modulus, i.e. it must be a valid plaintext for the encryption parameters. Dynamic memory allocations in the process are allocated from the memory pool pointed to by the given MemoryPoolHandle.
The input plaintext matrix should be known to contain i64
elements.
plain
- The plaintext polynomial to unbatch
sourcepub fn get_slot_count(&self) -> usize
pub fn get_slot_count(&self) -> usize
Returns the number of “Batched” slots in this encoder produces.
Trait Implementations
Auto Trait Implementations
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more