SEAL开源库源码10

SEAL开源库源码10

文章目录

seal/evaluator.h

Provides operations on ciphertexts. Due to the properties of the encryption scheme, the arithmetic operations pass
through the encryption layer to the underlying plaintext, changing it according to the type of the operation. Since
the plaintext elements are fundamentally polynomials in the polynomial quotient ring Z_T[x]/(X^N+1), where T is the
plaintext modulus and X^N+1 is the polynomial modulus, this is the ring where the arithmetic operations will take
place. BatchEncoder (batching) provider an alternative possibly more convenient view of the plaintext elements as
2-by-(N2/2) matrices of integers modulo the plaintext modulus. In the batching view the arithmetic operations act on
the matrices element-wise. Some of the operations only apply in the batching view, such as matrix row and column
rotations. Other operations such as relinearization have no semantic meaning but are necessary for performance
reasons.

@par Arithmetic Operations
The core operations are arithmetic operations, in particular multiplication and addition of ciphertexts. In addition
to these, we also provide negation, subtraction, squaring, exponentiation, and multiplication and addition of
several ciphertexts for convenience. in many cases some of the inputs to a computation are plaintext elements rather
than ciphertexts. For this we provide fast "plain" operations: plain addition, plain subtraction, and plain
multiplication.

@par Relinearization
One of the most important non-arithmetic operations is relinearization, which takes as input a ciphertext of size
K+1 and relinearization keys (at least K-1 keys are needed), and changes the size of the ciphertext down to 2
(minimum size). For most use-cases only one relinearization key suffices, in which case relinearization should be
performed after every multiplication. Homomorphic multiplication of ciphertexts of size K+1 and L+1 outputs a
ciphertext of size K+L+1, and the computational cost of multiplication is proportional to K*L. Plain multiplication
and addition operations of any type do not change the size. Relinearization requires relinearization keys to have
been generated.

@par Rotations
When batching is enabled, we provide operations for rotating the plaintext matrix rows cyclically left or right, and
for rotating the columns (swapping the rows). Rotations require Galois keys to have been generated.

@par Other Operations
We also provide operations for transforming ciphertexts to NTT form and back, and for transforming plaintext
polynomials to NTT form. These can be used in a very fast plain multiplication variant, that assumes the inputs to
be in NTT form. Since the NTT has to be done in any case in plain multiplication, this function can be used when
e.g. one plaintext input is used in several plain multiplication, and transforming it several times would not make
sense.

@par NTT form
When using the BFV/BGV scheme (scheme_type::bfv/bgv), all plaintexts and ciphertexts should remain by default in the
usual coefficient representation, i.e., not in NTT form. When using the CKKS scheme (scheme_type::ckks), all
plaintexts and ciphertexts should remain by default in NTT form. We call these scheme-specific NTT states the
"default NTT form". Some functions, such as add, work even if the inputs are not in the default state, but others,
such as multiply, will throw an exception. The output of all evaluation functions will be in the same state as the
input(s), with the exception of the transform_to_ntt and transform_from_ntt functions, which change the state.
Ideally, unless these two functions are called, all other functions should "just work".

@see EncryptionParameters for more details on encryption parameters.
@see BatchEncoder for more details on batching
@see RelinKeys for more details on relinearization keys.
@see GaloisKeys for more details on Galois keys.

Evaluator 类

传参都是引用

构造函数,利用 context 来构造

        /**
        Creates an Evaluator instance initialized with the specified SEALContext.

        @param[in] context The SEALContext
        @throws std::invalid_argument if the encryption parameters are not valid
        */
        Evaluator(const SEALContext &context);

negate_inplace 和 negate 函数,求负数

        /**
        Negates a ciphertext.

        @param[in] encrypted The ciphertext to negate
        @throws std::invalid_argument if encrypted is not valid for the encryption
        parameters
        */
        void negate_inplace(Ciphertext &encrypted) const;

        /**
        Negates a ciphertext and stores the result in the destination parameter.

        @param[in] encrypted The ciphertext to negate
        @param[out] destination The ciphertext to overwrite with the negated result
        @throws std::invalid_argument if encrypted is not valid for the encryption parameters
        @throws std::logic_error if result ciphertext is transparent
        */
        inline void negate(const Ciphertext &encrypted, Ciphertext &destination) const
        {
   
            destination = encrypted;
            negate_inplace(destination);
        }

