BGV API¶
Data Structures¶
Poseidon Supported parameter data structures, the following is specific to BGV:
1. BGV encoding and decoding class : BatchEncoder¶
Description:BatchEncoder is used to encode and decode message in the BGV encryption scheme.
Functions:
BatchEncoder(const PoseidonContext &context);
- context (const PoseidonContext &): The poseidon context.
Usage: Construction function creates a BatchEncoder. It is necessary that the encryption parameters given through the PoseidonContext object support batching.
void encode(const vector<uint32_t> &src, Plaintext &plain);
- src (const vector
&): The source message. - plain (Plaintext &): The source message.
Usage: A function used to encode a vector of integer into a plaintext.
void decode(const Plaintext &plain,vector<uint32_t> &res);
- plain (const Plaintext &plain): The plaintext.
- res (vector
&): The result message.
Usage: A function used to decode a plaintext into a vector of integer.
2. Plaintext matrix class : MatrixPlain ¶
Description: MatrixPlain is a class for storing plaintext matrix.
- Description: MatrixPlain is a class for storing plaintext matrix information.
c++
MatrixPlain()
: log_slots(0), n1(0), level(0),
scale(1.0), rot_index{}, plain_vec_pool{sz, std::map<int, Plaintext>()}, read_idx(0),
write_idx(0), is_precompute(false)
Members:
-
LogSlots (uint32_t): Indicates the logarithm to 2 of the number of matrix elements.
-
N1 (uint32_t): Indicates the number of rows in a matrix.
-
level (uint32_t): Indicates the level of the ciphertext module chain in which the matrix resides.
-
scale (double): Indicates the scaling factor of the matrix.
-
rot_index (vector
):Indicates the rotation index of a matrix element in a polynomial. -
plain_vec (map
):Indicates the polynomial corresponding to the matrix elements. -
plain_vec (map
):Indicates the polynomial corresponding to the matrix elements.
3. Evaluator class : EvaluatorBgvBase ¶
Description: EvaluatorBgvBase is a evaluator for homomorphic computation for BGV scheme.
Members: The member functions are listed in Chapter Evaluation Functions.
Evaluation Functions¶
1. Addition between ciphertexts : add¶
void add(const Ciphertext &ciph1, const Ciphertext &ciph2, Ciphertext &result) const;
Description:This function performs homomorphic addition on two ciphertexts.
- ciph1 (Ciphertext): representing a ciphertext.
- ciph2 (Ciphertext): representing another ciphertext.
- result (Ciphertext): storing the computation result.
Usage: add computes result = ciph1 + ciph2 .
2. Addition of ciphertext and plaintext : add_plain¶
void add_plain(const Ciphertext &ciph, const Plaintext &plain, Ciphertext &result) const;
- ciph (Ciphertext): representing a ciphertext.
- plain (Plaintext): representing a plaintext.
- result (Ciphertext): storing the computation result.
Usage: add_plain computes result = ciph + plain .
3. Ciphertext and ciphertext subtraction : sub¶
void sub(const Ciphertext &ciph1, const Ciphertext &ciph2, Ciphertext &result) const;
- ciph1 (Ciphertext): representing the minuend (the number from which another is to be subtracted).
- ciph2 (Ciphertext): representing the subtrahend (the number to be subtracted).
- result (Ciphertext): storing the computation result.
Usage: sub computes result = ciph1 - ciph2.
4. Subtraction plaintext from ciphertext : sub_plain¶
void sub_plain(const Ciphertext &ciph, const Plaintext &plain, Ciphertext &result) const;
- ciph (Ciphertext): representing the minuend (the number from which another is to be subtracted).
- plain (Plaintext): representing the subtrahend (the number to be subtracted).
- result (Ciphertext): storing the computation result.
- Usage:
sub_plaincomputes result = ciph - plain .
5. Multiplication between ciphertexts : multiply ( only software)¶
void multiply(const Ciphertext &ciph1, const Ciphertext &ciph2, Ciphertext &result) const;
- ciph1 (Ciphertext): representing a ciphertext.
- ciph2 (Ciphertext): representing another ciphertext.
- result (Ciphertext): storing the computation result.
Usage: multiply computes result = ciph1 * ciph2.
6. Multiplication between ciphertext and plaintext : multiply_plain¶
void multiply_plain(const Ciphertext &ciph, const Plaintext &plain, Ciphertext &result) const;
- ciph (Ciphertext): representing a ciphertext.
- plain (Plaintext): representing a plaintext.
- result (Ciphertext): storing the multiplication result.
Usage: multiply_plain computes result = ciph * plain.
7. Multiplication with relinearization : multiply_relin¶
void multiply_relin(const Ciphertext &ciph1, const Ciphertext &ciph2, Ciphertext &result, const RelinKeys &relin_key) const;
- ciph1 (Ciphertext): representing a ciphertext.
- ciph2 (Ciphertext): representing another ciphertext.
- result (Ciphertext): storing the computation result.
- relin_key (RelinKeys): representing the relinearization key.
Usage: multiply_relin computes result = ciph1 * ciph2 and relinearize the ciphertext size of result.
8. Relinearization : relinearize ( only software)¶
void relinearize(const Ciphertext &ciph, Ciphertext &result, const RelinKeys &relin_keys) const;
- ciph (Ciphertext): representing a ciphertext.
- result (Ciphertext): storing the computation result.
- relin_keys (RelinKeys): representing the relinearization key.
Usage: relinearize function performs relinearization operation on the ciphertext.
9. Ciphertext row rotation : rotate_col¶
void rotate_col(const Ciphertext &ciph, const GaloisKeys &galois_keys, Ciphertext &result) const override;
- ciph (Ciphertext): representing a ciphertext.
- result (Ciphertext): storing the ciphertext after column rotation.
- galois_keys (GaloisKeys): representing the encryption keys used for row rotation.
Usage: rotate_col performs a column rotation operation on a ciphertext.
10. Ciphertext column rotation : rotate_row¶
void rotate_row(const Ciphertext &ciph, Ciphertext &result, int step, const GaloisKeys &galois_keys) const;
- ciph (Ciphertext): representing a ciphertext.
- result (Ciphertext): storing the ciphertext after row rotation.
- step (int): An integer representing the rotation step length; a positive value indicates a left rotation while a negative value indicates a right rotation.
- galois_keys (GaloisKeys): representing the galois keys used for row rotation.
Usage: rotate_row performs a row rotation operation on a ciphertext.
11. Modulo drop: drop_modulus¶
void drop_modulus(const Ciphertext &ciph, Ciphertext &result, uint32_t level) const;
void drop_modulus_to_next(const Ciphertext &ciph, Ciphertext &result) const;
- ciph (Ciphertext): representing a ciphertext.
-
result (Ciphertext): storing the ciphertext after dropping the modulus.
-
level (uint32_t): The modulus level to switch into.
Usage: drop_modulus drops the modulus into a specific level. drop_modulus_to_next drops the modulus into the next level.
12. Number Theoretic Transform (forward) : ntt_fwd¶
void ntt_fwd(const Plaintext &plain, Plaintext &result, parms_id_type parms_id = parms_id_zero) const;
void ntt_fwd(const Ciphertext &ciph, Ciphertext &result) const;
- plain (Plaintext): representing a plaintext.
- ciph (Ciphertext): representing a ciphertext.
- id (Ciphertext): target parms_id of plain(only used in BGV and BFV).
- result (Ciphertext / Plaintext): store the transformed plaintext or ciphertext.
Usage: ntt_fwd performs the Number Theoretic Transform(NTT) on a plaintext or a ciphertext.
13. Number Theoretic Transform (inverse) : ntt_inv¶
void ntt_inv(const Ciphertext &ciph, Ciphertext &result) const;
- ciph (Ciphertext): representing a ciphertext.
- result (Ciphertext / Plaintext): storing the inverse transformed ciphertext or plaintext.
Usage: ntt_inv performs the Inverse Number Theoretic Transform(INTT) on a plaintext or a ciphertext.
14. Matrix multiplication of ciphertext and plaintext matrix : multiplyByDiagMatrixBSGS¶
void multiply_by_diag_matrix_bsgs(const Ciphertext &ciph, const MatrixPlain &plain_mat, Ciphertext &result, const GaloisKeys &rot_key) const;
- ciph (Ciphertext): representing a ciphertext.
- plain_mat (MatrixPlain): representing a plaintext matrix.
- result (Ciphertext): storing the computation result.
- rot_key (GaloisKeys): representing the galois key for rotation.
Usage: multiply_by_diag_matrix_bsgs multiplies a ciphertext with a plaintext matrix, performing the linear transformation over the ciphertext with the Baby-Step-Giant-Step (BSGS) algorithm to accelerate the operations.
The BSGS algorithm can be performed with the following equation.
notation:
A denotes the plain matrix
z denotes the ciphertext
N_1 is a divisor of N/2 and N_2 is N/2N_1
\odot denotes the Hadamard (component-wise) multiplication between vectors.
\rho(c;s) denotes the rotation of ciphertext c, the rotation step is s .