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

RNS/CRT #403

Draft
wants to merge 38 commits into
base: rns
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
aa07c5e
Fix evaluation of protocols
jonas-lj Oct 20, 2022
e7b6f65
Remove unused method
jonas-lj Oct 20, 2022
b571d99
Fix lifts and truncp
jonas-lj Oct 20, 2022
e06e6e3
Fix random bit
jonas-lj Oct 20, 2022
f5500aa
Add some docs
jonas-lj Oct 20, 2022
4ad1e94
Fix random mod p
jonas-lj Nov 8, 2022
2c2405d
Add dummy random mod p
jonas-lj Nov 9, 2022
83c39cd
Attempt outline of SemiHonestCRTDataSupplier
quackzar Nov 15, 2022
6b7214a
Attempt at preprocessing
jot2re Nov 17, 2022
ca4ee0f
Fix preprocessing
jonas-lj Nov 22, 2022
d1e81b0
Keep noise until the end of the protocol
jonas-lj Nov 22, 2022
2e73a28
Attempt at Semi Honest Noise
quackzar Nov 30, 2022
37fb256
fixup! Attempt at Semi Honest Noise
quackzar Nov 30, 2022
ee07915
Implement Covert Noise Generator
quackzar Jan 10, 2023
b1fff88
Forgot to set tests to use covert, and small fix
quackzar Jan 10, 2023
d958634
Extract CovertNoiseGenerator and use better randomness
quackzar Jan 11, 2023
9d728a6
Refactor CRTDataSupplier(s)
quackzar Jan 11, 2023
b4581f3
Don't reuse seed, reuse the Drbg
quackzar Jan 11, 2023
cbc4765
Remove redundant generics
quackzar Jan 12, 2023
fef58f5
Fix uses of CRTDataSupplier being non-generic
quackzar Mar 22, 2023
96eb827
Only use seed if it was computed in previous step
jonas-lj Mar 22, 2023
99b536b
incremental commit
jot2re May 9, 2023
d383db0
fix: fixed general runtime issue
jot2re May 9, 2023
f15565b
incremental: semihonest working
jot2re May 9, 2023
a58d2eb
refactor: improved code quality of change
jot2re May 9, 2023
f6d2236
fix: ensured correct and simpler openeing
jot2re May 9, 2023
c04e645
feat: started the steps of single bit error.
jot2re May 9, 2023
5bbf85d
refactor: refactored to remove some unused variables and support grea…
jot2re May 18, 2023
7777ce3
feat: added support for batching
jot2re May 19, 2023
a8a0a17
temp: outcommented code to allow for larger domains
jot2re May 19, 2023
61fbe4a
temp: started code to do gate evaluation properly
jot2re May 19, 2023
d3edf74
fix: fixed semi-honest noise generation
jot2re May 22, 2023
3f5c502
test: outcommented tests not working due to missing framework impleme…
jot2re May 26, 2023
a85d14d
refactor: removed personal reference
jot2re May 26, 2023
c58e087
Create bits in parallel
jonas-lj Aug 30, 2023
28c5fc4
Fix bit generation
jonas-lj Sep 5, 2023
759bb0a
refactor: making sure par is used whereever possible
jot2re Sep 13, 2023
dbbedcd
Remove constant
jonas-lj Sep 26, 2023
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
Expand Up @@ -8,7 +8,7 @@

