public void DecodeTest() { EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV) { PlainModulus = new SmallModulus(1024) }; SEALContext context = SEALContext.Create(parms); IntegerEncoder encoder = new IntegerEncoder(context); Plaintext plain = new Plaintext("0x^5 + 1x^4 + 1x^3 + 1x^1 + 0"); Assert.AreEqual(6ul, plain.CoeffCount); ulong resultU64 = encoder.DecodeUInt64(plain); Assert.AreEqual(26UL, resultU64); long resultI64 = encoder.DecodeInt64(plain); Assert.AreEqual(26L, resultI64); uint resultU32 = encoder.DecodeUInt32(plain); Assert.AreEqual(26U, resultU32); int resultI32 = encoder.DecodeInt32(plain); Assert.AreEqual(26, resultI32); BigUInt bui = encoder.DecodeBigUInt(plain); Assert.IsNotNull(bui); Assert.AreEqual(0, bui.CompareTo(26ul)); }
public void EncodeDecodeUlongTest() { int slots = 32; EncryptionParameters parms = new EncryptionParameters(SchemeType.CKKS); parms.PolyModulusDegree = (ulong)slots * 2; List <SmallModulus> coeffModulus = new List <SmallModulus>(4); coeffModulus.Add(DefaultParams.SmallMods40Bit(0)); coeffModulus.Add(DefaultParams.SmallMods40Bit(1)); coeffModulus.Add(DefaultParams.SmallMods40Bit(2)); coeffModulus.Add(DefaultParams.SmallMods40Bit(3)); parms.CoeffModulus = coeffModulus; SEALContext context = SEALContext.Create(parms); CKKSEncoder encoder = new CKKSEncoder(context); Plaintext plain = new Plaintext(); List <Complex> result = new List <Complex>(); long value = 15; encoder.Encode(value, plain); encoder.Decode(plain, result); for (int i = 0; i < slots; i++) { double tmp = Math.Abs(value - result[i].Real); Assert.IsTrue(tmp < 0.5); } }
public void CreateTest() { EncryptionParameters parms1 = new EncryptionParameters(SchemeType.BFV) { PlainModulus = new SmallModulus(8192) }; SEALContext context1 = SEALContext.Create(parms1); IntegerEncoder encoder = new IntegerEncoder(context1); Assert.IsNotNull(encoder); Assert.AreEqual(8192ul, encoder.PlainModulus.Value); EncryptionParameters parms2 = new EncryptionParameters(SchemeType.BFV) { PlainModulus = new SmallModulus(4096) }; SEALContext context2 = SEALContext.Create(parms2); IntegerEncoder encoder2 = new IntegerEncoder(context2); Assert.IsNotNull(encoder2); Assert.AreEqual(4096ul, encoder2.PlainModulus.Value); IntegerEncoder encoder3 = new IntegerEncoder(encoder); Assert.IsNotNull(encoder3); Assert.AreEqual(8192ul, encoder3.PlainModulus.Value); }
public void CreateTest() { List <SmallModulus> coeffModulus = new List <SmallModulus> { DefaultParams.SmallMods40Bit(0) }; EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV) { PolyModulusDegree = 64, PlainModulus = new SmallModulus(1 << 6), CoeffModulus = coeffModulus }; SEALContext context = SEALContext.Create(parms); KeyGenerator keygen = new KeyGenerator(context); SecretKey secret = keygen.SecretKey; SecretKey copy = new SecretKey(secret); Assert.AreEqual(64ul, copy.Data.CoeffCount); Assert.IsTrue(copy.Data.IsNTTForm); SecretKey copy2 = new SecretKey(); copy2.Set(copy); Assert.AreEqual(64ul, copy2.Data.CoeffCount); Assert.IsTrue(copy2.Data.IsNTTForm); }
public void EncodeDecodeVectorDoubleTest() { EncryptionParameters parms = new EncryptionParameters(SchemeType.CKKS) { PolyModulusDegree = 64, CoeffModulus = new List <SmallModulus>() { DefaultParams.SmallMods30Bit(0), DefaultParams.SmallMods30Bit(1) } }; SEALContext context = SEALContext.Create(parms); CKKSEncoder encoder = new CKKSEncoder(context); Plaintext plain = new Plaintext(); double[] values = new double[] { 0.1, 2.3, 34.4 }; encoder.Encode(values, scale: Math.Pow(2, 20), destination: plain); List <double> result = new List <double>(); encoder.Decode(plain, result); Assert.IsNotNull(result); Assert.AreEqual(0.1, result[0], delta: 0.001); Assert.AreEqual(2.3, result[1], delta: 0.001); Assert.AreEqual(34.4, result[2], delta: 0.001); }
public void SEALContextCreateTest() { EncryptionParameters encParams1 = new EncryptionParameters(SchemeType.BFV); EncryptionParameters encParams2 = new EncryptionParameters(SchemeType.CKKS); SEALContext context1 = SEALContext.Create(encParams1); SEALContext context2 = SEALContext.Create(encParams2); Assert.IsNotNull(context1); Assert.IsNotNull(context2); Assert.IsFalse(context1.ParametersSet); Assert.IsFalse(context2.ParametersSet); Assert.AreNotSame(context1.FirstParmsId, context1.LastParmsId); Assert.AreEqual(context1.FirstParmsId, context1.LastParmsId); SEALContext.ContextData data1 = context2.FirstContextData; SEALContext.ContextData data2 = context2.GetContextData(context2.FirstParmsId); Assert.AreNotSame(data1, data2); ulong[] totalCoeffMod1 = data1.TotalCoeffModulus; ulong[] totalCoeffMod2 = data2.TotalCoeffModulus; int bitCount1 = data1.TotalCoeffModulusBitCount; int bitCount2 = data2.TotalCoeffModulusBitCount; Assert.AreEqual(bitCount1, bitCount2); Assert.AreEqual(totalCoeffMod1.Length, totalCoeffMod2.Length); for (int i = 0; i < totalCoeffMod1.Length; i++) { Assert.AreEqual(totalCoeffMod1[i], totalCoeffMod2[i]); } }
public void EncodeDecodeDoubleTest() { EncryptionParameters parms = new EncryptionParameters(SchemeType.CKKS); parms.PolyModulusDegree = 64; List <SmallModulus> coeffModulus = new List <SmallModulus>(4); coeffModulus.Add(DefaultParams.SmallMods40Bit(0)); coeffModulus.Add(DefaultParams.SmallMods40Bit(1)); coeffModulus.Add(DefaultParams.SmallMods40Bit(2)); coeffModulus.Add(DefaultParams.SmallMods40Bit(3)); parms.CoeffModulus = coeffModulus; SEALContext context = SEALContext.Create(parms); int slots = 16; Plaintext plain = new Plaintext(); double delta = 1 << 16; List <Complex> result = new List <Complex>(); CKKSEncoder encoder = new CKKSEncoder(context); Assert.AreEqual(32ul, encoder.SlotCount); double value = 10d; encoder.Encode(value, delta, plain); encoder.Decode(plain, result); for (int i = 0; i < slots; i++) { double tmp = Math.Abs(value - result[i].Real); Assert.IsTrue(tmp < 0.5); } }
public void KeyStepTest() { EncryptionParameters parms = new EncryptionParameters(SchemeType.CKKS) { PolyModulusDegree = 64, CoeffModulus = new List <SmallModulus>() { DefaultParams.SmallMods60Bit(0) } }; SEALContext context = SEALContext.Create(parms); KeyGenerator keygen = new KeyGenerator(context); GaloisKeys keys = keygen.GaloisKeys(decompositionBitCount: 15, steps: new int[] { 1, 2, 3 }); Assert.IsNotNull(keys); Assert.AreEqual(15, keys.DecompositionBitCount); Assert.AreEqual(3ul, keys.Size); Assert.IsFalse(keys.HasKey(1)); Assert.IsTrue(keys.HasKey(3)); Assert.IsFalse(keys.HasKey(5)); Assert.IsFalse(keys.HasKey(7)); Assert.IsTrue(keys.HasKey(9)); Assert.IsFalse(keys.HasKey(11)); Assert.IsFalse(keys.HasKey(13)); Assert.IsFalse(keys.HasKey(15)); Assert.IsFalse(keys.HasKey(17)); Assert.IsFalse(keys.HasKey(19)); Assert.IsFalse(keys.HasKey(21)); Assert.IsFalse(keys.HasKey(23)); Assert.IsFalse(keys.HasKey(25)); Assert.IsTrue(keys.HasKey(27)); }
public void ExceptionsTest() { EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV) { PlainModulus = new SmallModulus(4ul) }; SEALContext context = SEALContext.Create(parms); SEALContext context_null = null; IntegerEncoder enc = new IntegerEncoder(context); IntegerEncoder copy_null = null; BigUInt bui_null = null; BigUInt bui = new BigUInt(5ul); Plaintext plain = new Plaintext(); Assert.ThrowsException <ArgumentNullException>(() => enc = new IntegerEncoder(context_null)); Assert.ThrowsException <ArgumentNullException>(() => enc = new IntegerEncoder(copy_null)); Assert.ThrowsException <ArgumentNullException>(() => enc.Encode(1ul, null)); Assert.ThrowsException <ArgumentNullException>(() => enc.Encode(1L, null)); Assert.ThrowsException <ArgumentNullException>(() => enc.Encode(1, null)); Assert.ThrowsException <ArgumentNullException>(() => enc.Encode(1u, null)); Assert.ThrowsException <ArgumentNullException>(() => enc.Encode(bui_null)); Assert.ThrowsException <ArgumentNullException>(() => enc.Encode(bui, null)); Assert.ThrowsException <ArgumentNullException>(() => enc.Encode(bui_null, plain)); Assert.ThrowsException <ArgumentNullException>(() => enc.DecodeUInt32(null)); Assert.ThrowsException <ArgumentNullException>(() => enc.DecodeUInt64(null)); Assert.ThrowsException <ArgumentNullException>(() => enc.DecodeInt32(null)); Assert.ThrowsException <ArgumentNullException>(() => enc.DecodeInt64(null)); Assert.ThrowsException <ArgumentNullException>(() => enc.DecodeBigUInt(null)); }
public void EncodeDecodeComplexTest() { EncryptionParameters parms = new EncryptionParameters(SchemeType.CKKS) { PolyModulusDegree = 64, CoeffModulus = new List <SmallModulus>() { DefaultParams.SmallMods40Bit(0), DefaultParams.SmallMods40Bit(1), DefaultParams.SmallMods40Bit(2), DefaultParams.SmallMods40Bit(3) } }; SEALContext context = SEALContext.Create(parms); CKKSEncoder encoder = new CKKSEncoder(context); Plaintext plain = new Plaintext(); Complex value = new Complex(3.1415, 2.71828); encoder.Encode(value, scale: Math.Pow(2, 20), destination: plain); List <Complex> result = new List <Complex>(); encoder.Decode(plain, result); Assert.IsTrue(result.Count > 0); Assert.AreEqual(3.1415, result[0].Real, delta: 0.0001); Assert.AreEqual(2.71828, result[0].Imaginary, delta: 0.0001); }
public HomomorphicKeyManager() { EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV); parms.PolyModulusDegree = 2048; parms.CoeffModulus = DefaultParams.CoeffModulus128(polyModulusDegree: 2048); parms.PlainModulus = new SmallModulus(1 << 8); Context = SEALContext.Create(parms); }
static SEALContext createContext(ulong keysize) { EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV); parms.PolyModulusDegree = keysize; parms.CoeffModulus = DefaultParams.CoeffModulus128(polyModulusDegree: keysize); parms.PlainModulus = new SmallModulus(1 << 8); return(SEALContext.Create(parms)); }
public void SEALContextParamsTest() { List <SmallModulus> coeffModulus = new List <SmallModulus> { DefaultParams.SmallMods30Bit(0), DefaultParams.SmallMods30Bit(1), DefaultParams.SmallMods30Bit(2) }; EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV) { PolyModulusDegree = 128, PlainModulus = new SmallModulus(1 << 6), CoeffModulus = coeffModulus }; SEALContext context = SEALContext.Create(parms); SEALContext.ContextData data = context.FirstContextData; Assert.IsNotNull(data); EncryptionParameters parms2 = data.Parms; Assert.AreEqual(parms.PolyModulusDegree, parms2.PolyModulusDegree); Assert.AreEqual(parms.NoiseStandardDeviation, parms2.NoiseStandardDeviation); EncryptionParameterQualifiers qualifiers = data.Qualifiers; Assert.IsNotNull(qualifiers); Assert.IsTrue(qualifiers.ParametersSet); Assert.IsFalse(qualifiers.UsingBatching); Assert.IsTrue(qualifiers.UsingFastPlainLift); Assert.IsTrue(qualifiers.UsingFFT); Assert.IsTrue(qualifiers.UsingNTT); Assert.IsTrue(qualifiers.UsingHEStdSecurity); ulong[] cdpm = data.CoeffDivPlainModulus; Assert.AreEqual(3, cdpm.Length); Assert.AreEqual(32ul, data.PlainUpperHalfThreshold); Assert.AreEqual(3, data.PlainUpperHalfIncrement.Length); Assert.IsNull(data.UpperHalfThreshold); Assert.IsNotNull(data.UpperHalfIncrement); Assert.AreEqual(3, data.UpperHalfIncrement.Length); Assert.AreEqual(2ul, data.ChainIndex); SEALContext.ContextData data2 = data.NextContextData; Assert.IsNotNull(data2); Assert.AreEqual(1ul, data2.ChainIndex); SEALContext.ContextData data3 = data2.NextContextData; Assert.IsNotNull(data3); Assert.AreEqual(0ul, data3.ChainIndex); Assert.IsNull(data3.NextContextData); }
static GlobalContext() { EncryptionParameters encParams = new EncryptionParameters(SchemeType.BFV) { PolyModulusDegree = 4096, CoeffModulus = DefaultParams.CoeffModulus128(polyModulusDegree: 4096) }; encParams.SetPlainModulus(0x133Ful); Context = SEALContext.Create(encParams); }
public static SEALContext GetContext() { var encryptionParameters = new EncryptionParameters(SchemeType.BFV) { PolyModulusDegree = 32768, CoeffModulus = DefaultParams.CoeffModulus128(polyModulusDegree: 32768) }; encryptionParameters.SetPlainModulus(0x133Ful); Debug.WriteLine("[COMMON]: Successfully created context"); return(SEALContext.Create(encryptionParameters)); }
public void ExceptionsTest() { List <SmallModulus> coeffModulus = new List <SmallModulus>() { DefaultParams.SmallMods60Bit(0) }; EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV) { PolyModulusDegree = 64, CoeffModulus = coeffModulus, PlainModulus = new SmallModulus(257) }; SEALContext context = SEALContext.Create(parms); BatchEncoder enc = new BatchEncoder(context); List <ulong> valu = new List <ulong>(); List <ulong> valu_null = null; List <long> vall = new List <long>(); List <long> vall_null = null; Plaintext plain = new Plaintext(); Plaintext plain_null = null; MemoryPoolHandle pool_uninit = new MemoryPoolHandle(); Assert.ThrowsException <ArgumentNullException>(() => enc = new BatchEncoder(null)); Assert.ThrowsException <ArgumentNullException>(() => enc.Encode(valu, plain_null)); Assert.ThrowsException <ArgumentNullException>(() => enc.Encode(valu_null, plain)); Assert.ThrowsException <ArgumentNullException>(() => enc.Encode(vall, plain_null)); Assert.ThrowsException <ArgumentNullException>(() => enc.Encode(vall_null, plain)); Assert.ThrowsException <ArgumentNullException>(() => enc.Encode(plain_null)); Assert.ThrowsException <ArgumentException>(() => enc.Encode(plain, pool_uninit)); Assert.ThrowsException <ArgumentNullException>(() => enc.Decode(plain, valu_null)); Assert.ThrowsException <ArgumentNullException>(() => enc.Decode(plain_null, valu)); Assert.ThrowsException <ArgumentException>(() => enc.Decode(plain, valu, pool_uninit)); Assert.ThrowsException <ArgumentNullException>(() => enc.Decode(plain, vall_null)); Assert.ThrowsException <ArgumentNullException>(() => enc.Decode(plain_null, vall)); Assert.ThrowsException <ArgumentException>(() => enc.Decode(plain, vall, pool_uninit)); Assert.ThrowsException <ArgumentNullException>(() => enc.Decode(plain_null)); Assert.ThrowsException <ArgumentException>(() => enc.Decode(plain, pool_uninit)); }
public void SaveLoadTest() { List <SmallModulus> coeffModulus = new List <SmallModulus> { DefaultParams.SmallMods40Bit(0) }; EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV) { PolyModulusDegree = 64, PlainModulus = new SmallModulus(1 << 6), CoeffModulus = coeffModulus }; SEALContext context = SEALContext.Create(parms); KeyGenerator keygen = new KeyGenerator(context); SecretKey secret = keygen.SecretKey; Assert.AreEqual(64ul, secret.Data.CoeffCount); Assert.IsTrue(secret.Data.IsNTTForm); Assert.AreNotEqual(ParmsId.Zero, secret.ParmsId); SecretKey secret2 = new SecretKey(); MemoryPoolHandle handle = secret2.Pool; Assert.IsNotNull(secret2); Assert.AreEqual(0ul, secret2.Data.CoeffCount); Assert.IsFalse(secret2.Data.IsNTTForm); ulong alloced = handle.AllocByteCount; using (MemoryStream stream = new MemoryStream()) { secret.Save(stream); stream.Seek(offset: 0, loc: SeekOrigin.Begin); secret2.Load(context, stream); } Assert.AreNotSame(secret, secret2); Assert.AreEqual(64ul, secret2.Data.CoeffCount); Assert.IsTrue(secret2.Data.IsNTTForm); Assert.AreNotEqual(ParmsId.Zero, secret2.ParmsId); Assert.AreEqual(secret.ParmsId, secret2.ParmsId); Assert.IsTrue(handle.AllocByteCount != alloced); }
public void SaveLoadTest() { List <SmallModulus> coeffModulus = new List <SmallModulus> { DefaultParams.SmallMods40Bit(0) }; EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV) { PolyModulusDegree = 64, PlainModulus = new SmallModulus(1 << 6), CoeffModulus = coeffModulus }; SEALContext context = SEALContext.Create(parms); KeyGenerator keygen = new KeyGenerator(context); PublicKey pub = keygen.PublicKey; Assert.IsNotNull(pub); Assert.AreEqual(2ul, pub.Data.Size); Assert.IsTrue(pub.Data.IsNTTForm); PublicKey pub2 = new PublicKey(); MemoryPoolHandle handle = pub2.Pool; Assert.AreEqual(0ul, pub2.Data.Size); Assert.IsFalse(pub2.Data.IsNTTForm); Assert.AreEqual(ParmsId.Zero, pub2.ParmsId); using (MemoryStream stream = new MemoryStream()) { pub.Save(stream); stream.Seek(offset: 0, loc: SeekOrigin.Begin); pub2.Load(context, stream); } Assert.AreNotSame(pub, pub2); Assert.AreEqual(2ul, pub2.Data.Size); Assert.IsTrue(pub2.Data.IsNTTForm); Assert.AreEqual(pub.ParmsId, pub2.ParmsId); Assert.AreNotEqual(ParmsId.Zero, pub2.ParmsId); Assert.IsTrue(handle.AllocByteCount != 0ul); }
public void EncodeInPlaceTest() { List <SmallModulus> coeffModulus = new List <SmallModulus>() { DefaultParams.SmallMods60Bit(0) }; EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV) { PolyModulusDegree = 64, CoeffModulus = coeffModulus, PlainModulus = new SmallModulus(257) }; SEALContext context = SEALContext.Create(parms); BatchEncoder encoder = new BatchEncoder(context); Assert.AreEqual(64ul, encoder.SlotCount); Plaintext plain = new Plaintext("6x^5 + 5x^4 + 4x^3 + 3x^2 + 2x^1 + 1"); Assert.AreEqual(6ul, plain.CoeffCount); encoder.Encode(plain); Assert.AreEqual(64ul, plain.CoeffCount); encoder.Decode(plain); Assert.AreEqual(64ul, plain.CoeffCount); for (ulong i = 0; i < 6; i++) { Assert.AreEqual((i + 1), plain[i]); } for (ulong i = 6; i < plain.CoeffCount; i++) { Assert.AreEqual(0ul, plain[i]); } }
public void EncodeDecodeVectorTest() { int slots = 32; EncryptionParameters parms = new EncryptionParameters(SchemeType.CKKS); parms.PolyModulusDegree = (ulong)slots * 2; List <SmallModulus> coeffModulus = new List <SmallModulus>(4); coeffModulus.Add(DefaultParams.SmallMods60Bit(0)); coeffModulus.Add(DefaultParams.SmallMods60Bit(1)); coeffModulus.Add(DefaultParams.SmallMods60Bit(2)); coeffModulus.Add(DefaultParams.SmallMods60Bit(3)); parms.CoeffModulus = coeffModulus; SEALContext context = SEALContext.Create(parms); CKKSEncoder encoder = new CKKSEncoder(context); List <Complex> values = new List <Complex>(slots); Random rnd = new Random(); int dataBound = 1 << 30; double delta = 1ul << 40; for (int i = 0; i < slots; i++) { values.Add(new Complex(rnd.Next() % dataBound, 0)); } Plaintext plain = new Plaintext(); encoder.Encode(values, delta, plain); List <Complex> result = new List <Complex>(); encoder.Decode(plain, result); for (int i = 0; i < slots; i++) { double tmp = Math.Abs(values[i].Real - result[i].Real); Assert.IsTrue(tmp < 0.5); } }
public void SEALContextCKKSParamsTest() { int slotSize = 4; List <SmallModulus> coeffModulus = new List <SmallModulus> { DefaultParams.SmallMods40Bit(0), DefaultParams.SmallMods40Bit(1), DefaultParams.SmallMods40Bit(2), DefaultParams.SmallMods40Bit(3) }; EncryptionParameters parms = new EncryptionParameters(SchemeType.CKKS) { PolyModulusDegree = 2 * (ulong)slotSize, CoeffModulus = coeffModulus }; SEALContext context = SEALContext.Create(parms); SEALContext.ContextData data = context.FirstContextData; Assert.IsNotNull(data); // This should be available in CKKS Assert.IsNotNull(data.UpperHalfThreshold); Assert.AreEqual(4, data.UpperHalfThreshold.Length); Assert.IsNull(data.UpperHalfIncrement); Assert.AreEqual(3ul, data.ChainIndex); SEALContext.ContextData data2 = data.NextContextData; Assert.IsNotNull(data2); Assert.AreEqual(2ul, data2.ChainIndex); SEALContext.ContextData data3 = data2.NextContextData; Assert.IsNotNull(data3); Assert.AreEqual(1ul, data3.ChainIndex); SEALContext.ContextData data4 = data3.NextContextData; Assert.IsNotNull(data4); Assert.AreEqual(0ul, data4.ChainIndex); Assert.IsNull(data4.NextContextData); }
public void SchemeIsCKKSTest() { List <SmallModulus> coeffModulus = new List <SmallModulus> { DefaultParams.SmallMods40Bit(0), DefaultParams.SmallMods40Bit(1), DefaultParams.SmallMods40Bit(2), DefaultParams.SmallMods40Bit(3) }; EncryptionParameters parms = new EncryptionParameters(SchemeType.CKKS) { PolyModulusDegree = 8, CoeffModulus = coeffModulus }; SEALContext context = SEALContext.Create(parms); Assert.ThrowsException <ArgumentException>(() => { BatchEncoder encoder = new BatchEncoder(context); }); }
public void ExpandModChainTest() { EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV) { PolyModulusDegree = 4096, CoeffModulus = DefaultParams.CoeffModulus128(polyModulusDegree: 4096), PlainModulus = new SmallModulus(1 << 20) }; SEALContext context1 = SEALContext.Create(parms); // By default there is a chain SEALContext.ContextData contextData = context1.FirstContextData; Assert.IsNotNull(contextData); Assert.IsNotNull(contextData.NextContextData); // This should not create a chain SEALContext context2 = SEALContext.Create(parms, expandModChain: false); contextData = context2.FirstContextData; Assert.IsNotNull(contextData); Assert.IsNull(contextData.NextContextData); }
public void PropertiesTest() { SEALContext context = GlobalContext.Context; Assert.IsTrue(context.FirstContextData.Qualifiers.ParametersSet); Assert.IsFalse(context.FirstContextData.Qualifiers.UsingBatching); Assert.IsTrue(context.FirstContextData.Qualifiers.UsingFastPlainLift); Assert.IsTrue(context.FirstContextData.Qualifiers.UsingFFT); Assert.IsTrue(context.FirstContextData.Qualifiers.UsingHEStdSecurity); Assert.IsTrue(context.FirstContextData.Qualifiers.UsingNTT); EncryptionParameters parms = new EncryptionParameters(SchemeType.CKKS) { PolyModulusDegree = 4096, CoeffModulus = DefaultParams.CoeffModulus128(4096) }; SEALContext context2 = SEALContext.Create(parms); Assert.IsTrue(context2.FirstContextData.Qualifiers.ParametersSet); Assert.IsTrue(context2.FirstContextData.Qualifiers.UsingBatching); Assert.IsFalse(context2.FirstContextData.Qualifiers.UsingFastPlainLift); Assert.IsTrue(context2.FirstContextData.Qualifiers.UsingFFT); Assert.IsTrue(context2.FirstContextData.Qualifiers.UsingHEStdSecurity); Assert.IsTrue(context2.FirstContextData.Qualifiers.UsingNTT); EncryptionParameterQualifiers qualifiers = new EncryptionParameterQualifiers(context2.FirstContextData.Qualifiers); Assert.IsNotNull(qualifiers); Assert.IsTrue(qualifiers.ParametersSet); Assert.IsTrue(qualifiers.UsingBatching); Assert.IsFalse(qualifiers.UsingFastPlainLift); Assert.IsTrue(qualifiers.UsingFFT); Assert.IsTrue(qualifiers.UsingHEStdSecurity); Assert.IsTrue(qualifiers.UsingNTT); }
public void EncodeTest() { EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV) { PlainModulus = new SmallModulus(1024) }; SEALContext context = SEALContext.Create(parms); IntegerEncoder encoder = new IntegerEncoder(context); Plaintext plain = encoder.Encode(10); Assert.IsNotNull(plain); Assert.AreEqual(4ul, plain.CoeffCount); Assert.AreEqual(0ul, plain[0]); Assert.AreEqual(1ul, plain[1]); Assert.AreEqual(0ul, plain[2]); Assert.AreEqual(1ul, plain[3]); plain = encoder.Encode(13u); Assert.AreEqual(4ul, plain.CoeffCount); Assert.AreEqual(1ul, plain[0]); Assert.AreEqual(0ul, plain[1]); Assert.AreEqual(1ul, plain[2]); Assert.AreEqual(1ul, plain[3]); plain = encoder.Encode(20L); Assert.AreEqual(5ul, plain.CoeffCount); Assert.AreEqual(0ul, plain[0]); Assert.AreEqual(0ul, plain[1]); Assert.AreEqual(1ul, plain[2]); Assert.AreEqual(0ul, plain[3]); Assert.AreEqual(1ul, plain[4]); plain = encoder.Encode(15ul); Assert.AreEqual(4ul, plain.CoeffCount); Assert.AreEqual(1ul, plain[0]); Assert.AreEqual(1ul, plain[1]); Assert.AreEqual(1ul, plain[2]); Assert.AreEqual(1ul, plain[3]); BigUInt bui = new BigUInt("AB"); plain = encoder.Encode(bui); Assert.AreEqual(8ul, plain.CoeffCount); Assert.AreEqual(1ul, plain[0]); Assert.AreEqual(1ul, plain[1]); Assert.AreEqual(0ul, plain[2]); Assert.AreEqual(1ul, plain[3]); Assert.AreEqual(0ul, plain[4]); Assert.AreEqual(1ul, plain[5]); Assert.AreEqual(0ul, plain[6]); Assert.AreEqual(1ul, plain[7]); Plaintext plain2 = new Plaintext(); encoder.Encode(10, plain2); Assert.AreEqual(4ul, plain2.CoeffCount); Assert.AreEqual(0ul, plain2[0]); Assert.AreEqual(1ul, plain2[1]); Assert.AreEqual(0ul, plain2[2]); Assert.AreEqual(1ul, plain2[3]); encoder.Encode(13u, plain2); Assert.AreEqual(4ul, plain2.CoeffCount); Assert.AreEqual(1ul, plain2[0]); Assert.AreEqual(0ul, plain2[1]); Assert.AreEqual(1ul, plain2[2]); Assert.AreEqual(1ul, plain2[3]); encoder.Encode(20L, plain2); Assert.AreEqual(5ul, plain2.CoeffCount); Assert.AreEqual(0ul, plain2[0]); Assert.AreEqual(0ul, plain2[1]); Assert.AreEqual(1ul, plain2[2]); Assert.AreEqual(0ul, plain2[3]); Assert.AreEqual(1ul, plain2[4]); encoder.Encode(15ul, plain2); Assert.AreEqual(4ul, plain2.CoeffCount); Assert.AreEqual(1ul, plain2[0]); Assert.AreEqual(1ul, plain2[1]); Assert.AreEqual(1ul, plain2[2]); Assert.AreEqual(1ul, plain2[3]); encoder.Encode(bui, plain2); Assert.AreEqual(8ul, plain2.CoeffCount); Assert.AreEqual(1ul, plain2[0]); Assert.AreEqual(1ul, plain2[1]); Assert.AreEqual(0ul, plain2[2]); Assert.AreEqual(1ul, plain2[3]); Assert.AreEqual(0ul, plain2[4]); Assert.AreEqual(1ul, plain2[5]); Assert.AreEqual(0ul, plain2[6]); Assert.AreEqual(1ul, plain2[7]); }
private static void HomoExample() { EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV); parms.PolyModulusDegree = 2048; parms.CoeffModulus = DefaultParams.CoeffModulus128(polyModulusDegree: 2048); parms.PlainModulus = new SmallModulus(1 << 8); SEALContext context = SEALContext.Create(parms); IntegerEncoder encoder = new IntegerEncoder(context); KeyGenerator keygen = new KeyGenerator(context); Microsoft.Research.SEAL.PublicKey publicKey = keygen.PublicKey; SecretKey secretKey = keygen.SecretKey; Encryptor encryptor = new Encryptor(context, publicKey); Evaluator evaluator = new Evaluator(context); Decryptor decryptor = new Decryptor(context, secretKey); int value1 = 5; Plaintext plain1 = encoder.Encode(value1); Console.WriteLine($"Encoded {value1} as polynomial {plain1.ToString()} (plain1)"); int value2 = -7; Plaintext plain2 = encoder.Encode(value2); Console.WriteLine($"Encoded {value2} as polynomial {plain2.ToString()} (plain2)"); Ciphertext encrypted1 = new Ciphertext(); Ciphertext encrypted2 = new Ciphertext(); Console.Write("Encrypting plain1: "); encryptor.Encrypt(plain1, encrypted1); Console.WriteLine("Done (encrypted1)"); Plaintext plainResult = new Plaintext(); decryptor.Decrypt(encrypted1, plainResult); Console.WriteLine(encoder.DecodeInt32(plainResult)); Console.Write("Encrypting plain2: "); encryptor.Encrypt(plain2, encrypted2); Console.WriteLine("Done (encrypted2)"); Console.WriteLine($"Noise budget in encrypted1: {decryptor.InvariantNoiseBudget(encrypted1)} bits"); Console.WriteLine($"Noise budget in encrypted2: {decryptor.InvariantNoiseBudget(encrypted2)} bits"); evaluator.NegateInplace(encrypted1); Console.WriteLine($"Noise budget in -encrypted1: {decryptor.InvariantNoiseBudget(encrypted1)} bits"); evaluator.AddInplace(encrypted1, encrypted2); Console.WriteLine($"Noise budget in -encrypted1 + encrypted2: {decryptor.InvariantNoiseBudget(encrypted1)} bits"); evaluator.MultiplyInplace(encrypted1, encrypted2); Console.WriteLine($"Noise budget in (-encrypted1 + encrypted2) * encrypted2: {decryptor.InvariantNoiseBudget(encrypted1)} bits"); plainResult = new Plaintext(); Console.Write("Decrypting result: "); decryptor.Decrypt(encrypted1, plainResult); Console.WriteLine("Done"); Console.WriteLine($"Plaintext polynomial: {plainResult.ToString()}"); Console.WriteLine($"Decoded integer: {encoder.DecodeInt32(plainResult)}"); }
public void ScaleTest() { List <SmallModulus> coeffModulus = new List <SmallModulus>() { DefaultParams.SmallMods40Bit(0), DefaultParams.SmallMods40Bit(1), DefaultParams.SmallMods40Bit(2), DefaultParams.SmallMods40Bit(3) }; EncryptionParameters parms = new EncryptionParameters(SchemeType.CKKS) { CoeffModulus = coeffModulus, PolyModulusDegree = 8 }; SEALContext context = SEALContext.Create(parms); KeyGenerator keygen = new KeyGenerator(context); GaloisKeys galoisKeys = keygen.GaloisKeys(decompositionBitCount: 4); Encryptor encryptor = new Encryptor(context, keygen.PublicKey); Evaluator evaluator = new Evaluator(context); CKKSEncoder encoder = new CKKSEncoder(context); MemoryPoolHandle pool = MemoryManager.GetPool(MMProfOpt.ForceNew); Assert.AreEqual(0ul, pool.AllocByteCount); Ciphertext encrypted = new Ciphertext(pool); Plaintext plain = new Plaintext(); MemoryPoolHandle cipherPool = encrypted.Pool; Assert.IsNotNull(cipherPool); Assert.AreEqual(0ul, cipherPool.AllocByteCount); List <Complex> input = new List <Complex>() { new Complex(1, 1), new Complex(2, 2), new Complex(3, 3), new Complex(4, 4) }; double delta = Math.Pow(2, 70); encoder.Encode(input, parms.ParmsId, delta, plain); encryptor.Encrypt(plain, encrypted); Assert.AreEqual(delta, encrypted.Scale, delta: Math.Pow(2, 60)); Ciphertext encrypted2 = new Ciphertext(); encrypted2.Set(encrypted); Assert.AreEqual(delta, encrypted2.Scale, delta: Math.Pow(2, 60)); evaluator.RescaleToNextInplace(encrypted); Assert.AreEqual(Math.Pow(2, 30), encrypted.Scale, delta: 10000); Assert.AreNotEqual(0ul, cipherPool.AllocByteCount); double newScale = Math.Pow(2, 10); encrypted.Scale = newScale; Assert.AreEqual(newScale, encrypted.Scale, delta: 100); }
public void TestLinearRegression_Encryption() { // these are the feature sets we will be encrypting and getting // the ML model results on. The plaintext versions of these values // should (in the encryped scheme) not be known by the evaluator double[][] testX = new double[6][]; testX[0] = new double[] { 43.45089541, 53.15091973, 53.07708932, 93.65456934, 65.23330105, 69.34856259, 62.91649012, 35.28814156, 108.1002775, 100.1735266 }; testX[1] = new double[] { 51.59952075, 99.48561775, 95.75948428, 126.6533636, 142.5235433, 90.97955769, 43.66586306, 85.31957886, 62.57644682, 66.12458533 }; testX[2] = new double[] { 94.77026243, 71.51229208, 85.33271407, 69.58347566, 107.8693045, 101.6701889, 89.88200921, 54.93440139, 105.5448532, 72.07947083 }; testX[3] = new double[] { 89.53820766, 100.199631, 86.19911875, 85.88717675, 33.92249944, 80.47113937, 65.34411148, 89.70004394, 75.00778202, 122.3514331 }; testX[4] = new double[] { 96.86101454, 97.54597612, 122.9960987, 86.1281547, 115.5539807, 107.888993, 65.51660154, 74.17007885, 48.04727402, 93.56952259 }; testX[5] = new double[] { 91.75121904, 121.2115065, 62.92763365, 99.4343452, 70.420912, 88.0580948, 71.82993308, 80.49171244, 87.11321454, 100.1459868 }; // setup the encryptor and other various components EncryptionParameters parms = new EncryptionParameters(SchemeType.CKKS); parms.PolyModulusDegree = 8192; parms.CoeffModulus = DefaultParams.CoeffModulus128(polyModulusDegree: 8192); SEALContext context = SEALContext.Create(parms); CKKSEncoder encoder = new CKKSEncoder(context); KeyGenerator keygen = new KeyGenerator(context); PublicKey publicKey = keygen.PublicKey; SecretKey secretKey = keygen.SecretKey; RelinKeys relinKeys = keygen.RelinKeys(decompositionBitCount: DefaultParams.DBCmax); Encryptor encryptor = new Encryptor(context, publicKey); Evaluator evaluator = new Evaluator(context); Decryptor decryptor = new Decryptor(context, secretKey); double scale = Math.Pow(2.0, 30); List <List <Ciphertext> > featureCiphers = new List <List <Ciphertext> >(); for (int i = 0; i < testX.Length; i++) { List <Ciphertext> curFeatureCiphers = new List <Ciphertext>(); foreach (var featureVal in testX[i]) { List <double> featureVector = new double[] { featureVal }.ToList(); Plaintext plain = new Plaintext(); encoder.Encode(featureVal, scale, plain); Ciphertext encrypted = new Ciphertext(); encryptor.Encrypt(plain, encrypted); curFeatureCiphers.Add(encrypted); } featureCiphers.Add(curFeatureCiphers); } // This is the 'evaluator' section // this is not a part of the client and would, in a cloud based solution, be run on the server // the server should not know the values of the input features, but it will do math on them // likewise, the client would not know the various weights and parameters, but will receive a result double[] weights_model = { 0.0921533, 0.14545279, 0.11066622, 0.06119513, 0.10041948, 0.19091597, 0.07359407, 0.04503237, 0.10848583, 0.10092494 }; double intercept_model = -2.484390163425502; double[] weights_groundTruth = { 0.1, 0.15, 0.1, 0.05, 0.1, 0.2, 0.05, 0.05, 0.1, 0.1 }; // we need to encode the weights/parameters/intercepts/etc. used by the model using the same public key as the client List <Ciphertext> weightsCTs = new List <Ciphertext>(); List <Ciphertext> scoreCTs = new List <Ciphertext>(); foreach (var weight in weights_model) { List <double> weightVector = new double[] { weight }.ToList(); List <double> scoreVector = new double[] { 0.0 }.ToList(); Plaintext weightPT = new Plaintext(); encoder.Encode(weight, scale, weightPT); Ciphertext weightCT = new Ciphertext(); encryptor.Encrypt(weightPT, weightCT); weightsCTs.Add(weightCT); } // next, we run the actual model's computation // linear regression is a basic dot product for (int i = 0; i < featureCiphers.Count(); i++) { List <Ciphertext> multResultCTs = new List <Ciphertext>(); for (var j = 0; j < weightsCTs.Count; j++) { Ciphertext multResultCT = new Ciphertext(); evaluator.Multiply(weightsCTs[j], featureCiphers[i][j], multResultCT); multResultCTs.Add(multResultCT); } Ciphertext dotProductResult = new Ciphertext(); evaluator.AddMany(multResultCTs, dotProductResult); Plaintext scorePT = new Plaintext(); encoder.Encode(intercept_model, dotProductResult.Scale, scorePT); Ciphertext scoreCT = new Ciphertext(); encryptor.Encrypt(scorePT, scoreCT); evaluator.AddInplace(scoreCT, dotProductResult); scoreCTs.Add(scoreCT); //evaluator.AddInplace(scoreCTs[i], interceptCT); } // we now have the encrypted version of the ML model. The below section is the client's again // in it, we decrypt the results given by the server using the private key generated previously List <List <double> > predictions = new List <List <double> >(); for (int i = 0; i < scoreCTs.Count; i++) { Plaintext plainResult = new Plaintext(); var encryptedResult = scoreCTs[i]; decryptor.Decrypt(encryptedResult, plainResult); List <double> result = new List <double>(); encoder.Decode(plainResult, result); predictions.Add(result); } // this is the output section. In practice, we would not have access to these values (as they represent the ground truth) // We are using them here merely to demonstrate that we can properly recover the proper ML model output double[] yGroundTruth = { 68.43881952, 87.75905253, 88.34053641, 83.87264322, 95.20322583, 89.61704108 }; double[] yTest = { 68.702934, 87.28860458, 88.40827187, 83.24001674, 94.53137951, 89.00229455 }; const double EPSILON = 1e-4; for (int i = 0; i < predictions.Count; i++) { var avgDecryption = predictions[i].Average(); var absDelta = Math.Abs(avgDecryption - yTest[i]); Assert.IsTrue(absDelta < EPSILON); } }
public void ExceptionsTest() { EncryptionParameters parms = new EncryptionParameters(SchemeType.CKKS) { PolyModulusDegree = 64, CoeffModulus = new List <SmallModulus>() { DefaultParams.SmallMods30Bit(0), DefaultParams.SmallMods30Bit(1) } }; SEALContext context = SEALContext.Create(parms); CKKSEncoder encoder = new CKKSEncoder(context); List <double> vald = new List <double>(); List <double> vald_null = null; List <Complex> valc = new List <Complex>(); List <Complex> valc_null = null; Plaintext plain = new Plaintext(); Plaintext plain_null = null; MemoryPoolHandle pool = MemoryManager.GetPool(MMProfOpt.ForceGlobal); Complex complex = new Complex(1, 2); Assert.ThrowsException <ArgumentNullException>(() => encoder = new CKKSEncoder(null)); Assert.ThrowsException <ArgumentNullException>(() => encoder.Encode(vald, ParmsId.Zero, 10.0, plain_null)); Assert.ThrowsException <ArgumentNullException>(() => encoder.Encode(vald, null, 10.0, plain)); Assert.ThrowsException <ArgumentNullException>(() => encoder.Encode(vald_null, ParmsId.Zero, 10.0, plain)); Assert.ThrowsException <ArgumentException>(() => encoder.Encode(vald, ParmsId.Zero, 10.0, plain, pool)); Assert.ThrowsException <ArgumentNullException>(() => encoder.Encode(valc, ParmsId.Zero, 10.0, plain_null)); Assert.ThrowsException <ArgumentNullException>(() => encoder.Encode(valc, null, 10.0, plain)); Assert.ThrowsException <ArgumentNullException>(() => encoder.Encode(valc_null, ParmsId.Zero, 10.0, plain)); Assert.ThrowsException <ArgumentException>(() => encoder.Encode(valc, ParmsId.Zero, 10.0, plain, pool)); Assert.ThrowsException <ArgumentNullException>(() => encoder.Encode(vald, 10.0, plain_null)); Assert.ThrowsException <ArgumentNullException>(() => encoder.Encode(vald_null, 10.0, plain)); Assert.ThrowsException <ArgumentException>(() => encoder.Encode(vald, -10.0, plain, pool)); Assert.ThrowsException <ArgumentNullException>(() => encoder.Encode(valc, 10.0, plain_null)); Assert.ThrowsException <ArgumentNullException>(() => encoder.Encode(valc_null, 10.0, plain)); Assert.ThrowsException <ArgumentException>(() => encoder.Encode(valc, -10.0, plain, pool)); Assert.ThrowsException <ArgumentNullException>(() => encoder.Encode(10.0, ParmsId.Zero, 20.0, plain_null)); Assert.ThrowsException <ArgumentNullException>(() => encoder.Encode(10.0, null, 20.0, plain)); Assert.ThrowsException <ArgumentException>(() => encoder.Encode(10.0, ParmsId.Zero, 20.0, plain, pool)); Assert.ThrowsException <ArgumentNullException>(() => encoder.Encode(10.0, 20.0, plain_null)); Assert.ThrowsException <ArgumentException>(() => encoder.Encode(10.0, -20.0, plain, pool)); Assert.ThrowsException <ArgumentNullException>(() => encoder.Encode(complex, ParmsId.Zero, 10.0, plain_null)); Assert.ThrowsException <ArgumentNullException>(() => encoder.Encode(complex, null, 10.0, plain)); Assert.ThrowsException <ArgumentException>(() => encoder.Encode(complex, ParmsId.Zero, 10.0, plain, pool)); Assert.ThrowsException <ArgumentNullException>(() => encoder.Encode(complex, 10.0, plain_null)); Assert.ThrowsException <ArgumentException>(() => encoder.Encode(complex, -10.0, plain, pool)); Assert.ThrowsException <ArgumentNullException>(() => encoder.Encode(10, ParmsId.Zero, plain_null)); Assert.ThrowsException <ArgumentNullException>(() => encoder.Encode(10, null, plain)); Assert.ThrowsException <ArgumentException>(() => encoder.Encode(10, ParmsId.Zero, plain)); Assert.ThrowsException <ArgumentNullException>(() => encoder.Encode(10, plain_null)); Assert.ThrowsException <ArgumentNullException>(() => encoder.Decode(plain, vald_null)); Assert.ThrowsException <ArgumentNullException>(() => encoder.Decode(plain_null, vald)); Assert.ThrowsException <ArgumentException>(() => encoder.Decode(plain, vald, pool)); Assert.ThrowsException <ArgumentNullException>(() => encoder.Decode(plain, valc_null)); Assert.ThrowsException <ArgumentNullException>(() => encoder.Decode(plain_null, valc)); Assert.ThrowsException <ArgumentException>(() => encoder.Decode(plain, valc, pool)); }
public void EncodeLongTest() { EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV); parms.PolyModulusDegree = 64; List <SmallModulus> coeffModulus = new List <SmallModulus>(); coeffModulus.Add(DefaultParams.SmallMods60Bit(0)); parms.CoeffModulus = coeffModulus; parms.PlainModulus = new SmallModulus(257); SEALContext context = SEALContext.Create(parms); BatchEncoder encoder = new BatchEncoder(context); Assert.AreEqual(64ul, encoder.SlotCount); List <long> plainList = new List <long>(); for (ulong i = 0; i < encoder.SlotCount; i++) { plainList.Add((long)i); } Plaintext plain = new Plaintext(); encoder.Encode(plainList, plain); List <long> plainList2 = new List <long>(); encoder.Decode(plain, plainList2); for (ulong i = 0; i < encoder.SlotCount; i++) { Assert.AreEqual(plainList[(int)i], plainList2[(int)i]); } for (ulong i = 0; i < encoder.SlotCount; i++) { plainList[(int)i] = 5; } encoder.Encode(plainList, plain); Assert.AreEqual("5", plain.ToString()); encoder.Decode(plain, plainList2); for (ulong i = 0; i < encoder.SlotCount; i++) { Assert.AreEqual(plainList[(int)i], plainList2[(int)i]); } List <long> shortList = new List <long>(); for (int i = 0; i < 20; i++) { shortList.Add((long)i); } encoder.Encode(shortList, plain); List <long> shortList2 = new List <long>(); encoder.Decode(plain, shortList2); Assert.AreEqual(20, shortList.Count); Assert.AreEqual(64, shortList2.Count); for (int i = 0; i < 20; i++) { Assert.AreEqual(shortList[i], shortList2[i]); } for (ulong i = 20; i < encoder.SlotCount; i++) { Assert.AreEqual(0L, shortList2[(int)i]); } }