add_inplace 和 add 函数,做加法

        /**
        Adds two ciphertexts. This function adds together encrypted1 and encrypted2 and stores the result in encrypted1.

        @param[in] encrypted1 The first ciphertext to add
        @param[in] encrypted2 The second ciphertext to add
        @throws std::invalid_argument if encrypted1 or encrypted2 is not valid for the encryption parameters
        @throws std::invalid_argument if encrypted1 and encrypted2 are in different NTT forms
        @throws std::invalid_argument if encrypted1 and encrypted2 are at different level or scale
        @throws std::logic_error if result ciphertext is transparent
        */
        void add_inplace(Ciphertext &encrypted1, const Ciphertext &encrypted2) const;

        /**
        Adds two ciphertexts. This function adds together encrypted1 and encrypted2 and stores the result in the
        destination parameter.

        @param[in] encrypted1 The first ciphertext to add
        @param[in] encrypted2 The second ciphertext to add
        @param[out] destination The ciphertext to overwrite with the addition result
        @throws std::invalid_argument if encrypted1 or encrypted2 is not valid for the encryption parameters
        @throws std::invalid_argument if encrypted1 and encrypted2 are in different NTT forms
        @throws std::invalid_argument if encrypted1 and encrypted2 are at different level or scale
        @throws std::logic_error if result ciphertext is transparent
        */
        inline void add(const Ciphertext &encrypted1, const Ciphertext &encrypted2, Ciphertext &destination) const
        {
   
            if (&encrypted2 == &destination)
            {
   
                add_inplace(destination, encrypted1);
            }
            else
            {
   
                destination = encrypted1;
                add_inplace(destination, encrypted2);
            }
        }

add_many 函数, Adds together a vector of ciphertexts and stores the result in the destination parameter.

        /**
        Adds together a vector of ciphertexts and stores the result in the destination parameter.

        @param[in] encrypteds The ciphertexts to add
        @param[out] destination The ciphertext to overwrite with the addition result
        @throws std::invalid_argument if encrypteds is empty
        @throws std::invalid_argument if encrypteds are not valid for the encryption
        parameters
        @throws std::invalid_argument if encrypteds are in different NTT forms
        @throws std::invalid_argument if encrypteds are at different level or scale
        @throws std::invalid_argument if destination is one of encrypteds
        @throws std::logic_error if result ciphertext is transparent
        */
        void add_many(const std::vector<Ciphertext> &encrypteds, Ciphertext &destination) const;

sub_inplace 和 sub 函数,做减法

        /**
        Subtracts two ciphertexts. This function computes the difference of encrypted1 and encrypted2, and stores the
        result in encrypted1.

        @param[in] encrypted1 The ciphertext to subtract from
        @param[in] encrypted2 The ciphertext to subtract
        @throws std::invalid_argument if encrypted1 or encrypted2 is not valid for the encryption parameters
        @throws std::invalid_argument if encrypted1 and encrypted2 are in different NTT forms
        @throws std::invalid_argument if encrypted1 and encrypted2 are at different level or scale
        @throws std::logic_error if result ciphertext is transparent
        */
        void sub_inplace(Ciphertext &encrypted1, const Ciphertext &encrypted2) const;

        /**
        Subtracts two ciphertexts. This function computes the difference of encrypted1 and encrypted2 and stores the
        result in the destination parameter.

        @param[in] encrypted1 The ciphertext to subtract from
        @param[in] encrypted2 The ciphertext to subtract
        @param[out] destination The ciphertext to overwrite with the subtraction result
        @throws std::invalid_argument if encrypted1 or encrypted2 is not valid for the encryption parameters
        @throws std::invalid_argument if encrypted1 and encrypted2 are in different NTT forms
        @throws std::invalid_argument if encrypted1 and encrypted2 are at different level or scale
        @throws std::logic_error if result ciphertext is transparent
        */
        inline void sub(const Ciphertext &encrypted1, const Ciphertext &encrypted2, Ciphertext &destination) const
        {
   
            if (&encrypted2 == &destination)
            {
   
                sub_inplace(destination, encrypted1);
                negate_inplace(destination);
            }
            else
            {
   
                destination = encrypted1;
                sub_inplace(destination, encrypted2);
            }
        }

