|
| 1 | +using System; |
| 2 | +using System.Text; |
| 3 | +using System.Linq; |
| 4 | +using System.Collections; |
| 5 | +using System.Threading.Tasks; |
| 6 | + |
| 7 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 8 | +using System.Collections.Generic; |
| 9 | + |
| 10 | +namespace CompactMPC.ObliviousTransfer.UnitTests |
| 11 | +{ |
| 12 | + internal class RandomOracleStub : RandomOracle |
| 13 | + { |
| 14 | + private Dictionary<byte[], byte[]> _valueDict; |
| 15 | + |
| 16 | + public RandomOracleStub(Dictionary<byte[], byte[]> valueDict) |
| 17 | + { |
| 18 | + _valueDict = new Dictionary<byte[], byte[]>(valueDict); |
| 19 | + } |
| 20 | + |
| 21 | + /// <inheritdoc /> |
| 22 | + /// <remarks> |
| 23 | + /// <exception cref="KeyNotFoundException">If the query is not found in the value dictionary passed into the constructor.</exception> |
| 24 | + /// </remarks> |
| 25 | + public override IEnumerable<byte> Invoke(byte[] query) |
| 26 | + { |
| 27 | + return _valueDict[query]; |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + [TestClass] |
| 32 | + public class RandomOracleTest |
| 33 | + { |
| 34 | + [TestMethod] |
| 35 | + public void TestStub() |
| 36 | + { |
| 37 | + Dictionary<byte[], byte[]> keyValuePairs = new Dictionary<byte[], byte[]>(); |
| 38 | + keyValuePairs[BitArray.FromBinaryString("1001").ToBytes()] = BitArray.FromBinaryString("100101011100101").ToBytes(); |
| 39 | + keyValuePairs[BitArray.FromBinaryString("1010").ToBytes()] = BitArray.FromBinaryString("101011110101100").ToBytes(); |
| 40 | + |
| 41 | + RandomOracle oracle = new RandomOracleStub(keyValuePairs); |
| 42 | + foreach (KeyValuePair<byte[], byte[]> pair in keyValuePairs) |
| 43 | + { |
| 44 | + Assert.AreEqual(pair.Value, oracle.Invoke(pair.Key)); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + [TestMethod] |
| 49 | + public void TestMask() |
| 50 | + { |
| 51 | + var query1 = BitArray.FromBinaryString("1001"); |
| 52 | + var query2 = BitArray.FromBinaryString("1010"); |
| 53 | + byte[] query1Bytes = query1.ToBytes(); |
| 54 | + byte[] query2Bytes = query2.ToBytes(); |
| 55 | + |
| 56 | + var value1 = BitArray.FromBinaryString("1001010111001011"); |
| 57 | + var value2 = BitArray.FromBinaryString("1010111101011001"); |
| 58 | + |
| 59 | + Dictionary<byte[], byte[]> keyValuePairs = new Dictionary<byte[], byte[]>(); |
| 60 | + keyValuePairs[query1Bytes] = value1.ToBytes(); |
| 61 | + keyValuePairs[query2Bytes] = value2.ToBytes(); |
| 62 | + |
| 63 | + RandomOracle oracle = new RandomOracleStub(keyValuePairs); |
| 64 | + |
| 65 | + var message1 = BitArray.FromBinaryString("0000000000000000"); |
| 66 | + var message2 = BitArray.FromBinaryString("1100110011001100"); |
| 67 | + byte[] message1Bytes = message1.ToBytes(); |
| 68 | + byte[] message2Bytes = message2.ToBytes(); |
| 69 | + |
| 70 | + byte[] masked11 = oracle.Mask(message1Bytes, query1Bytes); |
| 71 | + byte[] masked12 = oracle.Mask(message1Bytes, query2Bytes); |
| 72 | + CollectionAssert.AreEqual(keyValuePairs[query1Bytes], masked11); |
| 73 | + CollectionAssert.AreEqual(keyValuePairs[query2Bytes], masked12); |
| 74 | + |
| 75 | + |
| 76 | + byte[] masked21 = oracle.Mask(message2Bytes, query1Bytes); |
| 77 | + byte[] masked22 = oracle.Mask(message2Bytes, query2Bytes); |
| 78 | + |
| 79 | + BitArray expected12 = BitArray.FromBytes(keyValuePairs[query2Bytes], 16); |
| 80 | + expected12.Xor(message1); |
| 81 | + BitArray expected22 = BitArray.FromBytes(keyValuePairs[query2Bytes], 16); |
| 82 | + expected22.Xor(message2); |
| 83 | + |
| 84 | + CollectionAssert.AreEqual(expected12.ToBytes(), masked12); |
| 85 | + CollectionAssert.AreEqual(expected22.ToBytes(), masked22); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments