public void BinaryEncodeDecodeInt64NET() { var modulus = new BigUInt("FFFFFFFFFFFFFFFF"); var encoder = new BinaryEncoder(modulus); var poly = encoder.Encode(0L); Assert.AreEqual(0, poly.GetSignificantCoeffCount()); Assert.IsTrue(poly.IsZero); Assert.AreEqual(0L, encoder.DecodeInt64(poly)); var poly1 = encoder.Encode(1L); Assert.AreEqual(1, poly1.GetSignificantCoeffCount()); Assert.AreEqual(1, poly1.CoeffBitCount); Assert.AreEqual("1", poly1.ToString()); Assert.AreEqual(1L, encoder.DecodeInt64(poly1)); var poly2 = encoder.Encode(2L); Assert.AreEqual(2, poly2.GetSignificantCoeffCount()); Assert.AreEqual(1, poly2.CoeffBitCount); Assert.AreEqual("1x^1", poly2.ToString()); Assert.AreEqual(2L, encoder.DecodeInt64(poly2)); var poly3 = encoder.Encode(3L); Assert.AreEqual(2, poly3.GetSignificantCoeffCount()); Assert.AreEqual(1, poly3.CoeffBitCount); Assert.AreEqual("1x^1 + 1", poly3.ToString()); Assert.AreEqual(3L, encoder.DecodeInt64(poly3)); var poly4 = encoder.Encode(-1L); Assert.AreEqual(1, poly4.GetSignificantCoeffCount()); Assert.AreEqual(64, poly4.CoeffBitCount); Assert.AreEqual("FFFFFFFFFFFFFFFE", poly4.ToString()); Assert.AreEqual(-1L, encoder.DecodeInt64(poly4)); var poly5 = encoder.Encode(-2L); Assert.AreEqual(2, poly5.GetSignificantCoeffCount()); Assert.AreEqual(64, poly5.CoeffBitCount); Assert.AreEqual("FFFFFFFFFFFFFFFEx^1", poly5.ToString()); Assert.AreEqual(-2L, encoder.DecodeInt64(poly5)); var poly6 = encoder.Encode(-3L); Assert.AreEqual(2, poly6.GetSignificantCoeffCount()); Assert.AreEqual(64, poly6.CoeffBitCount); Assert.AreEqual("FFFFFFFFFFFFFFFEx^1 + FFFFFFFFFFFFFFFE", poly6.ToString()); Assert.AreEqual(-3L, encoder.DecodeInt64(poly6)); var poly7 = encoder.Encode(0x7FFFFFFFFFFFFFFFL); Assert.AreEqual(63, poly7.GetSignificantCoeffCount()); Assert.AreEqual(1, poly7.CoeffBitCount); for (int i = 0; i < 63; ++i) { Assert.AreEqual("1", poly7[i].ToString()); } Assert.AreEqual(0x7FFFFFFFFFFFFFFFL, encoder.DecodeInt64(poly7)); var poly8 = encoder.Encode(unchecked ((long)0x8000000000000000)); Assert.AreEqual(64, poly8.GetSignificantCoeffCount()); Assert.AreEqual(64, poly8.CoeffBitCount); Assert.AreEqual("FFFFFFFFFFFFFFFE", poly8[63].ToString()); for (int i = 0; i < 63; ++i) { Assert.IsTrue(poly8[i].IsZero); } Assert.AreEqual(unchecked ((long)0x8000000000000000), encoder.DecodeInt64(poly8)); var poly9 = encoder.Encode(0x80F02L); Assert.AreEqual(20, poly9.GetSignificantCoeffCount()); Assert.AreEqual(1, poly9.CoeffBitCount); for (int i = 0; i < 20; ++i) { if (i == 19 || (i >= 8 && i <= 11) || i == 1) { Assert.AreEqual("1", poly9[i].ToString()); } else { Assert.IsTrue(poly9[i].IsZero); } } Assert.AreEqual(0x80F02L, encoder.DecodeInt64(poly9)); var poly10 = encoder.Encode(-1073L); Assert.AreEqual(11, poly10.GetSignificantCoeffCount()); Assert.AreEqual(64, poly10.CoeffBitCount); Assert.AreEqual("FFFFFFFFFFFFFFFE", poly10[10].ToString()); Assert.IsTrue(poly10[9].IsZero); Assert.IsTrue(poly10[8].IsZero); Assert.IsTrue(poly10[7].IsZero); Assert.IsTrue(poly10[6].IsZero); Assert.AreEqual("FFFFFFFFFFFFFFFE", poly10[5].ToString()); Assert.AreEqual("FFFFFFFFFFFFFFFE", poly10[4].ToString()); Assert.IsTrue(poly10[3].IsZero); Assert.IsTrue(poly10[2].IsZero); Assert.IsTrue(poly10[1].IsZero); Assert.AreEqual("FFFFFFFFFFFFFFFE", poly10[0].ToString()); Assert.AreEqual(-1073L, encoder.DecodeInt64(poly10)); modulus.Set("FFFF"); var encoder2 = new BinaryEncoder(modulus); var poly11 = new BigPoly(6, 64); poly11[0].Set(1); poly11[1].Set("FFFE"); // -1 poly11[2].Set("FFFD"); // -2 poly11[3].Set("8000"); // -32767 poly11[4].Set("7FFF"); // 32767 poly11[5].Set("7FFE"); // 32766 Assert.AreEqual(1L + -1 * 2 + -2 * 4 + -32767 * 8 + 32767 * 16 + 32766 * 32, encoder2.DecodeInt64(poly11)); }
public void BinaryEncodeDecodeInt64NET() { var modulus = new SmallModulus(0xFFFFFFFFFFFFFFF); var encoder = new BinaryEncoder(modulus, MemoryPoolHandle.New()); var poly = encoder.Encode(0L); Assert.AreEqual(0, poly.SignificantCoeffCount()); Assert.IsTrue(poly.IsZero); Assert.AreEqual(0L, encoder.DecodeInt64(poly)); var poly1 = encoder.Encode(1L); Assert.AreEqual(1, poly1.SignificantCoeffCount()); Assert.AreEqual("1", poly1.ToString()); Assert.AreEqual(1L, encoder.DecodeInt64(poly1)); var poly2 = encoder.Encode(2L); Assert.AreEqual(2, poly2.SignificantCoeffCount()); Assert.AreEqual("1x^1", poly2.ToString()); Assert.AreEqual(2L, encoder.DecodeInt64(poly2)); var poly3 = encoder.Encode(3L); Assert.AreEqual(2, poly3.SignificantCoeffCount()); Assert.AreEqual("1x^1 + 1", poly3.ToString()); Assert.AreEqual(3L, encoder.DecodeInt64(poly3)); var poly4 = encoder.Encode(-1L); Assert.AreEqual(1, poly4.SignificantCoeffCount()); Assert.AreEqual("FFFFFFFFFFFFFFE", poly4.ToString()); Assert.AreEqual(-1L, encoder.DecodeInt64(poly4)); var poly5 = encoder.Encode(-2L); Assert.AreEqual(2, poly5.SignificantCoeffCount()); Assert.AreEqual("FFFFFFFFFFFFFFEx^1", poly5.ToString()); Assert.AreEqual(-2L, encoder.DecodeInt64(poly5)); var poly6 = encoder.Encode(-3L); Assert.AreEqual(2, poly6.SignificantCoeffCount()); Assert.AreEqual("FFFFFFFFFFFFFFEx^1 + FFFFFFFFFFFFFFE", poly6.ToString()); Assert.AreEqual(-3L, encoder.DecodeInt64(poly6)); var poly7 = encoder.Encode(0x7FFFFFFFFFFFFFFFL); Assert.AreEqual(63, poly7.SignificantCoeffCount()); for (int i = 0; i < 63; ++i) { Assert.AreEqual(1UL, poly7[i]); } Assert.AreEqual(0x7FFFFFFFFFFFFFFFL, encoder.DecodeInt64(poly7)); var poly8 = encoder.Encode(unchecked ((long)0x8000000000000000)); Assert.AreEqual(64, poly8.SignificantCoeffCount()); Assert.AreEqual(0xFFFFFFFFFFFFFFEUL, poly8[63]); for (int i = 0; i < 63; ++i) { Assert.IsTrue(poly8[i] == 0); } Assert.AreEqual(unchecked ((long)0x8000000000000000), encoder.DecodeInt64(poly8)); var poly9 = encoder.Encode(0x80F02L); Assert.AreEqual(20, poly9.SignificantCoeffCount()); for (int i = 0; i < 20; ++i) { if (i == 19 || (i >= 8 && i <= 11) || i == 1) { Assert.AreEqual(1UL, poly9[i]); } else { Assert.IsTrue(poly9[i] == 0); } } Assert.AreEqual(0x80F02L, encoder.DecodeInt64(poly9)); var poly10 = encoder.Encode(-1073L); Assert.AreEqual(11, poly10.SignificantCoeffCount()); Assert.AreEqual(0xFFFFFFFFFFFFFFEUL, poly10[10]); Assert.IsTrue(poly10[9] == 0); Assert.IsTrue(poly10[8] == 0); Assert.IsTrue(poly10[7] == 0); Assert.IsTrue(poly10[6] == 0); Assert.AreEqual(0xFFFFFFFFFFFFFFEUL, poly10[5]); Assert.AreEqual(0xFFFFFFFFFFFFFFEUL, poly10[4]); Assert.IsTrue(poly10[3] == 0); Assert.IsTrue(poly10[2] == 0); Assert.IsTrue(poly10[1] == 0); Assert.AreEqual(0xFFFFFFFFFFFFFFEUL, poly10[0]); Assert.AreEqual(-1073L, encoder.DecodeInt64(poly10)); modulus.Set(0xFFFF); var encoder2 = new BinaryEncoder(modulus, MemoryPoolHandle.New()); var poly11 = new Plaintext(6); poly11[0] = 1; poly11[1] = 0xFFFE; // -1 poly11[2] = 0xFFFD; // -2 poly11[3] = 0x8000; // -32767 poly11[4] = 0x7FFF; // 32767 poly11[5] = 0x7FFE; // 32766 Assert.AreEqual(1L + -1 * 2 + -2 * 4 + -32767 * 8 + 32767 * 16 + 32766 * 32, encoder2.DecodeInt64(poly11)); }