multiply_inplace 和 multiply 函数,做乘法

        /**
        Multiplies two ciphertexts. This functions computes the product of encrypted1 and encrypted2 and stores the
        result in encrypted1. Dynamic memory allocations in the process are allocated from the memory pool pointed to by
        the given MemoryPoolHandle.

        @param[in] encrypted1 The first ciphertext to multiply
        @param[in] encrypted2 The second ciphertext to multiply
        @param[in] pool The MemoryPoolHandle pointing to a valid memory pool
        @throws std::invalid_argument if encrypted1 or encrypted2 is not valid for the encryption parameters
        @throws std::invalid_argument if encrypted1 or encrypted2 is not in the default NTT form
        @throws std::invalid_argument if encrypted1 and encrypted2 are at different level
        @throws std::invalid_argument if the output scale is too large for the encryption parameters
        @throws std::invalid_argument if pool is uninitialized
        @throws std::logic_error if result ciphertext is transparent
        */
        void multiply_inplace(
            Ciphertext &encrypted1, const Ciphertext &encrypted2,
            MemoryPoolHandle pool = MemoryManager::GetPool()) const;

        /**
        Multiplies two ciphertexts. This functions computes the product of encrypted1 and encrypted2 and stores the
        result in the destination parameter. Dynamic memory allocations in the process are allocated from the memory
        pool pointed to by the given MemoryPoolHandle.

        @param[in] encrypted1 The first ciphertext to multiply
        @param[in] encrypted2 The second ciphertext to multiply
        @param[out] destination The ciphertext to overwrite with the multiplication result
        @param[in] pool The MemoryPoolHandle pointing to a valid memory pool
        @throws std::invalid_argument if encrypted1 or encrypted2 is not valid for the encryption parameters
        @throws std::invalid_argument if encrypted1 and encrypted2 are at different level
        @throws std::invalid_argument if encrypted1 or encrypted2 is not in the default NTT form
        @throws std::invalid_argument if the output scale is too large for the encryption parameters
        @throws std::invalid_argument if pool is uninitialized
        @throws std::logic_error if result ciphertext is transparent
        */
        inline void multiply(
            const Ciphertext &encrypted1, const Ciphertext &encrypted2, Ciphertext &destination,
            MemoryPoolHandle pool = MemoryManager::GetPool()) const
        {
   
            if (&encrypted2 == &destination)
            {
   
                multiply_inplace(destination, encrypted1, std::move(pool));
            }
            else
            {
   
                destination = encrypted1;
                multiply_inplace(destination, encrypted2, std::move(pool));
            }
        }

square_inplace 和 square 函数,做平方运算

        /**
        Squares a ciphertext. This functions computes the square of encrypted. Dynamic memory allocations in the process
        are allocated from the memory pool pointed to by the given MemoryPoolHandle.

        @param[in] encrypted The ciphertext to square
        @param[in] pool The MemoryPoolHandle pointing to a valid memory pool
        @throws std::invalid_argument if encrypted is not valid for the encryption parameters
        @throws std::invalid_argument if encrypted is not in the default NTT form
        @throws std::invalid_argument if the output scale is too large for the encryption parameters
        @throws std::invalid_argument if pool is uninitialized
        @throws std::logic_error if result ciphertext is transparent
        */
        void square_inplace(Ciphertext &encrypted, MemoryPoolHandle pool = MemoryManager::GetPool()) const;

        /**
        Squares a ciphertext. This functions computes the square of encrypted and stores the result in the destination
        parameter. Dynamic memory allocations in the process are allocated from the memory pool pointed to by the given
        MemoryPoolHandle.

        @param[in] encrypted The ciphertext to square
        @param[out] destination The ciphertext to overwrite with the square
        @param[in] pool The MemoryPoolHandle pointing to a valid memory pool
        @throws std::invalid_argument if encrypted is not valid for the encryption parameters
        @throws std::invalid_argument if encrypted is not in the default NTT form
        @throws std::invalid_argument if the output scale is too large for the encryption parameters
        @throws std::invalid_argument if pool is uninitialized
        @throws std::logic_error if result ciphertext is transparent
        */
        inline void square(
            const Ciphertext &encrypted, Ciphertext &destination,
            MemoryPoolHandle pool = MemoryManager::GetPool()) const
        {
   
            destination = encrypted;
            square_inplace(destination, std::move(pool));
        }

relinearize_inplace 和 relinearize 函数

        /**
        Relinearizes a ciphertext. This functions relinearizes encrypted, reducing its size down to 2. If the size of
        encrypted is K+1, the given relinearization keys need to have size at least K-1. Dynamic memory allocations in
        the process are allocated from the memory pool pointed to by the given MemoryPoolHandle.

        @param[in] encrypted The ciphertext to relinearize
        @param[in] relin_keys The relinearization keys
        @param[in] pool The MemoryPoolHandle pointing to a valid memory pool
        @throws std::invalid_argument if encrypted or relin_keys is not valid for the encryption parameters
        @throws std::invalid_argument if encrypted is not in the default NTT form
        @throws std::invalid_argument if relin_keys do not correspond to the top level parameters in the current context
        @throws std::invalid_argument if the size of relin_keys is too small
        @throws std::invalid_argument if pool is uninitialized
        @throws std::logic_error if keyswitching is not supported by the context
        @throws std::logic_error if result ciphertext is transparent
        */
        inline void relinearize_inplace(
            Ciphertext &encrypted, const RelinKeys &relin_keys, MemoryPoolHandle pool = MemoryManager::GetPool()) const
        {
   
            relinearize_internal(encrypted, relin_keys, 2, std::move(pool));
        }

        /**
        Relinearizes a ciphertext. This functions relinearizes encrypt
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值