Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

324 mac check spdz #343

Merged
merged 28 commits into from
Jun 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
a05c828
Do coin tossing in SPDZ mac check
n1v0lg Dec 10, 2018
aff65f4
Moving generic broadcast etc into core
n1v0lg Apr 23, 2019
6d78043
Fix seed length
n1v0lg Apr 24, 2019
0129537
Compatible maliciousmaccheck
n1v0lg Apr 24, 2019
6522466
SPDZ res. pool now supplies drbgs
n1v0lg Apr 25, 2019
6023277
Move broadcast test to core
n1v0lg Jun 4, 2019
fa0f98a
Move commitment test
n1v0lg Jun 4, 2019
6b16c16
Add drbg seed length to resource pool
n1v0lg Jun 4, 2019
0afa163
Malicious commitment tests
n1v0lg Jun 6, 2019
d487c9a
Test all parties cheating during commitment
n1v0lg Jun 6, 2019
15190f6
Plug generic commitment comp into spdz mac check
n1v0lg Jun 6, 2019
9bde357
Remove deprecated spdz commitment functionality
n1v0lg Jun 6, 2019
7ff4203
Clean up spdz mac check
n1v0lg Jun 6, 2019
2d2544c
Update malicious spdz mac check in tests
n1v0lg Jun 6, 2019
60d97e0
Kill unused malicious test classes
n1v0lg Jun 6, 2019
ae1fab1
Test for tampering with delta
n1v0lg Jun 6, 2019
0cedc1e
Test coin tossing
n1v0lg Jun 6, 2019
1ae7151
Test default constructor for coin tossing
n1v0lg Jun 6, 2019
156fa7b
Merge branch 'master' into 324-mac-check-spdz
n1v0lg Jun 6, 2019
532f83e
Broadcast code coverage
n1v0lg Jun 7, 2019
a5553b4
Remove unused method in spdz native prot.
n1v0lg Jun 7, 2019
b1576bd
Kill unused SpdzCommitment
n1v0lg Jun 7, 2019
5f9eb04
Addressing review comments
n1v0lg Jun 13, 2019
626fbfc
Remove unused noOfParties
n1v0lg Jun 17, 2019
13ba572
Fix line alignment in spdz mac check
n1v0lg Jun 18, 2019
f146673
Kill validMOD_P
n1v0lg Jun 18, 2019
e2c8cf9
Document broadcast computation
n1v0lg Jun 18, 2019
5cf79a1
Nit
n1v0lg Jun 18, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package dk.alexandra.fresco.lib.generic;

import dk.alexandra.fresco.framework.DRes;
import dk.alexandra.fresco.framework.builder.Computation;
import dk.alexandra.fresco.framework.builder.ProtocolBuilderImpl;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
* Generic active broadcast validation computation.
*
* <p>Allows each party to securely broadcast a list of messages and receive the other parties'
* messages.</p>
*
* <p>Uses generic native protocols {@link InsecureBroadcastProtocol} and {@link
* BroadcastValidationProtocol}.</p>
*/
public class BroadcastComputation<BuilderT extends ProtocolBuilderImpl<BuilderT>> implements
Computation<List<byte[]>, BuilderT> {

private final List<byte[]> input;
private final boolean runValidation;

/**
* Creates new {@link BroadcastComputation}.
*
* @param input list of messages this party will broadcast
* @param runValidation indicates if messages received from other parties need to be validated for
* consistency (this can be false for instance when there are only two parties participating in
* the broadcast)
*/
public BroadcastComputation(List<byte[]> input, boolean runValidation) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is reasonable to document this class and the constructor - especially when it it resonable to use one or the other

this.input = input;
this.runValidation = runValidation;
}

/**
* Default constructor to {@link #BroadcastComputation(List, boolean)} for when a party only
* broadcasts a single message.
*/
public BroadcastComputation(byte[] input, boolean runValidation) {
this(Collections.singletonList(input), runValidation);
}

/**
* Default constructor to {@link #BroadcastComputation(byte[], boolean)} for when a party only
* broadcasts a single message and validation is required.
*/
public BroadcastComputation(byte[] input) {
this(input, true);
}

