// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. using Microsoft.Research.SEAL; using Microsoft.VisualStudio.TestTools.UnitTesting; using System; using System.Collections.Generic; using System.Runtime.InteropServices.ComTypes; namespace SEALNetTest { [TestClass] public class BatchEncoderTests { [TestMethod] public void EncodeULongTest() { EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV); parms.PolyModulusDegree = 64; parms.CoeffModulus = CoeffModulus.Create(64, new int[]{ 60 }); parms.PlainModulus = new Modulus(257); SEALContext context = new SEALContext(parms, expandModChain: false, secLevel: SecLevelType.None); BatchEncoder encoder = new BatchEncoder(context); Assert.AreEqual(64ul, encoder.SlotCount); List plainList = new List(); for (ulong i = 0; i < encoder.SlotCount; i++) { plainList.Add((ulong)i); } Plaintext plain = new Plaintext(); encoder.Encode(plainList, plain); List plainList2 = new List(); encoder.Decode(plain, plainList2); for (ulong i = 0; i < encoder.SlotCount; i++) { Assert.AreEqual(plainList[checked((int)i)], plainList2[checked((int)i)]); } for (ulong i = 0; i < encoder.SlotCount; i++) { plainList[checked((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[checked((int)i)], plainList2[checked((int)i)]); } List shortList = new List(); for (ulong i = 0; i < 20; i++) { shortList.Add(i); } encoder.Encode(shortList, plain); List shortList2 = new List(); 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(0ul, shortList2[checked((int)i)]); } } [TestMethod] public void EncodeLongTest() { EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV); parms.PolyModulusDegree = 64; parms.CoeffModulus = CoeffModulus.Create(64, new int[] { 60 }); parms.PlainModulus = new Modulus(257); SEALContext context = new SEALContext(parms, expandModChain: false, secLevel: SecLevelType.None); BatchEncoder encoder = new BatchEncoder(context); Assert.AreEqual(64ul, encoder.SlotCount); List plainList = new List(); for (ulong i = 0; i < encoder.SlotCount; i++) { plainList.Add((long)i); } Plaintext plain = new Plaintext(); encoder.Encode(plainList, plain); List plainList2 = new List(); encoder.Decode(plain, plainList2); for (ulong i = 0; i < encoder.SlotCount; i++) { Assert.AreEqual(plainList[checked((int)i)], plainList2[checked((int)i)]); } for (ulong i = 0; i < encoder.SlotCount; i++) { plainList[checked((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[checked((int)i)], plainList2[checked((int)i)]); } List shortList = new List(); for (int i = 0; i < 20; i++) { shortList.Add((long)i); } encoder.Encode(shortList, plain); List shortList2 = new List(); 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[checked((int)i)]); } } [TestMethod] public void SchemeIsCKKSTest() { EncryptionParameters parms = new EncryptionParameters(SchemeType.CKKS) { PolyModulusDegree = 8, CoeffModulus = CoeffModulus.Create(8, new int[] { 40, 40, 40, 40 }) }; SEALContext context = new SEALContext(parms, expandModChain: false, secLevel: SecLevelType.None); Utilities.AssertThrows(() => { BatchEncoder encoder = new BatchEncoder(context); }); } [TestMethod] public void ExceptionsTest() { EncryptionParameters parms = new EncryptionParameters(SchemeType.BFV) { PolyModulusDegree = 64, CoeffModulus = CoeffModulus.Create(64, new int[] { 60 }), PlainModulus = new Modulus(257) }; SEALContext context = new SEALContext(parms, expandModChain: false, secLevel: SecLevelType.None); BatchEncoder enc = new BatchEncoder(context); List valu = new List(); List valu_null = null; List vall = new List(); List vall_null = null; Plaintext plain = new Plaintext(); Plaintext plain_null = null; MemoryPoolHandle pool_uninit = new MemoryPoolHandle(); Utilities.AssertThrows(() => enc = new BatchEncoder(null)); Utilities.AssertThrows(() => enc.Encode(valu, plain_null)); Utilities.AssertThrows(() => enc.Encode(valu_null, plain)); Utilities.AssertThrows(() => enc.Encode(vall, plain_null)); Utilities.AssertThrows(() => enc.Encode(vall_null, plain)); Utilities.AssertThrows(() => enc.Decode(plain, valu_null)); Utilities.AssertThrows(() => enc.Decode(plain_null, valu)); Utilities.AssertThrows(() => enc.Decode(plain, valu, pool_uninit)); Utilities.AssertThrows(() => enc.Decode(plain, vall_null)); Utilities.AssertThrows(() => enc.Decode(plain_null, vall)); Utilities.AssertThrows(() => enc.Decode(plain, vall, pool_uninit)); } } }