2021SC@SDUSC-SEAL全同态加密安全库(三)SEAL文件夹下的keygenerator——密钥生成

SEAL全同态加密安全库(三)SEAL文件夹下的keygenerator——密钥生成

keygenerator概述

本方法是用于生成相应的密钥,即密钥生成器,该模块是用于构建公钥、私钥、重线性化密钥的必要步骤

详细代码分析

首先是该类的两个构造函数

public KeyGenerator(SEALContext context)
        {
    
    
            if (null == context)
                throw new ArgumentNullException(nameof(context));
            if (!context.ParametersSet)
                throw new ArgumentException("Encryption parameters are not set correctly");

            NativeMethods.KeyGenerator_Create(context.NativePtr, out IntPtr ptr);
            NativePtr = ptr;
        }
public KeyGenerator(SEALContext context, SecretKey secretKey)
        {
    
    
            if (null == context)
                throw new ArgumentNullException(nameof(context));
            if (null == secretKey)
                throw new ArgumentNullException(nameof(secretKey));
            if (!context.ParametersSet)
                throw new ArgumentException("Encryption parameters are not set correctly");
            if (!ValCheck.IsValidFor(secretKey, context))
                throw new ArgumentException("Secret key is not valid for encryption parameters");

            NativeMethods.KeyGenerator_Create(context.NativePtr,
                secretKey.NativePtr, out IntPtr ptr);
            NativePtr = ptr;
        }
public SecretKey SecretKey
        {
    
    
            get
            {
    
    
                NativeMethods.KeyGenerator_SecretKey(NativePtr, out IntPtr secretKeyPtr);
                SecretKey secretKey = new SecretKey(secretKeyPtr);
                return secretKey;
            }
        }

接下来是创建公钥的方法,分为有参和无参两种情况

public void CreatePublicKey(out PublicKey destination)
        {
    
    
            NativeMethods.KeyGenerator_CreatePublicKey(NativePtr, false, out IntPtr pubKeyPtr);
            destination = new PublicKey(pubKeyPtr);
        }
public Serializable<PublicKey> CreatePublicKey()
        {
    
    
            NativeMethods.KeyGenerator_CreatePublicKey(NativePtr, true, out IntPtr pubKeyPtr);
            return new Serializable<PublicKey>(new PublicKey(pubKeyPtr));
        }
 public void CreateRelinKeys(out RelinKeys destination)
        {
    
    
            if (!UsingKeyswitching())
                throw new InvalidOperationException("Encryption parameters do not support keyswitching");

            NativeMethods.KeyGenerator_CreateRelinKeys(NativePtr, false, out IntPtr relinKeysPtr);
            destination = new RelinKeys(relinKeysPtr);
        }
 public Serializable<RelinKeys> CreateRelinKeys()
        {
    
    
            if (!UsingKeyswitching())
                throw new InvalidOperationException("Encryption parameters do not support keyswitching");

            NativeMethods.KeyGenerator_CreateRelinKeys(NativePtr, true, out IntPtr relinKeysPtr);
            return new Serializable<RelinKeys>(new RelinKeys(relinKeysPtr));
        }

 public void CreateGaloisKeys(IEnumerable<uint> galoisElts, out GaloisKeys destination)
        {
    
    
            if (null == galoisElts)
                throw new ArgumentNullException(nameof(galoisElts));
            if (!UsingKeyswitching())
                throw new InvalidOperationException("Encryption parameters do not support keyswitching");

            uint[] galoisEltsArr = galoisElts.ToArray();
            NativeMethods.KeyGenerator_CreateGaloisKeysFromElts(NativePtr,
                (ulong)galoisEltsArr.Length, galoisEltsArr, false, out IntPtr galoisKeysPtr);
            destination = new GaloisKeys(galoisKeysPtr);
        }
 public Serializable<GaloisKeys> CreateGaloisKeys(IEnumerable<uint> galoisElts)
        {
    
    
            if (null == galoisElts)
                throw new ArgumentNullException(nameof(galoisElts));
            if (!UsingKeyswitching())
                throw new InvalidOperationException("Encryption parameters do not support keyswitching");

            uint[] galoisEltsArr = galoisElts.ToArray();
            NativeMethods.KeyGenerator_CreateGaloisKeysFromElts(NativePtr,
                (ulong)galoisEltsArr.Length, galoisEltsArr, true, out IntPtr galoisKeysPtr);
            return new Serializable<GaloisKeys>(new GaloisKeys(galoisKeysPtr));
        }

 public void CreateGaloisKeys(IEnumerable<int> steps, out GaloisKeys destination)
        {
    
    
            if (null == steps)
                throw new ArgumentNullException(nameof(steps));
            if (!UsingKeyswitching())
                throw new InvalidOperationException("Encryption parameters do not support keyswitching");

            int[] stepsArr = steps.ToArray();
            NativeMethods.KeyGenerator_CreateGaloisKeysFromSteps(NativePtr,
                (ulong)stepsArr.Length, stepsArr, false, out IntPtr galoisKeysPtr);
            destination = new GaloisKeys(galoisKeysPtr);
        }
  public Serializable<GaloisKeys> CreateGaloisKeys(IEnumerable<int> steps)
        {
    
    
            if (null == steps)
                throw new ArgumentNullException(nameof(steps));
            if (!UsingKeyswitching())
                throw new InvalidOperationException("Encryption parameters do not support keyswitching");

            int[] stepsArr = steps.ToArray();
            NativeMethods.KeyGenerator_CreateGaloisKeysFromSteps(NativePtr,
                (ulong)stepsArr.Length, stepsArr, true, out IntPtr galoisKeysPtr);
            return new Serializable<GaloisKeys>(new GaloisKeys(galoisKeysPtr));
        }
 public void CreateGaloisKeys(out GaloisKeys destination)
        {
    
    
            if (!UsingKeyswitching())
                throw new InvalidOperationException("Encryption parameters do not support keyswitching");

            NativeMethods.KeyGenerator_CreateGaloisKeysAll(NativePtr, false, out IntPtr galoisKeysPtr);
            destination = new GaloisKeys(galoisKeysPtr);
        }

 public Serializable<GaloisKeys> CreateGaloisKeys()
        {
    
    
            if (!UsingKeyswitching())
                throw new InvalidOperationException("Encryption parameters do not support keyswitching");

            NativeMethods.KeyGenerator_CreateGaloisKeysAll(NativePtr, true, out IntPtr galoisKeysPtr);
            return new Serializable<GaloisKeys>(new GaloisKeys(galoisKeysPtr));
        }

到这里函数就执行完成了,主要就是生成的公钥与密钥,并且返回了它们。

猜你喜欢

转载自blog.csdn.net/weixin_46021936/article/details/120817857