diff --git a/core/src/main/java/dk/alexandra/fresco/framework/Party.java b/core/src/main/java/dk/alexandra/fresco/framework/Party.java index 3b7ab4985..03268a516 100644 --- a/core/src/main/java/dk/alexandra/fresco/framework/Party.java +++ b/core/src/main/java/dk/alexandra/fresco/framework/Party.java @@ -1,5 +1,8 @@ package dk.alexandra.fresco.framework; +import dk.alexandra.fresco.framework.util.ValidationUtils; +import java.util.Objects; + /** * FRESCO's view of a MPC party. */ @@ -17,8 +20,10 @@ public class Party { * @param port the tcp port to connect on */ public Party(int id, String host, int port) { + ValidationUtils.assertValidId(id); + this.id = id; - this.host = host; + this.host = Objects.requireNonNull(host); this.port = port; } diff --git a/core/src/main/java/dk/alexandra/fresco/framework/configuration/NetworkConfigurationImpl.java b/core/src/main/java/dk/alexandra/fresco/framework/configuration/NetworkConfigurationImpl.java index 147a596e7..536278bde 100644 --- a/core/src/main/java/dk/alexandra/fresco/framework/configuration/NetworkConfigurationImpl.java +++ b/core/src/main/java/dk/alexandra/fresco/framework/configuration/NetworkConfigurationImpl.java @@ -2,6 +2,7 @@ import dk.alexandra.fresco.framework.Party; import dk.alexandra.fresco.framework.util.Pair; +import dk.alexandra.fresco.framework.util.ValidationUtils; import java.util.HashSet; import java.util.Map; import java.util.Objects; @@ -14,14 +15,22 @@ public class NetworkConfigurationImpl implements NetworkConfiguration { private final Map parties; public NetworkConfigurationImpl(int myId, Map parties) { + // Validation Objects.requireNonNull(parties); checkAddressesUnique(parties); + ValidationUtils.assertValidId(myId); + if (parties.get(myId) == null) { + throw new RuntimeException(String.format("myId %d must be in the parties map: %s", myId, parties)); + } + + // Set fields this.myId = myId; this.parties = parties; } @Override public Party getParty(int id) { + ValidationUtils.assertValidId(id); return parties.get(id); } diff --git a/core/src/main/java/dk/alexandra/fresco/framework/network/socket/SocketNetwork.java b/core/src/main/java/dk/alexandra/fresco/framework/network/socket/SocketNetwork.java index 72ce7fc7e..8e2accbc3 100644 --- a/core/src/main/java/dk/alexandra/fresco/framework/network/socket/SocketNetwork.java +++ b/core/src/main/java/dk/alexandra/fresco/framework/network/socket/SocketNetwork.java @@ -3,6 +3,7 @@ import dk.alexandra.fresco.framework.configuration.NetworkConfiguration; import dk.alexandra.fresco.framework.network.CloseableNetwork; import dk.alexandra.fresco.framework.util.ExceptionConverter; +import dk.alexandra.fresco.framework.util.ValidationUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -136,7 +137,7 @@ public SocketNetwork(NetworkConfiguration conf, SocketFactory socketFactory, private void startCommunication(Map sockets) { for (Entry entry : sockets.entrySet()) { final int id = entry.getKey(); - inRange(id); + assertPartyIdInRange(id); Socket socket = entry.getValue(); Receiver receiver = new Receiver(socket); this.receivers.put(id, receiver); @@ -150,7 +151,7 @@ public void send(int partyId, byte[] data) { if (partyId == conf.getMyId()) { this.selfQueue.add(data); } else { - inRange(partyId); + assertPartyIdInRange(partyId); if (!senders.get(partyId).isRunning()) { throw new RuntimeException( "P" + conf.getMyId() + ": Unable to send to P" + partyId + ". Sender not running"); @@ -164,7 +165,7 @@ public byte[] receive(final int partyId) { if (partyId == conf.getMyId()) { return ExceptionConverter.safe(selfQueue::take, "Receiving from self failed"); } - inRange(partyId); + assertPartyIdInRange(partyId); byte[] data; data = receivers.get(partyId).pollMessage(RECEIVE_TIMEOUT); while (data == null) { @@ -182,11 +183,8 @@ public byte[] receive(final int partyId) { * * @param partyId an ID for a party */ - private void inRange(final int partyId) { - if (!(0 < partyId && partyId < getNoOfParties() + 1)) { - throw new IllegalArgumentException( - "Party id " + partyId + " not in range 1 ... " + getNoOfParties()); - } + private void assertPartyIdInRange(final int partyId) { + ValidationUtils.assertValidId(partyId, getNoOfParties()); } /** diff --git a/core/src/main/java/dk/alexandra/fresco/framework/sce/SecureComputationEngineImpl.java b/core/src/main/java/dk/alexandra/fresco/framework/sce/SecureComputationEngineImpl.java index f8232c3b8..9c6e5e610 100644 --- a/core/src/main/java/dk/alexandra/fresco/framework/sce/SecureComputationEngineImpl.java +++ b/core/src/main/java/dk/alexandra/fresco/framework/sce/SecureComputationEngineImpl.java @@ -10,6 +10,7 @@ import dk.alexandra.fresco.framework.sce.resources.ResourcePool; import dk.alexandra.fresco.suite.ProtocolSuite; import java.time.Duration; +import java.util.Objects; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; @@ -39,13 +40,13 @@ public class SecureComputationEngineImpl /** * Creates a new {@link SecureComputationEngineImpl}. * - * @param protocolSuite the {@link ProtocolSuite} to use to evaluate the secure computation - * @param evaluator the {@link ProtocolEvaluator} to run secure evaluation. + * @param protocolSuite {@link ProtocolSuite} to use to evaluate the secure computation. Not nullable. + * @param evaluator {@link ProtocolEvaluator} to run secure evaluation. Not nullable. */ public SecureComputationEngineImpl(ProtocolSuite protocolSuite, ProtocolEvaluator evaluator) { - this.protocolSuite = protocolSuite; - this.evaluator = evaluator; + this.protocolSuite = Objects.requireNonNull(protocolSuite); + this.evaluator = Objects.requireNonNull(evaluator); this.setup = false; } diff --git a/core/src/main/java/dk/alexandra/fresco/framework/sce/evaluator/BatchedProtocolEvaluator.java b/core/src/main/java/dk/alexandra/fresco/framework/sce/evaluator/BatchedProtocolEvaluator.java index 290b4d482..6ac15f3f9 100644 --- a/core/src/main/java/dk/alexandra/fresco/framework/sce/evaluator/BatchedProtocolEvaluator.java +++ b/core/src/main/java/dk/alexandra/fresco/framework/sce/evaluator/BatchedProtocolEvaluator.java @@ -5,6 +5,7 @@ import dk.alexandra.fresco.framework.network.Network; import dk.alexandra.fresco.framework.sce.resources.ResourcePool; import dk.alexandra.fresco.suite.ProtocolSuite; +import java.util.Objects; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -34,9 +35,9 @@ public BatchedProtocolEvaluator( public BatchedProtocolEvaluator( BatchEvaluationStrategy batchEvaluator, ProtocolSuite protocolSuite, int maxBatchSize) { - this.batchEvaluator = batchEvaluator; + this.batchEvaluator = Objects.requireNonNull(batchEvaluator); this.maxBatchSize = maxBatchSize; - this.protocolSuite = protocolSuite; + this.protocolSuite = Objects.requireNonNull(protocolSuite); } @Override diff --git a/core/src/main/java/dk/alexandra/fresco/framework/sce/resources/ResourcePoolImpl.java b/core/src/main/java/dk/alexandra/fresco/framework/sce/resources/ResourcePoolImpl.java index 4f3c51403..0a03941ec 100644 --- a/core/src/main/java/dk/alexandra/fresco/framework/sce/resources/ResourcePoolImpl.java +++ b/core/src/main/java/dk/alexandra/fresco/framework/sce/resources/ResourcePoolImpl.java @@ -1,5 +1,7 @@ package dk.alexandra.fresco.framework.sce.resources; +import dk.alexandra.fresco.framework.util.ValidationUtils; + /** * Container for resources needed by runtimes (protocol suites). */ @@ -12,10 +14,13 @@ public class ResourcePoolImpl implements ResourcePool { * Creates an instance of the default implementation of a resource pool. This contains the basic * resources needed within FRESCO. * - * @param myId The ID of the MPC party. + * @param myId The ID of the MPC party. Must be a valid id, but may lie + * outside of {@code noOfPlayers} as some subclasses create sub-networks, with fewer noOfPlayers. * @param noOfPlayers The amount of parties within the MPC computation. */ public ResourcePoolImpl(int myId, int noOfPlayers) { + ValidationUtils.assertValidId(myId); + ValidationUtils.assertValidId(noOfPlayers); this.myId = myId; this.noOfPlayers = noOfPlayers; } diff --git a/core/src/main/java/dk/alexandra/fresco/framework/util/ArithmeticDummyDataSupplier.java b/core/src/main/java/dk/alexandra/fresco/framework/util/ArithmeticDummyDataSupplier.java index 94e945514..a0df882da 100644 --- a/core/src/main/java/dk/alexandra/fresco/framework/util/ArithmeticDummyDataSupplier.java +++ b/core/src/main/java/dk/alexandra/fresco/framework/util/ArithmeticDummyDataSupplier.java @@ -9,8 +9,9 @@ /** * Supplies generic pre-processed material common across arithmetic SPDZ-like suites, including - * random elements, bits, and multiplication triples.

Uses {@link Random} to deterministically - * generate all material. NOT secure.

+ * random elements, bits, and multiplication triples. + * + *

Uses {@link Random} to deterministically generate all material. This is not secure, and should not be used in production code! */ public class ArithmeticDummyDataSupplier { @@ -21,8 +22,9 @@ public class ArithmeticDummyDataSupplier { private final Random random; private final SecretSharer sharer; private final ModularReductionAlgorithm reducer; - + public ArithmeticDummyDataSupplier(int myId, int noOfParties, BigInteger modulus) { + ValidationUtils.assertValidId(myId, noOfParties); this.myId = myId; this.noOfParties = noOfParties; this.modulus = modulus; diff --git a/core/src/main/java/dk/alexandra/fresco/framework/util/DrngImpl.java b/core/src/main/java/dk/alexandra/fresco/framework/util/DrngImpl.java index e58a6a3f5..f4bb57646 100644 --- a/core/src/main/java/dk/alexandra/fresco/framework/util/DrngImpl.java +++ b/core/src/main/java/dk/alexandra/fresco/framework/util/DrngImpl.java @@ -1,6 +1,7 @@ package dk.alexandra.fresco.framework.util; import java.math.BigInteger; +import java.util.Objects; /** * A simple implementation based on a deterministic bit generator. @@ -14,10 +15,10 @@ public class DrngImpl implements Drng { /** * Creates a number generator from a bit generator. - * @param drbg a deterministic random bit generator + * @param drbg a deterministic random bit generator. Not nullable. */ public DrngImpl(Drbg drbg) { - this.drbg = drbg; + this.drbg = Objects.requireNonNull(drbg); } @Override diff --git a/core/src/main/java/dk/alexandra/fresco/framework/util/ValidationUtils.java b/core/src/main/java/dk/alexandra/fresco/framework/util/ValidationUtils.java new file mode 100644 index 000000000..c226f4c68 --- /dev/null +++ b/core/src/main/java/dk/alexandra/fresco/framework/util/ValidationUtils.java @@ -0,0 +1,34 @@ +package dk.alexandra.fresco.framework.util; + +/** Contains methods for validating ids. */ +public final class ValidationUtils { + + private ValidationUtils() {} + + /** + * Validates that the given party id is within the valid range of ids, without a known max id. + * + * @param partyId Id to validate + * @exception IllegalArgumentException if id is not valid + */ + public static void assertValidId(int partyId) { + if (partyId < 1) { + throw new IllegalArgumentException(String.format("Party id %d must be one-indexed", partyId)); + } + } + + /** + * Validates that the given party id is within the valid range of ids, with a known max id. + * + * @param partyId Id to validate + * @param numParties Max id + * @exception IllegalArgumentException if id is not valid + */ + public static void assertValidId(int partyId, int numParties) { + assertValidId(partyId); + if (numParties < partyId) { + throw new IllegalArgumentException( + String.format("Party id %d must be in range [1,%d]", partyId, numParties)); + } + } +} diff --git a/core/src/main/java/dk/alexandra/fresco/lib/field/integer/BasicNumericContext.java b/core/src/main/java/dk/alexandra/fresco/lib/field/integer/BasicNumericContext.java index ed72ea78c..fdcabcc31 100644 --- a/core/src/main/java/dk/alexandra/fresco/lib/field/integer/BasicNumericContext.java +++ b/core/src/main/java/dk/alexandra/fresco/lib/field/integer/BasicNumericContext.java @@ -1,7 +1,9 @@ package dk.alexandra.fresco.lib.field.integer; import dk.alexandra.fresco.framework.builder.numeric.field.FieldDefinition; +import dk.alexandra.fresco.framework.util.ValidationUtils; import java.math.BigInteger; +import java.util.Objects; /** * Holds the most crucial properties about the finite field we are working within. @@ -24,12 +26,13 @@ public class BasicNumericContext { * have. * @param myId my party id * @param noOfParties number of parties in computation - * @param fieldDefinition the field definition used in the application + * @param fieldDefinition the field definition used in the application. Nullable. * @param defaultFixedPointPrecision the fixed point precision when using the fixed point library * @param statisticalSecurityParam the statistical security parameter */ public BasicNumericContext(int maxBitLength, int myId, int noOfParties, FieldDefinition fieldDefinition, int defaultFixedPointPrecision, int statisticalSecurityParam) { + ValidationUtils.assertValidId(myId, noOfParties); this.maxBitLength = maxBitLength; this.myId = myId; this.noOfParties = noOfParties; diff --git a/core/src/main/java/dk/alexandra/fresco/suite/dummy/arithmetic/DummyArithmeticResourcePoolImpl.java b/core/src/main/java/dk/alexandra/fresco/suite/dummy/arithmetic/DummyArithmeticResourcePoolImpl.java index 489405766..3a8a549eb 100644 --- a/core/src/main/java/dk/alexandra/fresco/suite/dummy/arithmetic/DummyArithmeticResourcePoolImpl.java +++ b/core/src/main/java/dk/alexandra/fresco/suite/dummy/arithmetic/DummyArithmeticResourcePoolImpl.java @@ -3,6 +3,7 @@ import dk.alexandra.fresco.framework.builder.numeric.field.FieldDefinition; import dk.alexandra.fresco.framework.sce.resources.ResourcePool; import dk.alexandra.fresco.framework.sce.resources.ResourcePoolImpl; +import dk.alexandra.fresco.framework.util.ValidationUtils; /** * Implements the resource pool needed for the Dummy Arithmetic suite. @@ -22,6 +23,7 @@ public class DummyArithmeticResourcePoolImpl extends ResourcePoolImpl public DummyArithmeticResourcePoolImpl(int myId, int noOfPlayers, FieldDefinition fieldDefinition) { super(myId, noOfPlayers); + ValidationUtils.assertValidId(myId, noOfPlayers); this.fieldDefinition = fieldDefinition; } diff --git a/core/src/test/java/dk/alexandra/fresco/framework/TestThreadRunner.java b/core/src/test/java/dk/alexandra/fresco/framework/TestThreadRunner.java index 739f291a7..9a2b6061e 100644 --- a/core/src/test/java/dk/alexandra/fresco/framework/TestThreadRunner.java +++ b/core/src/test/java/dk/alexandra/fresco/framework/TestThreadRunner.java @@ -193,15 +193,15 @@ public static openedValueStore, SpdzDataSupplier dataSupplier, Function drbgSupplier, int drbgSeedBitLength) { super(myId, noOfPlayers); - this.dataSupplier = dataSupplier; - this.openedValueStore = openedValueStore; + ValidationUtils.assertValidId(myId, noOfPlayers); + this.dataSupplier = Objects.requireNonNull(dataSupplier); + this.openedValueStore = Objects.requireNonNull(openedValueStore); this.messageDigest = ExceptionConverter.safe( () -> MessageDigest.getInstance("SHA-256"), "Configuration error, SHA-256 is needed for Spdz"); - this.drbgSupplier = drbgSupplier; + this.drbgSupplier = Objects.requireNonNull(drbgSupplier); this.drbgSeedBitLength = drbgSeedBitLength; } diff --git a/suite/spdz/src/main/java/dk/alexandra/fresco/suite/spdz/storage/SpdzMascotDataSupplier.java b/suite/spdz/src/main/java/dk/alexandra/fresco/suite/spdz/storage/SpdzMascotDataSupplier.java index 2cd029de1..7b225dbc8 100644 --- a/suite/spdz/src/main/java/dk/alexandra/fresco/suite/spdz/storage/SpdzMascotDataSupplier.java +++ b/suite/spdz/src/main/java/dk/alexandra/fresco/suite/spdz/storage/SpdzMascotDataSupplier.java @@ -5,6 +5,7 @@ import dk.alexandra.fresco.framework.network.Network; import dk.alexandra.fresco.framework.util.Drbg; import dk.alexandra.fresco.framework.util.StrictBitVector; +import dk.alexandra.fresco.framework.util.ValidationUtils; import dk.alexandra.fresco.suite.spdz.datatypes.SpdzInputMask; import dk.alexandra.fresco.suite.spdz.datatypes.SpdzSInt; import dk.alexandra.fresco.suite.spdz.datatypes.SpdzTriple; @@ -22,6 +23,7 @@ import java.util.ArrayDeque; import java.util.HashMap; import java.util.Map; +import java.util.Objects; import java.util.function.Function; import java.util.function.Supplier; import org.slf4j.Logger; @@ -61,7 +63,7 @@ public class SpdzMascotDataSupplier implements SpdzDataSupplier { * @param tripleNetwork network supplier for network to be used by Mascot instance * @param fieldDefinition field definition * @param modBitLength bit length of modulus - * @param preprocessedValues callback to generate exponentiation pipes + * @param preprocessedValues callback to generate exponentiation pipes. Nullable. * @param prgSeedLength bit length of prg * @param batchSize batch size in which Mascot will generate pre-processed material * @param ssk mac key share @@ -72,12 +74,13 @@ public SpdzMascotDataSupplier(int myId, int numberOfPlayers, int instanceId, Supplier tripleNetwork, FieldDefinition fieldDefinition, int modBitLength, Function preprocessedValues, int prgSeedLength, int batchSize, FieldElement ssk, Map seedOts, Drbg drbg) { + ValidationUtils.assertValidId(myId, numberOfPlayers); this.myId = myId; this.numberOfPlayers = numberOfPlayers; this.instanceId = instanceId; - this.tripleNetwork = tripleNetwork; - this.fieldDefinition = fieldDefinition; - this.preprocessedValues = preprocessedValues; + this.tripleNetwork = Objects.requireNonNull(tripleNetwork); + this.fieldDefinition = Objects.requireNonNull(fieldDefinition); + this.preprocessedValues = preprocessedValues; // Allow null. this.triples = new ArrayDeque<>(); this.masks = new HashMap<>(); for (int partyId = 1; partyId <= numberOfPlayers; partyId++) { @@ -88,9 +91,9 @@ public SpdzMascotDataSupplier(int myId, int numberOfPlayers, int instanceId, this.prgSeedLength = prgSeedLength; this.modBitLength = modBitLength; this.batchSize = batchSize; - this.ssk = ssk; - this.seedOts = seedOts; - this.drbg = drbg; + this.ssk = Objects.requireNonNull(ssk); + this.seedOts = Objects.requireNonNull(seedOts); + this.drbg = Objects.requireNonNull(drbg); } /** diff --git a/suite/spdz2k/src/main/java/dk/alexandra/fresco/suite/spdz2k/resource/Spdz2kResourcePoolImpl.java b/suite/spdz2k/src/main/java/dk/alexandra/fresco/suite/spdz2k/resource/Spdz2kResourcePoolImpl.java index 627ec4868..610e718fd 100644 --- a/suite/spdz2k/src/main/java/dk/alexandra/fresco/suite/spdz2k/resource/Spdz2kResourcePoolImpl.java +++ b/suite/spdz2k/src/main/java/dk/alexandra/fresco/suite/spdz2k/resource/Spdz2kResourcePoolImpl.java @@ -18,6 +18,7 @@ import dk.alexandra.fresco.framework.util.Drbg; import dk.alexandra.fresco.framework.util.ExceptionConverter; import dk.alexandra.fresco.framework.util.OpenedValueStore; +import dk.alexandra.fresco.framework.util.ValidationUtils; import dk.alexandra.fresco.lib.field.integer.BasicNumericContext; import dk.alexandra.fresco.suite.spdz2k.Spdz2kBuilder; import dk.alexandra.fresco.suite.spdz2k.datatypes.CompUInt; @@ -54,6 +55,7 @@ public Spdz2kResourcePoolImpl(int myId, int noOfPlayers, Drbg drbg, OpenedValueStore, PlainT> storage, Spdz2kDataSupplier supplier, CompUIntFactory factory) { super(myId, noOfPlayers); + ValidationUtils.assertValidId(myId, noOfPlayers); Objects.requireNonNull(storage); Objects.requireNonNull(supplier); Objects.requireNonNull(factory); diff --git a/tools/bitTriples/src/main/java/dk/alexandra/fresco/tools/bitTriples/BitTripleResourcePoolImpl.java b/tools/bitTriples/src/main/java/dk/alexandra/fresco/tools/bitTriples/BitTripleResourcePoolImpl.java index e5a84892f..55950611b 100644 --- a/tools/bitTriples/src/main/java/dk/alexandra/fresco/tools/bitTriples/BitTripleResourcePoolImpl.java +++ b/tools/bitTriples/src/main/java/dk/alexandra/fresco/tools/bitTriples/BitTripleResourcePoolImpl.java @@ -5,6 +5,7 @@ import dk.alexandra.fresco.framework.util.Drbg; import dk.alexandra.fresco.framework.util.ExceptionConverter; import dk.alexandra.fresco.framework.util.StrictBitVector; +import dk.alexandra.fresco.framework.util.ValidationUtils; import dk.alexandra.fresco.tools.bitTriples.prg.BytePrg; import dk.alexandra.fresco.tools.bitTriples.prg.BytePrgImpl; import dk.alexandra.fresco.tools.cointossing.CoinTossing; @@ -42,6 +43,7 @@ public BitTripleResourcePoolImpl( Drbg drbg, BitTripleSecurityParameters bitTripleSecurityParameters) { super(myId, noOfParties); + ValidationUtils.assertValidId(myId, noOfParties); this.drbg = drbg; this.instanceId = instanceId; this.bitTripleSecurityParameters = bitTripleSecurityParameters; diff --git a/tools/mascot/src/main/java/dk/alexandra/fresco/tools/mascot/MascotResourcePoolImpl.java b/tools/mascot/src/main/java/dk/alexandra/fresco/tools/mascot/MascotResourcePoolImpl.java index 0233fd4c3..cf242c239 100644 --- a/tools/mascot/src/main/java/dk/alexandra/fresco/tools/mascot/MascotResourcePoolImpl.java +++ b/tools/mascot/src/main/java/dk/alexandra/fresco/tools/mascot/MascotResourcePoolImpl.java @@ -6,6 +6,7 @@ import dk.alexandra.fresco.framework.util.Drbg; import dk.alexandra.fresco.framework.util.ExceptionConverter; import dk.alexandra.fresco.framework.util.StrictBitVector; +import dk.alexandra.fresco.framework.util.ValidationUtils; import dk.alexandra.fresco.tools.cointossing.CoinTossing; import dk.alexandra.fresco.tools.mascot.prg.FieldElementPrg; import dk.alexandra.fresco.tools.mascot.prg.FieldElementPrgImpl; @@ -14,6 +15,7 @@ import java.security.MessageDigest; import java.util.Map; +import java.util.Objects; public class MascotResourcePoolImpl extends ResourcePoolImpl implements MascotResourcePool { @@ -43,11 +45,12 @@ public MascotResourcePoolImpl(int myId, int noOfParties, int instanceId, Drbg dr Map seedOts, MascotSecurityParameters mascotSecurityParameters, FieldDefinition fieldDefinition) { super(myId, noOfParties); - this.drbg = drbg; + ValidationUtils.assertValidId(myId, noOfParties); + this.drbg = Objects.requireNonNull(drbg); this.instanceId = instanceId; - this.seedOts = seedOts; - this.fieldDefinition = fieldDefinition; - this.mascotSecurityParameters = mascotSecurityParameters; + this.seedOts = Objects.requireNonNull(seedOts); + this.fieldDefinition = Objects.requireNonNull(fieldDefinition); + this.mascotSecurityParameters = Objects.requireNonNull(mascotSecurityParameters); this.localSampler = new FieldElementPrgImpl( new StrictBitVector(mascotSecurityParameters.getPrgSeedLength(), drbg), this.fieldDefinition); diff --git a/tools/mascot/src/main/java/dk/alexandra/fresco/tools/mascot/commit/CommitmentBasedInput.java b/tools/mascot/src/main/java/dk/alexandra/fresco/tools/mascot/commit/CommitmentBasedInput.java index a67887219..47e2b22b4 100644 --- a/tools/mascot/src/main/java/dk/alexandra/fresco/tools/mascot/commit/CommitmentBasedInput.java +++ b/tools/mascot/src/main/java/dk/alexandra/fresco/tools/mascot/commit/CommitmentBasedInput.java @@ -8,6 +8,7 @@ import dk.alexandra.fresco.tools.mascot.broadcast.BroadcastingNetworkProxy; import java.util.ArrayList; import java.util.List; +import java.util.Objects; import java.util.stream.Collectors; /** @@ -30,9 +31,9 @@ public abstract class CommitmentBasedInput { */ public CommitmentBasedInput(MascotResourcePool resourcePool, Network network, ByteSerializer serializer) { - this.resourcePool = resourcePool; - this.network = network; - this.serializer = serializer; + this.resourcePool = Objects.requireNonNull(resourcePool); + this.network = Objects.requireNonNull(network); + this.serializer = Objects.requireNonNull(serializer); // for more than two parties, we need to use broadcast if (resourcePool.getNoOfParties() > 2) { this.broadcaster = diff --git a/tools/mascot/src/main/java/dk/alexandra/fresco/tools/mascot/cope/CopeInputter.java b/tools/mascot/src/main/java/dk/alexandra/fresco/tools/mascot/cope/CopeInputter.java index 838567731..907bc44da 100644 --- a/tools/mascot/src/main/java/dk/alexandra/fresco/tools/mascot/cope/CopeInputter.java +++ b/tools/mascot/src/main/java/dk/alexandra/fresco/tools/mascot/cope/CopeInputter.java @@ -5,12 +5,14 @@ import dk.alexandra.fresco.framework.network.Network; import dk.alexandra.fresco.framework.util.Pair; import dk.alexandra.fresco.framework.util.StrictBitVector; +import dk.alexandra.fresco.framework.util.ValidationUtils; import dk.alexandra.fresco.tools.mascot.MascotResourcePool; import dk.alexandra.fresco.tools.mascot.mult.MultiplyRightHelper; import dk.alexandra.fresco.tools.mascot.prg.FieldElementPrg; import dk.alexandra.fresco.tools.mascot.prg.FieldElementPrgImpl; import java.util.ArrayList; import java.util.List; +import java.util.Objects; import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.stream.Stream; @@ -41,9 +43,10 @@ public class CopeInputter { * seeds used in the Extend sub-protocol.

*/ public CopeInputter(MascotResourcePool resourcePool, Network network, int otherId) { + ValidationUtils.assertValidId(otherId); this.otherId = otherId; - this.resourcePool = resourcePool; - this.network = network; + this.resourcePool = Objects.requireNonNull(resourcePool); + this.network = Objects.requireNonNull(network); this.leftPrgs = new ArrayList<>(); this.rightPrgs = new ArrayList<>(); this.helper = new MultiplyRightHelper(resourcePool, network, otherId); diff --git a/tools/mascot/src/main/java/dk/alexandra/fresco/tools/mascot/elgen/AdditiveSecretSharer.java b/tools/mascot/src/main/java/dk/alexandra/fresco/tools/mascot/elgen/AdditiveSecretSharer.java index 601162380..d212bcac7 100644 --- a/tools/mascot/src/main/java/dk/alexandra/fresco/tools/mascot/elgen/AdditiveSecretSharer.java +++ b/tools/mascot/src/main/java/dk/alexandra/fresco/tools/mascot/elgen/AdditiveSecretSharer.java @@ -5,6 +5,7 @@ import dk.alexandra.fresco.framework.util.SecretSharer; import dk.alexandra.fresco.tools.mascot.prg.FieldElementPrg; import java.util.List; +import java.util.Objects; public class AdditiveSecretSharer implements SecretSharer { @@ -16,7 +17,7 @@ public class AdditiveSecretSharer implements SecretSharer { * @param sampler source of randomness */ AdditiveSecretSharer(FieldElementPrg sampler) { - this.sampler = sampler; + this.sampler = Objects.requireNonNull(sampler); } @Override diff --git a/tools/mascot/src/main/java/dk/alexandra/fresco/tools/mascot/elgen/ElementGeneration.java b/tools/mascot/src/main/java/dk/alexandra/fresco/tools/mascot/elgen/ElementGeneration.java index f26ea58f6..215a7a987 100644 --- a/tools/mascot/src/main/java/dk/alexandra/fresco/tools/mascot/elgen/ElementGeneration.java +++ b/tools/mascot/src/main/java/dk/alexandra/fresco/tools/mascot/elgen/ElementGeneration.java @@ -16,6 +16,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -44,13 +45,13 @@ public class ElementGeneration { */ public ElementGeneration(MascotResourcePool resourcePool, Network network, FieldElement macKeyShare, FieldElementPrg jointSampler) { - this.resourcePool = resourcePool; - this.network = network; + this.resourcePool = Objects.requireNonNull(resourcePool); + this.network = Objects.requireNonNull(network); this.fieldElementUtils = new FieldElementUtils(resourcePool.getFieldDefinition()); this.macChecker = new MacCheck(resourcePool, network); - this.macKeyShare = macKeyShare; - this.localSampler = resourcePool.getLocalSampler(); - this.jointSampler = jointSampler; + this.macKeyShare = Objects.requireNonNull(macKeyShare); + this.localSampler = Objects.requireNonNull(resourcePool.getLocalSampler()); + this.jointSampler = Objects.requireNonNull(jointSampler); this.sharer = new AdditiveSecretSharer(localSampler); this.copeSigners = new HashMap<>(); this.copeInputters = new HashMap<>(); diff --git a/tools/mascot/src/main/java/dk/alexandra/fresco/tools/mascot/prg/FieldElementPrgImpl.java b/tools/mascot/src/main/java/dk/alexandra/fresco/tools/mascot/prg/FieldElementPrgImpl.java index 97deb4001..8d9df92c4 100644 --- a/tools/mascot/src/main/java/dk/alexandra/fresco/tools/mascot/prg/FieldElementPrgImpl.java +++ b/tools/mascot/src/main/java/dk/alexandra/fresco/tools/mascot/prg/FieldElementPrgImpl.java @@ -7,6 +7,7 @@ import dk.alexandra.fresco.framework.util.Drng; import dk.alexandra.fresco.framework.util.DrngImpl; import dk.alexandra.fresco.framework.util.StrictBitVector; +import java.util.Objects; public class FieldElementPrgImpl implements FieldElementPrg { @@ -19,7 +20,7 @@ public class FieldElementPrgImpl implements FieldElementPrg { * @param seed seed to the underlying DRNG. */ public FieldElementPrgImpl(StrictBitVector seed, FieldDefinition definition) { - this.definition = definition; + this.definition = Objects.requireNonNull(definition); byte[] bytes = seed.toByteArray(); if (bytes.length != AesCtrDrbg.SEED_LENGTH) { this.drng = new DrngImpl(AesCtrDrbgFactory.fromDerivedSeed(bytes)); diff --git a/tools/mascot/src/main/java/dk/alexandra/fresco/tools/mascot/triple/TripleGeneration.java b/tools/mascot/src/main/java/dk/alexandra/fresco/tools/mascot/triple/TripleGeneration.java index 97a97dbd8..1b1b2198e 100644 --- a/tools/mascot/src/main/java/dk/alexandra/fresco/tools/mascot/triple/TripleGeneration.java +++ b/tools/mascot/src/main/java/dk/alexandra/fresco/tools/mascot/triple/TripleGeneration.java @@ -13,6 +13,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.stream.Stream; @@ -39,13 +40,13 @@ public class TripleGeneration { */ public TripleGeneration(MascotResourcePool resourcePool, Network network, ElementGeneration elementGeneration, FieldElementPrg jointSampler) { - this.resourcePool = resourcePool; + this.resourcePool = Objects.requireNonNull(resourcePool); this.fieldElementUtils = new FieldElementUtils(resourcePool.getFieldDefinition()); this.leftMultipliers = new HashMap<>(); this.rightMultipliers = new HashMap<>(); initializeMultipliers(resourcePool, network); - this.elementGeneration = elementGeneration; - this.jointSampler = jointSampler; + this.elementGeneration = Objects.requireNonNull(elementGeneration); + this.jointSampler = Objects.requireNonNull(jointSampler); } private void initializeMultipliers(MascotResourcePool resourcePool, Network network) { diff --git a/tools/mascot/src/test/java/dk/alexandra/fresco/tools/mascot/TestMascotResourcePoolImpl.java b/tools/mascot/src/test/java/dk/alexandra/fresco/tools/mascot/TestMascotResourcePoolImpl.java index 683bad215..08c241c5c 100644 --- a/tools/mascot/src/test/java/dk/alexandra/fresco/tools/mascot/TestMascotResourcePoolImpl.java +++ b/tools/mascot/src/test/java/dk/alexandra/fresco/tools/mascot/TestMascotResourcePoolImpl.java @@ -3,6 +3,7 @@ import dk.alexandra.fresco.framework.builder.numeric.field.BigIntegerFieldDefinition; import dk.alexandra.fresco.framework.util.AesCtrDrbg; import dk.alexandra.fresco.framework.util.ModulusFinder; +import java.util.Map; import org.junit.Test; public class TestMascotResourcePoolImpl { @@ -11,7 +12,7 @@ public class TestMascotResourcePoolImpl { @Test(expected = IllegalArgumentException.class) public void testCreateRotForSelf() { MascotResourcePool resourcePool = new MascotResourcePoolImpl(1, 1, 1, - new AesCtrDrbg(new byte[32]), null, new MascotSecurityParameters(), + new AesCtrDrbg(new byte[32]), Map.of(), new MascotSecurityParameters(), new BigIntegerFieldDefinition(ModulusFinder.findSuitableModulus(128))); resourcePool.createRot(1, null); } diff --git a/tools/mascot/src/test/java/dk/alexandra/fresco/tools/mascot/TestRuntime.java b/tools/mascot/src/test/java/dk/alexandra/fresco/tools/mascot/TestRuntime.java index e6bced97e..39806c53a 100644 --- a/tools/mascot/src/test/java/dk/alexandra/fresco/tools/mascot/TestRuntime.java +++ b/tools/mascot/src/test/java/dk/alexandra/fresco/tools/mascot/TestRuntime.java @@ -4,6 +4,7 @@ import dk.alexandra.fresco.framework.util.ExceptionConverter; import dk.alexandra.fresco.framework.util.Pair; import java.io.Closeable; +import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedList; import java.util.List; @@ -77,8 +78,41 @@ private void initializeExecutor(int noOfParties) { private List safeInvokeAll(List> tasks) { Callable>> runAll = () -> executor.invokeAll(tasks, timeout, TimeUnit.SECONDS); List> futures = ExceptionConverter.safe(runAll, "Invoke all failed"); - return futures.stream().map(future -> ExceptionConverter.safe(future::get, "Party task failed")) - .collect(Collectors.toList()); + + return collectFromFutures(futures); + } + + /** + * Utility for collecting all values from the given {link Future}s. + * + *

Will fail if any of the {@link Future}s fails, and show exceptions from + * all failed {@link Future}s. This is useful for cases where code running in + * {@link Future}s communicates with each other, as the first failing {@link + * Future} may not contain the root cause of the issue. + * + * @param futures Futures to collect from. + * @return Values of futures. + * @exception RuntimeException Raised if any future fails for any reason: + * cancellation or otherwise. Contains all failing exceptions as supressed. + */ + private static List collectFromFutures(Iterable> futures) { + final List collectedResults = new ArrayList<>(); + final List exceptions = new ArrayList<>(); + for (final Future future : futures) { + try { + collectedResults.add(future.get()); + } catch (Exception e) { + exceptions.add(e); + } + } + + if (!exceptions.isEmpty()) { + final RuntimeException err = new RuntimeException(String.format("Failures in %d futures", exceptions.size())); + exceptions.forEach(err::addSuppressed); + throw err; + } + + return collectedResults; } /** diff --git a/tools/mascot/src/test/java/dk/alexandra/fresco/tools/mascot/triple/TestTripleGeneration.java b/tools/mascot/src/test/java/dk/alexandra/fresco/tools/mascot/triple/TestTripleGeneration.java index 13749308e..82cdcdbe3 100644 --- a/tools/mascot/src/test/java/dk/alexandra/fresco/tools/mascot/triple/TestTripleGeneration.java +++ b/tools/mascot/src/test/java/dk/alexandra/fresco/tools/mascot/triple/TestTripleGeneration.java @@ -154,13 +154,14 @@ public void testTwoPartiesBatchedMult() { private void testMultiplePartiesTriple(List macKeyShares, int numTriples) { // set up runtime environment and get contexts - initContexts(macKeyShares.size()); + final int noOfParties = macKeyShares.size(); + initContexts(noOfParties); // define per party task with params List>> tasks = new ArrayList<>(); - for (int pid = 1; pid <= macKeyShares.size(); pid++) { - MascotTestContext partyCtx = contexts.get(pid); - FieldElement macKeyShare = macKeyShares.get(pid - 1); + for (int partyId = 1; partyId <= noOfParties; partyId++) { + MascotTestContext partyCtx = contexts.get(partyId); + FieldElement macKeyShare = macKeyShares.get(partyId - 1); Callable> partyTask = () -> runSinglePartyTriple(partyCtx, macKeyShare, numTriples); tasks.add(partyTask); diff --git a/tools/ot/src/main/java/dk/alexandra/fresco/tools/ot/otextension/BristolOtExtensionResourcePool.java b/tools/ot/src/main/java/dk/alexandra/fresco/tools/ot/otextension/BristolOtExtensionResourcePool.java index e0abdb280..dbc20b1b8 100644 --- a/tools/ot/src/main/java/dk/alexandra/fresco/tools/ot/otextension/BristolOtExtensionResourcePool.java +++ b/tools/ot/src/main/java/dk/alexandra/fresco/tools/ot/otextension/BristolOtExtensionResourcePool.java @@ -3,6 +3,7 @@ import dk.alexandra.fresco.framework.sce.resources.ResourcePoolImpl; import dk.alexandra.fresco.framework.util.Drbg; import dk.alexandra.fresco.framework.util.ExceptionConverter; +import dk.alexandra.fresco.framework.util.ValidationUtils; import dk.alexandra.fresco.tools.cointossing.CoinTossing; import java.security.MessageDigest; @@ -35,6 +36,7 @@ public BristolOtExtensionResourcePool(int myId, int otherId, int computationalSecurityParam, int lambdaSecurityParam, int instanceId, Drbg drbg, CoinTossing ct, RotList seedOts) { super(myId, 2); + ValidationUtils.assertValidId(otherId); if (computationalSecurityParam < 1 || lambdaSecurityParam < 1 || lambdaSecurityParam % 8 != 0 || computationalSecurityParam % 8 != 0) { @@ -57,12 +59,6 @@ public BristolOtExtensionResourcePool(int myId, int otherId, this.seedOts = seedOts; } - @Override - public int getNoOfParties() { - // By definition OT is a two-party protocol - return 2; - } - @Override public int getComputationalSecurityParameter() { return computationalSecurityParam; diff --git a/tools/ot/src/main/java/dk/alexandra/fresco/tools/ot/otextension/RotFactory.java b/tools/ot/src/main/java/dk/alexandra/fresco/tools/ot/otextension/RotFactory.java index 85c5322f7..eb9e546a0 100644 --- a/tools/ot/src/main/java/dk/alexandra/fresco/tools/ot/otextension/RotFactory.java +++ b/tools/ot/src/main/java/dk/alexandra/fresco/tools/ot/otextension/RotFactory.java @@ -1,11 +1,12 @@ package dk.alexandra.fresco.tools.ot.otextension; import dk.alexandra.fresco.framework.network.Network; +import java.util.Objects; /** * Abstract factory for a protocol instance of random OT extension. */ -public class RotFactory { +public final class RotFactory { private final OtExtensionResourcePool resources; private final Network network; @@ -17,8 +18,8 @@ public class RotFactory { * @param network The network instance */ public RotFactory(OtExtensionResourcePool resources, Network network) { - this.resources = resources; - this.network = network; + this.resources = Objects.requireNonNull(resources); + this.network = Objects.requireNonNull(network); } public OtExtensionResourcePool getResources() {