Skip to content

Commit de4a5e6

Browse files
committed
Added documentation and some review comments for selected classes
focused mostly on the OT implementation and dependencies.
1 parent dc6da7d commit de4a5e6

File tree

8 files changed

+155
-3
lines changed

8 files changed

+155
-3
lines changed

CompactMPC/BitArray.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public static int RequiredBytes(int numberOfBits)
2929
return RequiredBytes(numberOfBits, ElementsPerByte);
3030
}
3131

32+
// note(lumip): it seems to me like all these would be nicer if they would return the result
33+
// instead of inplace manipulation. Is there a specific design reason for the current way?
3234
public void Or(BitArray other)
3335
{
3436
if (other.Length != Length)

CompactMPC/Buffers/BufferBuilder.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
namespace CompactMPC.Buffers
88
{
9+
/// <summary>
10+
/// Allows to build a byte buffer by composing existing buffers and integer values.
11+
/// </summary>
912
public class BufferBuilder
1013
{
1114
private MessageComposer _composer;

CompactMPC/ObliviousTransfer/IGeneralizedObliviousTransfer.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,36 @@
1010

1111
namespace CompactMPC.ObliviousTransfer
1212
{
13+
/// <summary>
14+
/// Common interface for 1-out-of-4 bit oblivious transfer implementations.
15+
/// </summary>
16+
///
17+
/// For single bit for messages/options, see <see cref="IObliviousTransfer"/>.
1318
public interface IGeneralizedObliviousTransfer
1419
{
20+
/// <summary>
21+
/// Supplies the four options from the sender to the oblivious transfer.
22+
/// </summary>
23+
///
24+
/// To increase efficiency, several invocations of OT can be batched into one transmission.
25+
///
26+
/// <param name="channel">The network message channel to the receiver.</param>
27+
/// <param name="options">Array containing the four options supplied to 1-out-of-4 OT by the sender for each invocation.</param>
28+
/// <param name="numberOfInvocations">The number of OT invocations in the transmission.</param>
29+
/// <returns>An asynchronous task which performs the server side of the oblivious transfer.</returns>
1530
Task SendAsync(IMessageChannel channel, Quadruple<byte[]>[] options, int numberOfInvocations, int numberOfMessageBytes);
31+
32+
/// <summary>
33+
/// Supplies the selection index from the client side to the oblivious transfer and returns the corresponding option from the server.
34+
/// </summary>
35+
///
36+
/// To increase efficiency, several invocations of OT can be batched into one transmission
37+
///
38+
/// <param name="channel">The network message channel to the sender.</param>
39+
/// <param name="selectionIndices">Array containing the selection index supplied to 1-out-of-4 OT by the client for each invocation.</param>
40+
/// <param name="numberOfInvocations">The number of OT invocations in the transmission.</param>
41+
/// <returns>An asynchronous task performing the client side of the oblivious transfer and returning the array containing
42+
/// the options as selected by the client retrieved from the server.</returns>
1643
Task<byte[][]> ReceiveAsync(IMessageChannel channel, QuadrupleIndexArray selectionIndices, int numberOfInvocations, int numberOfMessageBytes);
1744
}
1845
}

CompactMPC/ObliviousTransfer/IObliviousTransfer.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,36 @@
1010

1111
namespace CompactMPC.ObliviousTransfer
1212
{
13+
/// <summary>
14+
/// Common interface for 1-out-of-4 single bit oblivious transfer implementations.
15+
/// </summary>
16+
///
17+
/// For a variant with arbitrary bit length for messages/options, see <see cref="IGeneralizedObliviousTransfer"/>.
1318
public interface IObliviousTransfer
1419
{
20+
/// <summary>
21+
/// Supplies the four options from the sender to the oblivious transfer.
22+
/// </summary>
23+
///
24+
/// To increase efficiency, several invocations of OT can be batched into one transmission.
25+
///
26+
/// <param name="channel">The network message channel to the receiver.</param>
27+
/// <param name="options">Array containing the four options supplied to 1-out-of-4 OT by the sender for each invocation.</param>
28+
/// <param name="numberOfInvocations">The number of OT invocations in the transmission.</param>
29+
/// <returns>An asynchronous task which performs the server side of the oblivious transfer.</returns>
1530
Task SendAsync(IMessageChannel channel, BitQuadrupleArray options, int numberOfInvocations);
31+
32+
/// <summary>
33+
/// Supplies the selection index from the client side to the oblivious transfer and returns the corresponding option from the server.
34+
/// </summary>
35+
///
36+
/// To increase efficiency, several invocations of OT can be batched into one transmission
37+
///
38+
/// <param name="channel">The network message channel to the sender.</param>
39+
/// <param name="selectionIndices">Array containing the selection index supplied to 1-out-of-4 OT by the client for each invocation.</param>
40+
/// <param name="numberOfInvocations">The number of OT invocations in the transmission.</param>
41+
/// <returns>An asynchronous task performing the client side of the oblivious transfer and returning the array containing
42+
/// the options as selected by the client retrieved from the server.</returns>
1643
Task<BitArray> ReceiveAsync(IMessageChannel channel, QuadrupleIndexArray selectionIndices, int numberOfInvocations);
1744
}
1845
}

CompactMPC/ObliviousTransfer/InsecureObliviousTransfer.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,21 @@
99

1010
namespace CompactMPC.ObliviousTransfer
1111
{
12+
/// <summary>
13+
/// Insecure (non-oblivious) implementation of the oblivious transfer interface.
14+
/// </summary>
15+
///
16+
/// Caution! This class is intended for testing and debugging purposes and does not provide any security.
17+
/// Hence, it should not be used with any sensitive or in production deployments. All options are SENT IN THE PLAIN.
1218
public class InsecureObliviousTransfer : GeneralizedObliviousTransfer
1319
{
1420
public override Task SendAsync(IMessageChannel channel, Quadruple<byte[]>[] options, int numberOfInvocations, int numberOfMessageBytes)
1521
{
22+
// note(lumip): common argument verification code.. wrap into a common method in a base class?
1623
if (options.Length != numberOfInvocations)
1724
throw new ArgumentException("Provided options must match the specified number of invocations.", nameof(options));
1825

19-
for (int i = 0; i < options.Length; ++i)
26+
for (int i = 0; i < numberOfInvocations; ++i)
2027
{
2128
foreach (byte[] message in options[i])
2229
{

CompactMPC/ObliviousTransfer/NaorPinkasObliviousTransfer.cs

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414

1515
namespace CompactMPC.ObliviousTransfer
1616
{
17+
// note(lumip): the implementation does not seem to actually follow any construction given in
18+
// [Moni Naor and Benny Pinkas: Computationally Secure Oblivious Transfer. 2005.]
19+
// so what is the exact reference here?
20+
// looks like
21+
// [Moni Naor and Benny Pinkas: Efficient oblivious transfer protocols 2001.]
1722
public class NaorPinkasObliviousTransfer : GeneralizedObliviousTransfer
1823
{
1924
private SecurityParameters _parameters;
@@ -37,6 +42,7 @@ public NaorPinkasObliviousTransfer(SecurityParameters parameters, CryptoContext
3742

3843
public override async Task SendAsync(IMessageChannel channel, Quadruple<byte[]>[] options, int numberOfInvocations, int numberOfMessageBytes)
3944
{
45+
// note(lumip): common argument verification code.. wrap into a common method in a base class?
4046
if (options.Length != numberOfInvocations)
4147
throw new ArgumentException("Provided options must match the specified number of invocations.", nameof(options));
4248

@@ -64,6 +70,8 @@ public override async Task SendAsync(IMessageChannel channel, Quadruple<byte[]>[
6470
});
6571

6672
BigInteger alpha = listOfExponents[0];
73+
// note(lumip): sender should probably verify that the all generated elements are different!
74+
// otherwise the receiver could recover two or more of the sent values
6775

6876
#if DEBUG
6977
stopwatch.Stop();
@@ -175,6 +183,11 @@ public override async Task<byte[][]> ReceiveAsync(IMessageChannel channel, Quadr
175183
return selectedOptions;
176184
}
177185

186+
/// <summary>
187+
/// Returns a random element from the group as well as the corresponding exponent for the group generator.
188+
/// </summary>
189+
/// <param name="exponent">The exponent with which the returned group element can be obtained from the group generator.</param>
190+
/// <returns>A random group element.</returns>
178191
private BigInteger GenerateGroupElement(out BigInteger exponent)
179192
{
180193
do
@@ -186,11 +199,22 @@ private BigInteger GenerateGroupElement(out BigInteger exponent)
186199
return BigInteger.ModPow(_parameters.G, exponent, _parameters.P);
187200
}
188201

202+
/// <summary>
203+
/// Multiplicatively inverts a group element.
204+
/// </summary>
205+
/// <param name="groupElement">The group element to be inverted.</param>
206+
/// <returns>The multiplicative inverse of the argument in the group.</returns>
189207
private BigInteger Invert(BigInteger groupElement)
190208
{
191209
return BigInteger.ModPow(groupElement, _parameters.Q - 1, _parameters.P);
192210
}
193211

212+
/// <summary>
213+
/// Asynchronously writes a list of group elements (BigInteger) to a message channel.
214+
/// </summary>
215+
/// <param name="channel">The network message channel.</param>
216+
/// <param name="groupElements">The list of group elements to write/send.</param>
217+
/// <returns></returns>
194218
private Task WriteGroupElements(IMessageChannel channel, IReadOnlyList<BigInteger> groupElements)
195219
{
196220
MessageComposer message = new MessageComposer(2 * groupElements.Count);
@@ -204,6 +228,12 @@ private Task WriteGroupElements(IMessageChannel channel, IReadOnlyList<BigIntege
204228
return channel.WriteMessageAsync(message.Compose());
205229
}
206230

231+
/// <summary>
232+
/// Asynchronously reads a specified number of group elements from a message channel.
233+
/// </summary>
234+
/// <param name="channel">The network message channel.</param>
235+
/// <param name="numberOfGroupElements">Number of group elements to read/receive.</param>
236+
/// <returns></returns>
207237
private async Task<BigInteger[]> ReadGroupElements(IMessageChannel channel, int numberOfGroupElements)
208238
{
209239
MessageDecomposer message = new MessageDecomposer(await channel.ReadMessageAsync());
@@ -246,10 +276,22 @@ private async Task<Quadruple<byte[]>[]> ReadOptions(IMessageChannel channel, int
246276
return options;
247277
}
248278

249-
private byte[] MaskOption(byte[] message, BigInteger groupElement, int invocationIndex, int optionIndex)
279+
/// <summary>
280+
/// Masks an option (i.e., a sender input message).
281+
/// </summary>
282+
///
283+
/// The option is XOR-masked with the output of a random oracle queried with the
284+
/// concatentation of the binary representations of the given groupElement, invocationIndex and optionIndex.
285+
///
286+
/// <param name="option">The sender input/option to be masked.</param>
287+
/// <param name="groupElement">The group element that contributes receiver choice to the query.</param>
288+
/// <param name="invocationIndex">The index of the OT invocation this options belongs to.</param>
289+
/// <param name="optionIndex">The index of the option.</param>
290+
/// <returns></returns>
291+
private byte[] MaskOption(byte[] option, BigInteger groupElement, int invocationIndex, int optionIndex)
250292
{
251293
byte[] query = BufferBuilder.From(groupElement.ToByteArray()).With(invocationIndex).With(optionIndex).Create();
252-
return _randomOracle.Mask(message, query);
294+
return _randomOracle.Mask(option, query);
253295
}
254296
}
255297
}

CompactMPC/ObliviousTransfer/RandomOracle.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,42 @@
66

77
namespace CompactMPC.ObliviousTransfer
88
{
9+
/// <summary>
10+
/// A random oracle as canonically defined.
11+
/// </summary>
12+
///
13+
/// As per the canonical definition of the random oracle, it produces a random but deterministic output
14+
/// for each unique query it is invoked on, i.e., the output is unpredictably random over different queries
15+
/// but providing the same query will always yield the same output sequence.
16+
///
17+
/// The RandomOracle class also provides a Mask message that masks a given message with the output of the
18+
/// random oracle for a given query. In that case the query can be seen as a shared key that allows for
19+
/// masking and unmasking (encryption/decryption) of a message.
20+
///
21+
/// Naturally, a true random oracle cannot be implemented so the outputs of all implementations of
22+
/// RandomOracle will not be perfectly random but computationally indistinguishable from true randomness
23+
/// by the usual cryptographic standards.
924
public abstract class RandomOracle
1025
{
26+
/// <summary>
27+
/// Supplies the response of the random oracle to the given query.
28+
/// </summary>
29+
///
30+
/// As per the canonical definition of the random oracle, it produces a random but deterministic output
31+
/// for each unique query, i.e., the output is unpredictably random over different queries but
32+
/// providing the same query will always yield the same output sequence.
33+
///
34+
/// <param name="query">The query for the random oracle.</param>
35+
/// <returns></returns>
1136
public abstract IEnumerable<byte> Invoke(byte[] query);
1237

38+
/// <summary>
39+
/// Masks a given message by applying bitwise XOR with the random oracle output stream.
40+
/// </summary>
41+
/// <param name="message">The message to be masked with the random oracle output.</param>
42+
/// <param name="query">The query for the random oracle.</param>
43+
/// <returns>Byte array containing the masked message.</returns>
44+
/// <exception cref="ArgumentException">Thrown when the random oracle does not provide enough data to mask the given message.</exception>
1345
public byte[] Mask(byte[] message, byte[] query)
1446
{
1547
byte[] result = new byte[message.Length];

SampleCircuits/SecureSetIntersection.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99

1010
namespace CompactMPC.SampleCircuits
1111
{
12+
/// <summary>
13+
/// Circuit which computes the intersection of sets given in a binary word representation.
14+
/// </summary>
15+
///
16+
/// In addition to the intersection result, a counter giving the cardinality of the intersection
17+
/// is also calculated.
1218
public class SecureSetIntersection
1319
{
1420
private SecureWord _intersection;
@@ -25,6 +31,9 @@ public SecureSetIntersection(SecureWord[] inputs, int numberOfCounterBits)
2531
_counter = counter.OfFixedLength(numberOfCounterBits);
2632
}
2733

34+
/// <summary>
35+
/// The binary encoding of the intersection set.
36+
/// </summary>
2837
public SecureWord Intersection
2938
{
3039
get
@@ -33,6 +42,9 @@ public SecureWord Intersection
3342
}
3443
}
3544

45+
/// <summary>
46+
/// The number of elements in the intersection set.
47+
/// </summary>
3648
public SecureInteger Counter
3749
{
3850
get

0 commit comments

Comments
 (0)