@Override
public DRes<List<byte[]>> buildComputation(BuilderT builder) {
return builder.par(par -> {
List<DRes<List<byte[]>>> broadcastValues = new ArrayList<>();
for (byte[] singleInput : input) {
broadcastValues.add(par.append(new InsecureBroadcastProtocol<>(singleInput)));
}
return () -> broadcastValues;
}).seq((seq, lst) -> {
List<byte[]> toValidate = lst.stream()
.flatMap(broadcast -> broadcast.out().stream())
.collect(Collectors.toList());
if (runValidation) {
seq.append(new BroadcastValidationProtocol<>(toValidate));
}
return () -> toValidate;
});
}

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package dk.alexandra.fresco.suite.spdz2k.protocols.natives;
package dk.alexandra.fresco.lib.generic;

import dk.alexandra.fresco.framework.NativeProtocol;
import dk.alexandra.fresco.framework.network.Network;
import dk.alexandra.fresco.suite.spdz2k.resource.SecureBroadcastUtil;
import dk.alexandra.fresco.framework.sce.resources.ResourcePool;
import java.util.Collections;
import java.util.List;

/**
* Generic native protocol implementing validation of previously received broadcast. <p>Used as a
* building block in {@link dk.alexandra.fresco.suite.spdz2k.protocols.computations.BroadcastComputation}.</p>
* building block in {@link BroadcastComputation}.</p>
*/
public class BroadcastValidationProtocol<ResourcePoolT extends ResourcePool> implements
NativeProtocol<Void, ResourcePoolT> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dk.alexandra.fresco.suite.spdz2k.protocols.computations;
package dk.alexandra.fresco.lib.generic;

import dk.alexandra.fresco.commitment.HashBasedCommitment;
import dk.alexandra.fresco.framework.DRes;
Expand All @@ -16,26 +16,24 @@ public class CoinTossingComputation implements Computation<byte[], ProtocolBuild

private final ByteSerializer<HashBasedCommitment> serializer;
private final byte[] ownSeed;
private final int noOfParties;
private final Drbg localDrbg;


public CoinTossingComputation(byte[] ownSeed, ByteSerializer<HashBasedCommitment> serializer,
int noOfParties, Drbg localDrbg) {
Drbg localDrbg) {
this.serializer = serializer;
this.ownSeed = ownSeed;
this.noOfParties = noOfParties;
this.localDrbg = localDrbg;
}

public CoinTossingComputation(int seedLength, ByteSerializer<HashBasedCommitment> serializer,
int noOfParties, Drbg localDrbg) {
this(generateSeed(seedLength), serializer, noOfParties, localDrbg);
Drbg localDrbg) {
this(generateSeed(seedLength), serializer, localDrbg);
}

@Override
public DRes<byte[]> buildComputation(ProtocolBuilderNumeric builder) {
return builder.seq(new Spdz2kCommitmentComputation(serializer, ownSeed, noOfParties, localDrbg))
return builder.seq(new CommitmentComputation(serializer, ownSeed, localDrbg))
.seq((seq, seeds) -> {
byte[] jointSeed = new byte[ownSeed.length];
for (byte[] seed : seeds) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +1,61 @@
package dk.alexandra.fresco.suite.spdz2k.protocols.computations;
package dk.alexandra.fresco.lib.generic;

import dk.alexandra.fresco.commitment.HashBasedCommitment;
import dk.alexandra.fresco.framework.DRes;
import dk.alexandra.fresco.framework.builder.Computation;
import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric;
import dk.alexandra.fresco.framework.network.serializers.ByteSerializer;
import dk.alexandra.fresco.framework.util.Drbg;
import dk.alexandra.fresco.suite.spdz2k.protocols.natives.InsecureBroadcastProtocol;
import dk.alexandra.fresco.framework.util.Pair;
import java.util.ArrayList;
import java.util.List;

/**
* Protocol for all parties to commit to a value and open it to the other parties.
* Protocol for all parties to commit to a value each and open it to the other parties.
*/
public class Spdz2kCommitmentComputation implements
public class CommitmentComputation implements
Computation<List<byte[]>, ProtocolBuilderNumeric> {

private final ByteSerializer<HashBasedCommitment> commitmentSerializer;
private final byte[] value;
private final int noOfParties;
private final Drbg localDrbg;

public Spdz2kCommitmentComputation(ByteSerializer<HashBasedCommitment> commitmentSerializer,
byte[] value, int noOfParties, Drbg localDrbg) {
public CommitmentComputation(ByteSerializer<HashBasedCommitment> commitmentSerializer,
byte[] value, Drbg localDrbg) {
this.commitmentSerializer = commitmentSerializer;
this.value = value;
this.noOfParties = noOfParties;
this.localDrbg = localDrbg;
}

@Override
public DRes<List<byte[]>> buildComputation(ProtocolBuilderNumeric builder) {
HashBasedCommitment ownCommitment = new HashBasedCommitment();
byte[] ownOpening = ownCommitment.commit(localDrbg, value);
return builder.seq(new BroadcastComputation<>(commitmentSerializer.serialize(ownCommitment)))
final int noOfParties = builder.getBasicNumericContext().getNoOfParties();
return builder.seq(
seq -> {
if (noOfParties > 2) {
n1v0lg marked this conversation as resolved.
Show resolved Hide resolved
return new BroadcastComputation<ProtocolBuilderNumeric>(
commitmentSerializer.serialize(ownCommitment))
.buildComputation(seq);
} else {
// when there are only two parties, parties can't send mutually inconsistent messages
// so no extra validation is necessary
return seq.append(new InsecureBroadcastProtocol<>(
commitmentSerializer.serialize(ownCommitment)));
}
})
.seq((seq, rawCommitments) -> {
DRes<List<byte[]>> openingsDRes = seq.append(new InsecureBroadcastProtocol<>(ownOpening));
DRes<List<byte[]>> res = seq.append(new InsecureBroadcastProtocol<>(ownOpening));
final Pair<DRes<List<byte[]>>, List<byte[]>> dResListPair = new Pair<>(res,
rawCommitments);
return () -> dResListPair;
})
.seq((seq, pair) -> {
List<HashBasedCommitment> commitments = commitmentSerializer
.deserializeList(rawCommitments);
return () -> open(commitments, openingsDRes.out(), noOfParties);
.deserializeList(pair.getSecond());
List<byte[]> opened = open(commitments, pair.getFirst().out(), noOfParties);
return () -> opened;
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dk.alexandra.fresco.suite.spdz2k.protocols.natives;
package dk.alexandra.fresco.lib.generic;

import dk.alexandra.fresco.framework.NativeProtocol;
import dk.alexandra.fresco.framework.network.Network;
Expand All @@ -7,7 +7,7 @@

/**
* Generic native protocol implementing insecure broadcast. <p>Used as a building block in {@link
* dk.alexandra.fresco.suite.spdz2k.protocols.computations.BroadcastComputation}.</p>
* BroadcastComputation}.</p>
*/
public class InsecureBroadcastProtocol<ResourcePoolT extends ResourcePool> implements
NativeProtocol<List<byte[]>, ResourcePoolT> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dk.alexandra.fresco.suite.spdz2k.resource;
package dk.alexandra.fresco.lib.generic;

import dk.alexandra.fresco.framework.MaliciousException;
import dk.alexandra.fresco.framework.network.Network;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;

import dk.alexandra.fresco.framework.MaliciousException;
Expand All @@ -11,23 +12,25 @@
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.junit.Before;
import org.junit.Test;

public class TestCommitment {

HashBasedCommitment comm;
Drbg rand;

@Before
public void setup() {
rand = AesCtrDrbgFactory.fromDerivedSeed((byte)0x42);
rand = AesCtrDrbgFactory.fromDerivedSeed((byte) 0x42);
comm = new HashBasedCommitment();
}

/**** POSITIVE TESTS. ****/
@Test
public void testHonestExecution() {
byte[] msg = { (byte) 0x12, (byte) 0x42 };
byte[] msg = {(byte) 0x12, (byte) 0x42};
byte[] openInfo = comm.commit(rand, msg);
byte[] res = comm.open(openInfo);
assertArrayEquals(res, msg);
Expand All @@ -43,7 +46,7 @@ public void testEmptyMessage() {

@Test
public void testSerialization() {
byte[] msg1 = new byte[] { 0x42 };
byte[] msg1 = new byte[]{0x42};
comm.commit(rand, msg1);
HashBasedCommitmentSerializer serializer = new HashBasedCommitmentSerializer();
byte[] serializedComm = serializer.serialize(comm);
Expand All @@ -54,10 +57,10 @@ public void testSerialization() {

@Test
public void testListSerialization() {
byte[] msg1 = new byte[] { 0x42 };
byte[] msg1 = new byte[]{0x42};
HashBasedCommitment comm1 = new HashBasedCommitment();
comm1.commit(rand, msg1);
byte[] msg2 = new byte[] { 0x56 };
byte[] msg2 = new byte[]{0x56};
HashBasedCommitment comm2 = new HashBasedCommitment();
comm2.commit(rand, msg2);
List<HashBasedCommitment> list = new ArrayList<>(2);
Expand Down Expand Up @@ -90,13 +93,13 @@ public void testIllegalInit() {
boolean thrown = false;
try {
// Randomness generator must not be null
byte[] val = new byte[] { 0x01 };
byte[] val = new byte[]{0x01};
comm = new HashBasedCommitment();
comm.commit(null, val);
} catch (NullPointerException e) {
thrown = true;
}
assertEquals(thrown, true);
assertTrue(thrown);
}

@Test
Expand All @@ -111,7 +114,7 @@ public void testAlreadyCommitted() {
assertEquals("Already committed", e.getMessage());
thrown = true;
}
assertEquals(true, thrown);
assertTrue(thrown);
// Check we can still open correctly
String res = new String(comm.open(openInfo));
assertEquals(firstMsg, res);
Expand All @@ -127,7 +130,7 @@ public void testNoCommitmentMade() {
assertEquals("No commitment to open", e.getMessage());
thrown = true;
}
assertEquals(true, thrown);
assertTrue(thrown);
}

@Test
Expand All @@ -143,7 +146,7 @@ public void testTooSmallOpening() {
e.getMessage());
thrown = true;
}
assertEquals(true, thrown);
assertTrue(thrown);
}

@Test
Expand All @@ -160,7 +163,7 @@ public void testBadOpening() {
e.getMessage());
thrown = true;
}
assertEquals(true, thrown);
assertTrue(thrown);
thrown = false;
try {
// Try to open using the opening info of another commitment
Expand All @@ -173,15 +176,34 @@ public void testBadOpening() {
"The opening info does not match the commitment.", e.getMessage());
thrown = true;
}
assertEquals(true, thrown);
assertTrue(thrown);
}

@Test
public void testSingleBitDiffBadOpening() {
Random random = new Random(42);
byte[] bytes = new byte[32];
random.nextBytes(bytes);
byte[] opening = comm.commit(rand, bytes);
boolean thrown = false;
try {
// flip bit
opening[1] = (byte) (opening[1] ^ 1);
comm.open(opening);
} catch (MaliciousException e) {
assertEquals("The opening info does not match the commitment.",
e.getMessage());
thrown = true;
}
assertTrue(thrown);
}

@SuppressWarnings("unlikely-arg-type")
@Test
public void testNotEqual() {
comm.commit(rand, new byte[] { 0x42 });
assertFalse(comm.equals(new HashBasedCommitment()));
assertFalse(comm.equals("something"));
comm.commit(rand, new byte[]{0x42});
assertNotEquals(comm, new HashBasedCommitment());
assertNotEquals("something", comm);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package dk.alexandra.fresco.lib.generic;

import org.junit.Test;

public class BroadcastValidationProtocolTest {

@Test(expected = IllegalStateException.class)
public void testOutThrows() {
new BroadcastValidationProtocol(new byte[1]).out();
}
}