/**
* A single step in the chained list of lambda that creates protocols to evaluate. The root step is
* defined in the ProbtocolBuilderImpl.
* defined in the ProtocolBuilderImpl.
*
* @param <InputT> the type of input for this binary step (the previous step's output).
* @param <BuilderT> the type iof builder, currently either numeric or binary
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ protected ProtocolBuilderImpl(
this.factory = factory;
}

private void createAndAppend(ProtocolProducer producer) {
protected void createAndAppend(ProtocolProducer producer) {
if (protocols == null) {
throw new IllegalStateException("Cannot build this twice, it has all ready been constructed");
}
Expand Down Expand Up @@ -93,4 +93,8 @@ public <R> BuildStep<Void, BuilderT, R> par(ComputationParallel<R, BuilderT> f)
createAndAppend(new LazyProtocolProducerDecorator(() -> builder.createProducer(null, factory)));
return builder;
}

public boolean isParallel() {
return parallel;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class ProtocolBuilderNumeric extends ProtocolBuilderImpl<ProtocolBuilderN
private BasicNumericContext basicNumericContext;
private Numeric numeric;
private PreprocessedValues preprocessedValues;
ProtocolBuilderNumeric(BuilderFactoryNumeric factory, boolean parallel) {
protected ProtocolBuilderNumeric(BuilderFactoryNumeric factory, boolean parallel) {
super(factory, parallel);
this.factory = factory;
this.basicNumericContext = factory.getBasicNumericContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

import dk.alexandra.fresco.framework.Party;
import dk.alexandra.fresco.framework.configuration.NetworkConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ServerSocketFactory;
import javax.net.SocketFactory;
import java.io.IOException;
import java.net.ConnectException;
import java.net.ServerSocket;
Expand All @@ -10,18 +15,7 @@
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import javax.net.ServerSocketFactory;
import javax.net.SocketFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.*;

public class Connector implements NetworkConnector {

Expand Down Expand Up @@ -85,11 +79,12 @@ private Map<Integer, Socket> connectNetwork(final NetworkConfiguration conf,
connectionService.submit(() -> connectServer(conf));
Duration remainingTime = timeout;
try {
Future<Map<Integer, Socket>> completed;

Instant start = Instant.now();
for (int i = 0; i < connectionThreads; i++) {
remainingTime = remainingTime.minus(Duration.between(start, Instant.now()));
Future<Map<Integer, Socket>> completed =
connectionService.poll(remainingTime.toMillis(), TimeUnit.MILLISECONDS);
completed = connectionService.poll(remainingTime.toMillis(), TimeUnit.MILLISECONDS);
if (completed == null) {
throw new TimeoutException("Timed out waiting for client connections");
} else {
Expand Down Expand Up @@ -134,7 +129,7 @@ private Map<Integer, Socket> connectClient(final NetworkConfiguration conf)
} catch (ConnectException e) {
// A connect exception is expected if the opposing side is not listening for our
// connection attempt yet. We ignore this and try again.
Thread.sleep(1 << ++attempts);
Thread.sleep(1L << ++attempts);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,24 @@ public int getNoOfParties() {
public void send(int id, byte[] data) {
ByteArrayOutputStream buffer = this.output
.computeIfAbsent(id, (i) -> new ByteArrayOutputStream());
if (data.length > Byte.MAX_VALUE) {
throw new IllegalStateException(
"Current implementation only supports small packages, data.length=" + data.length);
}
// if (data.length > Byte.MAX_VALUE) {
// byte[] buf = new byte[Byte.MAX_VALUE];
// int offset = 0;
// do {
// System.arraycopy(data, offset, buf, 0, Byte.MAX_VALUE);
// send(id, buf);
// offset += Byte.MAX_VALUE;
// } while (offset + Byte.MAX_VALUE <= data.length);
// if (data.length % Byte.MAX_VALUE != 0) {
// // we need to send a last block
// int length = data.length - offset;
// buf = new byte[length];
// System.arraycopy(data, offset, buf, 0, length);
// send(id, buf);
// }
// throw new IllegalStateException(
// "Current implementation only supports small packages, data.length=" + data.length);
// }
buffer.write(data.length);
buffer.write(data, 0, data.length);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

public class ProtocolCollectionList<ResourcePoolT extends ResourcePool>
implements ProtocolCollection<ResourcePoolT> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,9 @@ public T out() {
}
return result;
}

public NativeProtocol<T, ?> getProtocol() {
return protocol;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ public <ResourcePoolT extends ResourcePool> void runApplication(

network.sendToAll(ByteBuffer.allocate(4).putInt(myArraySize).array());
List<byte[]> received = network.receiveFromAll();
int[] allInputSizes = received.stream().mapToInt(binary -> ByteBuffer.allocate(4).put(binary).rewind().getInt()).toArray();
int[] allInputSizes = received.stream().mapToInt(binary -> binary[3]
| binary[2] << 8
| binary[1] << 16
| binary[0] << 24
).toArray();

if (myId == 1) {
// party input
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dk.alexandra.fresco.framework.DRes;
import dk.alexandra.fresco.framework.builder.Computation;
import dk.alexandra.fresco.framework.util.Pair;
import dk.alexandra.fresco.lib.common.math.AdvancedNumeric;
import dk.alexandra.fresco.framework.builder.numeric.Numeric;
import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric;
Expand All @@ -14,30 +15,25 @@ public class RandomAdditiveMask implements
Computation<AdvancedNumeric.RandomAdditiveMask, ProtocolBuilderNumeric> {

private final int noOfBits;

private List<DRes<SInt>> bits;
private DRes<SInt> value;

public RandomAdditiveMask(int noOfBits) {
this.noOfBits = noOfBits;
}

@Override
public DRes<AdvancedNumeric.RandomAdditiveMask> buildComputation(
ProtocolBuilderNumeric builder) {
Numeric numericBuilder = builder.numeric();
bits = new ArrayList<>();
for (int i = 0; i < noOfBits; i++) {
DRes<SInt> randomBit = numericBuilder.randomBit();
bits.add(randomBit);
}

MiscBigIntegerGenerators oIntGenerators = new MiscBigIntegerGenerators(builder.getBasicNumericContext().getModulus());
AdvancedNumeric advancedNumeric = AdvancedNumeric.using(builder);
List<BigInteger> twoPows = oIntGenerators.getTwoPowersList(noOfBits);
value = advancedNumeric.innerProductWithPublicPart(twoPows, bits);
return () -> new AdvancedNumeric.RandomAdditiveMask(
bits,
value.out());
ProtocolBuilderNumeric builder) {
return builder.par(par -> {
Numeric numericBuilder = par.numeric();
List<DRes<SInt>> bits = new ArrayList<>();
for (int i = 0; i < noOfBits; i++) {
DRes<SInt> randomBit = numericBuilder.randomBit();
bits.add(randomBit);
}
return DRes.of(bits);
}).par((par, bits) -> {
MiscBigIntegerGenerators oIntGenerators = new MiscBigIntegerGenerators(par.getBasicNumericContext().getModulus());
List<BigInteger> twoPows = oIntGenerators.getTwoPowersList(noOfBits);
return Pair.lazy(bits, AdvancedNumeric.using(par).innerProductWithPublicPart(twoPows, bits));
}).par((par, bitsAndValue) -> DRes.of(new AdvancedNumeric.RandomAdditiveMask(bitsAndValue.getFirst(), bitsAndValue.getSecond())));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import dk.alexandra.fresco.framework.value.SInt;
import dk.alexandra.fresco.lib.common.collections.sort.KeyedCompareAndSwap;
import dk.alexandra.fresco.lib.common.compare.MiscBigIntegerGenerators;
import dk.alexandra.fresco.lib.common.compare.RandomAdditiveMask;
import dk.alexandra.fresco.lib.common.math.integer.conditional.ConditionalSelect;
import dk.alexandra.fresco.lib.common.math.integer.conditional.SwapIf;
import dk.alexandra.fresco.lib.common.math.integer.binary.IntegerToBitsLogRounds;
Expand Down Expand Up @@ -178,7 +179,7 @@ public DRes<List<Pair<DRes<SInt>, List<DRes<SInt>>>>> keyedCompareAndSwap(
public DRes<SInt> bitsToInteger(List<DRes<SInt>> bits) {
MiscBigIntegerGenerators oIntGenerators = new MiscBigIntegerGenerators(
builder.getBasicNumericContext().getModulus());
return builder.seq(seq -> AdvancedNumeric.using(seq)
return builder.par(par -> AdvancedNumeric.using(par)
.innerProductWithPublicPart(oIntGenerators.getTwoPowersList(bits.size()), bits));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

import dk.alexandra.fresco.framework.DRes;
import dk.alexandra.fresco.framework.builder.Computation;
import dk.alexandra.fresco.framework.builder.numeric.Numeric;
import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric;
import dk.alexandra.fresco.framework.util.Pair;
import dk.alexandra.fresco.framework.value.SInt;
import dk.alexandra.fresco.lib.common.compare.MiscBigIntegerGenerators;
import dk.alexandra.fresco.lib.common.math.AdvancedNumeric;
import dk.alexandra.fresco.lib.common.math.AdvancedNumeric.RandomAdditiveMask;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
* Returns a number which is approximately the input shifted a number of positions to the right. The
Expand Down Expand Up @@ -37,20 +41,18 @@ public DRes<SInt> buildComputation(ProtocolBuilderNumeric builder) {
if (shifts >= maxBitLength) {
return builder.numeric().known(0);
}

return builder.seq(seq -> {

return builder.par(par -> {
/*
* Generate random additive mask of the same length as the input + some extra to avoid
* leakage.
*/
return AdvancedNumeric.using(seq)
.additiveMask(maxBitLength + builder.getBasicNumericContext().getStatisticalSecurityParam());
return AdvancedNumeric.using(par)
.additiveMask(maxBitLength + builder.getBasicNumericContext().getStatisticalSecurityParam());

}).seq((seq, randomAdditiveMask) -> {
}).par((par, randomAdditiveMask) -> {

DRes<SInt> result = seq.numeric().add(input, randomAdditiveMask.value);
DRes<BigInteger> open = seq.numeric().open(result);
DRes<SInt> result = par.numeric().add(input, randomAdditiveMask.value);
DRes<BigInteger> open = par.numeric().open(result);
return Pair.lazy(open, randomAdditiveMask);

}).seq((seq, maskedInput) -> {
Expand All @@ -62,10 +64,10 @@ public DRes<SInt> buildComputation(ProtocolBuilderNumeric builder) {
* rBottom = r (mod 2^shifts).
*/
final DRes<SInt> rBottom = AdvancedNumeric.using(seq)
.bitsToInteger(mask.bits.subList(0, shifts));
.bitsToInteger(mask.bits.subList(0, shifts));

BigInteger inverse =
BigInteger.ONE.shiftLeft(shifts).modInverse(seq.getBasicNumericContext().getModulus());
BigInteger.ONE.shiftLeft(shifts).modInverse(seq.getBasicNumericContext().getModulus());
DRes<SInt> rTop = seq.numeric().sub(mask.value, rBottom);

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ public DRes<SInt> buildComputation(ProtocolBuilderNumeric builder) {
result.add(numericBuilder.mult(nextA, nextB));
}
return () -> result;
}).seq((seq, list) -> AdvancedNumeric.using(seq).sum(list));
}).par((par, list) -> AdvancedNumeric.using(par).sum(list));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public DefaultAdvancedFixedNumeric(ProtocolBuilderNumeric builder) {

@Override
public DRes<SFixed> random() {
return builder.seq(seq -> {
return builder.par(par -> {
DRes<RandomAdditiveMask> random =
AdvancedNumeric.using(seq).additiveMask(seq.getBasicNumericContext().getDefaultFixedPointPrecision());
AdvancedNumeric.using(par).additiveMask(par.getBasicNumericContext().getDefaultFixedPointPrecision());
return random;
}).seq((seq, random) -> {
return () -> new SFixed(random.value);
Expand Down
57 changes: 57 additions & 0 deletions suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTAdd.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package dk.alexandra.fresco.suite.crt;

import dk.alexandra.fresco.framework.DRes;
import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool;
import dk.alexandra.fresco.framework.network.Network;
import dk.alexandra.fresco.framework.value.SInt;
import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt;
import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTResourcePool;
import dk.alexandra.fresco.suite.crt.protocols.framework.CRTNativeProtocol;
import dk.alexandra.fresco.suite.spdz.SpdzResourcePool;
import dk.alexandra.fresco.suite.spdz.gates.SpdzAddProtocol;
import dk.alexandra.fresco.suite.spdz.gates.SpdzMultProtocol;

public class CRTAdd extends CRTNativeProtocol<SInt, NumericResourcePool, NumericResourcePool> {

private final DRes<SInt> a;
private final DRes<SInt> b;
private CRTSInt out;
private EvaluationStatus status = EvaluationStatus.HAS_MORE_ROUNDS;
private SpdzAddProtocol spdzAddProtocolLeft;
private SpdzAddProtocol spdzAddProtocolRight;

public CRTAdd(DRes<SInt> a, DRes<SInt> b) {
this.a = a;
this.b = b;
}

@Override
public CRTSInt out() {
return out;
}

@Override
public EvaluationStatus evaluate(int round, CRTResourcePool CRTResourcePool,
Network network) {
if (round == 0) {
CRTSInt aOut = (CRTSInt) a.out();
DRes<SInt> aLeft = aOut.getLeft();
DRes<SInt> aRight = aOut.getRight();

CRTSInt bOut = (CRTSInt) b.out();
DRes<SInt> bLeft = bOut.getLeft();
DRes<SInt> bRight = bOut.getRight();

spdzAddProtocolLeft = new SpdzAddProtocol(aLeft, bLeft);
spdzAddProtocolRight = new SpdzAddProtocol(aRight, bRight);
}
spdzAddProtocolLeft.evaluate(round, (SpdzResourcePool) CRTResourcePool.getSubResourcePools().getFirst(), network);
status = spdzAddProtocolRight.evaluate(round, (SpdzResourcePool) CRTResourcePool.getSubResourcePools().getSecond(), network);
if (status == EvaluationStatus.IS_DONE) {
this.out = new CRTSInt(spdzAddProtocolLeft.out(), spdzAddProtocolRight.out());
return EvaluationStatus.IS_DONE;
} else {
return EvaluationStatus.HAS_MORE_ROUNDS;
}
}
}