From aa07c5e2b11e6a2bdfee313a106d697dee93f942 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Lindstr=C3=B8m?= Date: Thu, 20 Oct 2022 13:02:50 +0200 Subject: [PATCH 01/38] Fix evaluation of protocols --- .../builder/ProtocolBuilderImpl.java | 2 +- .../numeric/ProtocolBuilderNumeric.java | 2 +- .../framework/network/socket/Connector.java | 5 +- .../sce/evaluator/ProtocolCollectionList.java | 8 ++ .../lib/helper/SingleProtocolProducer.java | 5 + .../fresco/suite/crt/CRTBuilderFactory.java | 126 +++++++++++------- .../fresco/suite/crt/CRTNumericContext.java | 35 ++--- .../fresco/suite/crt/CRTProtocolSuite.java | 12 +- .../suite/crt/protocols/LiftPQProtocol.java | 16 +-- .../suite/crt/protocols/LiftQPProtocol.java | 10 +- .../suite/crt/protocols/Projection.java | 2 +- .../suite/crt/protocols/RandomModP.java | 3 +- .../fresco/suite/crt/protocols/Truncp.java | 3 +- .../framework/CRTAbstractProtocol.java | 1 + .../protocols/framework/CRTComputation.java | 5 +- .../framework/CRTNativeProtocol.java | 1 + .../framework/CRTSequentialStrategy.java | 28 ++++ .../framework/NativeProtocolWrapper.java | 31 +++++ .../ProtocolBuilderNumericWrapper.java | 27 ++++ .../crt/suites/DummyProtocolSupplier.java | 98 -------------- .../suites/ProtocolSuiteProtocolSupplier.java | 38 ------ .../crt/suites/SpdzProtocolSupplier.java | 102 -------------- .../suite/crt/AbstractDummyCRTTest.java | 25 ++-- .../fresco/suite/crt/AbstractSpdzCRTTest.java | 35 ++--- .../alexandra/fresco/suite/crt/TestCRT.java | 6 + 25 files changed, 256 insertions(+), 370 deletions(-) create mode 100644 suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/framework/CRTSequentialStrategy.java create mode 100644 suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/framework/NativeProtocolWrapper.java create mode 100644 suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/framework/ProtocolBuilderNumericWrapper.java delete mode 100644 suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/suites/DummyProtocolSupplier.java delete mode 100644 suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/suites/ProtocolSuiteProtocolSupplier.java delete mode 100644 suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/suites/SpdzProtocolSupplier.java diff --git a/core/src/main/java/dk/alexandra/fresco/framework/builder/ProtocolBuilderImpl.java b/core/src/main/java/dk/alexandra/fresco/framework/builder/ProtocolBuilderImpl.java index e54012916..2af9e483e 100644 --- a/core/src/main/java/dk/alexandra/fresco/framework/builder/ProtocolBuilderImpl.java +++ b/core/src/main/java/dk/alexandra/fresco/framework/builder/ProtocolBuilderImpl.java @@ -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"); } diff --git a/core/src/main/java/dk/alexandra/fresco/framework/builder/numeric/ProtocolBuilderNumeric.java b/core/src/main/java/dk/alexandra/fresco/framework/builder/numeric/ProtocolBuilderNumeric.java index db4c5aa8f..a4a2bebf1 100644 --- a/core/src/main/java/dk/alexandra/fresco/framework/builder/numeric/ProtocolBuilderNumeric.java +++ b/core/src/main/java/dk/alexandra/fresco/framework/builder/numeric/ProtocolBuilderNumeric.java @@ -14,7 +14,7 @@ public class ProtocolBuilderNumeric extends ProtocolBuilderImpl connectNetwork(final NetworkConfiguration conf, connectionService.submit(() -> connectServer(conf)); Duration remainingTime = timeout; try { + Future> completed; + Instant start = Instant.now(); for (int i = 0; i < connectionThreads; i++) { remainingTime = remainingTime.minus(Duration.between(start, Instant.now())); - Future> 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 { diff --git a/core/src/main/java/dk/alexandra/fresco/framework/sce/evaluator/ProtocolCollectionList.java b/core/src/main/java/dk/alexandra/fresco/framework/sce/evaluator/ProtocolCollectionList.java index 79d4c4632..a3e7a988c 100644 --- a/core/src/main/java/dk/alexandra/fresco/framework/sce/evaluator/ProtocolCollectionList.java +++ b/core/src/main/java/dk/alexandra/fresco/framework/sce/evaluator/ProtocolCollectionList.java @@ -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 implements ProtocolCollection { @@ -36,4 +38,10 @@ public Iterator> iterator() { public int size() { return protocols.size(); } + + public ProtocolCollectionList map(Function, NativeProtocol> map) { + ProtocolCollectionList out = new ProtocolCollectionList(capacity); + out.protocols.addAll(protocols.stream().map(map).collect(Collectors.toList())); + return out; + } } diff --git a/core/src/main/java/dk/alexandra/fresco/lib/helper/SingleProtocolProducer.java b/core/src/main/java/dk/alexandra/fresco/lib/helper/SingleProtocolProducer.java index 34b542ca6..cb180a179 100644 --- a/core/src/main/java/dk/alexandra/fresco/lib/helper/SingleProtocolProducer.java +++ b/core/src/main/java/dk/alexandra/fresco/lib/helper/SingleProtocolProducer.java @@ -48,4 +48,9 @@ public T out() { } return result; } + + public NativeProtocol getProtocol() { + return protocol; + } + } diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTBuilderFactory.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTBuilderFactory.java index 8865e8611..13a03fe2e 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTBuilderFactory.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTBuilderFactory.java @@ -7,26 +7,25 @@ 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.math.AdvancedNumeric; import dk.alexandra.fresco.lib.field.integer.BasicNumericContext; import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; -import dk.alexandra.fresco.suite.crt.protocols.CRTSIntProtocol; -import dk.alexandra.fresco.suite.crt.protocols.framework.CRTBigIntegerProtocol; -import dk.alexandra.fresco.suite.crt.suites.ProtocolSuiteProtocolSupplier; +import dk.alexandra.fresco.suite.crt.protocols.framework.ProtocolBuilderNumericWrapper; + import java.math.BigInteger; public class CRTBuilderFactory implements BuilderFactoryNumeric { - private final BasicNumericContext context; - private final ProtocolSuiteProtocolSupplier left; - private final ProtocolSuiteProtocolSupplier right; + private final CRTNumericContext context; + private final BuilderFactoryNumeric left; + private final BuilderFactoryNumeric right; private final BigInteger p, q; + private final ResourcePoolA resourcePoolLeft; + private final ResourcePoolB resourcePoolRight; public CRTBuilderFactory(ResourcePoolA resourcePoolLeft, - ProtocolSuiteProtocolSupplier leftPspp, - ResourcePoolB resourcePoolRight, - ProtocolSuiteProtocolSupplier rightPspp) { + BuilderFactoryNumeric left, + ResourcePoolB resourcePoolRight, BuilderFactoryNumeric right) { if (resourcePoolLeft.getMyId() != resourcePoolRight.getMyId() || resourcePoolLeft.getNoOfParties() != resourcePoolRight.getNoOfParties()) { @@ -34,8 +33,10 @@ public CRTBuilderFactory(ResourcePoolA resourcePoolLeft, "The protocol suites used must be configured with the same ID and number of players"); } - this.left = leftPspp; - this.right = rightPspp; + this.left = left; + this.resourcePoolLeft = resourcePoolLeft; + this.right = right; + this.resourcePoolRight = resourcePoolRight; this.p = resourcePoolLeft.getModulus(); this.q = resourcePoolRight.getModulus(); this.context = new CRTNumericContext( @@ -65,9 +66,12 @@ public DRes add(DRes a, DRes b) { DRes bLeft = bOut.getLeft(); DRes bRight = bOut.getRight(); - return par.append(new CRTSIntProtocol<>( - left.add(aLeft, bLeft), - right.add(aRight, bRight))); + Numeric l = left.createNumeric(new ProtocolBuilderNumericWrapper<>(par, left, resourcePoolLeft)); + Numeric r = right.createNumeric(new ProtocolBuilderNumericWrapper<>(par, right, resourcePoolRight)); + + return new CRTSInt( + l.add(aLeft, bLeft), + r.add(aRight, bRight)); }); } @@ -92,8 +96,12 @@ public DRes sub(DRes a, DRes b) { DRes bLeft = bOut.getLeft(); DRes bRight = bOut.getRight(); - return par.append(new CRTSIntProtocol<>( - left.sub(aLeft, bLeft), right.sub(aRight, bRight))); + Numeric l = left.createNumeric(new ProtocolBuilderNumericWrapper<>(par, left, resourcePoolLeft)); + Numeric r = right.createNumeric(new ProtocolBuilderNumericWrapper<>(par, right, resourcePoolRight)); + + return new CRTSInt( + l.sub(aLeft, bLeft), + r.sub(aRight, bRight)); }); } @@ -126,17 +134,30 @@ public DRes mult(DRes a, DRes b) { DRes bLeft = bOut.getLeft(); DRes bRight = bOut.getRight(); - return par.append(new CRTSIntProtocol<>( - left.mult(aLeft, bLeft), right.mult(aRight, bRight))); + Numeric l = left.createNumeric(new ProtocolBuilderNumericWrapper<>(par, left, resourcePoolLeft)); + Numeric r = right.createNumeric(new ProtocolBuilderNumericWrapper<>(par, right, resourcePoolRight)); + + return new CRTSInt( + l.mult(aLeft, bLeft), + r.mult(aRight, bRight)); }); } @Override public DRes mult(BigInteger a, DRes b) { - return builder.seq(seq -> { - Numeric numeric = createNumeric(seq); - DRes aSecret = numeric.known(a); - return numeric.mult(aSecret, b); + return builder.par(par -> { + Pair aRNS = Util.mapToCRT(a, p, q); + + CRTSInt bOut = (CRTSInt) b.out(); + DRes bLeft = bOut.getLeft(); + DRes bRight = bOut.getRight(); + + Numeric l = left.createNumeric(new ProtocolBuilderNumericWrapper<>(par, left, resourcePoolLeft)); + Numeric r = right.createNumeric(new ProtocolBuilderNumericWrapper<>(par, right, resourcePoolRight)); + + return new CRTSInt( + l.mult(aRNS.getFirst(), bLeft), + r.mult(aRNS.getSecond(), bRight)); }); } @@ -147,25 +168,41 @@ public DRes randomBit() { @Override public DRes randomElement() { - return builder.par(par -> par.append(new CRTSIntProtocol<>( - left.randomElement(), right.randomElement()))); + + return builder.par(par -> { + Numeric l = left.createNumeric(new ProtocolBuilderNumericWrapper<>(par, left, resourcePoolLeft)); + Numeric r = right.createNumeric(new ProtocolBuilderNumericWrapper<>(par, right, resourcePoolRight)); + return new CRTSInt(l.randomElement(), + r.randomElement()); + }); } @Override public DRes known(BigInteger value) { + Pair crt = Util.mapToCRT(value, p, q); + return builder.par(par -> { - Pair crt = mapToCRT(value); - return par.append(new CRTSIntProtocol<>( - left.known(crt.getFirst()), right.known(crt.getSecond()))); + Numeric l = left.createNumeric(new ProtocolBuilderNumericWrapper<>(par, left, resourcePoolLeft)); + Numeric r = right.createNumeric(new ProtocolBuilderNumericWrapper<>(par, right, resourcePoolRight)); + return new CRTSInt(l.known(crt.getFirst()), r.known(crt.getSecond())); }); } @Override public DRes input(BigInteger value, int inputParty) { + Pair crt; + if (value != null) { + crt = Util.mapToCRT(value, p, q); + } else { + crt = new Pair<>(null, null); + } + return builder.par(par -> { - Pair crt = mapToCRT(value); - return par.append(new CRTSIntProtocol<>( - left.input(crt.getFirst(), inputParty), right.input(crt.getSecond(), inputParty))); + Numeric l = left.createNumeric(new ProtocolBuilderNumericWrapper<>(par, left, resourcePoolLeft)); + Numeric r = right.createNumeric(new ProtocolBuilderNumericWrapper<>(par, right, resourcePoolRight)); + + return new CRTSInt(l.input(crt.getFirst(), inputParty), + r.input(crt.getSecond(), inputParty)); }); } @@ -173,28 +210,27 @@ public DRes input(BigInteger value, int inputParty) { public DRes open(DRes secretShare) { return builder.par(par -> { CRTSInt crtsInt = (CRTSInt) secretShare.out(); - return par.append(new CRTBigIntegerProtocol<>( - left.open(crtsInt.getLeft()), - right.open(crtsInt.getRight()), - p, q)); - }); + + Numeric l = left.createNumeric(new ProtocolBuilderNumericWrapper<>(par, left, resourcePoolLeft)); + Numeric r = right.createNumeric(new ProtocolBuilderNumericWrapper<>(par, right, resourcePoolRight)); + + return Pair.lazy(l.open(crtsInt.getLeft()), r.open(crtsInt.getRight())); + }).seq((seq, opened) -> DRes.of(Util.mapToBigInteger(opened.getFirst().out(), opened.getSecond().out(), p, q))); } @Override public DRes open(DRes secretShare, int outputParty) { return builder.par(par -> { CRTSInt crtsInt = (CRTSInt) secretShare.out(); - return par.append(new CRTBigIntegerProtocol<>( - left.open(crtsInt.getLeft(), outputParty), - right.open(crtsInt.getRight(), outputParty), - p, q)); - }); + + Numeric l = left.createNumeric(new ProtocolBuilderNumericWrapper<>(par, left, resourcePoolLeft)); + Numeric r = right.createNumeric(new ProtocolBuilderNumericWrapper<>(par, right, resourcePoolRight)); + + return Pair.lazy(l.open(crtsInt.getLeft(), outputParty), r.open(crtsInt.getRight(), outputParty)); + }).seq((seq, opened) -> DRes.of(opened.getFirst() == null ? null : Util.mapToBigInteger(opened.getFirst().out(), opened.getSecond().out(), p, q))); + } }; } - public Pair mapToCRT(BigInteger x) { - return Util.mapToCRT(x, p, q); - } - } diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTNumericContext.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTNumericContext.java index ef25dbe4a..2c4a41a42 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTNumericContext.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTNumericContext.java @@ -1,16 +1,15 @@ package dk.alexandra.fresco.suite.crt; -import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool; -import dk.alexandra.fresco.framework.util.Pair; +import dk.alexandra.fresco.framework.builder.numeric.BuilderFactoryNumeric; import dk.alexandra.fresco.lib.field.integer.BasicNumericContext; -import dk.alexandra.fresco.suite.crt.suites.ProtocolSuiteProtocolSupplier; + import java.math.BigInteger; -public class CRTNumericContext extends BasicNumericContext { +public class CRTNumericContext extends BasicNumericContext { private final BigInteger p, q; - private final ProtocolSuiteProtocolSupplier leftProtocolSupplier; - private final ProtocolSuiteProtocolSupplier rightProtocolSupplier; + private final BuilderFactoryNumeric left; + private final BuilderFactoryNumeric right; /** * Construct a new BasicNumericContext. @@ -20,13 +19,13 @@ public class CRTNumericContext left, ProtocolSuiteProtocolSupplier right, - BigInteger p, BigInteger q) { + BuilderFactoryNumeric left, BuilderFactoryNumeric right, + BigInteger p, BigInteger q) { super(maxBitLength, myId, noOfParties, new CRTRingDefinition(p, q), p.bitLength()); this.p = p; this.q = q; - this.leftProtocolSupplier = left; - this.rightProtocolSupplier = right; + this.left = left; + this.right = right; } /** Get the modulus of the left ring in the RNS representation. */ @@ -39,14 +38,18 @@ public BigInteger getRightModulus() { return q; } - /** Get a {@link ProtocolSuiteProtocolSupplier} for the MPC system on the left ring. */ - public ProtocolSuiteProtocolSupplier getLeftProtocolSupplier() { - return leftProtocolSupplier; + /** + * Get a {@link ProtocolSuiteProtocolSupplier} for the MPC system on the left ring. + */ + public BuilderFactoryNumeric getLeft() { + return left; } - /** Get a {@link ProtocolSuiteProtocolSupplier} for the MPC system on the right ring. */ - public ProtocolSuiteProtocolSupplier getRightProtocolSupplier() { - return rightProtocolSupplier; + /** + * Get a {@link ProtocolSuiteProtocolSupplier} for the MPC system on the right ring. + */ + public BuilderFactoryNumeric getRight() { + return right; } diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTProtocolSuite.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTProtocolSuite.java index 159f8c24d..e894dded8 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTProtocolSuite.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTProtocolSuite.java @@ -7,16 +7,15 @@ import dk.alexandra.fresco.framework.util.Pair; import dk.alexandra.fresco.suite.ProtocolSuiteNumeric; import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTResourcePool; -import dk.alexandra.fresco.suite.crt.suites.ProtocolSuiteProtocolSupplier; public class CRTProtocolSuite implements ProtocolSuiteNumeric> { - private final ProtocolSuiteProtocolSupplier left; - private final ProtocolSuiteProtocolSupplier right; + private final BuilderFactoryNumeric left; + private final BuilderFactoryNumeric right; - public CRTProtocolSuite(ProtocolSuiteProtocolSupplier left, - ProtocolSuiteProtocolSupplier right) { + public CRTProtocolSuite(BuilderFactoryNumeric left, + BuilderFactoryNumeric right) { this.left = left; this.right = right; } @@ -34,18 +33,15 @@ public RoundSynchronization> createRoundSynchronization() @Override public void beforeBatch(ProtocolCollection> nativeProtocols, CRTResourcePool resourcePool, Network network) { - } @Override public void finishedBatch(int gatesEvaluated, CRTResourcePool resourcePool, Network network) { - } @Override public void finishedEval(CRTResourcePool resourcePool, Network network) { - } }; } diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftPQProtocol.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftPQProtocol.java index 953335437..e062c5b2b 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftPQProtocol.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftPQProtocol.java @@ -23,22 +23,16 @@ public LiftPQProtocol(DRes value) { @Override public DRes buildComputation(ProtocolBuilderNumeric builder, - CRTNumericContext context) { + CRTNumericContext context) { return builder.seq(seq -> seq.append(new CorrelatedNoiseProtocol<>())).seq((seq, noise) -> { this.r = (CRTSInt) noise.out(); - DRes xBar; - if (value.out() instanceof CRTSInt) { - xBar = seq.append(context.getLeftProtocolSupplier().add(((CRTSInt) value.out()).getLeft(), r.getLeft())); - } else { - xBar = seq.append(context.getLeftProtocolSupplier().add(value, r.getLeft())); - } - return seq.append(context.getLeftProtocolSupplier().open(xBar)); + DRes xBar = context.getLeft().createNumeric(seq).add(value, r.getLeft()); + return context.getLeft().createNumeric(seq).open(xBar); }).seq((seq, xBarOpen) -> { - DRes xBarRight = context.getRightProtocolSupplier().known(xBarOpen); - DRes xRight = context.getRightProtocolSupplier().sub(xBarRight, r.getRight()); - return new CRTSInt(null, xRight); + DRes xBarRight = context.getRight().createNumeric(seq).known(xBarOpen); + return context.getRight().createNumeric(seq).sub(xBarRight, r.getRight()); }); } diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftQPProtocol.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftQPProtocol.java index b597e0b44..052c3ef9a 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftQPProtocol.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftQPProtocol.java @@ -23,14 +23,14 @@ public LiftQPProtocol(DRes value) { @Override public DRes buildComputation(ProtocolBuilderNumeric builder, - CRTNumericContext context) { + CRTNumericContext context) { return builder.seq(seq -> new CorrelatedNoiseProtocol<>()).seq((seq, noise) -> { this.r = (CRTSInt) noise.out(); - DRes xBar = seq.append(context.getRightProtocolSupplier().add(value, r.getRight())); - return seq.append(context.getRightProtocolSupplier().open(xBar)); + DRes xBar = context.getRight().createNumeric(seq).add(value, r.getRight()); + return context.getRight().createNumeric(seq).open(xBar); }).seq((seq, xBarOpen) -> { - DRes xBarRight = context.getLeftProtocolSupplier().known(xBarOpen); - return context.getLeftProtocolSupplier().sub(xBarRight, r.getLeft()); + DRes xBarRight = context.getLeft().createNumeric(seq).known(xBarOpen); + return context.getLeft().createNumeric(seq).sub(xBarRight, r.getLeft()); }); } diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/Projection.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/Projection.java index 5baf6e773..0d20d8709 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/Projection.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/Projection.java @@ -23,7 +23,7 @@ public Projection(DRes crt, Coordinate coordinate) { @Override public DRes buildComputation(ProtocolBuilderNumeric builder, - CRTNumericContext context) { + CRTNumericContext context) { BigInteger coefficient; if (coordinate == Coordinate.LEFT) { coefficient = context.getRightModulus().multiply(context.getRightModulus().modInverse(context.getLeftModulus())); diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/RandomModP.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/RandomModP.java index 5ad095bf1..a0df1a4ba 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/RandomModP.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/RandomModP.java @@ -5,7 +5,6 @@ import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; import dk.alexandra.fresco.framework.value.SInt; import dk.alexandra.fresco.suite.crt.CRTNumericContext; -import dk.alexandra.fresco.suite.crt.CRTRingDefinition; import dk.alexandra.fresco.suite.crt.protocols.Projection.Coordinate; import dk.alexandra.fresco.suite.crt.protocols.framework.CRTComputation; @@ -14,7 +13,7 @@ public class RandomModP buildComputation(ProtocolBuilderNumeric builder, - CRTNumericContext context) { + CRTNumericContext context) { return builder.seq(seq -> seq.numeric().randomElement()) .pairInPar( (seq, r) -> seq.seq(new Projection<>(r, Coordinate.LEFT)), diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/Truncp.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/Truncp.java index 8bd38d7ff..cbd58fffc 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/Truncp.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/Truncp.java @@ -6,7 +6,6 @@ import dk.alexandra.fresco.framework.util.Pair; import dk.alexandra.fresco.framework.value.SInt; import dk.alexandra.fresco.suite.crt.CRTNumericContext; -import dk.alexandra.fresco.suite.crt.CRTRingDefinition; import dk.alexandra.fresco.suite.crt.protocols.Projection.Coordinate; import dk.alexandra.fresco.suite.crt.protocols.framework.CRTComputation; import java.math.BigInteger; @@ -22,7 +21,7 @@ public Truncp(DRes value) { @Override public DRes buildComputation(ProtocolBuilderNumeric builder, - CRTNumericContext context) { + CRTNumericContext context) { // The multiplicative inverse of p mod q BigInteger n2 = context.getLeftModulus().modInverse(context.getRightModulus()); diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/framework/CRTAbstractProtocol.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/framework/CRTAbstractProtocol.java index bbbbae0a2..9784b200a 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/framework/CRTAbstractProtocol.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/framework/CRTAbstractProtocol.java @@ -3,6 +3,7 @@ import dk.alexandra.fresco.framework.NativeProtocol; import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool; import dk.alexandra.fresco.framework.network.Network; +import dk.alexandra.fresco.framework.util.Pair; import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTResourcePool; import java.util.function.BiFunction; diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/framework/CRTComputation.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/framework/CRTComputation.java index ea495d61c..3e79a9fc1 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/framework/CRTComputation.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/framework/CRTComputation.java @@ -5,18 +5,17 @@ import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool; import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; import dk.alexandra.fresco.suite.crt.CRTNumericContext; -import dk.alexandra.fresco.suite.crt.CRTRingDefinition; public abstract class CRTComputation implements Computation { - public abstract DRes buildComputation(ProtocolBuilderNumeric builder, CRTNumericContext context); + public abstract DRes buildComputation(ProtocolBuilderNumeric builder, CRTNumericContext context); @Override public DRes buildComputation(ProtocolBuilderNumeric builder) { return buildComputation(builder, - (CRTNumericContext) builder.getBasicNumericContext()); + (CRTNumericContext) builder.getBasicNumericContext()); } } diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/framework/CRTNativeProtocol.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/framework/CRTNativeProtocol.java index f77681257..3e687fe76 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/framework/CRTNativeProtocol.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/framework/CRTNativeProtocol.java @@ -2,6 +2,7 @@ import dk.alexandra.fresco.framework.NativeProtocol; import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool; +import dk.alexandra.fresco.framework.util.Pair; import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTResourcePool; public abstract class CRTNativeProtocol implements + BatchEvaluationStrategy> { + + @Override + public void processBatch(ProtocolCollection> protocols, + CRTResourcePool resourcePool, + NetworkBatchDecorator network) { + for (NativeProtocol> protocol : protocols) { + int round = 0; + NativeProtocol.EvaluationStatus status; + do { + status = protocol.evaluate(round, resourcePool, network); + // send phase + network.flush(); + round++; + } while (status.equals(NativeProtocol.EvaluationStatus.HAS_MORE_ROUNDS)); + } + } +} \ No newline at end of file diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/framework/NativeProtocolWrapper.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/framework/NativeProtocolWrapper.java new file mode 100644 index 000000000..92504ce81 --- /dev/null +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/framework/NativeProtocolWrapper.java @@ -0,0 +1,31 @@ +package dk.alexandra.fresco.suite.crt.protocols.framework; + +import dk.alexandra.fresco.framework.NativeProtocol; +import dk.alexandra.fresco.framework.network.Network; +import dk.alexandra.fresco.framework.sce.resources.ResourcePool; + +public class NativeProtocolWrapper implements NativeProtocol { + + private final NativeProtocol protocol; + + public ResourcePoolT getResourcePool() { + return resourcePool; + } + + private final ResourcePoolT resourcePool; + + public NativeProtocolWrapper(NativeProtocol protocol, ResourcePoolT resourcePool) { + this.protocol = protocol; + this.resourcePool = resourcePool; + } + + @Override + public OutputT out() { + return protocol.out(); + } + + @Override + public EvaluationStatus evaluate(int round, ResourcePoolT resourcePool, Network network) { + return protocol.evaluate(round, this.resourcePool, network); + } +} diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/framework/ProtocolBuilderNumericWrapper.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/framework/ProtocolBuilderNumericWrapper.java new file mode 100644 index 000000000..498eca1fa --- /dev/null +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/framework/ProtocolBuilderNumericWrapper.java @@ -0,0 +1,27 @@ +package dk.alexandra.fresco.suite.crt.protocols.framework; + +import dk.alexandra.fresco.framework.DRes; +import dk.alexandra.fresco.framework.NativeProtocol; +import dk.alexandra.fresco.framework.builder.numeric.BuilderFactoryNumeric; +import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; +import dk.alexandra.fresco.framework.sce.resources.ResourcePool; +import dk.alexandra.fresco.lib.helper.SingleProtocolProducer; + +public class ProtocolBuilderNumericWrapper extends ProtocolBuilderNumeric { + + private final ResourcePoolT resourcePool; + private final ProtocolBuilderNumeric builder; + + public ProtocolBuilderNumericWrapper(ProtocolBuilderNumeric builder, BuilderFactoryNumeric factory, ResourcePoolT resourcePool) { + super(factory, true); + this.builder = builder; + this.resourcePool = resourcePool; + } + + @Override + public DRes append(NativeProtocol nativeProtocol) { + return builder.append(new NativeProtocolWrapper<>((NativeProtocol) nativeProtocol, resourcePool)); + } + + +} diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/suites/DummyProtocolSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/suites/DummyProtocolSupplier.java deleted file mode 100644 index 65157b898..000000000 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/suites/DummyProtocolSupplier.java +++ /dev/null @@ -1,98 +0,0 @@ -package dk.alexandra.fresco.suite.crt.suites; - -import dk.alexandra.fresco.framework.DRes; -import dk.alexandra.fresco.framework.NativeProtocol; -import dk.alexandra.fresco.framework.builder.numeric.field.FieldDefinition; -import dk.alexandra.fresco.framework.value.SInt; -import dk.alexandra.fresco.suite.dummy.arithmetic.DummyArithmeticAddProtocol; -import dk.alexandra.fresco.suite.dummy.arithmetic.DummyArithmeticCloseProtocol; -import dk.alexandra.fresco.suite.dummy.arithmetic.DummyArithmeticKnownProtocol; -import dk.alexandra.fresco.suite.dummy.arithmetic.DummyArithmeticMultProtocol; -import dk.alexandra.fresco.suite.dummy.arithmetic.DummyArithmeticOpenProtocol; -import dk.alexandra.fresco.suite.dummy.arithmetic.DummyArithmeticOpenToAllProtocol; -import dk.alexandra.fresco.suite.dummy.arithmetic.DummyArithmeticRandomBitProtocol; -import dk.alexandra.fresco.suite.dummy.arithmetic.DummyArithmeticRandomElementProtocol; -import dk.alexandra.fresco.suite.dummy.arithmetic.DummyArithmeticResourcePool; -import dk.alexandra.fresco.suite.dummy.arithmetic.DummyArithmeticSInt; -import dk.alexandra.fresco.suite.dummy.arithmetic.DummyArithmeticSubtractProtocol; -import java.math.BigInteger; -import java.util.Random; - -public class DummyProtocolSupplier implements - ProtocolSuiteProtocolSupplier { - - private final Random random; - private final FieldDefinition field; - - public DummyProtocolSupplier(Random random, FieldDefinition field) { - this.random = random; - this.field = field; - } - - @Override - public NativeProtocol known(BigInteger value) { - return new DummyArithmeticKnownProtocol(value); - } - - @Override - public NativeProtocol input(BigInteger value, int playerId) { - return new DummyArithmeticCloseProtocol(value, playerId); - } - - @Override - public NativeProtocol open(DRes value, - int playerId) { - return new DummyArithmeticOpenProtocol(value, playerId); - } - - @Override - public NativeProtocol open(DRes value) { - return new DummyArithmeticOpenToAllProtocol(value); - } - - @Override - public NativeProtocol add(DRes a, DRes b) { - return new DummyArithmeticAddProtocol(a, b); - } - - @Override - public NativeProtocol add(BigInteger a, DRes b) { - return new DummyArithmeticAddProtocol(new DummyArithmeticSInt(field.createElement(a)), b); - } - - @Override - public NativeProtocol sub(DRes a, DRes b) { - return new DummyArithmeticSubtractProtocol(a, b); - } - - @Override - public NativeProtocol sub(DRes a, BigInteger b) { - return new DummyArithmeticSubtractProtocol(a, new DummyArithmeticSInt(field.createElement(b))); - } - - @Override - public NativeProtocol sub(BigInteger a, DRes b) { - return new DummyArithmeticMultProtocol(new DummyArithmeticSInt(field.createElement(a)), b); - } - - @Override - public NativeProtocol mult(DRes a, DRes b) { - return new DummyArithmeticMultProtocol(a, b); - } - - @Override - public NativeProtocol mult(BigInteger a, DRes b) { - return new DummyArithmeticMultProtocol(new DummyArithmeticSInt(field.createElement(a)), b); - } - - @Override - public NativeProtocol randomElement() { - return new DummyArithmeticRandomElementProtocol(random); - } - - @Override - public NativeProtocol randomBit() { - return new DummyArithmeticRandomBitProtocol(random); - } - -} diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/suites/ProtocolSuiteProtocolSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/suites/ProtocolSuiteProtocolSupplier.java deleted file mode 100644 index 29eebbedb..000000000 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/suites/ProtocolSuiteProtocolSupplier.java +++ /dev/null @@ -1,38 +0,0 @@ -package dk.alexandra.fresco.suite.crt.suites; - -import dk.alexandra.fresco.framework.DRes; -import dk.alexandra.fresco.framework.NativeProtocol; -import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool; -import dk.alexandra.fresco.framework.builder.numeric.field.FieldDefinition; -import dk.alexandra.fresco.framework.value.SInt; -import java.math.BigInteger; - -public interface ProtocolSuiteProtocolSupplier { - - NativeProtocol known(BigInteger value); - - NativeProtocol input(BigInteger value, int playerId); - - NativeProtocol open(DRes value, int playerId); - - NativeProtocol open(DRes value); - - NativeProtocol add(DRes a, DRes b); - - NativeProtocol add(BigInteger a, DRes b); - - NativeProtocol sub(DRes a, DRes b); - - NativeProtocol sub(DRes a, BigInteger b); - - NativeProtocol sub(BigInteger a, DRes b); - - NativeProtocol mult(DRes a, DRes b); - - NativeProtocol mult(BigInteger a, DRes b); - - NativeProtocol randomElement(); - - NativeProtocol randomBit(); - -} diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/suites/SpdzProtocolSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/suites/SpdzProtocolSupplier.java deleted file mode 100644 index 5f578c8fa..000000000 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/suites/SpdzProtocolSupplier.java +++ /dev/null @@ -1,102 +0,0 @@ -package dk.alexandra.fresco.suite.crt.suites; - -import dk.alexandra.fresco.framework.DRes; -import dk.alexandra.fresco.framework.NativeProtocol; -import dk.alexandra.fresco.framework.builder.numeric.field.FieldDefinition; -import dk.alexandra.fresco.framework.value.SInt; -import dk.alexandra.fresco.suite.dummy.arithmetic.DummyArithmeticAddProtocol; -import dk.alexandra.fresco.suite.dummy.arithmetic.DummyArithmeticCloseProtocol; -import dk.alexandra.fresco.suite.dummy.arithmetic.DummyArithmeticKnownProtocol; -import dk.alexandra.fresco.suite.dummy.arithmetic.DummyArithmeticMultProtocol; -import dk.alexandra.fresco.suite.dummy.arithmetic.DummyArithmeticOpenProtocol; -import dk.alexandra.fresco.suite.dummy.arithmetic.DummyArithmeticOpenToAllProtocol; -import dk.alexandra.fresco.suite.dummy.arithmetic.DummyArithmeticRandomBitProtocol; -import dk.alexandra.fresco.suite.dummy.arithmetic.DummyArithmeticRandomElementProtocol; -import dk.alexandra.fresco.suite.dummy.arithmetic.DummyArithmeticResourcePool; -import dk.alexandra.fresco.suite.dummy.arithmetic.DummyArithmeticSInt; -import dk.alexandra.fresco.suite.dummy.arithmetic.DummyArithmeticSubtractProtocol; -import dk.alexandra.fresco.suite.spdz.SpdzRandomBitProtocol; -import dk.alexandra.fresco.suite.spdz.SpdzResourcePool; -import dk.alexandra.fresco.suite.spdz.gates.SpdzAddProtocol; -import dk.alexandra.fresco.suite.spdz.gates.SpdzAddProtocolKnownLeft; -import dk.alexandra.fresco.suite.spdz.gates.SpdzInputProtocol; -import dk.alexandra.fresco.suite.spdz.gates.SpdzKnownSIntProtocol; -import dk.alexandra.fresco.suite.spdz.gates.SpdzMultProtocol; -import dk.alexandra.fresco.suite.spdz.gates.SpdzMultProtocolKnownLeft; -import dk.alexandra.fresco.suite.spdz.gates.SpdzOutputSingleProtocol; -import dk.alexandra.fresco.suite.spdz.gates.SpdzOutputToAllProtocol; -import dk.alexandra.fresco.suite.spdz.gates.SpdzRandomProtocol; -import dk.alexandra.fresco.suite.spdz.gates.SpdzSubtractProtocol; -import dk.alexandra.fresco.suite.spdz.gates.SpdzSubtractProtocolKnownLeft; -import dk.alexandra.fresco.suite.spdz.gates.SpdzSubtractProtocolKnownRight; -import java.math.BigInteger; -import java.util.Random; - -public class SpdzProtocolSupplier implements - ProtocolSuiteProtocolSupplier { - - @Override - public NativeProtocol known(BigInteger value) { - return new SpdzKnownSIntProtocol(value); - } - - @Override - public NativeProtocol input(BigInteger value, int playerId) { - return new SpdzInputProtocol(value, playerId); - } - - @Override - public NativeProtocol open(DRes value, int playerId) { - return new SpdzOutputSingleProtocol(value, playerId); - } - - @Override - public NativeProtocol open(DRes value) { - return new SpdzOutputToAllProtocol(value); - } - - @Override - public NativeProtocol add(DRes a, DRes b) { - return new SpdzAddProtocol(a, b); - } - - @Override - public NativeProtocol add(BigInteger a, DRes b) { - return new SpdzAddProtocolKnownLeft(a, b); - } - - @Override - public NativeProtocol sub(DRes a, DRes b) { - return new SpdzSubtractProtocol(a, b); - } - - @Override - public NativeProtocol sub(DRes a, BigInteger b) { - return new SpdzSubtractProtocolKnownRight(a, b); - } - - @Override - public NativeProtocol sub(BigInteger a, DRes b) { - return new SpdzSubtractProtocolKnownLeft(a, b); - } - - @Override - public NativeProtocol mult(DRes a, DRes b) { - return new SpdzMultProtocol(a, b); - } - - @Override - public NativeProtocol mult(BigInteger a, DRes b) { - return new SpdzMultProtocolKnownLeft(a, b); - } - - @Override - public NativeProtocol randomElement() { - return new SpdzRandomProtocol(); - } - - @Override - public NativeProtocol randomBit() { - return new SpdzRandomBitProtocol(); - } -} diff --git a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractDummyCRTTest.java b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractDummyCRTTest.java index 64a06bf20..85dc2ad55 100644 --- a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractDummyCRTTest.java +++ b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractDummyCRTTest.java @@ -14,25 +14,20 @@ import dk.alexandra.fresco.framework.sce.evaluator.BatchEvaluationStrategy; import dk.alexandra.fresco.framework.sce.evaluator.BatchedProtocolEvaluator; import dk.alexandra.fresco.framework.sce.evaluator.EvaluationStrategy; -import dk.alexandra.fresco.logging.DefaultPerformancePrinter; -import dk.alexandra.fresco.logging.PerformanceLogger; -import dk.alexandra.fresco.logging.PerformanceLoggerCountingAggregate; -import dk.alexandra.fresco.logging.PerformancePrinter; +import dk.alexandra.fresco.lib.field.integer.BasicNumericContext; import dk.alexandra.fresco.suite.ProtocolSuiteNumeric; import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTDataSupplier; import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTDummyDataSupplier; import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTResourcePool; import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTResourcePoolImpl; -import dk.alexandra.fresco.suite.crt.suites.DummyProtocolSupplier; +import dk.alexandra.fresco.suite.crt.protocols.framework.CRTSequentialStrategy; +import dk.alexandra.fresco.suite.dummy.arithmetic.DummyArithmeticBuilderFactory; import dk.alexandra.fresco.suite.dummy.arithmetic.DummyArithmeticResourcePool; import dk.alexandra.fresco.suite.dummy.arithmetic.DummyArithmeticResourcePoolImpl; import dk.alexandra.fresco.suite.dummy.arithmetic.DummyArithmeticSInt; + import java.math.BigInteger; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Random; +import java.util.*; /** * Abstract class which handles a lot of boiler plate testing code. This makes running a single test @@ -64,8 +59,10 @@ public void runTest( for (int playerId : netConf.keySet()) { + + BatchEvaluationStrategy> batchEvaluationStrategy = - evalStrategy.getStrategy(); + new CRTSequentialStrategy<>(); DummyArithmeticResourcePool rpLeft = new DummyArithmeticResourcePoolImpl(playerId, noOfParties, DEFAULT_FIELD_LEFT); DummyArithmeticResourcePool rpRight = new DummyArithmeticResourcePoolImpl(playerId, @@ -73,8 +70,10 @@ public void runTest( ProtocolSuiteNumeric> ps = new CRTProtocolSuite<>( - new DummyProtocolSupplier(new Random(0), DEFAULT_FIELD_LEFT), - new DummyProtocolSupplier(new Random(1), DEFAULT_FIELD_RIGHT)); + new DummyArithmeticBuilderFactory(new BasicNumericContext(DEFAULT_FIELD_LEFT.getBitLength() - 24, + playerId, noOfParties, DEFAULT_FIELD_LEFT, 16, 40)), + new DummyArithmeticBuilderFactory(new BasicNumericContext(DEFAULT_FIELD_RIGHT.getBitLength()- 40, + playerId, noOfParties, DEFAULT_FIELD_RIGHT, 16, 40))); ProtocolEvaluator> evaluator = new BatchedProtocolEvaluator<>(batchEvaluationStrategy, ps); diff --git a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSpdzCRTTest.java b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSpdzCRTTest.java index d0e4405e0..1154cbd66 100644 --- a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSpdzCRTTest.java +++ b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSpdzCRTTest.java @@ -1,8 +1,5 @@ package dk.alexandra.fresco.suite.crt; -import static dk.alexandra.fresco.suite.spdz.configuration.PreprocessingStrategy.DUMMY; -import static dk.alexandra.fresco.suite.spdz.configuration.PreprocessingStrategy.MASCOT; - import dk.alexandra.fresco.framework.DRes; import dk.alexandra.fresco.framework.ProtocolEvaluator; import dk.alexandra.fresco.framework.TestThreadRunner; @@ -25,23 +22,15 @@ import dk.alexandra.fresco.framework.sce.evaluator.EvaluationStrategy; import dk.alexandra.fresco.framework.sce.resources.storage.FilebasedStreamedStorageImpl; import dk.alexandra.fresco.framework.sce.resources.storage.InMemoryStorage; -import dk.alexandra.fresco.framework.util.AesCtrDrbg; -import dk.alexandra.fresco.framework.util.AesCtrDrbgFactory; -import dk.alexandra.fresco.framework.util.Drbg; -import dk.alexandra.fresco.framework.util.OpenedValueStoreImpl; -import dk.alexandra.fresco.framework.util.Pair; +import dk.alexandra.fresco.framework.util.*; import dk.alexandra.fresco.framework.value.SInt; import dk.alexandra.fresco.lib.field.integer.BasicNumericContext; import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTDataSupplier; import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTDummyDataSupplier; import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTResourcePool; import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTResourcePoolImpl; -import dk.alexandra.fresco.suite.crt.suites.SpdzProtocolSupplier; -import dk.alexandra.fresco.suite.spdz.NetManager; -import dk.alexandra.fresco.suite.spdz.SpdzBuilder; -import dk.alexandra.fresco.suite.spdz.SpdzProtocolSuite; -import dk.alexandra.fresco.suite.spdz.SpdzResourcePool; -import dk.alexandra.fresco.suite.spdz.SpdzResourcePoolImpl; +import dk.alexandra.fresco.suite.crt.protocols.framework.CRTSequentialStrategy; +import dk.alexandra.fresco.suite.spdz.*; import dk.alexandra.fresco.suite.spdz.configuration.PreprocessingStrategy; import dk.alexandra.fresco.suite.spdz.datatypes.SpdzSInt; import dk.alexandra.fresco.suite.spdz.storage.SpdzDataSupplier; @@ -51,16 +40,16 @@ import dk.alexandra.fresco.tools.ot.base.DummyOt; import dk.alexandra.fresco.tools.ot.base.Ot; import dk.alexandra.fresco.tools.ot.otextension.RotList; + import java.math.BigInteger; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Random; +import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.IntStream; +import static dk.alexandra.fresco.suite.spdz.configuration.PreprocessingStrategy.DUMMY; +import static dk.alexandra.fresco.suite.spdz.configuration.PreprocessingStrategy.MASCOT; + /** * Abstract class which handles a lot of boiler plate testing code. This makes running a single test * using different parameters quite easy. @@ -117,11 +106,13 @@ public void runTest( for (int playerId : netConf.keySet()) { - BatchEvaluationStrategy> strategy = evalStrategy - .getStrategy(); + BatchEvaluationStrategy> strategy = new CRTSequentialStrategy<>(); CRTProtocolSuite ps = new CRTProtocolSuite<>( - new SpdzProtocolSupplier(), new SpdzProtocolSupplier()); + new SpdzBuilder(new BasicNumericContext(DEFAULT_FIELD_LEFT.getBitLength() - 24, + playerId, noOfParties, DEFAULT_FIELD_LEFT, 16, 40)), + new SpdzBuilder(new BasicNumericContext(DEFAULT_FIELD_RIGHT.getBitLength() - 40, + playerId, noOfParties, DEFAULT_FIELD_RIGHT, 16, 40))); ProtocolEvaluator> evaluator = new BatchedProtocolEvaluator<>(strategy, ps); diff --git a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/TestCRT.java b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/TestCRT.java index 7dbe9c39f..f7f3a85d5 100644 --- a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/TestCRT.java +++ b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/TestCRT.java @@ -31,6 +31,7 @@ import dk.alexandra.fresco.suite.crt.BasicCRTTests.TestTruncp; import dk.alexandra.fresco.suite.crt.fixed.CRTAdvancedFixedNumeric; import dk.alexandra.fresco.suite.crt.fixed.CRTFixedNumeric; +import dk.alexandra.fresco.suite.dummy.arithmetic.BasicArithmeticTests; import dk.alexandra.fresco.suite.dummy.arithmetic.BasicArithmeticTests.TestLotsMult; import dk.alexandra.fresco.suite.dummy.arithmetic.BasicArithmeticTests.TestOutputToSingleParty; import dk.alexandra.fresco.suite.dummy.arithmetic.BasicArithmeticTests.TestRandomBit; @@ -52,6 +53,11 @@ public void testInput() { new AbstractSpdzCRTTest().runTest(new TestInput<>(), EvaluationStrategy.SEQUENTIAL, PreprocessingStrategy.DUMMY, 2); } + @Test + public void testKnown() { + new AbstractSpdzCRTTest().runTest(new BasicArithmeticTests.TestKnownSInt<>(), EvaluationStrategy.SEQUENTIAL, PreprocessingStrategy.DUMMY, 2); + } + @Test public void testSum() { new AbstractSpdzCRTTest().runTest(new TestSum<>(), EvaluationStrategy.SEQUENTIAL, PreprocessingStrategy.DUMMY, 2); From e7b6f65b75eaa38851397672aa4766c5c0daff84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Lindstr=C3=B8m?= Date: Thu, 20 Oct 2022 13:11:18 +0200 Subject: [PATCH 02/38] Remove unused method --- .../framework/sce/evaluator/ProtocolCollectionList.java | 6 ------ 1 file changed, 6 deletions(-) diff --git a/core/src/main/java/dk/alexandra/fresco/framework/sce/evaluator/ProtocolCollectionList.java b/core/src/main/java/dk/alexandra/fresco/framework/sce/evaluator/ProtocolCollectionList.java index a3e7a988c..2b2afe5a3 100644 --- a/core/src/main/java/dk/alexandra/fresco/framework/sce/evaluator/ProtocolCollectionList.java +++ b/core/src/main/java/dk/alexandra/fresco/framework/sce/evaluator/ProtocolCollectionList.java @@ -38,10 +38,4 @@ public Iterator> iterator() { public int size() { return protocols.size(); } - - public ProtocolCollectionList map(Function, NativeProtocol> map) { - ProtocolCollectionList out = new ProtocolCollectionList(capacity); - out.protocols.addAll(protocols.stream().map(map).collect(Collectors.toList())); - return out; - } } From b571d993b309d93cb1456c4f86151f045897c53e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Lindstr=C3=B8m?= Date: Thu, 20 Oct 2022 14:46:39 +0200 Subject: [PATCH 03/38] Fix lifts and truncp --- .../builder/ProtocolBuilderImpl.java | 4 + .../fresco/suite/crt/CRTBuilderFactory.java | 35 +- .../fresco/suite/crt/CRTNumericContext.java | 52 +- .../resource/CRTDummyDataSupplier.java | 3 +- .../suite/crt/protocols/LiftPQProtocol.java | 58 +- .../suite/crt/protocols/LiftQPProtocol.java | 26 +- .../fresco/suite/crt/protocols/Truncp.java | 21 +- .../protocols/framework/CRTComputation.java | 4 +- .../ProtocolBuilderNumericWrapper.java | 4 +- .../fresco/suite/crt/BasicCRTTests.java | 498 +++++++++--------- 10 files changed, 388 insertions(+), 317 deletions(-) diff --git a/core/src/main/java/dk/alexandra/fresco/framework/builder/ProtocolBuilderImpl.java b/core/src/main/java/dk/alexandra/fresco/framework/builder/ProtocolBuilderImpl.java index 2af9e483e..e16148d62 100644 --- a/core/src/main/java/dk/alexandra/fresco/framework/builder/ProtocolBuilderImpl.java +++ b/core/src/main/java/dk/alexandra/fresco/framework/builder/ProtocolBuilderImpl.java @@ -93,4 +93,8 @@ public BuildStep par(ComputationParallel f) createAndAppend(new LazyProtocolProducerDecorator(() -> builder.createProducer(null, factory))); return builder; } + + public boolean isParallel() { + return parallel; + } } diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTBuilderFactory.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTBuilderFactory.java index 13a03fe2e..756f027cd 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTBuilderFactory.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTBuilderFactory.java @@ -41,7 +41,7 @@ public CRTBuilderFactory(ResourcePoolA resourcePoolLeft, this.q = resourcePoolRight.getModulus(); this.context = new CRTNumericContext( p.bitLength() + q.bitLength() - 40, //TODO - resourcePoolLeft.getMyId(), resourcePoolLeft.getNoOfParties(), left, right, p, q); + resourcePoolLeft.getMyId(), resourcePoolLeft.getNoOfParties(), left, right, p, q, resourcePoolLeft, resourcePoolRight); } @Override @@ -49,6 +49,7 @@ public BasicNumericContext getBasicNumericContext() { return context; } + @Override public Numeric createNumeric(ProtocolBuilderNumeric builder) { return new Numeric() { @@ -66,8 +67,8 @@ public DRes add(DRes a, DRes b) { DRes bLeft = bOut.getLeft(); DRes bRight = bOut.getRight(); - Numeric l = left.createNumeric(new ProtocolBuilderNumericWrapper<>(par, left, resourcePoolLeft)); - Numeric r = right.createNumeric(new ProtocolBuilderNumericWrapper<>(par, right, resourcePoolRight)); + Numeric l = context.leftNumeric(par); + Numeric r = context.rightNumeric(par); return new CRTSInt( l.add(aLeft, bLeft), @@ -96,8 +97,8 @@ public DRes sub(DRes a, DRes b) { DRes bLeft = bOut.getLeft(); DRes bRight = bOut.getRight(); - Numeric l = left.createNumeric(new ProtocolBuilderNumericWrapper<>(par, left, resourcePoolLeft)); - Numeric r = right.createNumeric(new ProtocolBuilderNumericWrapper<>(par, right, resourcePoolRight)); + Numeric l = context.leftNumeric(par); + Numeric r = context.rightNumeric(par); return new CRTSInt( l.sub(aLeft, bLeft), @@ -134,8 +135,8 @@ public DRes mult(DRes a, DRes b) { DRes bLeft = bOut.getLeft(); DRes bRight = bOut.getRight(); - Numeric l = left.createNumeric(new ProtocolBuilderNumericWrapper<>(par, left, resourcePoolLeft)); - Numeric r = right.createNumeric(new ProtocolBuilderNumericWrapper<>(par, right, resourcePoolRight)); + Numeric l = context.leftNumeric(par); + Numeric r = context.rightNumeric(par); return new CRTSInt( l.mult(aLeft, bLeft), @@ -152,8 +153,8 @@ public DRes mult(BigInteger a, DRes b) { DRes bLeft = bOut.getLeft(); DRes bRight = bOut.getRight(); - Numeric l = left.createNumeric(new ProtocolBuilderNumericWrapper<>(par, left, resourcePoolLeft)); - Numeric r = right.createNumeric(new ProtocolBuilderNumericWrapper<>(par, right, resourcePoolRight)); + Numeric l = context.leftNumeric(par); + Numeric r = context.rightNumeric(par); return new CRTSInt( l.mult(aRNS.getFirst(), bLeft), @@ -170,8 +171,8 @@ public DRes randomBit() { public DRes randomElement() { return builder.par(par -> { - Numeric l = left.createNumeric(new ProtocolBuilderNumericWrapper<>(par, left, resourcePoolLeft)); - Numeric r = right.createNumeric(new ProtocolBuilderNumericWrapper<>(par, right, resourcePoolRight)); + Numeric l = context.leftNumeric(par); + Numeric r = context.rightNumeric(par); return new CRTSInt(l.randomElement(), r.randomElement()); }); @@ -182,8 +183,8 @@ public DRes known(BigInteger value) { Pair crt = Util.mapToCRT(value, p, q); return builder.par(par -> { - Numeric l = left.createNumeric(new ProtocolBuilderNumericWrapper<>(par, left, resourcePoolLeft)); - Numeric r = right.createNumeric(new ProtocolBuilderNumericWrapper<>(par, right, resourcePoolRight)); + Numeric l = context.leftNumeric(par); + Numeric r = context.rightNumeric(par); return new CRTSInt(l.known(crt.getFirst()), r.known(crt.getSecond())); }); } @@ -198,8 +199,8 @@ public DRes input(BigInteger value, int inputParty) { } return builder.par(par -> { - Numeric l = left.createNumeric(new ProtocolBuilderNumericWrapper<>(par, left, resourcePoolLeft)); - Numeric r = right.createNumeric(new ProtocolBuilderNumericWrapper<>(par, right, resourcePoolRight)); + Numeric l = context.leftNumeric(par); + Numeric r = context.rightNumeric(par); return new CRTSInt(l.input(crt.getFirst(), inputParty), r.input(crt.getSecond(), inputParty)); @@ -211,8 +212,8 @@ public DRes open(DRes secretShare) { return builder.par(par -> { CRTSInt crtsInt = (CRTSInt) secretShare.out(); - Numeric l = left.createNumeric(new ProtocolBuilderNumericWrapper<>(par, left, resourcePoolLeft)); - Numeric r = right.createNumeric(new ProtocolBuilderNumericWrapper<>(par, right, resourcePoolRight)); + Numeric l = context.leftNumeric(par); + Numeric r = context.rightNumeric(par); return Pair.lazy(l.open(crtsInt.getLeft()), r.open(crtsInt.getRight())); }).seq((seq, opened) -> DRes.of(Util.mapToBigInteger(opened.getFirst().out(), opened.getSecond().out(), p, q))); diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTNumericContext.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTNumericContext.java index 2c4a41a42..72114aae1 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTNumericContext.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTNumericContext.java @@ -1,16 +1,26 @@ package dk.alexandra.fresco.suite.crt; +import dk.alexandra.fresco.framework.DRes; import dk.alexandra.fresco.framework.builder.numeric.BuilderFactoryNumeric; +import dk.alexandra.fresco.framework.builder.numeric.Numeric; +import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; +import dk.alexandra.fresco.framework.sce.resources.ResourcePool; +import dk.alexandra.fresco.framework.value.SInt; import dk.alexandra.fresco.lib.field.integer.BasicNumericContext; +import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; +import dk.alexandra.fresco.suite.crt.protocols.framework.ProtocolBuilderNumericWrapper; import java.math.BigInteger; -public class CRTNumericContext extends BasicNumericContext { +public class CRTNumericContext extends BasicNumericContext { private final BigInteger p, q; private final BuilderFactoryNumeric left; private final BuilderFactoryNumeric right; + private final ResourcePoolA resourcePoolLeft; + private final ResourcePoolB resourcePoolRight; + /** * Construct a new BasicNumericContext. * @@ -20,12 +30,14 @@ public class CRTNumericContext extends BasicNumericContext { */ public CRTNumericContext(int maxBitLength, int myId, int noOfParties, BuilderFactoryNumeric left, BuilderFactoryNumeric right, - BigInteger p, BigInteger q) { + BigInteger p, BigInteger q, ResourcePoolA resourcePoolLeft, ResourcePoolB resourcePoolRight) { super(maxBitLength, myId, noOfParties, new CRTRingDefinition(p, q), p.bitLength()); this.p = p; this.q = q; this.left = left; this.right = right; + this.resourcePoolLeft = resourcePoolLeft; + this.resourcePoolRight = resourcePoolRight; } /** Get the modulus of the left ring in the RNS representation. */ @@ -53,4 +65,40 @@ public BuilderFactoryNumeric getRight() { } + public ResourcePoolA getResourcePoolLeft() { + return resourcePoolLeft; + } + + public ResourcePoolB getResourcePoolRight() { + return resourcePoolRight; + } + + /** + * Get an instance of Numeric for the left scheme using the given builder. + */ + public Numeric leftNumeric(ProtocolBuilderNumeric builder) { + return left.createNumeric(new ProtocolBuilderNumericWrapper<>(builder, left, resourcePoolLeft)); + } + + /** + * Get an instance of Numeric for the right scheme using the given builder. + */ + public Numeric rightNumeric(ProtocolBuilderNumeric builder) { + return right.createNumeric(new ProtocolBuilderNumericWrapper<>(builder, right, resourcePoolRight)); + } + + public DRes getLeft(DRes crtInteger) { + if (crtInteger.out() == null || !(crtInteger.out() instanceof CRTSInt)) { + throw new IllegalArgumentException("Input must be a non-null CRTSInt"); + } + return ((CRTSInt) crtInteger).getLeft(); + } + + public DRes getRight(DRes crtInteger) { + if (crtInteger.out() == null || !(crtInteger.out() instanceof CRTSInt)) { + throw new IllegalArgumentException("Input must be a non-null CRTSInt"); + } + return ((CRTSInt) crtInteger.out()).getRight(); + } + } diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDummyDataSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDummyDataSupplier.java index 4c8da904b..29e353e32 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDummyDataSupplier.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDummyDataSupplier.java @@ -35,8 +35,7 @@ public CRTDummyDataSupplier(int myId, int players, FieldDefinition leftField, public CRTSInt getCorrelatedNoise() { BigInteger r = Util.randomBigInteger(random, fp.getModulus()); BigInteger l = Util - .randomBigInteger(random, BigInteger.valueOf(players).add(BigInteger.ONE)); - + .randomBigInteger(random, BigInteger.valueOf(players)); return new CRTSInt(wrapperLeft.apply(r), wrapperRight.apply(r.add(l.multiply(fp.getModulus())))); } diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftPQProtocol.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftPQProtocol.java index e062c5b2b..de6783354 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftPQProtocol.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftPQProtocol.java @@ -1,10 +1,12 @@ package dk.alexandra.fresco.suite.crt.protocols; import dk.alexandra.fresco.framework.DRes; +import dk.alexandra.fresco.framework.builder.numeric.Numeric; import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool; import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; import dk.alexandra.fresco.framework.value.SInt; import dk.alexandra.fresco.suite.crt.CRTNumericContext; +import dk.alexandra.fresco.suite.crt.Util; import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; import dk.alexandra.fresco.suite.crt.protocols.framework.CRTComputation; @@ -12,28 +14,36 @@ * Given a secret shared [x]p in p, output [x - ep]q for some 0 ≤ e ≤ n in q. */ public class LiftPQProtocol extends - CRTComputation { - - private final DRes value; - private CRTSInt r; - - public LiftPQProtocol(DRes value) { - this.value = value; - } - - @Override - public DRes buildComputation(ProtocolBuilderNumeric builder, - CRTNumericContext context) { - return builder.seq(seq -> seq.append(new CorrelatedNoiseProtocol<>())).seq((seq, noise) -> { - this.r = (CRTSInt) noise.out(); - - DRes xBar = context.getLeft().createNumeric(seq).add(value, r.getLeft()); - return context.getLeft().createNumeric(seq).open(xBar); - - }).seq((seq, xBarOpen) -> { - DRes xBarRight = context.getRight().createNumeric(seq).known(xBarOpen); - return context.getRight().createNumeric(seq).sub(xBarRight, r.getRight()); - }); - } - + CRTComputation { + + private final DRes value; + private CRTSInt r; + + // The right value is ignored in the input + public LiftPQProtocol(DRes value) { + this.value = value; + } + + @Override + public DRes buildComputation(ProtocolBuilderNumeric builder, + CRTNumericContext context) { + return builder.seq(seq -> seq.append(new CorrelatedNoiseProtocol<>())).seq((seq, noise) -> { + this.r = (CRTSInt) noise.out(); + + // Add noise to the left value. The right is ignored. + DRes xBar = context.leftNumeric(seq).add(((CRTSInt) value.out()).getLeft(), r.getLeft()); + CRTSInt output = new CRTSInt(xBar, context.rightNumeric(seq).known(0)); + return seq.numeric().open(output); // TODO: We only need to open the left, so we should create an openLeft function + + }).seq((seq, xBarOpen) -> { + Numeric right = context.rightNumeric(seq); + + // Extract the left value from xBar + DRes xBarRight = right.known(Util.mapToCRT(xBarOpen, context.getLeftModulus(), context.getRightModulus()).getFirst()); + + // Remove the noise and return + DRes adjusted = right.sub(xBarRight, r.getRight()); + return new CRTSInt(((CRTSInt) value.out()).getLeft(), adjusted); + }); + } } diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftQPProtocol.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftQPProtocol.java index 052c3ef9a..c2c8e3dbe 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftQPProtocol.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftQPProtocol.java @@ -1,10 +1,12 @@ package dk.alexandra.fresco.suite.crt.protocols; import dk.alexandra.fresco.framework.DRes; +import dk.alexandra.fresco.framework.builder.numeric.Numeric; import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool; import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; import dk.alexandra.fresco.framework.value.SInt; import dk.alexandra.fresco.suite.crt.CRTNumericContext; +import dk.alexandra.fresco.suite.crt.Util; import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; import dk.alexandra.fresco.suite.crt.protocols.framework.CRTComputation; @@ -23,14 +25,26 @@ public LiftQPProtocol(DRes value) { @Override public DRes buildComputation(ProtocolBuilderNumeric builder, - CRTNumericContext context) { - return builder.seq(seq -> new CorrelatedNoiseProtocol<>()).seq((seq, noise) -> { + CRTNumericContext context) { + return builder.seq(seq -> seq.append(new CorrelatedNoiseProtocol<>())).seq((seq, noise) -> { this.r = (CRTSInt) noise.out(); - DRes xBar = context.getRight().createNumeric(seq).add(value, r.getRight()); - return context.getRight().createNumeric(seq).open(xBar); + + // Add noise to the right value. The left is ignored. + DRes xBar = context.rightNumeric(seq).add(((CRTSInt) value.out()).getRight(), r.getRight()); + CRTSInt output = new CRTSInt(context.leftNumeric(seq).known(0), xBar); + return seq.numeric().open(output); // TODO: We only need to open the right, so we should create an openRight function + + // TODO: We haven't added the noise (c from the protocol) + }).seq((seq, xBarOpen) -> { - DRes xBarRight = context.getLeft().createNumeric(seq).known(xBarOpen); - return context.getLeft().createNumeric(seq).sub(xBarRight, r.getLeft()); + Numeric left = context.leftNumeric(seq); + + // Extract the right value from xBar + DRes xBarLeft = left.known(Util.mapToCRT(xBarOpen, context.getLeftModulus(), context.getRightModulus()).getSecond()); + + // Remove the noise and return + DRes adjusted = left.sub(xBarLeft, r.getLeft()); + return new CRTSInt(adjusted, ((CRTSInt) value.out()).getRight()); }); } diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/Truncp.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/Truncp.java index cbd58fffc..aa190db14 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/Truncp.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/Truncp.java @@ -1,16 +1,18 @@ package dk.alexandra.fresco.suite.crt.protocols; import dk.alexandra.fresco.framework.DRes; +import dk.alexandra.fresco.framework.builder.numeric.Numeric; import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool; 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.suite.crt.CRTNumericContext; +import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; import dk.alexandra.fresco.suite.crt.protocols.Projection.Coordinate; import dk.alexandra.fresco.suite.crt.protocols.framework.CRTComputation; import java.math.BigInteger; -/** Compute x / p as integer division (rounded down) */ +/** Compute x / p as integer division (rounded down) with a potential error */ public class Truncp extends CRTComputation { private final DRes value; @@ -21,19 +23,18 @@ public Truncp(DRes value) { @Override public DRes buildComputation(ProtocolBuilderNumeric builder, - CRTNumericContext context) { + CRTNumericContext context) { // The multiplicative inverse of p mod q BigInteger n2 = context.getLeftModulus().modInverse(context.getRightModulus()); - return builder.par(par -> { - DRes liftedX1 = new LiftPQProtocol<>(value).buildComputation(par); - DRes x2 = new Projection<>(value, Coordinate.RIGHT).buildComputation(par); - return Pair.lazy(liftedX1, x2); - }).seq((seq, x) -> { - DRes y2 = seq.numeric().mult(n2, seq.numeric().sub(x.getSecond(), x.getFirst())); + return builder.seq(seq -> new LiftPQProtocol<>(value).buildComputation(seq)).seq((seq, liftedX1) -> { + Numeric rightNumeric = context.rightNumeric(seq); + DRes y2Right = rightNumeric.sub(context.getRight(value), context.getRight(liftedX1)); + y2Right = rightNumeric.mult(n2, y2Right); + CRTSInt y2 = new CRTSInt(context.leftNumeric(seq).known(0), y2Right); DRes y1 = new LiftQPProtocol<>(y2).buildComputation(seq); - return seq.numeric().add(y1, y2); - }); + return Pair.lazy(y1, y2Right); + }).seq((seq, y) -> new CRTSInt(context.getLeft(y.getFirst().out()), y.getSecond())); } } diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/framework/CRTComputation.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/framework/CRTComputation.java index 3e79a9fc1..0e4f39abc 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/framework/CRTComputation.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/framework/CRTComputation.java @@ -11,11 +11,11 @@ public abstract class CRTComputation implements Computation { - public abstract DRes buildComputation(ProtocolBuilderNumeric builder, CRTNumericContext context); + public abstract DRes buildComputation(ProtocolBuilderNumeric builder, CRTNumericContext context); @Override public DRes buildComputation(ProtocolBuilderNumeric builder) { return buildComputation(builder, - (CRTNumericContext) builder.getBasicNumericContext()); + (CRTNumericContext) builder.getBasicNumericContext()); } } diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/framework/ProtocolBuilderNumericWrapper.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/framework/ProtocolBuilderNumericWrapper.java index 498eca1fa..8ea325f49 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/framework/ProtocolBuilderNumericWrapper.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/framework/ProtocolBuilderNumericWrapper.java @@ -13,14 +13,14 @@ public class ProtocolBuilderNumericWrapper e private final ProtocolBuilderNumeric builder; public ProtocolBuilderNumericWrapper(ProtocolBuilderNumeric builder, BuilderFactoryNumeric factory, ResourcePoolT resourcePool) { - super(factory, true); + super(factory, builder.isParallel()); this.builder = builder; this.resourcePool = resourcePool; } @Override public DRes append(NativeProtocol nativeProtocol) { - return builder.append(new NativeProtocolWrapper<>((NativeProtocol) nativeProtocol, resourcePool)); + return builder.append(new NativeProtocolWrapper<>((NativeProtocol) nativeProtocol, resourcePool)); } diff --git a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/BasicCRTTests.java b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/BasicCRTTests.java index 406041067..8db4b1c6e 100644 --- a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/BasicCRTTests.java +++ b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/BasicCRTTests.java @@ -12,334 +12,328 @@ import dk.alexandra.fresco.lib.fixed.FixedNumeric; import dk.alexandra.fresco.lib.fixed.SFixed; import dk.alexandra.fresco.suite.crt.fixed.CRTFixedNumeric; -import dk.alexandra.fresco.suite.crt.protocols.CorrelatedNoiseProtocol; -import dk.alexandra.fresco.suite.crt.protocols.LiftPQProtocol; -import dk.alexandra.fresco.suite.crt.protocols.LiftQPProtocol; -import dk.alexandra.fresco.suite.crt.protocols.Projection; +import dk.alexandra.fresco.suite.crt.protocols.*; import dk.alexandra.fresco.suite.crt.protocols.Projection.Coordinate; -import dk.alexandra.fresco.suite.crt.protocols.RandomModP; -import dk.alexandra.fresco.suite.crt.protocols.Truncp; +import org.junit.Assert; + import java.math.BigDecimal; import java.math.BigInteger; import java.util.List; import java.util.Random; import java.util.stream.Collectors; import java.util.stream.Stream; -import org.junit.Assert; public class BasicCRTTests { - public static class TestInput - extends TestThreadFactory { + public static class TestInput + extends TestThreadFactory { - @Override - public TestThread next() { - BigInteger value = BigInteger.valueOf(10); - return new TestThread() { @Override - public void test() { - Application app = producer -> { - Numeric numeric = producer.numeric(); - DRes input = numeric.input(value, 1); - return numeric.open(input); - }; - BigInteger output = runApplication(app); - - Assert.assertEquals(value, output); + public TestThread next() { + BigInteger value = BigInteger.valueOf(10); + return new TestThread() { + @Override + public void test() { + Application app = producer -> { + Numeric numeric = producer.numeric(); + DRes input = numeric.input(value, 1); + return numeric.open(input); + }; + BigInteger output = runApplication(app); + + Assert.assertEquals(value, output); + } + }; } - }; } - } - - public static class TestCorrelatedNoise - extends TestThreadFactory { - @Override - public TestThread next() { - return new TestThread() { + public static class TestCorrelatedNoise + extends TestThreadFactory { @Override - public void test() { - Application app = producer -> producer - .seq(seq -> seq.append( - new CorrelatedNoiseProtocol<>())).seq((seq, r) -> seq.numeric().open(r)); - BigInteger output = runApplication(app); + public TestThread next() { + return new TestThread() { - CRTRingDefinition ring = (CRTRingDefinition) this.getFieldDefinition(); - Pair crt = ring.integerToRNS(output); + @Override + public void test() { + Application app = producer -> producer + .seq(seq -> seq.append( + new CorrelatedNoiseProtocol<>())).seq((seq, r) -> seq.numeric().open(r)); + BigInteger output = runApplication(app); - Assert.assertEquals(crt.getFirst(), crt.getSecond().mod(ring.getLeftModulus())); + CRTRingDefinition ring = (CRTRingDefinition) this.getFieldDefinition(); + Pair crt = ring.integerToRNS(output); + + System.out.println(crt.getFirst() + ", " + crt.getSecond()); + + Assert.assertEquals(crt.getFirst(), crt.getSecond().mod(ring.getLeftModulus())); + } + }; } - }; } - } - - public static class TestProjectionLeft - extends TestThreadFactory { - @Override - public TestThread next() { - return new TestThread() { + public static class TestProjectionLeft + extends TestThreadFactory { @Override - public void test() { - BigInteger x = BigInteger.valueOf(7); - BigInteger y = BigInteger.valueOf(11); - Application app = producer -> producer.seq(seq -> { - CRTRingDefinition ring = (CRTRingDefinition) this.getFieldDefinition(); - BigInteger toConvert = ring.RNStoBigInteger(x, y); - return new Projection<>(seq.numeric().known(toConvert), Coordinate.LEFT) - .buildComputation(seq); - }).seq((seq, r) -> seq.numeric().open(r)); - BigInteger output = runApplication(app); - - CRTRingDefinition ring = (CRTRingDefinition) this.getFieldDefinition(); - Pair crt = Util.mapToCRT(output, ring.getLeftModulus(), ring.getRightModulus()); - - Assert.assertEquals(x, crt.getFirst()); - Assert.assertEquals(BigInteger.ZERO, crt.getSecond()); + public TestThread next() { + return new TestThread() { + + @Override + public void test() { + BigInteger x = BigInteger.valueOf(7); + BigInteger y = BigInteger.valueOf(11); + Application app = producer -> producer.seq(seq -> { + CRTRingDefinition ring = (CRTRingDefinition) this.getFieldDefinition(); + BigInteger toConvert = ring.RNStoBigInteger(x, y); + return new Projection<>(seq.numeric().known(toConvert), Coordinate.LEFT) + .buildComputation(seq); + }).seq((seq, r) -> seq.numeric().open(r)); + BigInteger output = runApplication(app); + + CRTRingDefinition ring = (CRTRingDefinition) this.getFieldDefinition(); + Pair crt = Util.mapToCRT(output, ring.getLeftModulus(), ring.getRightModulus()); + + Assert.assertEquals(x, crt.getFirst()); + Assert.assertEquals(BigInteger.ZERO, crt.getSecond()); + } + }; } - }; } - } - public static class TestProjectionRight - extends TestThreadFactory { - - @Override - public TestThread next() { - return new TestThread() { + public static class TestProjectionRight + extends TestThreadFactory { @Override - public void test() { - BigInteger x = BigInteger.valueOf(7); - BigInteger y = BigInteger.valueOf(11); - Application app = producer -> producer.seq(seq -> { - CRTRingDefinition ring = (CRTRingDefinition) this.getFieldDefinition(); - BigInteger toConvert = ring.RNStoBigInteger(x, y); - return new Projection<>(seq.numeric().known(toConvert), Coordinate.RIGHT) - .buildComputation(seq); - }).seq((seq, r) -> seq.numeric().open(r)); - BigInteger output = runApplication(app); - - CRTRingDefinition ring = (CRTRingDefinition) this.getFieldDefinition(); - Pair crt = Util.mapToCRT(output, ring.getLeftModulus(), ring.getRightModulus()); - - Assert.assertEquals(BigInteger.ZERO, crt.getFirst()); - Assert.assertEquals(y, crt.getSecond()); + public TestThread next() { + return new TestThread() { + + @Override + public void test() { + BigInteger x = BigInteger.valueOf(7); + BigInteger y = BigInteger.valueOf(11); + Application app = producer -> producer.seq(seq -> { + CRTRingDefinition ring = (CRTRingDefinition) this.getFieldDefinition(); + BigInteger toConvert = ring.RNStoBigInteger(x, y); + return new Projection<>(seq.numeric().known(toConvert), Coordinate.RIGHT) + .buildComputation(seq); + }).seq((seq, r) -> seq.numeric().open(r)); + BigInteger output = runApplication(app); + + CRTRingDefinition ring = (CRTRingDefinition) this.getFieldDefinition(); + Pair crt = Util.mapToCRT(output, ring.getLeftModulus(), ring.getRightModulus()); + + Assert.assertEquals(BigInteger.ZERO, crt.getFirst()); + Assert.assertEquals(y, crt.getSecond()); + } + }; } - }; } - } - - public static class TestLiftPQ - extends TestThreadFactory { - @Override - public TestThread next() { - return new TestThread() { + public static class TestLiftPQ + extends TestThreadFactory { @Override - public void test() { - CRTRingDefinition ring = (CRTRingDefinition) this.getFieldDefinition(); - BigInteger value = ring.getLeftModulus().subtract(BigInteger.TEN); - Application app = producer -> producer.seq(seq -> { - BigInteger toConvert = ring.RNStoBigInteger(value, BigInteger.ZERO); - - return new LiftPQProtocol<>(seq.numeric().known(toConvert)).buildComputation(seq); - }).seq((seq, r) -> seq.numeric().open(r)); - BigInteger output = runApplication(app); - - Pair crt = ring.integerToRNS(output); - - Assert.assertEquals(value, crt.getSecond()); + public TestThread next() { + return new TestThread() { + + @Override + public void test() { + CRTRingDefinition ring = (CRTRingDefinition) this.getFieldDefinition(); + BigInteger value = new BigInteger("12345"); + Application app = producer -> producer.seq(seq -> { + BigInteger toConvert = ring.RNStoBigInteger(value, BigInteger.ZERO); + return new LiftPQProtocol<>(seq.numeric().known(toConvert)).buildComputation(seq); + }).seq((seq, r) -> seq.numeric().open(r)); + BigInteger output = runApplication(app); + Assert.assertEquals(value, output.mod(ring.getLeftModulus())); + } + }; } - }; } - } - - public static class TestLiftQP - extends TestThreadFactory { - @Override - public TestThread next() { - return new TestThread() { + public static class TestLiftQP + extends TestThreadFactory { @Override - public void test() { - BigInteger value = BigInteger.valueOf(1234); - Application app = producer -> producer.seq(seq -> { - CRTRingDefinition ring = (CRTRingDefinition) this.getFieldDefinition(); - BigInteger toConvert = ring.RNStoBigInteger(BigInteger.ZERO, value); - return new LiftQPProtocol<>(seq.numeric().known(toConvert)).buildComputation(seq); - - }).seq((seq, r) -> seq.numeric().open(r)); - BigInteger output = runApplication(app); - - CRTRingDefinition ring = (CRTRingDefinition) this.getFieldDefinition(); - Pair crt = Util.mapToCRT(output, ring.getLeftModulus(), ring.getRightModulus()); - - Assert.assertEquals(value, crt.getFirst()); + public TestThread next() { + return new TestThread() { + + @Override + public void test() { + BigInteger value = BigInteger.valueOf(1234); + Application app = producer -> producer.seq(seq -> { + CRTRingDefinition ring = (CRTRingDefinition) this.getFieldDefinition(); + BigInteger toConvert = ring.RNStoBigInteger(BigInteger.ZERO, value); + return new LiftQPProtocol<>(seq.numeric().known(toConvert)).buildComputation(seq); + + }).seq((seq, r) -> seq.numeric().open(r)); + BigInteger output = runApplication(app); + + CRTRingDefinition ring = (CRTRingDefinition) this.getFieldDefinition(); + Pair crt = Util.mapToCRT(output, ring.getLeftModulus(), ring.getRightModulus()); + + Assert.assertEquals(value, crt.getFirst()); + } + }; } - }; } - } - public static class TestTruncp - extends TestThreadFactory { - - @Override - public TestThread next() { - return new TestThread() { + public static class TestTruncp + extends TestThreadFactory { @Override - public void test() { - BigInteger value = new BigInteger(84, new Random(1234)); - Application app = producer -> producer.seq(seq -> { - DRes x = seq.numeric().known(value); - DRes y = new Truncp<>(x).buildComputation(seq); - return seq.numeric().open(y); - }); - BigInteger output = runApplication(app); - CRTRingDefinition ring = (CRTRingDefinition) this.getFieldDefinition(); - Assert.assertEquals(value.divide(ring.getLeftModulus()), output); + public TestThread next() { + return new TestThread() { + + @Override + public void test() { + BigInteger value = new BigInteger(84, new Random(1234)); + Application app = producer -> producer.seq(seq -> { + DRes x = seq.numeric().known(value); + DRes y = new Truncp<>(x).buildComputation(seq); + return seq.numeric().open(y); + }); + BigInteger output = runApplication(app); + CRTRingDefinition ring = (CRTRingDefinition) this.getFieldDefinition(); + Assert.assertTrue(output.subtract(value.divide(ring.getLeftModulus())).compareTo(BigInteger.valueOf(this.conf.getResourcePool().getNoOfParties())) <= 0); + } + }; } - }; } - } - - public static class TestFixedPointInput - extends TestThreadFactory { - @Override - public TestThread next() { - return new TestThread() { + public static class TestFixedPointInput + extends TestThreadFactory { @Override - public void test() { + public TestThread next() { + return new TestThread() { - double input = Math.PI; + @Override + public void test() { - Application app = producer -> producer.seq(seq -> { - FixedNumeric numeric = new CRTFixedNumeric(seq); - DRes x = numeric.input(input, 1); - return numeric.open(x); - }); - BigDecimal output = runApplication(app); - Assert.assertEquals(input, output.doubleValue(), 0.001); + double input = Math.PI; + Application app = producer -> producer.seq(seq -> { + FixedNumeric numeric = new CRTFixedNumeric(seq); + DRes x = numeric.input(input, 1); + return numeric.open(x); + }); + BigDecimal output = runApplication(app); + Assert.assertEquals(input, output.doubleValue(), 0.001); + + } + }; } - }; } - } - - public static class TestFixedPointMultiplication - extends TestThreadFactory { - @Override - public TestThread next() { - return new TestThread() { + public static class TestFixedPointMultiplication + extends TestThreadFactory { @Override - public void test() { - - double a = Math.PI; - double b = 7.001; - - Application app = producer -> producer.seq(seq -> { - FixedNumeric numeric = new CRTFixedNumeric(seq); - DRes x = numeric.input(a, 1); - DRes y = numeric.input(b, 2); - return numeric.open(numeric.mult(x, y)); - }); - BigDecimal output = runApplication(app); - Assert.assertEquals(a*b, output.doubleValue(), 0.001); - + public TestThread next() { + return new TestThread() { + + @Override + public void test() { + + double a = Math.PI; + double b = 7.001; + + Application app = producer -> producer.seq(seq -> { + FixedNumeric numeric = new CRTFixedNumeric(seq); + DRes x = numeric.input(a, 1); + DRes y = numeric.input(b, 2); + return numeric.open(numeric.mult(x, y)); + }); + BigDecimal output = runApplication(app); + Assert.assertEquals(a * b, output.doubleValue(), 0.001); + + } + }; } - }; } - } - - public static class TestFixedPointDivision - extends TestThreadFactory { - @Override - public TestThread next() { - return new TestThread() { + public static class TestFixedPointDivision + extends TestThreadFactory { @Override - public void test() { + public TestThread next() { + return new TestThread() { - double a = 7.001; - double b = Math.PI; + @Override + public void test() { - Application app = producer -> producer.seq(seq -> { - FixedNumeric numeric = new CRTFixedNumeric(seq); - DRes y = numeric.input(a, 2); - return numeric.open(numeric.div(y, b)); - }); - BigDecimal output = runApplication(app); - Assert.assertEquals(a/b, output.doubleValue(), 0.001); + double a = 7.001; + double b = Math.PI; + Application app = producer -> producer.seq(seq -> { + FixedNumeric numeric = new CRTFixedNumeric(seq); + DRes y = numeric.input(a, 2); + return numeric.open(numeric.div(y, b)); + }); + BigDecimal output = runApplication(app); + Assert.assertEquals(a / b, output.doubleValue(), 0.001); + + } + }; } - }; } - } - - public static class TestFixedPointSecretDivision - extends TestThreadFactory { - @Override - public TestThread next() { - return new TestThread() { + public static class TestFixedPointSecretDivision + extends TestThreadFactory { @Override - public void test() { - - double a = 17.90 + Math.PI; - double b = 0.2; - - Application app = producer -> producer.seq(seq -> { - FixedNumeric numeric = new CRTFixedNumeric(seq); - DRes x = numeric.input(a, 1); - DRes y = numeric.input(b, 2); - return numeric.open(numeric.div(x, y)); - }); - BigDecimal output = runApplication(app); - Assert.assertEquals(a/b, output.doubleValue(), 0.01); + public TestThread next() { + return new TestThread() { + + @Override + public void test() { + + double a = 17.90 + Math.PI; + double b = 0.2; + + Application app = producer -> producer.seq(seq -> { + FixedNumeric numeric = new CRTFixedNumeric(seq); + DRes x = numeric.input(a, 1); + DRes y = numeric.input(b, 2); + return numeric.open(numeric.div(x, y)); + }); + BigDecimal output = runApplication(app); + Assert.assertEquals(a / b, output.doubleValue(), 0.01); + } + }; } - }; } - } - - public static class TestRandomModP - extends TestThreadFactory { - @Override - public TestThread next() { - return new TestThread() { + public static class TestRandomModP + extends TestThreadFactory { @Override - public void test() { - - int n = 100; - - Application, ProtocolBuilderNumeric> app = producer -> producer.seq(seq -> { - List> output = Stream.generate(() -> seq.seq(new RandomModP<>())).limit(n).map(seq.numeric()::open).collect( - Collectors.toList()); - return DRes.of(output); - }).seq((seq, output) -> DRes.of(output.stream().map(DRes::out).collect(Collectors.toList()))); - List output = runApplication(app); - - CRTRingDefinition ring = (CRTRingDefinition) this.getFieldDefinition(); - for (BigInteger r : output) { - if (r.compareTo(ring.getLeftModulus()) >= 0) { - Assert.fail(); - } - } + public TestThread next() { + return new TestThread() { + + @Override + public void test() { + + int n = 100; + + Application, ProtocolBuilderNumeric> app = producer -> producer.seq(seq -> { + List> output = Stream.generate(() -> seq.seq(new RandomModP<>())).limit(n).map(seq.numeric()::open).collect( + Collectors.toList()); + return DRes.of(output); + }).seq((seq, output) -> DRes.of(output.stream().map(DRes::out).collect(Collectors.toList()))); + List output = runApplication(app); + + CRTRingDefinition ring = (CRTRingDefinition) this.getFieldDefinition(); + for (BigInteger r : output) { + if (r.compareTo(ring.getLeftModulus()) >= 0) { + Assert.fail(); + } + } + } + }; } - }; } - } } From e06e6e3deba0ef349f9fd4f62789f54094e97627 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Lindstr=C3=B8m?= Date: Thu, 20 Oct 2022 14:49:07 +0200 Subject: [PATCH 04/38] Fix random bit --- .../java/dk/alexandra/fresco/suite/crt/CRTBuilderFactory.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTBuilderFactory.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTBuilderFactory.java index 756f027cd..5df017f3a 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTBuilderFactory.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTBuilderFactory.java @@ -164,7 +164,7 @@ public DRes mult(BigInteger a, DRes b) { @Override public DRes randomBit() { - return builder.seq(seq -> builder.numeric().known(0)); // TODO + return builder.seq(seq -> seq.numeric().known(0)); // TODO } @Override From f5500aadc62273b08cd617b9a69f9580af33d1e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Lindstr=C3=B8m?= Date: Thu, 20 Oct 2022 14:55:49 +0200 Subject: [PATCH 05/38] Add some docs --- .../fresco/suite/crt/protocols/LiftPQProtocol.java | 2 +- .../fresco/suite/crt/protocols/LiftQPProtocol.java | 2 +- .../fresco/suite/crt/protocols/RandomModP.java | 2 +- .../protocols/framework/NativeProtocolWrapper.java | 11 ++++++----- .../framework/ProtocolBuilderNumericWrapper.java | 5 +++-- 5 files changed, 12 insertions(+), 10 deletions(-) diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftPQProtocol.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftPQProtocol.java index de6783354..885b3c38a 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftPQProtocol.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftPQProtocol.java @@ -11,7 +11,7 @@ import dk.alexandra.fresco.suite.crt.protocols.framework.CRTComputation; /** - * Given a secret shared [x]p in p, output [x - ep]q for some 0 ≤ e ≤ n in q. + * Given a secret shared ([x]p, [_]q) in p, output ([x]p, [x - ep]q) for some 0 ≤ e ≤ n in q. */ public class LiftPQProtocol extends CRTComputation { diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftQPProtocol.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftQPProtocol.java index c2c8e3dbe..55db3c40f 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftQPProtocol.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftQPProtocol.java @@ -11,7 +11,7 @@ import dk.alexandra.fresco.suite.crt.protocols.framework.CRTComputation; /** - * Given a secret shared [x]q in q with x < q - np, output [x]p in p. + * Given a secret shared ([_]p, [x]q) in p, output ([x]p, [x]q). */ public class LiftQPProtocol extends CRTComputation { diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/RandomModP.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/RandomModP.java index a0df1a4ba..428a8b24c 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/RandomModP.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/RandomModP.java @@ -13,7 +13,7 @@ public class RandomModP buildComputation(ProtocolBuilderNumeric builder, - CRTNumericContext context) { + CRTNumericContext context) { return builder.seq(seq -> seq.numeric().randomElement()) .pairInPar( (seq, r) -> seq.seq(new Projection<>(r, Coordinate.LEFT)), diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/framework/NativeProtocolWrapper.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/framework/NativeProtocolWrapper.java index 92504ce81..3b91363d3 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/framework/NativeProtocolWrapper.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/framework/NativeProtocolWrapper.java @@ -4,14 +4,11 @@ import dk.alexandra.fresco.framework.network.Network; import dk.alexandra.fresco.framework.sce.resources.ResourcePool; +// When native protocols are actually evaluated in an evaluation strategy, it's unknown which of the resource pools, each protocol will use. To fix this, +// we create this wrapper which gives a reference to the relevant resource pool for each single protocol in the {@link ProtocolBuilderNumericWrapper}. public class NativeProtocolWrapper implements NativeProtocol { private final NativeProtocol protocol; - - public ResourcePoolT getResourcePool() { - return resourcePool; - } - private final ResourcePoolT resourcePool; public NativeProtocolWrapper(NativeProtocol protocol, ResourcePoolT resourcePool) { @@ -19,6 +16,10 @@ public NativeProtocolWrapper(NativeProtocol protocol, Re this.resourcePool = resourcePool; } + public ResourcePoolT getResourcePool() { + return resourcePool; + } + @Override public OutputT out() { return protocol.out(); diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/framework/ProtocolBuilderNumericWrapper.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/framework/ProtocolBuilderNumericWrapper.java index 8ea325f49..5485a6835 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/framework/ProtocolBuilderNumericWrapper.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/framework/ProtocolBuilderNumericWrapper.java @@ -5,8 +5,9 @@ import dk.alexandra.fresco.framework.builder.numeric.BuilderFactoryNumeric; import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; import dk.alexandra.fresco.framework.sce.resources.ResourcePool; -import dk.alexandra.fresco.lib.helper.SingleProtocolProducer; +// When native protocols are actually evaluated in an evaluation strategy, it's unknown which of the resource pools, each protocol will use. To fix this, +// we create a wrapper around NativeProcol's which gives each of them reference to the relevant resource pool. public class ProtocolBuilderNumericWrapper extends ProtocolBuilderNumeric { private final ResourcePoolT resourcePool; @@ -20,7 +21,7 @@ public ProtocolBuilderNumericWrapper(ProtocolBuilderNumeric builder, BuilderFact @Override public DRes append(NativeProtocol nativeProtocol) { - return builder.append(new NativeProtocolWrapper<>((NativeProtocol) nativeProtocol, resourcePool)); + return builder.append(new NativeProtocolWrapper<>((NativeProtocol) nativeProtocol, resourcePool)); } From 4ad1e94df57d702a314a178d670b7a738428f720 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Lindstr=C3=B8m?= Date: Tue, 8 Nov 2022 11:23:17 +0100 Subject: [PATCH 06/38] Fix random mod p --- .../alexandra/fresco/suite/crt/CRTBuilderFactory.java | 1 - .../fresco/suite/crt/protocols/RandomModP.java | 8 ++++---- .../fresco/suite/crt/AbstractDummyCRTTest.java | 6 +++--- .../fresco/suite/crt/AbstractSpdzCRTTest.java | 2 +- .../java/dk/alexandra/fresco/suite/crt/TestCRT.java | 11 +++++++++++ 5 files changed, 19 insertions(+), 9 deletions(-) diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTBuilderFactory.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTBuilderFactory.java index 5df017f3a..368b61f50 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTBuilderFactory.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTBuilderFactory.java @@ -169,7 +169,6 @@ public DRes randomBit() { @Override public DRes randomElement() { - return builder.par(par -> { Numeric l = context.leftNumeric(par); Numeric r = context.rightNumeric(par); diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/RandomModP.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/RandomModP.java index 428a8b24c..1faaf4bac 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/RandomModP.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/RandomModP.java @@ -14,10 +14,10 @@ public class RandomModP buildComputation(ProtocolBuilderNumeric builder, CRTNumericContext context) { + //TODO: This assumes that the bit length of q is three that of p return builder.seq(seq -> seq.numeric().randomElement()) - .pairInPar( - (seq, r) -> seq.seq(new Projection<>(r, Coordinate.LEFT)), - (seq, r) -> seq.seq(new LiftPQProtocol<>(r))) - .seq((seq, rPair) -> seq.numeric().add(rPair.getFirst(), rPair.getSecond())); + .seq((seq, r) -> seq.seq(new Truncp(r))) + .seq((seq, r) -> seq.seq(new Truncp(r))) + .seq((seq, r) -> seq.seq(new Truncp(r))); } } diff --git a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractDummyCRTTest.java b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractDummyCRTTest.java index 85dc2ad55..7c5af32d5 100644 --- a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractDummyCRTTest.java +++ b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractDummyCRTTest.java @@ -35,10 +35,12 @@ */ public class AbstractDummyCRTTest { + // Note that the modulus on the right should have twice the bit length of that to the left in order for RandomModP to + // work correctly. protected static final FieldDefinition DEFAULT_FIELD_LEFT = MersennePrimeFieldDefinition.find(64); protected static final FieldDefinition DEFAULT_FIELD_RIGHT = new BigIntegerFieldDefinition( - new BigInteger(128 + 40, new Random(1234)).nextProbablePrime()); + new BigInteger(152 + 40, new Random(1234)).nextProbablePrime()); public void runTest( TestThreadRunner.TestThreadFactory, ProtocolBuilderNumeric> f, @@ -59,8 +61,6 @@ public void runTest( for (int playerId : netConf.keySet()) { - - BatchEvaluationStrategy> batchEvaluationStrategy = new CRTSequentialStrategy<>(); DummyArithmeticResourcePool rpLeft = new DummyArithmeticResourcePoolImpl(playerId, diff --git a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSpdzCRTTest.java b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSpdzCRTTest.java index 1154cbd66..d1f25415a 100644 --- a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSpdzCRTTest.java +++ b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSpdzCRTTest.java @@ -59,7 +59,7 @@ public class AbstractSpdzCRTTest { protected static final FieldDefinition DEFAULT_FIELD_LEFT = MersennePrimeFieldDefinition.find(64); protected static final FieldDefinition DEFAULT_FIELD_RIGHT = new BigIntegerFieldDefinition( - new BigInteger(128 + 40, new Random(1234)).nextProbablePrime()); + new BigInteger(152 + 40, new Random(1234)).nextProbablePrime()); private static final int PRG_SEED_LENGTH = 256; private static Drbg getDrbg(int myId) { diff --git a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/TestCRT.java b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/TestCRT.java index f7f3a85d5..b2765ef69 100644 --- a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/TestCRT.java +++ b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/TestCRT.java @@ -6,6 +6,7 @@ import dk.alexandra.fresco.lib.common.math.integer.TestProductAndSum.TestSum; import dk.alexandra.fresco.lib.common.math.integer.binary.BinaryOperationsTests.TestGenerateRandomBitMask; import dk.alexandra.fresco.lib.common.math.integer.binary.BinaryOperationsTests.TestRightShift; +import dk.alexandra.fresco.lib.common.math.integer.division.DivisionTests; import dk.alexandra.fresco.lib.common.math.integer.linalg.LinAlgTests.TestInnerProductClosed; import dk.alexandra.fresco.lib.common.math.integer.linalg.LinAlgTests.TestInnerProductOpen; import dk.alexandra.fresco.lib.common.math.integer.mod.Mod2mTests.TestMod2m; @@ -83,6 +84,16 @@ public void testMultAndAdd() { new AbstractSpdzCRTTest().runTest(new TestSumAndMult<>(), EvaluationStrategy.SEQUENTIAL, PreprocessingStrategy.MASCOT, 2); } + @Test + public void testDivision() { + new AbstractSpdzCRTTest().runTest(new DivisionTests.TestDivision<>(), EvaluationStrategy.SEQUENTIAL, PreprocessingStrategy.DUMMY, 2); + } + + @Test + public void testDivisionKnownDivisor() { + new AbstractSpdzCRTTest().runTest(new DivisionTests.TestKnownDivisorDivision<>(), EvaluationStrategy.SEQUENTIAL, PreprocessingStrategy.DUMMY, 2); + } + @Test public void testRightShift() { new AbstractSpdzCRTTest().runTest(new TestRightShift<>(), EvaluationStrategy.SEQUENTIAL, PreprocessingStrategy.DUMMY, 2); From 2c2405d1513b67998eab0ba7dcbbae051848aff7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Lindstr=C3=B8m?= Date: Wed, 9 Nov 2022 09:55:03 +0100 Subject: [PATCH 07/38] Add dummy random mod p --- .../suite/crt/protocols/LiftQPProtocol.java | 57 +++++++++++-------- .../suite/crt/protocols/RandomModP.java | 7 +-- .../fresco/suite/crt/AbstractSpdzCRTTest.java | 4 +- 3 files changed, 37 insertions(+), 31 deletions(-) diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftQPProtocol.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftQPProtocol.java index 55db3c40f..d6976f28c 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftQPProtocol.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftQPProtocol.java @@ -10,42 +10,49 @@ import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; import dk.alexandra.fresco.suite.crt.protocols.framework.CRTComputation; +import java.math.BigInteger; + /** * Given a secret shared ([_]p, [x]q) in p, output ([x]p, [x]q). */ public class LiftQPProtocol extends - CRTComputation { + CRTComputation { + + private final DRes value; + private CRTSInt r; - private final DRes value; - private CRTSInt r; + public LiftQPProtocol(DRes value) { + this.value = value; + } - public LiftQPProtocol(DRes value) { - this.value = value; - } + @Override + public DRes buildComputation(ProtocolBuilderNumeric builder, + CRTNumericContext context) { + //q' = q / 2 is divisible by p, so adding it to the input only affects the output by q mod p = 1 if there's an overflow. + BigInteger qPrime = new BigInteger("3138550867693340351802905239100779285196644626743924002860"); - @Override - public DRes buildComputation(ProtocolBuilderNumeric builder, - CRTNumericContext context) { - return builder.seq(seq -> seq.append(new CorrelatedNoiseProtocol<>())).seq((seq, noise) -> { - this.r = (CRTSInt) noise.out(); + return builder.seq(seq -> seq.append(new CorrelatedNoiseProtocol<>())).seq((seq, noise) -> { + this.r = (CRTSInt) noise.out(); + return seq.numeric().add(qPrime, value); + }).seq((seq, value) -> { - // Add noise to the right value. The left is ignored. - DRes xBar = context.rightNumeric(seq).add(((CRTSInt) value.out()).getRight(), r.getRight()); - CRTSInt output = new CRTSInt(context.leftNumeric(seq).known(0), xBar); - return seq.numeric().open(output); // TODO: We only need to open the right, so we should create an openRight function + // Add noise to the right value. The left is ignored. + DRes xBar = context.rightNumeric(seq).add(((CRTSInt) value.out()).getRight(), r.getRight()); + CRTSInt output = new CRTSInt(context.leftNumeric(seq).known(0), xBar); + return seq.numeric().open(output); // TODO: We only need to open the right, so we should create an openRight function - // TODO: We haven't added the noise (c from the protocol) + // TODO: We haven't added the noise (c from the protocol) - }).seq((seq, xBarOpen) -> { - Numeric left = context.leftNumeric(seq); + }).seq((seq, xBarOpen) -> { + Numeric left = context.leftNumeric(seq); - // Extract the right value from xBar - DRes xBarLeft = left.known(Util.mapToCRT(xBarOpen, context.getLeftModulus(), context.getRightModulus()).getSecond()); + // Extract the right value from xBar + DRes xBarLeft = left.known(Util.mapToCRT(xBarOpen, context.getLeftModulus(), context.getRightModulus()).getSecond()); - // Remove the noise and return - DRes adjusted = left.sub(xBarLeft, r.getLeft()); - return new CRTSInt(adjusted, ((CRTSInt) value.out()).getRight()); - }); - } + // Remove the noise and return + DRes adjusted = left.sub(xBarLeft, r.getLeft()); + return new CRTSInt(adjusted, ((CRTSInt) value.out()).getRight()); + }); + } } diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/RandomModP.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/RandomModP.java index 1faaf4bac..f021c8085 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/RandomModP.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/RandomModP.java @@ -14,10 +14,7 @@ public class RandomModP buildComputation(ProtocolBuilderNumeric builder, CRTNumericContext context) { - //TODO: This assumes that the bit length of q is three that of p - return builder.seq(seq -> seq.numeric().randomElement()) - .seq((seq, r) -> seq.seq(new Truncp(r))) - .seq((seq, r) -> seq.seq(new Truncp(r))) - .seq((seq, r) -> seq.seq(new Truncp(r))); + //TODO + return builder.numeric().known(7); } } diff --git a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSpdzCRTTest.java b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSpdzCRTTest.java index d1f25415a..e2c1c5c13 100644 --- a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSpdzCRTTest.java +++ b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSpdzCRTTest.java @@ -58,8 +58,10 @@ public class AbstractSpdzCRTTest { protected static final FieldDefinition DEFAULT_FIELD_LEFT = MersennePrimeFieldDefinition.find(64); + + // q = p^3 + 139p + 1 where q = DEFAULT_FIELD_RIGHT and p = DEFAULT_FIELD_LEFT. protected static final FieldDefinition DEFAULT_FIELD_RIGHT = new BigIntegerFieldDefinition( - new BigInteger(152 + 40, new Random(1234)).nextProbablePrime()); + new BigInteger("6277101735386680703605810478201558570393289253487848005721")); //152 + 40, new Random(1234)).nextProbablePrime()); private static final int PRG_SEED_LENGTH = 256; private static Drbg getDrbg(int myId) { From 83c39cde5f6fc9d3f96341fa65ca1b8ff58a55ab Mon Sep 17 00:00:00 2001 From: Mikkel Wienberg Madsen Date: Tue, 15 Nov 2022 11:08:33 +0100 Subject: [PATCH 08/38] Attempt outline of SemiHonestCRTDataSupplier --- .../fresco/framework/builder/BuildStep.java | 2 +- .../fresco/demo/InputSumExample.java | 6 +- .../fresco/suite/crt/CRTBuilderFactory.java | 8 +- .../fresco/suite/crt/CRTProtocolSuite.java | 8 ++ .../resource/CRTSemiHonestDataSupplier.java | 99 +++++++++++++++++++ .../crt/AbstractSemiHonestDummyCRTTest.java | 95 ++++++++++++++++++ .../alexandra/fresco/suite/crt/TestCRT.java | 14 +-- .../TestSpdzMacCheckTamperWithValues.java | 11 +-- 8 files changed, 224 insertions(+), 19 deletions(-) create mode 100644 suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java create mode 100644 suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSemiHonestDummyCRTTest.java diff --git a/core/src/main/java/dk/alexandra/fresco/framework/builder/BuildStep.java b/core/src/main/java/dk/alexandra/fresco/framework/builder/BuildStep.java index 2cbba9bdb..24b6edd78 100644 --- a/core/src/main/java/dk/alexandra/fresco/framework/builder/BuildStep.java +++ b/core/src/main/java/dk/alexandra/fresco/framework/builder/BuildStep.java @@ -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 the type of input for this binary step (the previous step's output). * @param the type iof builder, currently either numeric or binary diff --git a/demos/sum/src/main/java/dk/alexandra/fresco/demo/InputSumExample.java b/demos/sum/src/main/java/dk/alexandra/fresco/demo/InputSumExample.java index e026b9122..548d485c9 100644 --- a/demos/sum/src/main/java/dk/alexandra/fresco/demo/InputSumExample.java +++ b/demos/sum/src/main/java/dk/alexandra/fresco/demo/InputSumExample.java @@ -43,7 +43,11 @@ public void runApplication( network.sendToAll(ByteBuffer.allocate(4).putInt(myArraySize).array()); List 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 diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTBuilderFactory.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTBuilderFactory.java index 368b61f50..55e84938f 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTBuilderFactory.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTBuilderFactory.java @@ -16,7 +16,7 @@ public class CRTBuilderFactory implements BuilderFactoryNumeric { - private final CRTNumericContext context; + private final CRTNumericContext context; private final BuilderFactoryNumeric left; private final BuilderFactoryNumeric right; private final BigInteger p, q; @@ -39,9 +39,9 @@ public CRTBuilderFactory(ResourcePoolA resourcePoolLeft, this.resourcePoolRight = resourcePoolRight; this.p = resourcePoolLeft.getModulus(); this.q = resourcePoolRight.getModulus(); - this.context = new CRTNumericContext( - p.bitLength() + q.bitLength() - 40, //TODO - resourcePoolLeft.getMyId(), resourcePoolLeft.getNoOfParties(), left, right, p, q, resourcePoolLeft, resourcePoolRight); + this.context = new CRTNumericContext<>( + p.bitLength() + q.bitLength() - 40, //TODO + resourcePoolLeft.getMyId(), resourcePoolLeft.getNoOfParties(), left, right, p, q, resourcePoolLeft, resourcePoolRight); } @Override diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTProtocolSuite.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTProtocolSuite.java index e894dded8..b3eb71fd0 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTProtocolSuite.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTProtocolSuite.java @@ -20,6 +20,14 @@ public CRTProtocolSuite(BuilderFactoryNumeric left, this.right = right; } + public BuilderFactoryNumeric getLeft() { + return left; + } + + public BuilderFactoryNumeric getRight() { + return right; + } + @Override public BuilderFactoryNumeric init(CRTResourcePool resourcePool) { Pair resourcePools = resourcePool.getSubResourcePools(); diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java new file mode 100644 index 000000000..f71773768 --- /dev/null +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java @@ -0,0 +1,99 @@ +package dk.alexandra.fresco.suite.crt.datatypes.resource; + +import dk.alexandra.fresco.framework.Application; +import dk.alexandra.fresco.framework.DRes; +import dk.alexandra.fresco.framework.builder.ComputationParallel; +import dk.alexandra.fresco.framework.builder.numeric.Numeric; +import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool; +import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; +import dk.alexandra.fresco.framework.builder.numeric.field.FieldDefinition; +import dk.alexandra.fresco.framework.util.Pair; +import dk.alexandra.fresco.suite.crt.CRTProtocolSuite; +import dk.alexandra.fresco.suite.crt.Util; +import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; + +import java.math.BigInteger; +import java.security.SecureRandom; +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +public class CRTSemiHonestDataSupplier + implements CRTDataSupplier { + + private final int myId; + private final int players; + private final CRTProtocolSuite suite; + private final Random random; + private final ArrayDeque noisePairs; + private final FieldDefinition fp; + private final FieldDefinition fq; + + private class NoiseGenerator implements ComputationParallel, ProtocolBuilderNumeric> { + + private final int batchSize; + + public NoiseGenerator(int batchSize) { + this.batchSize = batchSize; + } + + @Override + public DRes> buildComputation(ProtocolBuilderNumeric builder) { + return builder.par(par -> { + Numeric numeric = par.numeric(); + List list = new ArrayList<>(batchSize); + for (int i = 0; i < batchSize; i++) { + // TODO: Actually do the protocol + BigInteger r = Util.randomBigInteger(random, suite.getLeft().getBasicNumericContext().getModulus()); + BigInteger l = Util.randomBigInteger(random, BigInteger.valueOf(players)); + CRTSInt noisePair = new CRTSInt( + numeric.known(fp.createElement(r).toBigInteger()), + numeric.known(fq.createElement(r.add(l.multiply(fp.getModulus()))).toBigInteger()) + ); + list.add(noisePair); + } + return DRes.of(list); + }); + } + } + + public CRTSemiHonestDataSupplier(int myId, int players, CRTProtocolSuite suite) { + this.myId = myId; + this.players = players; + this.suite = suite; + + this.random = new SecureRandom(); + this.fp = suite.getLeft().getBasicNumericContext().getFieldDefinition(); + this.fq = suite.getLeft().getBasicNumericContext().getFieldDefinition(); + + this.noisePairs = new ArrayDeque<>(); + + Application app = builder -> { + DRes> res = builder.seq(new NoiseGenerator(10)); + return builder.seq(seq -> { + noisePairs.addAll(res.out()); + return null; + }); + }; + suite.getLeft().createSequential().seq(app); + } + + @Override + public CRTSInt getCorrelatedNoise() { + return noisePairs.pop(); + } + + @Override + public CRTSInt getRandomBit() { + return new CRTSInt( + suite.getLeft().createSequential().numeric().randomBit(), + suite.getRight().createSequential().numeric().randomBit() + ); + } + + @Override + public Pair getFieldDefinitions() { + return new Pair<>(fp, fq); + } +} diff --git a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSemiHonestDummyCRTTest.java b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSemiHonestDummyCRTTest.java new file mode 100644 index 000000000..cc55c7e0e --- /dev/null +++ b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSemiHonestDummyCRTTest.java @@ -0,0 +1,95 @@ +package dk.alexandra.fresco.suite.crt; + +import dk.alexandra.fresco.framework.ProtocolEvaluator; +import dk.alexandra.fresco.framework.TestThreadRunner; +import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; +import dk.alexandra.fresco.framework.builder.numeric.field.BigIntegerFieldDefinition; +import dk.alexandra.fresco.framework.builder.numeric.field.FieldDefinition; +import dk.alexandra.fresco.framework.builder.numeric.field.MersennePrimeFieldDefinition; +import dk.alexandra.fresco.framework.configuration.NetworkConfiguration; +import dk.alexandra.fresco.framework.configuration.NetworkUtil; +import dk.alexandra.fresco.framework.network.socket.SocketNetwork; +import dk.alexandra.fresco.framework.sce.SecureComputationEngine; +import dk.alexandra.fresco.framework.sce.SecureComputationEngineImpl; +import dk.alexandra.fresco.framework.sce.evaluator.BatchEvaluationStrategy; +import dk.alexandra.fresco.framework.sce.evaluator.BatchedProtocolEvaluator; +import dk.alexandra.fresco.framework.sce.evaluator.EvaluationStrategy; +import dk.alexandra.fresco.lib.field.integer.BasicNumericContext; +import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTDataSupplier; +import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTResourcePool; +import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTResourcePoolImpl; +import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTSemiHonestDataSupplier; +import dk.alexandra.fresco.suite.crt.protocols.framework.CRTSequentialStrategy; +import dk.alexandra.fresco.suite.dummy.arithmetic.DummyArithmeticBuilderFactory; +import dk.alexandra.fresco.suite.dummy.arithmetic.DummyArithmeticResourcePool; +import dk.alexandra.fresco.suite.dummy.arithmetic.DummyArithmeticResourcePoolImpl; + +import java.math.BigInteger; +import java.util.*; + +/** + * Abstract class which handles a lot of boiler plate testing code. This makes running a single test + * using different parameters quite easy. + */ +public class AbstractSemiHonestDummyCRTTest { + + // Note that the modulus on the right should have twice the bit length of that to the left in order for RandomModP to + // work correctly. + protected static final FieldDefinition DEFAULT_FIELD_LEFT = + MersennePrimeFieldDefinition.find(64); + protected static final FieldDefinition DEFAULT_FIELD_RIGHT = new BigIntegerFieldDefinition( + new BigInteger(152 + 40, new Random(1234)).nextProbablePrime()); + + public void runTest( + TestThreadRunner.TestThreadFactory, ProtocolBuilderNumeric> f, + EvaluationStrategy evalStrategy, int noOfParties) { + + List ports = new ArrayList<>(noOfParties); + for (int i = 1; i <= noOfParties; i++) { + ports.add(9000 + i * (noOfParties - 1)); + } + + Map netConf = + NetworkUtil.getNetworkConfigurations(ports); + Map, + ProtocolBuilderNumeric> + > conf = new HashMap<>(); + + for (int playerId : netConf.keySet()) { + + BatchEvaluationStrategy> batchEvaluationStrategy = + new CRTSequentialStrategy<>(); + DummyArithmeticResourcePool rpLeft = new DummyArithmeticResourcePoolImpl(playerId, + noOfParties, DEFAULT_FIELD_LEFT); + DummyArithmeticResourcePool rpRight = new DummyArithmeticResourcePoolImpl(playerId, + noOfParties, DEFAULT_FIELD_RIGHT); + + CRTProtocolSuite ps = new CRTProtocolSuite<>( + new DummyArithmeticBuilderFactory(new BasicNumericContext(DEFAULT_FIELD_LEFT.getBitLength() - 24, + playerId, noOfParties, DEFAULT_FIELD_LEFT, 16, 40)), + new DummyArithmeticBuilderFactory(new BasicNumericContext(DEFAULT_FIELD_RIGHT.getBitLength()- 40, + playerId, noOfParties, DEFAULT_FIELD_RIGHT, 16, 40))); + ProtocolEvaluator> evaluator = + new BatchedProtocolEvaluator<>(batchEvaluationStrategy, ps); + + NetworkConfiguration partyNetConf = netConf.get(playerId); + SecureComputationEngine, ProtocolBuilderNumeric> sce = + new SecureComputationEngineImpl<>(ps, evaluator); + + CRTDataSupplier dataSupplier = new CRTSemiHonestDataSupplier<>(playerId, noOfParties, ps); + + TestThreadRunner.TestThreadConfiguration< + CRTResourcePool, + ProtocolBuilderNumeric> ttc = + new TestThreadRunner.TestThreadConfiguration<>(sce, + () -> new CRTResourcePoolImpl<>(playerId, noOfParties, dataSupplier, rpLeft, rpRight), + () -> new SocketNetwork(partyNetConf)); + conf.put(playerId, ttc); + } + + TestThreadRunner.run(f, conf); + } + +} diff --git a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/TestCRT.java b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/TestCRT.java index b2765ef69..eb6d7a896 100644 --- a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/TestCRT.java +++ b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/TestCRT.java @@ -111,40 +111,40 @@ public void testMults() { @Test public void testCorrelatedNoise() { - new AbstractDummyCRTTest() + new AbstractSemiHonestDummyCRTTest() .runTest(new TestCorrelatedNoise<>(), EvaluationStrategy.SEQUENTIAL, 2); } @Test public void testGenerateRandomBitMask() { - new AbstractDummyCRTTest().runTest(new TestGenerateRandomBitMask<>(), EvaluationStrategy.SEQUENTIAL, 2); + new AbstractSemiHonestDummyCRTTest().runTest(new TestGenerateRandomBitMask<>(), EvaluationStrategy.SEQUENTIAL, 2); } @Test public void testRandomBit() { - new AbstractDummyCRTTest().runTest(new TestRandomBit<>(), EvaluationStrategy.SEQUENTIAL, 2); + new AbstractSemiHonestDummyCRTTest().runTest(new TestRandomBit<>(), EvaluationStrategy.SEQUENTIAL, 2); } @Test public void testLiftPQ() { - new AbstractDummyCRTTest().runTest(new TestLiftPQ<>(), EvaluationStrategy.SEQUENTIAL, 2); + new AbstractSemiHonestDummyCRTTest().runTest(new TestLiftPQ<>(), EvaluationStrategy.SEQUENTIAL, 2); } @Test public void testLiftQP() { - new AbstractDummyCRTTest().runTest(new TestLiftQP<>(), EvaluationStrategy.SEQUENTIAL, 2); + new AbstractSemiHonestDummyCRTTest().runTest(new TestLiftQP<>(), EvaluationStrategy.SEQUENTIAL, 2); } @Test public void testProjectionLeft() { - new AbstractDummyCRTTest() + new AbstractSemiHonestDummyCRTTest() .runTest(new TestProjectionLeft<>(), EvaluationStrategy.SEQUENTIAL, 2); } @Test public void testProjectionRight() { - new AbstractDummyCRTTest() + new AbstractSemiHonestDummyCRTTest() .runTest(new TestProjectionRight<>(), EvaluationStrategy.SEQUENTIAL, 2); } diff --git a/suite/spdz/src/test/java/dk/alexandra/fresco/suite/spdz/maccheck/TestSpdzMacCheckTamperWithValues.java b/suite/spdz/src/test/java/dk/alexandra/fresco/suite/spdz/maccheck/TestSpdzMacCheckTamperWithValues.java index afea133d3..29f3b9343 100644 --- a/suite/spdz/src/test/java/dk/alexandra/fresco/suite/spdz/maccheck/TestSpdzMacCheckTamperWithValues.java +++ b/suite/spdz/src/test/java/dk/alexandra/fresco/suite/spdz/maccheck/TestSpdzMacCheckTamperWithValues.java @@ -1,7 +1,5 @@ package dk.alexandra.fresco.suite.spdz.maccheck; -import static org.junit.Assert.assertThat; - import dk.alexandra.fresco.framework.Application; import dk.alexandra.fresco.framework.DRes; import dk.alexandra.fresco.framework.MaliciousException; @@ -9,18 +7,19 @@ import dk.alexandra.fresco.framework.TestThreadRunner.TestThreadFactory; import dk.alexandra.fresco.framework.builder.numeric.Numeric; import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; -import dk.alexandra.fresco.framework.builder.numeric.field.BigIntegerFieldDefinition; import dk.alexandra.fresco.framework.builder.numeric.field.MersennePrimeFieldDefinition; -import dk.alexandra.fresco.framework.util.ModulusFinder; import dk.alexandra.fresco.framework.value.SInt; import dk.alexandra.fresco.suite.spdz.AbstractSpdzTest; import dk.alexandra.fresco.suite.spdz.SpdzResourcePool; import dk.alexandra.fresco.suite.spdz.configuration.PreprocessingStrategy; import dk.alexandra.fresco.suite.spdz.datatypes.SpdzSInt; -import java.math.BigInteger; import org.hamcrest.core.IsInstanceOf; import org.junit.Test; +import java.math.BigInteger; + +import static org.junit.Assert.assertEquals; + public class TestSpdzMacCheckTamperWithValues extends AbstractSpdzTest { private static MersennePrimeFieldDefinition definition = MersennePrimeFieldDefinition.find(8); @@ -72,7 +71,7 @@ public void test() { try { runApplication(app); } catch (Exception e) { - assertThat(e.getCause(), IsInstanceOf.instanceOf(MaliciousException.class)); + assertEquals(e.getCause(), IsInstanceOf.instanceOf(MaliciousException.class)); } } }; From 6b7214a82549ffa8ad2e6590a289458e957fcd73 Mon Sep 17 00:00:00 2001 From: Tore Kasper Frederiksen Date: Thu, 17 Nov 2022 09:44:58 +0100 Subject: [PATCH 09/38] Attempt at preprocessing --- .../datatypes/resource/CRTDataSupplier.java | 4 +- .../resource/CRTDummyDataSupplier.java | 3 +- .../resource/CRTResourcePoolImpl.java | 6 +- .../resource/CRTSemiHonestDataSupplier.java | 59 +++++++++---------- .../protocols/CorrelatedNoiseProtocol.java | 8 ++- .../suite/crt/protocols/LiftPQProtocol.java | 3 +- .../suite/crt/protocols/LiftQPProtocol.java | 3 +- .../crt/AbstractSemiHonestDummyCRTTest.java | 20 +++++-- .../fresco/suite/crt/AbstractSpdzCRTTest.java | 5 +- .../fresco/suite/crt/BasicCRTTests.java | 2 +- 10 files changed, 65 insertions(+), 48 deletions(-) diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDataSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDataSupplier.java index e02d9396f..e1287ae06 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDataSupplier.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDataSupplier.java @@ -1,5 +1,6 @@ package dk.alexandra.fresco.suite.crt.datatypes.resource; +import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; import dk.alexandra.fresco.framework.builder.numeric.field.FieldDefinition; import dk.alexandra.fresco.framework.util.Pair; import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; @@ -11,7 +12,8 @@ public interface CRTDataSupplier { * * @return r */ - CRTSInt getCorrelatedNoise(); + CRTSInt getCorrelatedNoise( + ProtocolBuilderNumeric builder); /** * Supply the next random bit diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDummyDataSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDummyDataSupplier.java index 29e353e32..beeb69e9a 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDummyDataSupplier.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDummyDataSupplier.java @@ -1,5 +1,6 @@ package dk.alexandra.fresco.suite.crt.datatypes.resource; +import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; import dk.alexandra.fresco.framework.builder.numeric.field.FieldDefinition; import dk.alexandra.fresco.framework.util.Pair; import dk.alexandra.fresco.framework.value.SInt; @@ -32,7 +33,7 @@ public CRTDummyDataSupplier(int myId, int players, FieldDefinition leftField, } @Override - public CRTSInt getCorrelatedNoise() { + public CRTSInt getCorrelatedNoise(ProtocolBuilderNumeric builder) { BigInteger r = Util.randomBigInteger(random, fp.getModulus()); BigInteger l = Util .randomBigInteger(random, BigInteger.valueOf(players)); diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTResourcePoolImpl.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTResourcePoolImpl.java index 7e3262f2b..6dc41c1df 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTResourcePoolImpl.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTResourcePoolImpl.java @@ -19,8 +19,8 @@ public CRTResourcePoolImpl(int myId, int noOfPlayers, CRTDataSupplier dataSuppli super(myId, noOfPlayers); this.dataSupplier = dataSupplier; this.definition = new CRTRingDefinition( - dataSupplier.getFieldDefinitions().getFirst().getModulus(), - dataSupplier.getFieldDefinitions().getSecond().getModulus()); + resourcePoolLeft.getFieldDefinition().getModulus(), + resourcePoolRight.getFieldDefinition().getModulus()); this.resourcePools = new Pair<>(resourcePoolLeft, resourcePoolRight); } @@ -31,7 +31,7 @@ public Pair getSubResourcePools() { @Override public Pair getFieldDefinitions() { - return dataSupplier.getFieldDefinitions(); + return new Pair<>(resourcePools.getFirst().getFieldDefinition(), resourcePools.getSecond().getFieldDefinition()); } @Override diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java index f71773768..f85080ef9 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java @@ -1,6 +1,5 @@ package dk.alexandra.fresco.suite.crt.datatypes.resource; -import dk.alexandra.fresco.framework.Application; import dk.alexandra.fresco.framework.DRes; import dk.alexandra.fresco.framework.builder.ComputationParallel; import dk.alexandra.fresco.framework.builder.numeric.Numeric; @@ -8,7 +7,6 @@ import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; import dk.alexandra.fresco.framework.builder.numeric.field.FieldDefinition; import dk.alexandra.fresco.framework.util.Pair; -import dk.alexandra.fresco.suite.crt.CRTProtocolSuite; import dk.alexandra.fresco.suite.crt.Util; import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; @@ -19,18 +17,19 @@ import java.util.List; import java.util.Random; -public class CRTSemiHonestDataSupplier +public class CRTSemiHonestDataSupplier implements CRTDataSupplier { private final int myId; private final int players; - private final CRTProtocolSuite suite; + private final CRTResourcePool resourcePool; private final Random random; - private final ArrayDeque noisePairs; - private final FieldDefinition fp; - private final FieldDefinition fq; + private ArrayDeque noisePairs = new ArrayDeque<>(); + private final FieldDefinition leftDef; + private final FieldDefinition rightDef; - private class NoiseGenerator implements ComputationParallel, ProtocolBuilderNumeric> { + private class NoiseGenerator implements ComputationParallel, ProtocolBuilderNumeric> { private final int batchSize; @@ -39,61 +38,57 @@ public NoiseGenerator(int batchSize) { } @Override - public DRes> buildComputation(ProtocolBuilderNumeric builder) { + public DRes> buildComputation(ProtocolBuilderNumeric builder) { return builder.par(par -> { Numeric numeric = par.numeric(); List list = new ArrayList<>(batchSize); for (int i = 0; i < batchSize; i++) { // TODO: Actually do the protocol - BigInteger r = Util.randomBigInteger(random, suite.getLeft().getBasicNumericContext().getModulus()); + BigInteger r = Util.randomBigInteger(random, resourcePool.getFieldDefinitions().getFirst().getModulus()); BigInteger l = Util.randomBigInteger(random, BigInteger.valueOf(players)); CRTSInt noisePair = new CRTSInt( - numeric.known(fp.createElement(r).toBigInteger()), - numeric.known(fq.createElement(r.add(l.multiply(fp.getModulus()))).toBigInteger()) + numeric.known(leftDef.createElement(r).toBigInteger()), + numeric.known(rightDef.createElement(r.add(l.multiply(leftDef.getModulus()))).toBigInteger()) ); list.add(noisePair); } return DRes.of(list); + }).seq((seq, res) -> { + ArrayDeque resDeque = new ArrayDeque<>(); + resDeque.addAll(res); + return ()-> resDeque; }); } } - public CRTSemiHonestDataSupplier(int myId, int players, CRTProtocolSuite suite) { + public CRTSemiHonestDataSupplier(int myId, int players, CRTResourcePool resourcePool) { this.myId = myId; this.players = players; - this.suite = suite; + this.resourcePool = resourcePool; this.random = new SecureRandom(); - this.fp = suite.getLeft().getBasicNumericContext().getFieldDefinition(); - this.fq = suite.getLeft().getBasicNumericContext().getFieldDefinition(); + this.leftDef = resourcePool.getFieldDefinitions().getFirst(); + this.rightDef = resourcePool.getFieldDefinitions().getFirst(); - this.noisePairs = new ArrayDeque<>(); - - Application app = builder -> { - DRes> res = builder.seq(new NoiseGenerator(10)); - return builder.seq(seq -> { - noisePairs.addAll(res.out()); - return null; - }); - }; - suite.getLeft().createSequential().seq(app); } @Override - public CRTSInt getCorrelatedNoise() { + public CRTSInt getCorrelatedNoise(ProtocolBuilderNumeric builder) { + if (!noisePairs.isEmpty()) { + return noisePairs.pop(); + } + noisePairs = builder.seq(new NoiseGenerator(10)).out(); return noisePairs.pop(); } @Override public CRTSInt getRandomBit() { - return new CRTSInt( - suite.getLeft().createSequential().numeric().randomBit(), - suite.getRight().createSequential().numeric().randomBit() - ); + return null; } @Override public Pair getFieldDefinitions() { - return new Pair<>(fp, fq); + return new Pair<>(leftDef, rightDef); } } diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/CorrelatedNoiseProtocol.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/CorrelatedNoiseProtocol.java index 06d1e37f9..0c0f54924 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/CorrelatedNoiseProtocol.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/CorrelatedNoiseProtocol.java @@ -1,7 +1,9 @@ package dk.alexandra.fresco.suite.crt.protocols; import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool; +import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; import dk.alexandra.fresco.framework.network.Network; +import dk.alexandra.fresco.framework.sce.SecureComputationEngine; import dk.alexandra.fresco.framework.value.SInt; import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTResourcePool; @@ -11,13 +13,17 @@ public class CorrelatedNoiseProtocol extends CRTNativeProtocol { + private final ProtocolBuilderNumeric builder; private CRTSInt r; + public CorrelatedNoiseProtocol(ProtocolBuilderNumeric builder) { + this.builder = builder; + } @Override public EvaluationStatus evaluate(int round, CRTResourcePool resourcePool, Network network) { - this.r = resourcePool.getDataSupplier().getCorrelatedNoise(); + this.r = resourcePool.getDataSupplier().getCorrelatedNoise(builder); return EvaluationStatus.IS_DONE; } diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftPQProtocol.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftPQProtocol.java index 885b3c38a..6420f765f 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftPQProtocol.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftPQProtocol.java @@ -27,7 +27,8 @@ public LiftPQProtocol(DRes value) { @Override public DRes buildComputation(ProtocolBuilderNumeric builder, CRTNumericContext context) { - return builder.seq(seq -> seq.append(new CorrelatedNoiseProtocol<>())).seq((seq, noise) -> { + DRes noise = new CorrelatedNoiseProtocol<>(builder); + return builder.seq(seq -> { this.r = (CRTSInt) noise.out(); // Add noise to the left value. The right is ignored. diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftQPProtocol.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftQPProtocol.java index d6976f28c..3ddee011f 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftQPProtocol.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftQPProtocol.java @@ -31,7 +31,8 @@ public DRes buildComputation(ProtocolBuilderNumeric builder, //q' = q / 2 is divisible by p, so adding it to the input only affects the output by q mod p = 1 if there's an overflow. BigInteger qPrime = new BigInteger("3138550867693340351802905239100779285196644626743924002860"); - return builder.seq(seq -> seq.append(new CorrelatedNoiseProtocol<>())).seq((seq, noise) -> { + DRes noise = new CorrelatedNoiseProtocol<>(builder); + return builder.seq(seq -> { this.r = (CRTSInt) noise.out(); return seq.numeric().add(qPrime, value); }).seq((seq, value) -> { diff --git a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSemiHonestDummyCRTTest.java b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSemiHonestDummyCRTTest.java index cc55c7e0e..ced1e4b7a 100644 --- a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSemiHonestDummyCRTTest.java +++ b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSemiHonestDummyCRTTest.java @@ -8,6 +8,7 @@ import dk.alexandra.fresco.framework.builder.numeric.field.MersennePrimeFieldDefinition; import dk.alexandra.fresco.framework.configuration.NetworkConfiguration; import dk.alexandra.fresco.framework.configuration.NetworkUtil; +import dk.alexandra.fresco.framework.network.Network; import dk.alexandra.fresco.framework.network.socket.SocketNetwork; import dk.alexandra.fresco.framework.sce.SecureComputationEngine; import dk.alexandra.fresco.framework.sce.SecureComputationEngineImpl; @@ -26,6 +27,7 @@ import java.math.BigInteger; import java.util.*; +import java.util.function.Supplier; /** * Abstract class which handles a lot of boiler plate testing code. This makes running a single test @@ -65,27 +67,35 @@ public void runTest( noOfParties, DEFAULT_FIELD_LEFT); DummyArithmeticResourcePool rpRight = new DummyArithmeticResourcePoolImpl(playerId, noOfParties, DEFAULT_FIELD_RIGHT); + CRTResourcePool rp = + new CRTResourcePoolImpl<>(playerId, noOfParties, null, rpLeft, rpRight); - CRTProtocolSuite ps = new CRTProtocolSuite<>( + CRTProtocolSuite ps = + new CRTProtocolSuite<>( new DummyArithmeticBuilderFactory(new BasicNumericContext(DEFAULT_FIELD_LEFT.getBitLength() - 24, playerId, noOfParties, DEFAULT_FIELD_LEFT, 16, 40)), new DummyArithmeticBuilderFactory(new BasicNumericContext(DEFAULT_FIELD_RIGHT.getBitLength()- 40, playerId, noOfParties, DEFAULT_FIELD_RIGHT, 16, 40))); - ProtocolEvaluator> evaluator = + ProtocolEvaluator> evaluator = new BatchedProtocolEvaluator<>(batchEvaluationStrategy, ps); NetworkConfiguration partyNetConf = netConf.get(playerId); - SecureComputationEngine, ProtocolBuilderNumeric> sce = + SecureComputationEngine, ProtocolBuilderNumeric> sce = new SecureComputationEngineImpl<>(ps, evaluator); - CRTDataSupplier dataSupplier = new CRTSemiHonestDataSupplier<>(playerId, noOfParties, ps); + Supplier networkSupplier = () -> new SocketNetwork(partyNetConf); + CRTDataSupplier dataSupplier = new CRTSemiHonestDataSupplier(playerId + , noOfParties, rp); TestThreadRunner.TestThreadConfiguration< CRTResourcePool, ProtocolBuilderNumeric> ttc = new TestThreadRunner.TestThreadConfiguration<>(sce, () -> new CRTResourcePoolImpl<>(playerId, noOfParties, dataSupplier, rpLeft, rpRight), - () -> new SocketNetwork(partyNetConf)); + networkSupplier); conf.put(playerId, ttc); } diff --git a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSpdzCRTTest.java b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSpdzCRTTest.java index e2c1c5c13..44ddf82e6 100644 --- a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSpdzCRTTest.java +++ b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSpdzCRTTest.java @@ -110,14 +110,15 @@ public void runTest( BatchEvaluationStrategy> strategy = new CRTSequentialStrategy<>(); - CRTProtocolSuite ps = new CRTProtocolSuite<>( + CRTProtocolSuite ps = + new CRTProtocolSuite<>( new SpdzBuilder(new BasicNumericContext(DEFAULT_FIELD_LEFT.getBitLength() - 24, playerId, noOfParties, DEFAULT_FIELD_LEFT, 16, 40)), new SpdzBuilder(new BasicNumericContext(DEFAULT_FIELD_RIGHT.getBitLength() - 40, playerId, noOfParties, DEFAULT_FIELD_RIGHT, 16, 40))); ProtocolEvaluator> evaluator = - new BatchedProtocolEvaluator<>(strategy, ps); + new BatchedProtocolEvaluator>(strategy, ps); SecureComputationEngine, ProtocolBuilderNumeric> sce = new SecureComputationEngineImpl<>(ps, evaluator); diff --git a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/BasicCRTTests.java b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/BasicCRTTests.java index 8db4b1c6e..e8e047d54 100644 --- a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/BasicCRTTests.java +++ b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/BasicCRTTests.java @@ -58,7 +58,7 @@ public TestThread next() { public void test() { Application app = producer -> producer .seq(seq -> seq.append( - new CorrelatedNoiseProtocol<>())).seq((seq, r) -> seq.numeric().open(r)); + new CorrelatedNoiseProtocol<>(seq))).seq((seq, r) -> seq.numeric().open(r)); BigInteger output = runApplication(app); CRTRingDefinition ring = (CRTRingDefinition) this.getFieldDefinition(); From ca4ee0f5a259e0840b7bfdf013e548a261500927 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Lindstr=C3=B8m?= Date: Tue, 22 Nov 2022 13:03:52 +0100 Subject: [PATCH 10/38] Fix preprocessing --- .../fresco/suite/crt/CRTBuilderFactory.java | 17 +++++----- .../fresco/suite/crt/CRTNumericContext.java | 28 +++++++++------- .../fresco/suite/crt/CRTProtocolSuite.java | 3 +- .../datatypes/resource/CRTDataSupplier.java | 4 +-- .../resource/CRTDummyDataSupplier.java | 7 ++-- .../resource/CRTSemiHonestDataSupplier.java | 33 ++++++++++--------- .../protocols/CorrelatedNoiseProtocol.java | 23 ++++--------- .../suite/crt/protocols/LiftPQProtocol.java | 7 ++-- .../suite/crt/protocols/LiftQPProtocol.java | 3 +- .../fresco/suite/crt/BasicCRTTests.java | 3 +- 10 files changed, 60 insertions(+), 68 deletions(-) diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTBuilderFactory.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTBuilderFactory.java index 55e84938f..9cb03e1e1 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTBuilderFactory.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTBuilderFactory.java @@ -9,6 +9,7 @@ import dk.alexandra.fresco.framework.value.SInt; import dk.alexandra.fresco.lib.field.integer.BasicNumericContext; 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.ProtocolBuilderNumericWrapper; import java.math.BigInteger; @@ -23,25 +24,25 @@ public class CRTBuilderFactory resourcePool, + BuilderFactoryNumeric left, + BuilderFactoryNumeric right) { - if (resourcePoolLeft.getMyId() != resourcePoolRight.getMyId() - || resourcePoolLeft.getNoOfParties() != resourcePoolRight.getNoOfParties()) { + if (resourcePool.getSubResourcePools().getFirst().getMyId() != resourcePool.getSubResourcePools().getSecond().getMyId() + || resourcePool.getSubResourcePools().getFirst().getNoOfParties() != resourcePool.getSubResourcePools().getSecond().getNoOfParties()) { throw new IllegalArgumentException( "The protocol suites used must be configured with the same ID and number of players"); } this.left = left; - this.resourcePoolLeft = resourcePoolLeft; + this.resourcePoolLeft = resourcePool.getSubResourcePools().getFirst(); this.right = right; - this.resourcePoolRight = resourcePoolRight; + this.resourcePoolRight = resourcePool.getSubResourcePools().getSecond(); this.p = resourcePoolLeft.getModulus(); this.q = resourcePoolRight.getModulus(); this.context = new CRTNumericContext<>( p.bitLength() + q.bitLength() - 40, //TODO - resourcePoolLeft.getMyId(), resourcePoolLeft.getNoOfParties(), left, right, p, q, resourcePoolLeft, resourcePoolRight); + resourcePoolLeft.getMyId(), resourcePoolLeft.getNoOfParties(), left, right, p, q, resourcePool); } @Override diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTNumericContext.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTNumericContext.java index 72114aae1..56e607c51 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTNumericContext.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTNumericContext.java @@ -3,23 +3,24 @@ import dk.alexandra.fresco.framework.DRes; import dk.alexandra.fresco.framework.builder.numeric.BuilderFactoryNumeric; import dk.alexandra.fresco.framework.builder.numeric.Numeric; +import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool; import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; import dk.alexandra.fresco.framework.sce.resources.ResourcePool; import dk.alexandra.fresco.framework.value.SInt; import dk.alexandra.fresco.lib.field.integer.BasicNumericContext; 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.ProtocolBuilderNumericWrapper; import java.math.BigInteger; -public class CRTNumericContext extends BasicNumericContext { +public class CRTNumericContext extends BasicNumericContext { private final BigInteger p, q; private final BuilderFactoryNumeric left; private final BuilderFactoryNumeric right; - private final ResourcePoolA resourcePoolLeft; - private final ResourcePoolB resourcePoolRight; + private final CRTResourcePool resourcePool; /** * Construct a new BasicNumericContext. @@ -30,14 +31,13 @@ public class CRTNumericContext resourcePool) { super(maxBitLength, myId, noOfParties, new CRTRingDefinition(p, q), p.bitLength()); this.p = p; this.q = q; this.left = left; this.right = right; - this.resourcePoolLeft = resourcePoolLeft; - this.resourcePoolRight = resourcePoolRight; + this.resourcePool = resourcePool; } /** Get the modulus of the left ring in the RNS representation. */ @@ -51,14 +51,14 @@ public BigInteger getRightModulus() { } /** - * Get a {@link ProtocolSuiteProtocolSupplier} for the MPC system on the left ring. + * Get a {@link BuilderFactoryNumeric} for the MPC system on the left ring. */ public BuilderFactoryNumeric getLeft() { return left; } /** - * Get a {@link ProtocolSuiteProtocolSupplier} for the MPC system on the right ring. + * Get a {@link BuilderFactoryNumeric} for the MPC system on the right ring. */ public BuilderFactoryNumeric getRight() { return right; @@ -66,25 +66,29 @@ public BuilderFactoryNumeric getRight() { public ResourcePoolA getResourcePoolLeft() { - return resourcePoolLeft; + return resourcePool.getSubResourcePools().getFirst(); } public ResourcePoolB getResourcePoolRight() { - return resourcePoolRight; + return resourcePool.getSubResourcePools().getSecond(); + } + + public CRTResourcePool getResourcePool() { + return resourcePool; } /** * Get an instance of Numeric for the left scheme using the given builder. */ public Numeric leftNumeric(ProtocolBuilderNumeric builder) { - return left.createNumeric(new ProtocolBuilderNumericWrapper<>(builder, left, resourcePoolLeft)); + return left.createNumeric(new ProtocolBuilderNumericWrapper<>(builder, left, resourcePool.getSubResourcePools().getFirst())); } /** * Get an instance of Numeric for the right scheme using the given builder. */ public Numeric rightNumeric(ProtocolBuilderNumeric builder) { - return right.createNumeric(new ProtocolBuilderNumericWrapper<>(builder, right, resourcePoolRight)); + return right.createNumeric(new ProtocolBuilderNumericWrapper<>(builder, right, resourcePool.getSubResourcePools().getSecond())); } public DRes getLeft(DRes crtInteger) { diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTProtocolSuite.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTProtocolSuite.java index b3eb71fd0..31c2a6e89 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTProtocolSuite.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTProtocolSuite.java @@ -31,8 +31,7 @@ public BuilderFactoryNumeric getRight() { @Override public BuilderFactoryNumeric init(CRTResourcePool resourcePool) { Pair resourcePools = resourcePool.getSubResourcePools(); - return new CRTBuilderFactory<>(resourcePools.getFirst(), left, resourcePools.getSecond(), - right); + return new CRTBuilderFactory<>(resourcePool, left, right); } @Override diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDataSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDataSupplier.java index e1287ae06..00651cd9c 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDataSupplier.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDataSupplier.java @@ -1,5 +1,6 @@ package dk.alexandra.fresco.suite.crt.datatypes.resource; +import dk.alexandra.fresco.framework.DRes; import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; import dk.alexandra.fresco.framework.builder.numeric.field.FieldDefinition; import dk.alexandra.fresco.framework.util.Pair; @@ -12,8 +13,7 @@ public interface CRTDataSupplier { * * @return r */ - CRTSInt getCorrelatedNoise( - ProtocolBuilderNumeric builder); + DRes getCorrelatedNoise(ProtocolBuilderNumeric builder); /** * Supply the next random bit diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDummyDataSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDummyDataSupplier.java index beeb69e9a..d87d2e31e 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDummyDataSupplier.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDummyDataSupplier.java @@ -1,5 +1,6 @@ package dk.alexandra.fresco.suite.crt.datatypes.resource; +import dk.alexandra.fresco.framework.DRes; import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; import dk.alexandra.fresco.framework.builder.numeric.field.FieldDefinition; import dk.alexandra.fresco.framework.util.Pair; @@ -33,12 +34,12 @@ public CRTDummyDataSupplier(int myId, int players, FieldDefinition leftField, } @Override - public CRTSInt getCorrelatedNoise(ProtocolBuilderNumeric builder) { + public DRes getCorrelatedNoise(ProtocolBuilderNumeric builder) { BigInteger r = Util.randomBigInteger(random, fp.getModulus()); BigInteger l = Util .randomBigInteger(random, BigInteger.valueOf(players)); - return new CRTSInt(wrapperLeft.apply(r), - wrapperRight.apply(r.add(l.multiply(fp.getModulus())))); + return DRes.of(new CRTSInt(wrapperLeft.apply(r), + wrapperRight.apply(r.add(l.multiply(fp.getModulus()))))); } @Override diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java index f85080ef9..e24616346 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java @@ -2,13 +2,16 @@ import dk.alexandra.fresco.framework.DRes; import dk.alexandra.fresco.framework.builder.ComputationParallel; +import dk.alexandra.fresco.framework.builder.ProtocolBuilder; import dk.alexandra.fresco.framework.builder.numeric.Numeric; import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool; import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; import dk.alexandra.fresco.framework.builder.numeric.field.FieldDefinition; import dk.alexandra.fresco.framework.util.Pair; +import dk.alexandra.fresco.suite.crt.CRTNumericContext; import dk.alexandra.fresco.suite.crt.Util; import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; +import dk.alexandra.fresco.suite.crt.protocols.framework.CRTComputation; import java.math.BigInteger; import java.security.SecureRandom; @@ -29,7 +32,7 @@ public class CRTSemiHonestDataSupplier, ProtocolBuilderNumeric> { + private class NoiseGenerator extends CRTComputation, ResourcePoolL, ResourcePoolR> { private final int batchSize; @@ -38,7 +41,7 @@ public NoiseGenerator(int batchSize) { } @Override - public DRes> buildComputation(ProtocolBuilderNumeric builder) { + public DRes> buildComputation(ProtocolBuilderNumeric builder, CRTNumericContext context) { return builder.par(par -> { Numeric numeric = par.numeric(); List list = new ArrayList<>(batchSize); @@ -47,18 +50,15 @@ public DRes> buildComputation(ProtocolBuilderNumeric builder BigInteger r = Util.randomBigInteger(random, resourcePool.getFieldDefinitions().getFirst().getModulus()); BigInteger l = Util.randomBigInteger(random, BigInteger.valueOf(players)); CRTSInt noisePair = new CRTSInt( - numeric.known(leftDef.createElement(r).toBigInteger()), - numeric.known(rightDef.createElement(r.add(l.multiply(leftDef.getModulus()))).toBigInteger()) + context.leftNumeric(par).known(leftDef.createElement(r).toBigInteger()), + context.rightNumeric(par).known(rightDef.createElement(r.add(l.multiply(leftDef.getModulus()))).toBigInteger()) ); list.add(noisePair); } - return DRes.of(list); - }).seq((seq, res) -> { - ArrayDeque resDeque = new ArrayDeque<>(); - resDeque.addAll(res); - return ()-> resDeque; + return () -> list; }); } + } public CRTSemiHonestDataSupplier(int myId, int players, CRTResourcePool getCorrelatedNoise(ProtocolBuilderNumeric builder) { + if (noisePairs.isEmpty()) { + return builder.seq(new NoiseGenerator(10)).seq((seq, noise) -> { + noisePairs.addAll(noise); + CRTSInt out = noisePairs.pop(); + return DRes.of(out); + }); + } else { + return DRes.of(noisePairs.pop()); } - noisePairs = builder.seq(new NoiseGenerator(10)).out(); - return noisePairs.pop(); } @Override diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/CorrelatedNoiseProtocol.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/CorrelatedNoiseProtocol.java index 0c0f54924..9b2f2b196 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/CorrelatedNoiseProtocol.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/CorrelatedNoiseProtocol.java @@ -1,34 +1,23 @@ package dk.alexandra.fresco.suite.crt.protocols; +import dk.alexandra.fresco.framework.DRes; import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool; import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; import dk.alexandra.fresco.framework.network.Network; import dk.alexandra.fresco.framework.sce.SecureComputationEngine; import dk.alexandra.fresco.framework.value.SInt; +import dk.alexandra.fresco.suite.crt.CRTNumericContext; 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.CRTComputation; import dk.alexandra.fresco.suite.crt.protocols.framework.CRTNativeProtocol; /** Generate a pair of correlated noise, eg (r, r + ep) for some 0 ≤ e ≤ n. */ public class CorrelatedNoiseProtocol extends - CRTNativeProtocol { - - private final ProtocolBuilderNumeric builder; - private CRTSInt r; - - public CorrelatedNoiseProtocol(ProtocolBuilderNumeric builder) { - this.builder = builder; - } - @Override - public EvaluationStatus evaluate(int round, - CRTResourcePool resourcePool, Network network) { - - this.r = resourcePool.getDataSupplier().getCorrelatedNoise(builder); - return EvaluationStatus.IS_DONE; - } + CRTComputation { @Override - public SInt out() { - return r; + public DRes buildComputation(ProtocolBuilderNumeric builder, CRTNumericContext context) { + return context.getResourcePool().getDataSupplier().getCorrelatedNoise(builder); } } diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftPQProtocol.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftPQProtocol.java index 6420f765f..adeb5cb19 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftPQProtocol.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftPQProtocol.java @@ -27,12 +27,9 @@ public LiftPQProtocol(DRes value) { @Override public DRes buildComputation(ProtocolBuilderNumeric builder, CRTNumericContext context) { - DRes noise = new CorrelatedNoiseProtocol<>(builder); - return builder.seq(seq -> { - this.r = (CRTSInt) noise.out(); - + return builder.seq(new CorrelatedNoiseProtocol<>()).seq((seq, noise) -> { // Add noise to the left value. The right is ignored. - DRes xBar = context.leftNumeric(seq).add(((CRTSInt) value.out()).getLeft(), r.getLeft()); + DRes xBar = context.leftNumeric(seq).add(((CRTSInt) value.out()).getLeft(), noise.getLeft()); CRTSInt output = new CRTSInt(xBar, context.rightNumeric(seq).known(0)); return seq.numeric().open(output); // TODO: We only need to open the left, so we should create an openLeft function diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftQPProtocol.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftQPProtocol.java index 3ddee011f..fbe2543da 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftQPProtocol.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftQPProtocol.java @@ -31,8 +31,7 @@ public DRes buildComputation(ProtocolBuilderNumeric builder, //q' = q / 2 is divisible by p, so adding it to the input only affects the output by q mod p = 1 if there's an overflow. BigInteger qPrime = new BigInteger("3138550867693340351802905239100779285196644626743924002860"); - DRes noise = new CorrelatedNoiseProtocol<>(builder); - return builder.seq(seq -> { + return builder.seq(new CorrelatedNoiseProtocol<>()).seq((seq, noise) -> { this.r = (CRTSInt) noise.out(); return seq.numeric().add(qPrime, value); }).seq((seq, value) -> { diff --git a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/BasicCRTTests.java b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/BasicCRTTests.java index e8e047d54..8d9579c5f 100644 --- a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/BasicCRTTests.java +++ b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/BasicCRTTests.java @@ -57,8 +57,7 @@ public TestThread next() { @Override public void test() { Application app = producer -> producer - .seq(seq -> seq.append( - new CorrelatedNoiseProtocol<>(seq))).seq((seq, r) -> seq.numeric().open(r)); + .seq(new CorrelatedNoiseProtocol<>()).seq((seq, r) -> seq.numeric().open(r)); BigInteger output = runApplication(app); CRTRingDefinition ring = (CRTRingDefinition) this.getFieldDefinition(); From d1e81b01c5c580bfefa16f4b42d2b0c53b1fff06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Lindstr=C3=B8m?= Date: Tue, 22 Nov 2022 13:05:08 +0100 Subject: [PATCH 11/38] Keep noise until the end of the protocol --- .../alexandra/fresco/suite/crt/protocols/LiftPQProtocol.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftPQProtocol.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftPQProtocol.java index adeb5cb19..68af5a60b 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftPQProtocol.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftPQProtocol.java @@ -28,8 +28,10 @@ public LiftPQProtocol(DRes value) { public DRes buildComputation(ProtocolBuilderNumeric builder, CRTNumericContext context) { return builder.seq(new CorrelatedNoiseProtocol<>()).seq((seq, noise) -> { + this.r = noise; + // Add noise to the left value. The right is ignored. - DRes xBar = context.leftNumeric(seq).add(((CRTSInt) value.out()).getLeft(), noise.getLeft()); + DRes xBar = context.leftNumeric(seq).add(((CRTSInt) value.out()).getLeft(), r.getLeft()); CRTSInt output = new CRTSInt(xBar, context.rightNumeric(seq).known(0)); return seq.numeric().open(output); // TODO: We only need to open the left, so we should create an openLeft function From 2e73a287cefecb7faba57c0131378cd47fc6332f Mon Sep 17 00:00:00 2001 From: Mikkel Wienberg Madsen Date: Wed, 30 Nov 2022 09:59:02 +0100 Subject: [PATCH 12/38] Attempt at Semi Honest Noise --- .../resource/CRTSemiHonestDataSupplier.java | 40 ++++++++----------- .../crt/AbstractSemiHonestDummyCRTTest.java | 4 +- 2 files changed, 18 insertions(+), 26 deletions(-) diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java index e24616346..8212b354e 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java @@ -1,13 +1,12 @@ package dk.alexandra.fresco.suite.crt.datatypes.resource; import dk.alexandra.fresco.framework.DRes; -import dk.alexandra.fresco.framework.builder.ComputationParallel; -import dk.alexandra.fresco.framework.builder.ProtocolBuilder; import dk.alexandra.fresco.framework.builder.numeric.Numeric; import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool; import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; import dk.alexandra.fresco.framework.builder.numeric.field.FieldDefinition; import dk.alexandra.fresco.framework.util.Pair; +import dk.alexandra.fresco.framework.value.SInt; import dk.alexandra.fresco.suite.crt.CRTNumericContext; import dk.alexandra.fresco.suite.crt.Util; import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; @@ -18,19 +17,16 @@ import java.util.ArrayDeque; import java.util.ArrayList; import java.util.List; -import java.util.Random; public class CRTSemiHonestDataSupplier implements CRTDataSupplier { - private final int myId; private final int players; - private final CRTResourcePool resourcePool; - private final Random random; - private ArrayDeque noisePairs = new ArrayDeque<>(); - private final FieldDefinition leftDef; - private final FieldDefinition rightDef; + private final SecureRandom random; + private final ArrayDeque noisePairs = new ArrayDeque<>(); + private final FieldDefinition fp; + private final FieldDefinition fq; private class NoiseGenerator extends CRTComputation, ResourcePoolL, ResourcePoolR> { @@ -43,33 +39,29 @@ public NoiseGenerator(int batchSize) { @Override public DRes> buildComputation(ProtocolBuilderNumeric builder, CRTNumericContext context) { return builder.par(par -> { - Numeric numeric = par.numeric(); + Numeric left = context.leftNumeric(par); + Numeric right = context.rightNumeric(par); List list = new ArrayList<>(batchSize); for (int i = 0; i < batchSize; i++) { - // TODO: Actually do the protocol - BigInteger r = Util.randomBigInteger(random, resourcePool.getFieldDefinitions().getFirst().getModulus()); + DRes rp = left.randomElement(); + // not really sure how `l` (delta) should be selected. BigInteger l = Util.randomBigInteger(random, BigInteger.valueOf(players)); - CRTSInt noisePair = new CRTSInt( - context.leftNumeric(par).known(leftDef.createElement(r).toBigInteger()), - context.rightNumeric(par).known(rightDef.createElement(r.add(l.multiply(leftDef.getModulus()))).toBigInteger()) - ); + DRes rq = right.add(rp, left.known(l.multiply(fp.getModulus()))); + CRTSInt noisePair = new CRTSInt(rp, rq); list.add(noisePair); } - return () -> list; + return DRes.of(list); }); } } - public CRTSemiHonestDataSupplier(int myId, int players, CRTResourcePool resourcePool) { - this.myId = myId; this.players = players; - this.resourcePool = resourcePool; - this.random = new SecureRandom(); - this.leftDef = resourcePool.getFieldDefinitions().getFirst(); - this.rightDef = resourcePool.getFieldDefinitions().getFirst(); + this.fp = resourcePool.getFieldDefinitions().getFirst(); + this.fq = resourcePool.getFieldDefinitions().getFirst(); } @Override @@ -92,6 +84,6 @@ public CRTSInt getRandomBit() { @Override public Pair getFieldDefinitions() { - return new Pair<>(leftDef, rightDef); + return new Pair<>(fp, fq); } } diff --git a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSemiHonestDummyCRTTest.java b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSemiHonestDummyCRTTest.java index ced1e4b7a..c30251e98 100644 --- a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSemiHonestDummyCRTTest.java +++ b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSemiHonestDummyCRTTest.java @@ -87,8 +87,8 @@ public void runTest( Supplier networkSupplier = () -> new SocketNetwork(partyNetConf); CRTDataSupplier dataSupplier = new CRTSemiHonestDataSupplier(playerId - , noOfParties, rp); + DummyArithmeticResourcePool>( + noOfParties, rp); TestThreadRunner.TestThreadConfiguration< CRTResourcePool, From 37fb256ca2390e93c253193d9392e7dcd1dedfe4 Mon Sep 17 00:00:00 2001 From: Mikkel Wienberg Madsen Date: Wed, 30 Nov 2022 15:43:29 +0100 Subject: [PATCH 13/38] fixup! Attempt at Semi Honest Noise --- .../datatypes/resource/CRTSemiHonestDataSupplier.java | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java index 8212b354e..6affee207 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java @@ -8,11 +8,9 @@ import dk.alexandra.fresco.framework.util.Pair; import dk.alexandra.fresco.framework.value.SInt; import dk.alexandra.fresco.suite.crt.CRTNumericContext; -import dk.alexandra.fresco.suite.crt.Util; import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; import dk.alexandra.fresco.suite.crt.protocols.framework.CRTComputation; -import java.math.BigInteger; import java.security.SecureRandom; import java.util.ArrayDeque; import java.util.ArrayList; @@ -39,15 +37,13 @@ public NoiseGenerator(int batchSize) { @Override public DRes> buildComputation(ProtocolBuilderNumeric builder, CRTNumericContext context) { return builder.par(par -> { + int partyNo = context.getMyId(); Numeric left = context.leftNumeric(par); Numeric right = context.rightNumeric(par); List list = new ArrayList<>(batchSize); for (int i = 0; i < batchSize; i++) { - DRes rp = left.randomElement(); - // not really sure how `l` (delta) should be selected. - BigInteger l = Util.randomBigInteger(random, BigInteger.valueOf(players)); - DRes rq = right.add(rp, left.known(l.multiply(fp.getModulus()))); - CRTSInt noisePair = new CRTSInt(rp, rq); + DRes r = left.randomElement(); + CRTSInt noisePair = new CRTSInt(r, r); list.add(noisePair); } return DRes.of(list); From ee0791553dc84d6e1a2fa9b412b9661b3b083a00 Mon Sep 17 00:00:00 2001 From: Mikkel Wienberg Madsen Date: Tue, 10 Jan 2023 14:47:11 +0100 Subject: [PATCH 14/38] Implement Covert Noise Generator --- .../resource/CRTCovertDataSupplier.java | 135 ++++++++++++++++++ .../resource/CRTSemiHonestDataSupplier.java | 40 +----- .../resource/SemiHonestNoiseGenerator.java | 39 +++++ .../suite/crt/AbstractCovertDummyCRTTest.java | 105 ++++++++++++++ .../crt/AbstractSemiHonestDummyCRTTest.java | 4 +- .../alexandra/fresco/suite/crt/TestCRT.java | 40 ++++++ 6 files changed, 322 insertions(+), 41 deletions(-) create mode 100644 suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTCovertDataSupplier.java create mode 100644 suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/SemiHonestNoiseGenerator.java create mode 100644 suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractCovertDummyCRTTest.java diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTCovertDataSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTCovertDataSupplier.java new file mode 100644 index 000000000..533ff15eb --- /dev/null +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTCovertDataSupplier.java @@ -0,0 +1,135 @@ +package dk.alexandra.fresco.suite.crt.datatypes.resource; + +import dk.alexandra.fresco.framework.DRes; +import dk.alexandra.fresco.framework.MaliciousException; +import dk.alexandra.fresco.framework.builder.numeric.Numeric; +import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool; +import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; +import dk.alexandra.fresco.framework.builder.numeric.field.FieldDefinition; +import dk.alexandra.fresco.framework.util.Pair; +import dk.alexandra.fresco.framework.value.SInt; +import dk.alexandra.fresco.suite.crt.CRTNumericContext; +import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; +import dk.alexandra.fresco.suite.crt.protocols.framework.CRTComputation; + +import java.math.BigInteger; +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.List; + +public class CRTCovertDataSupplier + implements CRTDataSupplier { + + private final ArrayDeque noisePairs = new ArrayDeque<>(); + private final FieldDefinition fp; + private final FieldDefinition fq; + + private final int partySize; + private final int myId; + private final int deterrenceFactor = 10; + + private class CovertNoiseGenerator extends CRTComputation, ResourcePoolL, ResourcePoolR> { + + private final int batchSize; + private final SemiHonestNoiseGenerator noiseGenerator; + + public CovertNoiseGenerator(int batchSize) { + this.batchSize = batchSize; + this.noiseGenerator = new SemiHonestNoiseGenerator<>(deterrenceFactor * batchSize); + } + + @Override + public DRes> buildComputation(ProtocolBuilderNumeric builder, + CRTNumericContext context) { + return builder.par(noiseGenerator::buildComputation).par((par, honestNoisePairs) -> { + // Sample batchsize random noisepairs to keep, mark all other to be checked/selected + Numeric numeric = par.numeric(); + List> toKeep = new ArrayList<>(batchSize); + for (int i = 0; i < batchSize; i++) { + DRes c = numeric.open(numeric.randomElement()); // TODO: check if this is smart + toKeep.add(c); + } + return Pair.lazy(toKeep, honestNoisePairs); + }).par((par, pair) -> { // Open selected pairs + + // prepare pairs indices to keep + int[] toKeep = new int[batchSize]; + int i = 0; + for (DRes keep : pair.getFirst()) { + long k = keep.out().longValue(); + // ensure we have c between 0 and gamma. + toKeep[i] = (int) k % deterrenceFactor; // TODO: this might have to be done differently + } + List honestNoisePairs = pair.getSecond(); + + // check all other pairs + int j = 0; + List, DRes>> testSet = new ArrayList<>(); + for (CRTSInt noisePair : honestNoisePairs) { + i = j / batchSize; // current batch no. + if (j == toKeep[i] + i*batchSize) // skip the once marked to keep. + // scale them back up, so they correspond to a batch. + continue; + j++; + DRes leftClosed = noisePair.getLeft(); + DRes rightClosed = noisePair.getRight(); + par.numeric().open(noisePair); + DRes leftOpened = context.leftNumeric(par).open(leftClosed); + DRes rightOpened = context.rightNumeric(par).open(rightClosed); + testSet.add(new Pair<>(leftOpened, rightOpened)); + } + + // filter out the ones we need. + List noisePairs = new ArrayList<>(batchSize); + for (i = 0; i < batchSize; i++) { + int k = toKeep[i] + i*batchSize; + noisePairs.add(honestNoisePairs.get(k)); + } + return Pair.lazy(noisePairs, testSet); + }).par((par, pair) -> { // Check selected pairs + List, DRes>> testSet = pair.getSecond(); + for (Pair, DRes> noisePair : testSet) { + BigInteger rp = noisePair.getFirst().out(); + BigInteger rq = noisePair.getFirst().out(); + if (rp.compareTo(rq) != 0) { + throw new MaliciousException("Cheating party, terminating"); + } + } + List noisePairs = pair.getFirst(); + return DRes.of(noisePairs); + }); + } + } + + public CRTCovertDataSupplier(CRTResourcePool resourcePool) { + this.fp = resourcePool.getFieldDefinitions().getFirst(); + this.fq = resourcePool.getFieldDefinitions().getFirst(); + this.partySize = resourcePool.getNoOfParties(); + this.myId = resourcePool.getMyId(); + } + + @Override + public DRes getCorrelatedNoise(ProtocolBuilderNumeric builder) { + if (noisePairs.isEmpty()) { + return builder.seq(new CovertNoiseGenerator(10)).seq((seq, noise) -> { + noisePairs.addAll(noise); + CRTSInt out = noisePairs.pop(); + return DRes.of(out); + }); + } else { + return DRes.of(noisePairs.pop()); + } + } + + @Override + public CRTSInt getRandomBit() { + return null; + } + + @Override + public Pair getFieldDefinitions() { + return new Pair<>(fp, fq); + } +} diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java index 6affee207..046510d8c 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java @@ -1,61 +1,25 @@ package dk.alexandra.fresco.suite.crt.datatypes.resource; import dk.alexandra.fresco.framework.DRes; -import dk.alexandra.fresco.framework.builder.numeric.Numeric; import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool; import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; import dk.alexandra.fresco.framework.builder.numeric.field.FieldDefinition; import dk.alexandra.fresco.framework.util.Pair; -import dk.alexandra.fresco.framework.value.SInt; -import dk.alexandra.fresco.suite.crt.CRTNumericContext; import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; -import dk.alexandra.fresco.suite.crt.protocols.framework.CRTComputation; -import java.security.SecureRandom; import java.util.ArrayDeque; -import java.util.ArrayList; -import java.util.List; public class CRTSemiHonestDataSupplier implements CRTDataSupplier { - private final int players; - private final SecureRandom random; private final ArrayDeque noisePairs = new ArrayDeque<>(); private final FieldDefinition fp; private final FieldDefinition fq; - private class NoiseGenerator extends CRTComputation, ResourcePoolL, ResourcePoolR> { - private final int batchSize; - - public NoiseGenerator(int batchSize) { - this.batchSize = batchSize; - } - - @Override - public DRes> buildComputation(ProtocolBuilderNumeric builder, CRTNumericContext context) { - return builder.par(par -> { - int partyNo = context.getMyId(); - Numeric left = context.leftNumeric(par); - Numeric right = context.rightNumeric(par); - List list = new ArrayList<>(batchSize); - for (int i = 0; i < batchSize; i++) { - DRes r = left.randomElement(); - CRTSInt noisePair = new CRTSInt(r, r); - list.add(noisePair); - } - return DRes.of(list); - }); - } - - } - - public CRTSemiHonestDataSupplier(int players, CRTResourcePool resourcePool) { - this.players = players; - this.random = new SecureRandom(); this.fp = resourcePool.getFieldDefinitions().getFirst(); this.fq = resourcePool.getFieldDefinitions().getFirst(); } @@ -63,7 +27,7 @@ public CRTSemiHonestDataSupplier(int players, CRTResourcePool getCorrelatedNoise(ProtocolBuilderNumeric builder) { if (noisePairs.isEmpty()) { - return builder.seq(new NoiseGenerator(10)).seq((seq, noise) -> { + return builder.seq(new SemiHonestNoiseGenerator(10)).seq((seq, noise) -> { noisePairs.addAll(noise); CRTSInt out = noisePairs.pop(); return DRes.of(out); diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/SemiHonestNoiseGenerator.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/SemiHonestNoiseGenerator.java new file mode 100644 index 000000000..27f32007f --- /dev/null +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/SemiHonestNoiseGenerator.java @@ -0,0 +1,39 @@ +package dk.alexandra.fresco.suite.crt.datatypes.resource; + +import dk.alexandra.fresco.framework.DRes; +import dk.alexandra.fresco.framework.builder.numeric.Numeric; +import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool; +import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; +import dk.alexandra.fresco.framework.value.SInt; +import dk.alexandra.fresco.suite.crt.CRTNumericContext; +import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; +import dk.alexandra.fresco.suite.crt.protocols.framework.CRTComputation; + +import java.util.ArrayList; +import java.util.List; + +public class SemiHonestNoiseGenerator + extends CRTComputation, ResourcePoolL, ResourcePoolR> { + + private final int batchSize; + + public SemiHonestNoiseGenerator(int batchSize) { + this.batchSize = batchSize; + } + + @Override + public DRes> buildComputation(ProtocolBuilderNumeric builder, + CRTNumericContext context) { + return builder.par(par -> { + Numeric left = context.leftNumeric(par); + List list = new ArrayList<>(batchSize); + for (int i = 0; i < batchSize; i++) { + DRes r = left.randomElement(); + CRTSInt noisePair = new CRTSInt(r, r); + list.add(noisePair); + } + return DRes.of(list); + }); + } + +} diff --git a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractCovertDummyCRTTest.java b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractCovertDummyCRTTest.java new file mode 100644 index 000000000..c734d7756 --- /dev/null +++ b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractCovertDummyCRTTest.java @@ -0,0 +1,105 @@ +package dk.alexandra.fresco.suite.crt; + +import dk.alexandra.fresco.framework.ProtocolEvaluator; +import dk.alexandra.fresco.framework.TestThreadRunner; +import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; +import dk.alexandra.fresco.framework.builder.numeric.field.BigIntegerFieldDefinition; +import dk.alexandra.fresco.framework.builder.numeric.field.FieldDefinition; +import dk.alexandra.fresco.framework.builder.numeric.field.MersennePrimeFieldDefinition; +import dk.alexandra.fresco.framework.configuration.NetworkConfiguration; +import dk.alexandra.fresco.framework.configuration.NetworkUtil; +import dk.alexandra.fresco.framework.network.Network; +import dk.alexandra.fresco.framework.network.socket.SocketNetwork; +import dk.alexandra.fresco.framework.sce.SecureComputationEngine; +import dk.alexandra.fresco.framework.sce.SecureComputationEngineImpl; +import dk.alexandra.fresco.framework.sce.evaluator.BatchEvaluationStrategy; +import dk.alexandra.fresco.framework.sce.evaluator.BatchedProtocolEvaluator; +import dk.alexandra.fresco.framework.sce.evaluator.EvaluationStrategy; +import dk.alexandra.fresco.lib.field.integer.BasicNumericContext; +import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTCovertDataSupplier; +import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTDataSupplier; +import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTResourcePool; +import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTResourcePoolImpl; +import dk.alexandra.fresco.suite.crt.protocols.framework.CRTSequentialStrategy; +import dk.alexandra.fresco.suite.dummy.arithmetic.DummyArithmeticBuilderFactory; +import dk.alexandra.fresco.suite.dummy.arithmetic.DummyArithmeticResourcePool; +import dk.alexandra.fresco.suite.dummy.arithmetic.DummyArithmeticResourcePoolImpl; + +import java.math.BigInteger; +import java.util.*; +import java.util.function.Supplier; + +/** + * Abstract class which handles a lot of boiler plate testing code. This makes running a single test + * using different parameters quite easy. + */ +public class AbstractCovertDummyCRTTest { + + // Note that the modulus on the right should have twice the bit length of that to the left in order for RandomModP to + // work correctly. + protected static final FieldDefinition DEFAULT_FIELD_LEFT = + MersennePrimeFieldDefinition.find(64); + protected static final FieldDefinition DEFAULT_FIELD_RIGHT = new BigIntegerFieldDefinition( + new BigInteger(152 + 40, new Random(1234)).nextProbablePrime()); + + public void runTest( + TestThreadRunner.TestThreadFactory, ProtocolBuilderNumeric> f, + EvaluationStrategy evalStrategy, int noOfParties) { + + List ports = new ArrayList<>(noOfParties); + for (int i = 1; i <= noOfParties; i++) { + ports.add(9000 + i * (noOfParties - 1)); + } + + Map netConf = + NetworkUtil.getNetworkConfigurations(ports); + Map, + ProtocolBuilderNumeric> + > conf = new HashMap<>(); + + for (int playerId : netConf.keySet()) { + + BatchEvaluationStrategy> batchEvaluationStrategy = + new CRTSequentialStrategy<>(); + DummyArithmeticResourcePool rpLeft = new DummyArithmeticResourcePoolImpl(playerId, + noOfParties, DEFAULT_FIELD_LEFT); + DummyArithmeticResourcePool rpRight = new DummyArithmeticResourcePoolImpl(playerId, + noOfParties, DEFAULT_FIELD_RIGHT); + CRTResourcePool rp = + new CRTResourcePoolImpl<>(playerId, noOfParties, null, rpLeft, rpRight); + + CRTProtocolSuite ps = + new CRTProtocolSuite<>( + new DummyArithmeticBuilderFactory(new BasicNumericContext(DEFAULT_FIELD_LEFT.getBitLength() - 24, + playerId, noOfParties, DEFAULT_FIELD_LEFT, 16, 40)), + new DummyArithmeticBuilderFactory(new BasicNumericContext(DEFAULT_FIELD_RIGHT.getBitLength()- 40, + playerId, noOfParties, DEFAULT_FIELD_RIGHT, 16, 40))); + ProtocolEvaluator> evaluator = + new BatchedProtocolEvaluator<>(batchEvaluationStrategy, ps); + + NetworkConfiguration partyNetConf = netConf.get(playerId); + SecureComputationEngine, ProtocolBuilderNumeric> sce = + new SecureComputationEngineImpl<>(ps, evaluator); + + Supplier networkSupplier = () -> new SocketNetwork(partyNetConf); + CRTDataSupplier dataSupplier = new CRTCovertDataSupplier( + rp); + + TestThreadRunner.TestThreadConfiguration< + CRTResourcePool, + ProtocolBuilderNumeric> ttc = + new TestThreadRunner.TestThreadConfiguration<>(sce, + () -> new CRTResourcePoolImpl<>(playerId, noOfParties, dataSupplier, rpLeft, rpRight), + networkSupplier); + conf.put(playerId, ttc); + } + + TestThreadRunner.run(f, conf); + } + +} diff --git a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSemiHonestDummyCRTTest.java b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSemiHonestDummyCRTTest.java index c30251e98..b876ca3ed 100644 --- a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSemiHonestDummyCRTTest.java +++ b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSemiHonestDummyCRTTest.java @@ -86,9 +86,7 @@ public void runTest( new SecureComputationEngineImpl<>(ps, evaluator); Supplier networkSupplier = () -> new SocketNetwork(partyNetConf); - CRTDataSupplier dataSupplier = new CRTSemiHonestDataSupplier( - noOfParties, rp); + CRTDataSupplier dataSupplier = new CRTSemiHonestDataSupplier<>(rp); TestThreadRunner.TestThreadConfiguration< CRTResourcePool, diff --git a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/TestCRT.java b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/TestCRT.java index eb6d7a896..5cdc313e8 100644 --- a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/TestCRT.java +++ b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/TestCRT.java @@ -148,6 +148,46 @@ public void testProjectionRight() { .runTest(new TestProjectionRight<>(), EvaluationStrategy.SEQUENTIAL, 2); } + @Test + public void testCovertCorrelatedNoise() { + new AbstractSemiHonestDummyCRTTest() + .runTest(new TestCorrelatedNoise<>(), EvaluationStrategy.SEQUENTIAL, 2); + } + + + @Test + public void testCovertGenerateRandomBitMask() { + new AbstractSemiHonestDummyCRTTest().runTest(new TestGenerateRandomBitMask<>(), EvaluationStrategy.SEQUENTIAL, 2); + } + + @Test + public void testCovertRandomBit() { + new AbstractSemiHonestDummyCRTTest().runTest(new TestRandomBit<>(), EvaluationStrategy.SEQUENTIAL, 2); + } + + @Test + public void testCovertLiftPQ() { + new AbstractSemiHonestDummyCRTTest().runTest(new TestLiftPQ<>(), EvaluationStrategy.SEQUENTIAL, 2); + } + + @Test + public void testCovertLiftQP() { + new AbstractSemiHonestDummyCRTTest().runTest(new TestLiftQP<>(), EvaluationStrategy.SEQUENTIAL, 2); + } + + @Test + public void testCovertProjectionLeft() { + new AbstractSemiHonestDummyCRTTest() + .runTest(new TestProjectionLeft<>(), EvaluationStrategy.SEQUENTIAL, 2); + } + + @Test + public void testCovertProjectionRight() { + new AbstractSemiHonestDummyCRTTest() + .runTest(new TestProjectionRight<>(), EvaluationStrategy.SEQUENTIAL, 2); + } + + @Test public void testTruncp() { new AbstractSpdzCRTTest() From b1fff8808217f5f15205954f7f0cf47fcb108e3d Mon Sep 17 00:00:00 2001 From: Mikkel Wienberg Madsen Date: Tue, 10 Jan 2023 15:07:55 +0100 Subject: [PATCH 15/38] Forgot to set tests to use covert, and small fix --- .../datatypes/resource/CRTCovertDataSupplier.java | 12 ++++++------ .../dk/alexandra/fresco/suite/crt/TestCRT.java | 15 +++++++-------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTCovertDataSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTCovertDataSupplier.java index 533ff15eb..01f8fe16b 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTCovertDataSupplier.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTCovertDataSupplier.java @@ -25,8 +25,6 @@ public class CRTCovertDataSupplier, ResourcePoolL, ResourcePoolR> { @@ -51,15 +49,17 @@ public DRes> buildComputation(ProtocolBuilderNumeric builder, toKeep.add(c); } return Pair.lazy(toKeep, honestNoisePairs); + }).par((par, pair) -> { // Open selected pairs // prepare pairs indices to keep - int[] toKeep = new int[batchSize]; int i = 0; + int[] toKeep = new int[batchSize]; for (DRes keep : pair.getFirst()) { - long k = keep.out().longValue(); + long k = keep.out().abs().longValue(); // ensure we have c between 0 and gamma. toKeep[i] = (int) k % deterrenceFactor; // TODO: this might have to be done differently + i++; } List honestNoisePairs = pair.getSecond(); @@ -87,7 +87,9 @@ public DRes> buildComputation(ProtocolBuilderNumeric builder, noisePairs.add(honestNoisePairs.get(k)); } return Pair.lazy(noisePairs, testSet); + }).par((par, pair) -> { // Check selected pairs + List, DRes>> testSet = pair.getSecond(); for (Pair, DRes> noisePair : testSet) { BigInteger rp = noisePair.getFirst().out(); @@ -106,8 +108,6 @@ public CRTCovertDataSupplier(CRTResourcePool resourcePool) { this.fp = resourcePool.getFieldDefinitions().getFirst(); this.fq = resourcePool.getFieldDefinitions().getFirst(); - this.partySize = resourcePool.getNoOfParties(); - this.myId = resourcePool.getMyId(); } @Override diff --git a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/TestCRT.java b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/TestCRT.java index 5cdc313e8..449e9b5f4 100644 --- a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/TestCRT.java +++ b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/TestCRT.java @@ -150,40 +150,39 @@ public void testProjectionRight() { @Test public void testCovertCorrelatedNoise() { - new AbstractSemiHonestDummyCRTTest() + new AbstractCovertDummyCRTTest() .runTest(new TestCorrelatedNoise<>(), EvaluationStrategy.SEQUENTIAL, 2); } - @Test public void testCovertGenerateRandomBitMask() { - new AbstractSemiHonestDummyCRTTest().runTest(new TestGenerateRandomBitMask<>(), EvaluationStrategy.SEQUENTIAL, 2); + new AbstractCovertDummyCRTTest().runTest(new TestGenerateRandomBitMask<>(), EvaluationStrategy.SEQUENTIAL, 2); } @Test public void testCovertRandomBit() { - new AbstractSemiHonestDummyCRTTest().runTest(new TestRandomBit<>(), EvaluationStrategy.SEQUENTIAL, 2); + new AbstractCovertDummyCRTTest().runTest(new TestRandomBit<>(), EvaluationStrategy.SEQUENTIAL, 2); } @Test public void testCovertLiftPQ() { - new AbstractSemiHonestDummyCRTTest().runTest(new TestLiftPQ<>(), EvaluationStrategy.SEQUENTIAL, 2); + new AbstractCovertDummyCRTTest().runTest(new TestLiftPQ<>(), EvaluationStrategy.SEQUENTIAL, 2); } @Test public void testCovertLiftQP() { - new AbstractSemiHonestDummyCRTTest().runTest(new TestLiftQP<>(), EvaluationStrategy.SEQUENTIAL, 2); + new AbstractCovertDummyCRTTest().runTest(new TestLiftQP<>(), EvaluationStrategy.SEQUENTIAL, 2); } @Test public void testCovertProjectionLeft() { - new AbstractSemiHonestDummyCRTTest() + new AbstractCovertDummyCRTTest() .runTest(new TestProjectionLeft<>(), EvaluationStrategy.SEQUENTIAL, 2); } @Test public void testCovertProjectionRight() { - new AbstractSemiHonestDummyCRTTest() + new AbstractCovertDummyCRTTest() .runTest(new TestProjectionRight<>(), EvaluationStrategy.SEQUENTIAL, 2); } From d9586341f956641f627fbf8d616f3b9ccf711be8 Mon Sep 17 00:00:00 2001 From: Mikkel Wienberg Madsen Date: Wed, 11 Jan 2023 10:59:03 +0100 Subject: [PATCH 16/38] Extract CovertNoiseGenerator and use better randomness --- .../resource/CRTCovertDataSupplier.java | 103 +++-------------- .../resource/CovertNoiseGenerator.java | 106 ++++++++++++++++++ 2 files changed, 119 insertions(+), 90 deletions(-) create mode 100644 suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CovertNoiseGenerator.java diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTCovertDataSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTCovertDataSupplier.java index 01f8fe16b..402272a42 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTCovertDataSupplier.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTCovertDataSupplier.java @@ -1,119 +1,40 @@ package dk.alexandra.fresco.suite.crt.datatypes.resource; import dk.alexandra.fresco.framework.DRes; -import dk.alexandra.fresco.framework.MaliciousException; -import dk.alexandra.fresco.framework.builder.numeric.Numeric; import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool; import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; import dk.alexandra.fresco.framework.builder.numeric.field.FieldDefinition; import dk.alexandra.fresco.framework.util.Pair; -import dk.alexandra.fresco.framework.value.SInt; -import dk.alexandra.fresco.suite.crt.CRTNumericContext; import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; -import dk.alexandra.fresco.suite.crt.protocols.framework.CRTComputation; -import java.math.BigInteger; import java.util.ArrayDeque; -import java.util.ArrayList; -import java.util.List; public class CRTCovertDataSupplier implements CRTDataSupplier { private final ArrayDeque noisePairs = new ArrayDeque<>(); - private final FieldDefinition fp; - private final FieldDefinition fq; - private final int deterrenceFactor = 10; - private class CovertNoiseGenerator extends CRTComputation, ResourcePoolL, ResourcePoolR> { + private final CovertNoiseGenerator noiseGenerator; + private final CRTResourcePool resourcePool; - private final int batchSize; - private final SemiHonestNoiseGenerator noiseGenerator; - - public CovertNoiseGenerator(int batchSize) { - this.batchSize = batchSize; - this.noiseGenerator = new SemiHonestNoiseGenerator<>(deterrenceFactor * batchSize); - } - - @Override - public DRes> buildComputation(ProtocolBuilderNumeric builder, - CRTNumericContext context) { - return builder.par(noiseGenerator::buildComputation).par((par, honestNoisePairs) -> { - // Sample batchsize random noisepairs to keep, mark all other to be checked/selected - Numeric numeric = par.numeric(); - List> toKeep = new ArrayList<>(batchSize); - for (int i = 0; i < batchSize; i++) { - DRes c = numeric.open(numeric.randomElement()); // TODO: check if this is smart - toKeep.add(c); - } - return Pair.lazy(toKeep, honestNoisePairs); - - }).par((par, pair) -> { // Open selected pairs - - // prepare pairs indices to keep - int i = 0; - int[] toKeep = new int[batchSize]; - for (DRes keep : pair.getFirst()) { - long k = keep.out().abs().longValue(); - // ensure we have c between 0 and gamma. - toKeep[i] = (int) k % deterrenceFactor; // TODO: this might have to be done differently - i++; - } - List honestNoisePairs = pair.getSecond(); - - // check all other pairs - int j = 0; - List, DRes>> testSet = new ArrayList<>(); - for (CRTSInt noisePair : honestNoisePairs) { - i = j / batchSize; // current batch no. - if (j == toKeep[i] + i*batchSize) // skip the once marked to keep. - // scale them back up, so they correspond to a batch. - continue; - j++; - DRes leftClosed = noisePair.getLeft(); - DRes rightClosed = noisePair.getRight(); - par.numeric().open(noisePair); - DRes leftOpened = context.leftNumeric(par).open(leftClosed); - DRes rightOpened = context.rightNumeric(par).open(rightClosed); - testSet.add(new Pair<>(leftOpened, rightOpened)); - } - - // filter out the ones we need. - List noisePairs = new ArrayList<>(batchSize); - for (i = 0; i < batchSize; i++) { - int k = toKeep[i] + i*batchSize; - noisePairs.add(honestNoisePairs.get(k)); - } - return Pair.lazy(noisePairs, testSet); - - }).par((par, pair) -> { // Check selected pairs - - List, DRes>> testSet = pair.getSecond(); - for (Pair, DRes> noisePair : testSet) { - BigInteger rp = noisePair.getFirst().out(); - BigInteger rq = noisePair.getFirst().out(); - if (rp.compareTo(rq) != 0) { - throw new MaliciousException("Cheating party, terminating"); - } - } - List noisePairs = pair.getFirst(); - return DRes.of(noisePairs); - }); - } + public CRTCovertDataSupplier(CRTResourcePool resourcePool) { + this(resourcePool, 8, 2, 40); } + public CRTCovertDataSupplier(CRTResourcePool resourcePool) { - this.fp = resourcePool.getFieldDefinitions().getFirst(); - this.fq = resourcePool.getFieldDefinitions().getFirst(); + ResourcePoolR> resourcePool, int batchSize, int deterrenceFactor, int securityParam) { + this.resourcePool = resourcePool; + this.noiseGenerator = new CovertNoiseGenerator<>(batchSize, deterrenceFactor, securityParam); } @Override public DRes getCorrelatedNoise(ProtocolBuilderNumeric builder) { if (noisePairs.isEmpty()) { - return builder.seq(new CovertNoiseGenerator(10)).seq((seq, noise) -> { + return builder.seq(noiseGenerator).seq((seq, noise) -> { noisePairs.addAll(noise); CRTSInt out = noisePairs.pop(); return DRes.of(out); @@ -130,6 +51,8 @@ public CRTSInt getRandomBit() { @Override public Pair getFieldDefinitions() { - return new Pair<>(fp, fq); + return new Pair<>(resourcePool.getSubResourcePools().getFirst().getFieldDefinition(), + resourcePool.getSubResourcePools().getSecond().getFieldDefinition() + ); } } diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CovertNoiseGenerator.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CovertNoiseGenerator.java new file mode 100644 index 000000000..7070aa0eb --- /dev/null +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CovertNoiseGenerator.java @@ -0,0 +1,106 @@ +package dk.alexandra.fresco.suite.crt.datatypes.resource; + +import dk.alexandra.fresco.framework.DRes; +import dk.alexandra.fresco.framework.MaliciousException; +import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool; +import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; +import dk.alexandra.fresco.framework.util.AesCtrDrbg; +import dk.alexandra.fresco.framework.util.Pair; +import dk.alexandra.fresco.framework.value.SInt; +import dk.alexandra.fresco.suite.crt.CRTNumericContext; +import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; +import dk.alexandra.fresco.suite.crt.protocols.framework.CRTComputation; +import dk.alexandra.fresco.tools.commitment.CoinTossingComputation; +import dk.alexandra.fresco.tools.commitment.HashBasedCommitmentSerializer; + +import java.math.BigInteger; +import java.util.ArrayList; +import java.util.List; + +public class CovertNoiseGenerator + extends CRTComputation, ResourcePoolL, ResourcePoolR> { + + private final int batchSize; + private final int deterrenceFactor; + private final int securityParam; + + private final SemiHonestNoiseGenerator noiseGenerator; + private final CoinTossingComputation coinToss; + + private byte[] seed; + + public CovertNoiseGenerator(int batchSize, int deterrenceFactor, int securityParam) { + this.batchSize = batchSize; + this.deterrenceFactor = deterrenceFactor; + this.securityParam = securityParam; + + AesCtrDrbg localDrbg = new AesCtrDrbg(); + HashBasedCommitmentSerializer commitmentSerializer = new HashBasedCommitmentSerializer(); + this.noiseGenerator = new SemiHonestNoiseGenerator<>(this.deterrenceFactor * batchSize); + this.coinToss = new CoinTossingComputation(32, commitmentSerializer, localDrbg); + } + + @Override + public DRes> buildComputation(ProtocolBuilderNumeric builder, + CRTNumericContext context) { + return builder.par(noiseGenerator::buildComputation).par((par, honestNoisePairs) -> { + // Sample batchsize random noise pairs to keep, mark all other to be checked/selected + DRes newSeed; + if (this.seed == null) { + newSeed = coinToss.buildComputation(par); + } else { // reuse seed if already computed. + newSeed = DRes.of(this.seed); + } + return Pair.lazy(newSeed, honestNoisePairs); + }).par((par, pair) -> { // Open selected pairs + // prepare pairs indices to keep + this.seed = pair.getFirst().out(); + + AesCtrDrbg jointDrbg = new AesCtrDrbg(seed); + int[] toKeep = new int[batchSize]; + for (int i = 0; i < batchSize; i++) { + byte[] sample = new byte[securityParam + Integer.numberOfLeadingZeros(deterrenceFactor)]; + jointDrbg.nextBytes(sample); + toKeep[i] = new BigInteger(1, sample).mod(BigInteger.valueOf(deterrenceFactor)).intValue(); + } + + // check all other pairs + List honestNoisePairs = pair.getSecond(); + int i, j = 0; + List, DRes>> testSet = new ArrayList, DRes>>(); + for (CRTSInt noisePair : honestNoisePairs) { + i = j / deterrenceFactor; // current batch no. + if (j == toKeep[i] + i * batchSize) // skip the once marked to keep. + // scale them back up, so they correspond to a batch. + continue; + j++; + DRes leftClosed = noisePair.getLeft(); + DRes rightClosed = noisePair.getRight(); + DRes leftOpened = context.leftNumeric(par).open(leftClosed); + DRes rightOpened = context.rightNumeric(par).open(rightClosed); + testSet.add(new Pair<>(leftOpened, rightOpened)); + } + + // filter out the ones we need. + List noisePairs = new ArrayList<>(batchSize); + for (i = 0; i < batchSize; i++) { + int k = toKeep[i] + i * deterrenceFactor; + noisePairs.add(honestNoisePairs.get(k)); + } + return Pair.lazy(noisePairs, testSet); + + }).par((par, pair) -> { // Check selected pairs + + List, DRes>> testSet = pair.getSecond(); + for (Pair, DRes> noisePair : testSet) { + BigInteger rp = noisePair.getFirst().out(); + BigInteger rq = noisePair.getFirst().out(); + if (rp.compareTo(rq) != 0) { + throw new MaliciousException("Cheating party, terminating"); + } + } + List noisePairs = pair.getFirst(); + return DRes.of(noisePairs); + }); + } +} \ No newline at end of file From 9d728a65aeb5509e2df06de7905212822c2ebc37 Mon Sep 17 00:00:00 2001 From: Mikkel Wienberg Madsen Date: Wed, 11 Jan 2023 12:35:38 +0100 Subject: [PATCH 17/38] Refactor CRTDataSupplier(s) --- .../resource/CRTCovertDataSupplier.java | 55 +++---------------- .../datatypes/resource/CRTDataSupplier.java | 39 +++++++++++-- .../resource/CRTDummyDataSupplier.java | 5 +- .../datatypes/resource/CRTNoiseGenerator.java | 18 ++++++ .../resource/CRTSemiHonestDataSupplier.java | 47 ++-------------- .../resource/CovertNoiseGenerator.java | 3 +- .../resource/SemiHonestNoiseGenerator.java | 3 +- 7 files changed, 69 insertions(+), 101 deletions(-) create mode 100644 suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTNoiseGenerator.java diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTCovertDataSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTCovertDataSupplier.java index 402272a42..bfe1f5265 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTCovertDataSupplier.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTCovertDataSupplier.java @@ -1,58 +1,17 @@ package dk.alexandra.fresco.suite.crt.datatypes.resource; -import dk.alexandra.fresco.framework.DRes; import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool; -import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; -import dk.alexandra.fresco.framework.builder.numeric.field.FieldDefinition; -import dk.alexandra.fresco.framework.util.Pair; -import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; -import java.util.ArrayDeque; +public class CRTCovertDataSupplier + extends CRTDataSupplier> { -public class CRTCovertDataSupplier - implements CRTDataSupplier { - - private final ArrayDeque noisePairs = new ArrayDeque<>(); - - - private final CovertNoiseGenerator noiseGenerator; - private final CRTResourcePool resourcePool; - - public CRTCovertDataSupplier(CRTResourcePool resourcePool) { + public CRTCovertDataSupplier(CRTResourcePool resourcePool) { this(resourcePool, 8, 2, 40); } - - public CRTCovertDataSupplier(CRTResourcePool resourcePool, int batchSize, int deterrenceFactor, int securityParam) { - this.resourcePool = resourcePool; - this.noiseGenerator = new CovertNoiseGenerator<>(batchSize, deterrenceFactor, securityParam); - } - - @Override - public DRes getCorrelatedNoise(ProtocolBuilderNumeric builder) { - if (noisePairs.isEmpty()) { - return builder.seq(noiseGenerator).seq((seq, noise) -> { - noisePairs.addAll(noise); - CRTSInt out = noisePairs.pop(); - return DRes.of(out); - }); - } else { - return DRes.of(noisePairs.pop()); - } - } - - @Override - public CRTSInt getRandomBit() { - return null; - } - - @Override - public Pair getFieldDefinitions() { - return new Pair<>(resourcePool.getSubResourcePools().getFirst().getFieldDefinition(), - resourcePool.getSubResourcePools().getSecond().getFieldDefinition() - ); + public CRTCovertDataSupplier(CRTResourcePool resourcePool, + int batchSize, int deterrenceFactor, int securityParam) { + super(new CovertNoiseGenerator<>(batchSize, deterrenceFactor, securityParam), resourcePool); } } diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDataSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDataSupplier.java index 00651cd9c..bbfaf5727 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDataSupplier.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDataSupplier.java @@ -1,26 +1,57 @@ package dk.alexandra.fresco.suite.crt.datatypes.resource; import dk.alexandra.fresco.framework.DRes; +import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool; import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; import dk.alexandra.fresco.framework.builder.numeric.field.FieldDefinition; import dk.alexandra.fresco.framework.util.Pair; import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; -public interface CRTDataSupplier { +import java.util.ArrayDeque; + +public abstract class CRTDataSupplier> { + + private final ArrayDeque noisePairs = new ArrayDeque<>(); + private final T noiseGenerator; + private final CRTResourcePool resourcePool; + + + protected CRTDataSupplier(T noiseGenerator, CRTResourcePool resourcePool) { + this.noiseGenerator = noiseGenerator; + this.resourcePool = resourcePool; + } /** * Supplies the next correlated noise * * @return r */ - DRes getCorrelatedNoise(ProtocolBuilderNumeric builder); + public DRes getCorrelatedNoise(ProtocolBuilderNumeric builder) { + if (noisePairs.isEmpty()) { + return builder.seq(noiseGenerator).seq((seq, noise) -> { + noisePairs.addAll(noise); + CRTSInt out = noisePairs.pop(); + return DRes.of(out); + }); + } else { + return DRes.of(noisePairs.pop()); + } + } /** * Supply the next random bit * * @return b */ - CRTSInt getRandomBit(); + public CRTSInt getRandomBit() { + return null; + } + + public Pair getFieldDefinitions() { + return new Pair<>(resourcePool.getSubResourcePools().getFirst().getFieldDefinition(), + resourcePool.getSubResourcePools().getSecond().getFieldDefinition() + ); + } - Pair getFieldDefinitions(); } diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDummyDataSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDummyDataSupplier.java index d87d2e31e..5726dda62 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDummyDataSupplier.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDummyDataSupplier.java @@ -11,10 +11,9 @@ import java.util.Random; import java.util.function.Function; -public class CRTDummyDataSupplier implements CRTDataSupplier { +public class CRTDummyDataSupplier extends CRTDataSupplier { private final FieldDefinition fp, fq; - private final int myId; private final int players; private final Random random; private final Function wrapperLeft, wrapperRight; @@ -22,7 +21,7 @@ public class CRTDummyDataSupplier implements CRTDataSupplier { public CRTDummyDataSupplier(int myId, int players, FieldDefinition leftField, FieldDefinition rightField, Function wrapperLeft, Function wrapperRight) { - this.myId = myId; + super(null, null); this.players = players; this.fp = leftField; this.fq = rightField; diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTNoiseGenerator.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTNoiseGenerator.java new file mode 100644 index 000000000..d0076c779 --- /dev/null +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTNoiseGenerator.java @@ -0,0 +1,18 @@ +package dk.alexandra.fresco.suite.crt.datatypes.resource; + +import dk.alexandra.fresco.framework.DRes; +import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool; +import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; +import dk.alexandra.fresco.suite.crt.CRTNumericContext; +import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; +import dk.alexandra.fresco.suite.crt.protocols.framework.CRTComputation; + +import java.util.List; + +public abstract class CRTNoiseGenerator + extends CRTComputation, ResourcePoolL, ResourcePoolR> { + + @Override + abstract public DRes> buildComputation(ProtocolBuilderNumeric builder, + CRTNumericContext context); +} diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java index 046510d8c..cf6f43bb4 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java @@ -1,49 +1,12 @@ package dk.alexandra.fresco.suite.crt.datatypes.resource; -import dk.alexandra.fresco.framework.DRes; import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool; -import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; -import dk.alexandra.fresco.framework.builder.numeric.field.FieldDefinition; -import dk.alexandra.fresco.framework.util.Pair; -import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; -import java.util.ArrayDeque; +public class CRTSemiHonestDataSupplier + extends CRTDataSupplier> { -public class CRTSemiHonestDataSupplier - implements CRTDataSupplier { - - private final ArrayDeque noisePairs = new ArrayDeque<>(); - private final FieldDefinition fp; - private final FieldDefinition fq; - - - public CRTSemiHonestDataSupplier(CRTResourcePool resourcePool) { - this.fp = resourcePool.getFieldDefinitions().getFirst(); - this.fq = resourcePool.getFieldDefinitions().getFirst(); - } - - @Override - public DRes getCorrelatedNoise(ProtocolBuilderNumeric builder) { - if (noisePairs.isEmpty()) { - return builder.seq(new SemiHonestNoiseGenerator(10)).seq((seq, noise) -> { - noisePairs.addAll(noise); - CRTSInt out = noisePairs.pop(); - return DRes.of(out); - }); - } else { - return DRes.of(noisePairs.pop()); - } - } - - @Override - public CRTSInt getRandomBit() { - return null; - } - - @Override - public Pair getFieldDefinitions() { - return new Pair<>(fp, fq); + public CRTSemiHonestDataSupplier(CRTResourcePool resourcePool) { + super(new SemiHonestNoiseGenerator<>(10), resourcePool); } } diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CovertNoiseGenerator.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CovertNoiseGenerator.java index 7070aa0eb..beb8da5f5 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CovertNoiseGenerator.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CovertNoiseGenerator.java @@ -9,7 +9,6 @@ import dk.alexandra.fresco.framework.value.SInt; import dk.alexandra.fresco.suite.crt.CRTNumericContext; import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; -import dk.alexandra.fresco.suite.crt.protocols.framework.CRTComputation; import dk.alexandra.fresco.tools.commitment.CoinTossingComputation; import dk.alexandra.fresco.tools.commitment.HashBasedCommitmentSerializer; @@ -18,7 +17,7 @@ import java.util.List; public class CovertNoiseGenerator - extends CRTComputation, ResourcePoolL, ResourcePoolR> { + extends CRTNoiseGenerator { private final int batchSize; private final int deterrenceFactor; diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/SemiHonestNoiseGenerator.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/SemiHonestNoiseGenerator.java index 27f32007f..3d73dd2c9 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/SemiHonestNoiseGenerator.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/SemiHonestNoiseGenerator.java @@ -7,13 +7,12 @@ import dk.alexandra.fresco.framework.value.SInt; import dk.alexandra.fresco.suite.crt.CRTNumericContext; import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; -import dk.alexandra.fresco.suite.crt.protocols.framework.CRTComputation; import java.util.ArrayList; import java.util.List; public class SemiHonestNoiseGenerator - extends CRTComputation, ResourcePoolL, ResourcePoolR> { + extends CRTNoiseGenerator { private final int batchSize; From b4581f3318249a99cbabd3e21396e18d81a1f7fb Mon Sep 17 00:00:00 2001 From: Mikkel Wienberg Madsen Date: Wed, 11 Jan 2023 14:50:26 +0100 Subject: [PATCH 18/38] Don't reuse seed, reuse the Drbg --- .../framework/network/socket/Connector.java | 20 ++++++----------- .../resource/CovertNoiseGenerator.java | 22 +++++++++---------- 2 files changed, 18 insertions(+), 24 deletions(-) diff --git a/core/src/main/java/dk/alexandra/fresco/framework/network/socket/Connector.java b/core/src/main/java/dk/alexandra/fresco/framework/network/socket/Connector.java index 4bb64b020..a3d395cee 100644 --- a/core/src/main/java/dk/alexandra/fresco/framework/network/socket/Connector.java +++ b/core/src/main/java/dk/alexandra/fresco/framework/network/socket/Connector.java @@ -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; @@ -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 { @@ -135,7 +129,7 @@ private Map 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); } } } diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CovertNoiseGenerator.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CovertNoiseGenerator.java index beb8da5f5..bbe61cac9 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CovertNoiseGenerator.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CovertNoiseGenerator.java @@ -25,8 +25,8 @@ public class CovertNoiseGenerator noiseGenerator; private final CoinTossingComputation coinToss; + private AesCtrDrbg jointDrbg; - private byte[] seed; public CovertNoiseGenerator(int batchSize, int deterrenceFactor, int securityParam) { this.batchSize = batchSize; @@ -44,18 +44,18 @@ public DRes> buildComputation(ProtocolBuilderNumeric builder, CRTNumericContext context) { return builder.par(noiseGenerator::buildComputation).par((par, honestNoisePairs) -> { // Sample batchsize random noise pairs to keep, mark all other to be checked/selected - DRes newSeed; - if (this.seed == null) { - newSeed = coinToss.buildComputation(par); - } else { // reuse seed if already computed. - newSeed = DRes.of(this.seed); - } - return Pair.lazy(newSeed, honestNoisePairs); + DRes seed = null; + if (this.jointDrbg == null) { + seed = coinToss.buildComputation(par); + } // reuse seed if already computed. + + return Pair.lazy(seed, honestNoisePairs); }).par((par, pair) -> { // Open selected pairs // prepare pairs indices to keep - this.seed = pair.getFirst().out(); - - AesCtrDrbg jointDrbg = new AesCtrDrbg(seed); + byte[] seed = pair.getFirst().out(); + if (this.jointDrbg == null) { + jointDrbg = new AesCtrDrbg(seed); + } int[] toKeep = new int[batchSize]; for (int i = 0; i < batchSize; i++) { byte[] sample = new byte[securityParam + Integer.numberOfLeadingZeros(deterrenceFactor)]; From cbc47656c966f8c73988c1cb84dfe504f2f8d890 Mon Sep 17 00:00:00 2001 From: Mikkel Wienberg Madsen Date: Thu, 12 Jan 2023 09:25:10 +0100 Subject: [PATCH 19/38] Remove redundant generics --- .../crt/datatypes/resource/CRTCovertDataSupplier.java | 2 +- .../suite/crt/datatypes/resource/CRTDataSupplier.java | 7 +++---- .../suite/crt/datatypes/resource/CRTDummyDataSupplier.java | 3 ++- .../crt/datatypes/resource/CRTSemiHonestDataSupplier.java | 2 +- .../suite/crt/datatypes/resource/CovertNoiseGenerator.java | 3 ++- .../{CRTNoiseGenerator.java => NoiseGenerator.java} | 2 +- .../crt/datatypes/resource/SemiHonestNoiseGenerator.java | 2 +- 7 files changed, 11 insertions(+), 10 deletions(-) rename suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/{CRTNoiseGenerator.java => NoiseGenerator.java} (85%) diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTCovertDataSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTCovertDataSupplier.java index bfe1f5265..537e78708 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTCovertDataSupplier.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTCovertDataSupplier.java @@ -3,7 +3,7 @@ import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool; public class CRTCovertDataSupplier - extends CRTDataSupplier> { + extends CRTDataSupplier { public CRTCovertDataSupplier(CRTResourcePool resourcePool) { diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDataSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDataSupplier.java index bbfaf5727..345fd938a 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDataSupplier.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDataSupplier.java @@ -9,15 +9,14 @@ import java.util.ArrayDeque; -public abstract class CRTDataSupplier> { +public abstract class CRTDataSupplier { private final ArrayDeque noisePairs = new ArrayDeque<>(); - private final T noiseGenerator; + private final NoiseGenerator noiseGenerator; private final CRTResourcePool resourcePool; - protected CRTDataSupplier(T noiseGenerator, CRTResourcePool resourcePool) { + protected CRTDataSupplier(NoiseGenerator noiseGenerator, CRTResourcePool resourcePool) { this.noiseGenerator = noiseGenerator; this.resourcePool = resourcePool; } diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDummyDataSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDummyDataSupplier.java index 5726dda62..1fa878e7d 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDummyDataSupplier.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDummyDataSupplier.java @@ -1,6 +1,7 @@ package dk.alexandra.fresco.suite.crt.datatypes.resource; import dk.alexandra.fresco.framework.DRes; +import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool; import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; import dk.alexandra.fresco.framework.builder.numeric.field.FieldDefinition; import dk.alexandra.fresco.framework.util.Pair; @@ -11,7 +12,7 @@ import java.util.Random; import java.util.function.Function; -public class CRTDummyDataSupplier extends CRTDataSupplier { +public class CRTDummyDataSupplier extends CRTDataSupplier { private final FieldDefinition fp, fq; private final int players; diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java index cf6f43bb4..75c9bd34a 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java @@ -4,7 +4,7 @@ public class CRTSemiHonestDataSupplier - extends CRTDataSupplier> { + extends CRTDataSupplier { public CRTSemiHonestDataSupplier(CRTResourcePool resourcePool) { super(new SemiHonestNoiseGenerator<>(10), resourcePool); diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CovertNoiseGenerator.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CovertNoiseGenerator.java index bbe61cac9..7d16f4aa6 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CovertNoiseGenerator.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CovertNoiseGenerator.java @@ -17,7 +17,7 @@ import java.util.List; public class CovertNoiseGenerator - extends CRTNoiseGenerator { + extends NoiseGenerator { private final int batchSize; private final int deterrenceFactor; @@ -56,6 +56,7 @@ public DRes> buildComputation(ProtocolBuilderNumeric builder, if (this.jointDrbg == null) { jointDrbg = new AesCtrDrbg(seed); } + int[] toKeep = new int[batchSize]; for (int i = 0; i < batchSize; i++) { byte[] sample = new byte[securityParam + Integer.numberOfLeadingZeros(deterrenceFactor)]; diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTNoiseGenerator.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/NoiseGenerator.java similarity index 85% rename from suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTNoiseGenerator.java rename to suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/NoiseGenerator.java index d0076c779..020590153 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTNoiseGenerator.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/NoiseGenerator.java @@ -9,7 +9,7 @@ import java.util.List; -public abstract class CRTNoiseGenerator +public abstract class NoiseGenerator extends CRTComputation, ResourcePoolL, ResourcePoolR> { @Override diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/SemiHonestNoiseGenerator.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/SemiHonestNoiseGenerator.java index 3d73dd2c9..1b232e5df 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/SemiHonestNoiseGenerator.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/SemiHonestNoiseGenerator.java @@ -12,7 +12,7 @@ import java.util.List; public class SemiHonestNoiseGenerator - extends CRTNoiseGenerator { + extends NoiseGenerator { private final int batchSize; From fef58f5c586801e21a9439002d744826dd7362b0 Mon Sep 17 00:00:00 2001 From: Mikkel Wienberg Madsen Date: Wed, 22 Mar 2023 10:50:05 +0100 Subject: [PATCH 20/38] Fix uses of CRTDataSupplier being non-generic --- .../dk/alexandra/fresco/suite/crt/AbstractDummyCRTTest.java | 2 +- .../java/dk/alexandra/fresco/suite/crt/AbstractSpdzCRTTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractDummyCRTTest.java b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractDummyCRTTest.java index 7c5af32d5..ccc3962d8 100644 --- a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractDummyCRTTest.java +++ b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractDummyCRTTest.java @@ -81,7 +81,7 @@ public void runTest( SecureComputationEngine, ProtocolBuilderNumeric> sce = new SecureComputationEngineImpl<>(ps, evaluator); - CRTDataSupplier dataSupplier = new CRTDummyDataSupplier(playerId, noOfParties, + CRTDataSupplier dataSupplier = new CRTDummyDataSupplier<>(playerId, noOfParties, DEFAULT_FIELD_LEFT, DEFAULT_FIELD_RIGHT, x -> new DummyArithmeticSInt(DEFAULT_FIELD_LEFT.createElement(x)), y -> new DummyArithmeticSInt(DEFAULT_FIELD_RIGHT.createElement(y))); diff --git a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSpdzCRTTest.java b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSpdzCRTTest.java index 44ddf82e6..9f3984f87 100644 --- a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSpdzCRTTest.java +++ b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSpdzCRTTest.java @@ -123,7 +123,7 @@ public void runTest( SecureComputationEngine, ProtocolBuilderNumeric> sce = new SecureComputationEngineImpl<>(ps, evaluator); - CRTDataSupplier dataSupplier = new CRTDummyDataSupplier(playerId, noOfParties, + CRTDataSupplier dataSupplier = new CRTDummyDataSupplier<>(playerId, noOfParties, DEFAULT_FIELD_LEFT, DEFAULT_FIELD_RIGHT, x -> toSpdzSInt(x, playerId, noOfParties, DEFAULT_FIELD_LEFT, new Random(1234), new BigInteger(DEFAULT_FIELD_LEFT.getModulus().bitLength(), new Random(0)) From 96eb82797942755b8e7af1036629253f14b3cadb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Lindstr=C3=B8m?= Date: Wed, 22 Mar 2023 14:57:10 +0100 Subject: [PATCH 21/38] Only use seed if it was computed in previous step --- .../suite/crt/datatypes/resource/CovertNoiseGenerator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CovertNoiseGenerator.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CovertNoiseGenerator.java index 7d16f4aa6..9d00e0931 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CovertNoiseGenerator.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CovertNoiseGenerator.java @@ -52,8 +52,8 @@ public DRes> buildComputation(ProtocolBuilderNumeric builder, return Pair.lazy(seed, honestNoisePairs); }).par((par, pair) -> { // Open selected pairs // prepare pairs indices to keep - byte[] seed = pair.getFirst().out(); if (this.jointDrbg == null) { + byte[] seed = pair.getFirst().out(); jointDrbg = new AesCtrDrbg(seed); } From 99b536b0b9bef2313eaa03841a113d0f47d4f73b Mon Sep 17 00:00:00 2001 From: Tore Frederiksen Date: Tue, 9 May 2023 12:52:07 +0300 Subject: [PATCH 22/38] incremental commit --- .../suite/crt/datatypes/CRTCombinedPad.java | 30 +++++ .../suite/crt/datatypes/CRTPadPair.java | 22 ++++ .../resource/CRTCovertDataSupplier.java | 3 +- ...r.java => CRTCovertDummyDataSupplier.java} | 23 +++- .../datatypes/resource/CRTDataSupplier.java | 15 ++- .../resource/CRTSemiHonestDataSupplier.java | 3 +- .../CRTSemiHonestDummyDataSupplier.java | 57 ++++++++ .../resource/CovertNoiseGenerator.java | 124 ++++++++++++------ .../datatypes/resource/NoiseGenerator.java | 8 +- .../datatypes/resource/PaddingGenerator.java | 79 +++++++++++ .../resource/SemiHonestNoiseGenerator.java | 4 +- .../protocols/CorrelatedNoiseProtocol.java | 7 +- .../suite/crt/protocols/LiftPQProtocol.java | 2 +- .../suite/crt/protocols/LiftQPProtocol.java | 2 +- .../suite/crt/AbstractDummyCRTTest.java | 11 +- .../fresco/suite/crt/AbstractSpdzCRTTest.java | 12 +- .../fresco/suite/crt/BasicCRTTests.java | 2 +- 17 files changed, 326 insertions(+), 78 deletions(-) create mode 100644 suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/CRTCombinedPad.java create mode 100644 suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/CRTPadPair.java rename suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/{CRTDummyDataSupplier.java => CRTCovertDummyDataSupplier.java} (57%) create mode 100644 suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDummyDataSupplier.java create mode 100644 suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/PaddingGenerator.java diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/CRTCombinedPad.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/CRTCombinedPad.java new file mode 100644 index 000000000..a11a1075c --- /dev/null +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/CRTCombinedPad.java @@ -0,0 +1,30 @@ +package dk.alexandra.fresco.suite.crt.datatypes; + +import dk.alexandra.fresco.framework.DRes; +import dk.alexandra.fresco.framework.value.SInt; + +import java.util.List; + +public class CRTCombinedPad { + private final CRTSInt noisePair; + private final DRes rho; + private final DRes psi; + + public CRTCombinedPad(CRTSInt noisePair, DRes rho, DRes psi) { + this.noisePair = noisePair; + this.rho = rho; + this.psi = psi; + } + + public CRTSInt getNoisePair() { + return noisePair; + } + + public DRes getRho() { + return rho; + } + + public DRes getPsi() { + return psi; + } +} diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/CRTPadPair.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/CRTPadPair.java new file mode 100644 index 000000000..127d94d38 --- /dev/null +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/CRTPadPair.java @@ -0,0 +1,22 @@ +package dk.alexandra.fresco.suite.crt.datatypes; + +import dk.alexandra.fresco.framework.DRes; +import dk.alexandra.fresco.framework.value.SInt; + +public class CRTPadPair { + private final SInt rhoPad; + private final SInt psiPad; + + public CRTPadPair(SInt rhoPad, SInt psiPad) { + this.rhoPad = rhoPad; + this.psiPad = psiPad; + } + + public SInt getRho() { + return rhoPad; + } + + public SInt getPsi() { + return psiPad; + } +} diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTCovertDataSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTCovertDataSupplier.java index 537e78708..465d2b0c3 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTCovertDataSupplier.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTCovertDataSupplier.java @@ -1,9 +1,10 @@ package dk.alexandra.fresco.suite.crt.datatypes.resource; import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool; +import dk.alexandra.fresco.suite.crt.datatypes.CRTCombinedPad; public class CRTCovertDataSupplier - extends CRTDataSupplier { + extends CRTDataSupplier { public CRTCovertDataSupplier(CRTResourcePool resourcePool) { diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDummyDataSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTCovertDummyDataSupplier.java similarity index 57% rename from suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDummyDataSupplier.java rename to suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTCovertDummyDataSupplier.java index 1fa878e7d..9d0943d33 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDummyDataSupplier.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTCovertDummyDataSupplier.java @@ -7,23 +7,27 @@ import dk.alexandra.fresco.framework.util.Pair; import dk.alexandra.fresco.framework.value.SInt; import dk.alexandra.fresco.suite.crt.Util; +import dk.alexandra.fresco.suite.crt.datatypes.CRTCombinedPad; import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; import java.math.BigInteger; import java.util.Random; import java.util.function.Function; -public class CRTDummyDataSupplier extends CRTDataSupplier { +public class CRTCovertDummyDataSupplier extends CRTDataSupplier { private final FieldDefinition fp, fq; private final int players; + private final int statisticalSecurity; private final Random random; private final Function wrapperLeft, wrapperRight; - public CRTDummyDataSupplier(int myId, int players, FieldDefinition leftField, - FieldDefinition rightField, Function wrapperLeft, - Function wrapperRight) { + // todo could aggregate the semihonest Dummy data supplier to avoid code-copy + public CRTCovertDummyDataSupplier(int myId, int players, int statisticalSecurity, FieldDefinition leftField, + FieldDefinition rightField, Function wrapperLeft, + Function wrapperRight) { super(null, null); this.players = players; + this.statisticalSecurity = statisticalSecurity; this.fp = leftField; this.fq = rightField; this.wrapperLeft = wrapperLeft; @@ -34,12 +38,17 @@ public CRTDummyDataSupplier(int myId, int players, FieldDefinition leftField, } @Override - public DRes getCorrelatedNoise(ProtocolBuilderNumeric builder) { + public DRes getCorrelatedNoise(ProtocolBuilderNumeric builder) { BigInteger r = Util.randomBigInteger(random, fp.getModulus()); BigInteger l = Util .randomBigInteger(random, BigInteger.valueOf(players)); - return DRes.of(new CRTSInt(wrapperLeft.apply(r), - wrapperRight.apply(r.add(l.multiply(fp.getModulus()))))); + BigInteger rho = new BigInteger(statisticalSecurity, random); + BigInteger psi = new BigInteger(statisticalSecurity, random); + return DRes.of( + new CRTCombinedPad( + new CRTSInt(wrapperLeft.apply(r), + wrapperRight.apply(r.add(l.multiply(fp.getModulus())))), + wrapperRight.apply(rho), wrapperRight.apply(psi))); } @Override diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDataSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDataSupplier.java index 345fd938a..62fa58580 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDataSupplier.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDataSupplier.java @@ -5,18 +5,19 @@ import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; import dk.alexandra.fresco.framework.builder.numeric.field.FieldDefinition; import dk.alexandra.fresco.framework.util.Pair; +import dk.alexandra.fresco.suite.crt.datatypes.CRTCombinedPad; import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; import java.util.ArrayDeque; -public abstract class CRTDataSupplier { +public abstract class CRTDataSupplier { - private final ArrayDeque noisePairs = new ArrayDeque<>(); - private final NoiseGenerator noiseGenerator; + private final ArrayDeque noisePairs = new ArrayDeque<>(); + private final NoiseGenerator noiseGenerator; private final CRTResourcePool resourcePool; - protected CRTDataSupplier(NoiseGenerator noiseGenerator, CRTResourcePool resourcePool) { + protected CRTDataSupplier(NoiseGenerator noiseGenerator, CRTResourcePool resourcePool) { this.noiseGenerator = noiseGenerator; this.resourcePool = resourcePool; } @@ -26,11 +27,11 @@ protected CRTDataSupplier(NoiseGenerator noiseGenerator, CRTResourcePool getCorrelatedNoise(ProtocolBuilderNumeric builder) { + public DRes getCorrelatedNoise(ProtocolBuilderNumeric builder) { if (noisePairs.isEmpty()) { return builder.seq(noiseGenerator).seq((seq, noise) -> { noisePairs.addAll(noise); - CRTSInt out = noisePairs.pop(); + NoiseT out = noisePairs.pop(); return DRes.of(out); }); } else { @@ -40,7 +41,7 @@ public DRes getCorrelatedNoise(ProtocolBuilderNumeric builder) { /** * Supply the next random bit - * + * todo * @return b */ public CRTSInt getRandomBit() { diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java index 75c9bd34a..77ae44dfe 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java @@ -1,10 +1,11 @@ package dk.alexandra.fresco.suite.crt.datatypes.resource; import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool; +import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; public class CRTSemiHonestDataSupplier - extends CRTDataSupplier { + extends CRTDataSupplier { public CRTSemiHonestDataSupplier(CRTResourcePool resourcePool) { super(new SemiHonestNoiseGenerator<>(10), resourcePool); diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDummyDataSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDummyDataSupplier.java new file mode 100644 index 000000000..fbe90b8ff --- /dev/null +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDummyDataSupplier.java @@ -0,0 +1,57 @@ +package dk.alexandra.fresco.suite.crt.datatypes.resource; + +import dk.alexandra.fresco.framework.DRes; +import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool; +import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; +import dk.alexandra.fresco.framework.builder.numeric.field.FieldDefinition; +import dk.alexandra.fresco.framework.util.Pair; +import dk.alexandra.fresco.framework.value.SInt; +import dk.alexandra.fresco.suite.crt.Util; +import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; + +import java.math.BigInteger; +import java.util.Random; +import java.util.function.Function; + +public class CRTSemiHonestDummyDataSupplier extends CRTDataSupplier { + + private final FieldDefinition fp, fq; + private final int players; + private final Random random; + private final Function wrapperLeft, wrapperRight; + + public CRTSemiHonestDummyDataSupplier(int myId, int players, FieldDefinition leftField, + FieldDefinition rightField, Function wrapperLeft, + Function wrapperRight) { + super(null, null); + this.players = players; + this.fp = leftField; + this.fq = rightField; + this.wrapperLeft = wrapperLeft; + this.wrapperRight = wrapperRight; + + this.random = new Random(1234); + + } + + @Override + public DRes getCorrelatedNoise(ProtocolBuilderNumeric builder) { + BigInteger r = Util.randomBigInteger(random, fp.getModulus()); + BigInteger l = Util + .randomBigInteger(random, BigInteger.valueOf(players)); + return DRes.of(new CRTSInt(wrapperLeft.apply(r), + wrapperRight.apply(r.add(l.multiply(fp.getModulus()))))); + } + + @Override + public CRTSInt getRandomBit() { + BigInteger bit = Util.randomBigInteger(random, BigInteger.valueOf(2)); + return new CRTSInt(wrapperLeft.apply(bit), + wrapperRight.apply(bit)); + } + + @Override + public Pair getFieldDefinitions() { + return new Pair<>(fp, fq); + } +} diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CovertNoiseGenerator.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CovertNoiseGenerator.java index 9d00e0931..28ae8688e 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CovertNoiseGenerator.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CovertNoiseGenerator.java @@ -5,9 +5,11 @@ import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool; import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; import dk.alexandra.fresco.framework.util.AesCtrDrbg; +import dk.alexandra.fresco.framework.util.Drbg; import dk.alexandra.fresco.framework.util.Pair; import dk.alexandra.fresco.framework.value.SInt; import dk.alexandra.fresco.suite.crt.CRTNumericContext; +import dk.alexandra.fresco.suite.crt.datatypes.CRTCombinedPad; import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; import dk.alexandra.fresco.tools.commitment.CoinTossingComputation; import dk.alexandra.fresco.tools.commitment.HashBasedCommitmentSerializer; @@ -17,39 +19,62 @@ import java.util.List; public class CovertNoiseGenerator - extends NoiseGenerator { + extends NoiseGenerator { private final int batchSize; private final int deterrenceFactor; private final int securityParam; private final SemiHonestNoiseGenerator noiseGenerator; + private final PaddingGenerator padGenerator; private final CoinTossingComputation coinToss; private AesCtrDrbg jointDrbg; + private Drbg localDrbg; public CovertNoiseGenerator(int batchSize, int deterrenceFactor, int securityParam) { + this(batchSize, deterrenceFactor, securityParam, new AesCtrDrbg()); + } + + public CovertNoiseGenerator(int batchSize, int deterrenceFactor, int securityParam, Drbg localDrbg) { this.batchSize = batchSize; this.deterrenceFactor = deterrenceFactor; this.securityParam = securityParam; - AesCtrDrbg localDrbg = new AesCtrDrbg(); + this.localDrbg = localDrbg; HashBasedCommitmentSerializer commitmentSerializer = new HashBasedCommitmentSerializer(); this.noiseGenerator = new SemiHonestNoiseGenerator<>(this.deterrenceFactor * batchSize); + this.padGenerator = new PaddingGenerator(this.deterrenceFactor * batchSize, securityParam, localDrbg); this.coinToss = new CoinTossingComputation(32, commitmentSerializer, localDrbg); } @Override - public DRes> buildComputation(ProtocolBuilderNumeric builder, - CRTNumericContext context) { - return builder.par(noiseGenerator::buildComputation).par((par, honestNoisePairs) -> { + public DRes> buildComputation(ProtocolBuilderNumeric builder, + CRTNumericContext context) { + return builder.par(par -> { + DRes> semiHonestNoise = noiseGenerator.buildComputation(par, context); + DRes> rhoPad = padGenerator.buildComputation(par, context); + DRes> psiPad = padGenerator.buildComputation(par, context); // Sample batchsize random noise pairs to keep, mark all other to be checked/selected - DRes seed = null; + DRes seed; if (this.jointDrbg == null) { seed = coinToss.buildComputation(par); } // reuse seed if already computed. - - return Pair.lazy(seed, honestNoisePairs); + else { + seed = null; + } + return () -> new Object[] { + semiHonestNoise, rhoPad, psiPad, seed, + }; + }).par((par, data) -> { + List combinedPads = new ArrayList<>(securityParam * batchSize); + for (int i = 0; i < deterrenceFactor * batchSize; i++) { + combinedPads.add(new CRTCombinedPad( + ((DRes>) data[0]).out().get(i), + ((DRes>) data[1]).out().get(i).getRight(), + ((DRes>) data[2]).out().get(i).getRight())); + } + return Pair.lazy((DRes) data[3], combinedPads); }).par((par, pair) -> { // Open selected pairs // prepare pairs indices to keep if (this.jointDrbg == null) { @@ -57,50 +82,71 @@ public DRes> buildComputation(ProtocolBuilderNumeric builder, jointDrbg = new AesCtrDrbg(seed); } + // Sample indexes of elements to keep int[] toKeep = new int[batchSize]; for (int i = 0; i < batchSize; i++) { - byte[] sample = new byte[securityParam + Integer.numberOfLeadingZeros(deterrenceFactor)]; + // We need securityParam + log(deterrenceFactor) bits to ensure uniform randomness + byte[] sample = new byte[1+((securityParam + Integer.numberOfLeadingZeros(deterrenceFactor))/8)]; jointDrbg.nextBytes(sample); toKeep[i] = new BigInteger(1, sample).mod(BigInteger.valueOf(deterrenceFactor)).intValue(); } // check all other pairs - List honestNoisePairs = pair.getSecond(); - int i, j = 0; - List, DRes>> testSet = new ArrayList, DRes>>(); - for (CRTSInt noisePair : honestNoisePairs) { + List combinedPad = pair.getSecond(); + int i = 0; + List> padTestSet = new ArrayList<>(); + List, DRes>> noisePairTestSet = new ArrayList<>(); + List noisePairsToKeep = new ArrayList<>(batchSize); + List> rhoToKeep = new ArrayList<>(batchSize); + List> psiToKeep = new ArrayList<>(batchSize); + for (int j = 0; j < batchSize*deterrenceFactor; j++) { i = j / deterrenceFactor; // current batch no. - if (j == toKeep[i] + i * batchSize) // skip the once marked to keep. + if (j == toKeep[i] + i * deterrenceFactor) {// skip the once marked to keep. // scale them back up, so they correspond to a batch. - continue; - j++; - DRes leftClosed = noisePair.getLeft(); - DRes rightClosed = noisePair.getRight(); - DRes leftOpened = context.leftNumeric(par).open(leftClosed); - DRes rightOpened = context.rightNumeric(par).open(rightClosed); - testSet.add(new Pair<>(leftOpened, rightOpened)); - } - - // filter out the ones we need. - List noisePairs = new ArrayList<>(batchSize); - for (i = 0; i < batchSize; i++) { - int k = toKeep[i] + i * deterrenceFactor; - noisePairs.add(honestNoisePairs.get(k)); + noisePairsToKeep.add(combinedPad.get(j).getNoisePair()); + rhoToKeep.add(combinedPad.get(j).getRho()); + psiToKeep.add(combinedPad.get(j).getPsi()); + } else { + DRes leftClosed = combinedPad.get(j).getNoisePair().getLeft(); + DRes rightClosed = combinedPad.get(j).getNoisePair().getRight(); + DRes leftOpened = context.leftNumeric(par).open(leftClosed); + DRes rightOpened = context.rightNumeric(par).open(rightClosed); + noisePairTestSet.add(new Pair<>(leftOpened, rightOpened)); + DRes rhoToTest = context.rightNumeric(par).open(combinedPad.get(j).getRho()); + DRes psiToTest = context.rightNumeric(par).open(combinedPad.get(j).getPsi()); + padTestSet.add(rhoToTest); + padTestSet.add(psiToTest); + } } - return Pair.lazy(noisePairs, testSet); - - }).par((par, pair) -> { // Check selected pairs - - List, DRes>> testSet = pair.getSecond(); - for (Pair, DRes> noisePair : testSet) { - BigInteger rp = noisePair.getFirst().out(); - BigInteger rq = noisePair.getFirst().out(); + Object[] data = new Object[] { + noisePairsToKeep, noisePairTestSet, rhoToKeep, psiToKeep, padTestSet + }; + return () -> data; + }).par((par, data) -> { // Check selected pairs + List, DRes>> noiseTestSet = (List, DRes>>) data[1]; + List> padTestSet = (List>) data[4]; + BigInteger modulo = BigInteger.valueOf(2).pow(securityParam); + for (int i = 0; i < batchSize*(deterrenceFactor-1); i++) { + BigInteger rp = noiseTestSet.get(i).getFirst().out(); + // TODO there was a bug here previously + BigInteger rq = noiseTestSet.get(i).getSecond().out(); if (rp.compareTo(rq) != 0) { - throw new MaliciousException("Cheating party, terminating"); + throw new MaliciousException("Cheating in the noise pair"); } } - List noisePairs = pair.getFirst(); - return DRes.of(noisePairs); + for (int i = 0; i < 2*batchSize*(deterrenceFactor-1); i++) { + if (padTestSet.get(i).out().compareTo(modulo) >= 0) { + throw new MaliciousException("Cheating in the size of psi or rho"); + } + } + List noisePairs = (List) data[0]; + List rhoToKeep = (List) data[2]; + List psiToKeep = (List) data[3]; + List padsToUse = new ArrayList<>(batchSize); + for (int i = 0; i < batchSize; i++) { + padsToUse.add(new CRTCombinedPad(noisePairs.get(i), rhoToKeep.get(i), psiToKeep.get(i))); + } + return () -> padsToUse; }); } } \ No newline at end of file diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/NoiseGenerator.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/NoiseGenerator.java index 020590153..3b484d830 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/NoiseGenerator.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/NoiseGenerator.java @@ -9,10 +9,10 @@ import java.util.List; -public abstract class NoiseGenerator - extends CRTComputation, ResourcePoolL, ResourcePoolR> { +public abstract class NoiseGenerator + extends CRTComputation, ResourcePoolL, ResourcePoolR> { @Override - abstract public DRes> buildComputation(ProtocolBuilderNumeric builder, - CRTNumericContext context); + abstract public DRes> buildComputation(ProtocolBuilderNumeric builder, + CRTNumericContext context); } diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/PaddingGenerator.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/PaddingGenerator.java new file mode 100644 index 000000000..bde4e7c35 --- /dev/null +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/PaddingGenerator.java @@ -0,0 +1,79 @@ +package dk.alexandra.fresco.suite.crt.datatypes.resource; + +import dk.alexandra.fresco.framework.DRes; +import dk.alexandra.fresco.framework.builder.Computation; +import dk.alexandra.fresco.framework.builder.ProtocolBuilder; +import dk.alexandra.fresco.framework.builder.numeric.Numeric; +import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool; +import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; +import dk.alexandra.fresco.framework.util.AesCtrDrbg; +import dk.alexandra.fresco.framework.util.Drbg; +import dk.alexandra.fresco.framework.util.Pair; +import dk.alexandra.fresco.framework.value.SInt; +import dk.alexandra.fresco.suite.crt.CRTNumericContext; +import dk.alexandra.fresco.suite.crt.datatypes.CRTPadPair; +import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; +import dk.alexandra.fresco.suite.crt.protocols.framework.CRTComputation; + +import java.math.BigInteger; +import java.util.ArrayList; +import java.util.List; + +public class PaddingGenerator //implements Computation>, ProtocolBuilderNumeric> { + + extends CRTComputation, ResourcePoolL, ResourcePoolR> { + + private final int batchSize; + private final int securityParam; + private final Drbg localDrbg; + + public PaddingGenerator(int batchSize, int securityParam) { + this(batchSize, securityParam, new AesCtrDrbg()); + } + + public PaddingGenerator(int batchSize, int securityParam, Drbg localDrbg) { + this.batchSize = batchSize; + this.securityParam = securityParam; + this.localDrbg = localDrbg; + } + + @Override + public DRes> buildComputation(ProtocolBuilderNumeric builder, CRTNumericContext context) { + return builder.par(par -> { + Numeric right = context.rightNumeric(par); + List res = new ArrayList<>(batchSize); + for (int i = 0; i < batchSize; i++) { +// DRes sharedRho = par.numeric().known(0); + for (int j = 1; j <= par.getBasicNumericContext().getNoOfParties(); j++) { + DRes curRho; + if (j == par.getBasicNumericContext().getMyId()) { + BigInteger modulo = BigInteger.valueOf(2).pow(securityParam); + byte[] sample = new byte[1 + (securityParam / 8)]; + localDrbg.nextBytes(sample); + BigInteger myRhoShare = new BigInteger(1, sample).mod(modulo); + curRho = right.input(myRhoShare, par.getBasicNumericContext().getMyId()); + } else { + curRho = right.input(null, j); + } + res.add(new CRTSInt(null, curRho)); +// res.put(j, curRho); +// sharedRho = par.numeric().add(sharedRho, curRho); + } +// res.add(sharedRho); + } + return () -> res; +// return () -> res.stream().map(DRes::out).collect(Collectors.toList()); +// }).par((par, data) -> { +// Numeric right = context.rightNumeric(par); +// List res = new ArrayList<>(batchSize); +// for (int i = 0; i < batchSize; i++) { +// DRes sharedRho = right.known(0); +// for (DRes cur: data.get(i)) { +// sharedRho = right.add(sharedRho, cur); +// } +// res.add(new CRTSInt(null, sharedRho)); +// } +// return ()-> res; + }); + } +} \ No newline at end of file diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/SemiHonestNoiseGenerator.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/SemiHonestNoiseGenerator.java index 1b232e5df..ce27fc2d5 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/SemiHonestNoiseGenerator.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/SemiHonestNoiseGenerator.java @@ -12,7 +12,7 @@ import java.util.List; public class SemiHonestNoiseGenerator - extends NoiseGenerator { + extends NoiseGenerator { private final int batchSize; @@ -22,7 +22,7 @@ public SemiHonestNoiseGenerator(int batchSize) { @Override public DRes> buildComputation(ProtocolBuilderNumeric builder, - CRTNumericContext context) { + CRTNumericContext context) { return builder.par(par -> { Numeric left = context.leftNumeric(par); List list = new ArrayList<>(batchSize); diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/CorrelatedNoiseProtocol.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/CorrelatedNoiseProtocol.java index 9b2f2b196..2f16ff541 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/CorrelatedNoiseProtocol.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/CorrelatedNoiseProtocol.java @@ -7,17 +7,18 @@ import dk.alexandra.fresco.framework.sce.SecureComputationEngine; import dk.alexandra.fresco.framework.value.SInt; import dk.alexandra.fresco.suite.crt.CRTNumericContext; +import dk.alexandra.fresco.suite.crt.datatypes.CRTCombinedPad; 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.CRTComputation; import dk.alexandra.fresco.suite.crt.protocols.framework.CRTNativeProtocol; /** Generate a pair of correlated noise, eg (r, r + ep) for some 0 ≤ e ≤ n. */ -public class CorrelatedNoiseProtocol extends - CRTComputation { +public class CorrelatedNoiseProtocol extends + CRTComputation { @Override - public DRes buildComputation(ProtocolBuilderNumeric builder, CRTNumericContext context) { + public DRes buildComputation(ProtocolBuilderNumeric builder, CRTNumericContext context) { return context.getResourcePool().getDataSupplier().getCorrelatedNoise(builder); } } diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftPQProtocol.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftPQProtocol.java index 68af5a60b..541ebea95 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftPQProtocol.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftPQProtocol.java @@ -28,7 +28,7 @@ public LiftPQProtocol(DRes value) { public DRes buildComputation(ProtocolBuilderNumeric builder, CRTNumericContext context) { return builder.seq(new CorrelatedNoiseProtocol<>()).seq((seq, noise) -> { - this.r = noise; + this.r = noise.getNoisePair(); // Add noise to the left value. The right is ignored. DRes xBar = context.leftNumeric(seq).add(((CRTSInt) value.out()).getLeft(), r.getLeft()); diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftQPProtocol.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftQPProtocol.java index fbe2543da..6e7613ec1 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftQPProtocol.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftQPProtocol.java @@ -32,7 +32,7 @@ public DRes buildComputation(ProtocolBuilderNumeric builder, BigInteger qPrime = new BigInteger("3138550867693340351802905239100779285196644626743924002860"); return builder.seq(new CorrelatedNoiseProtocol<>()).seq((seq, noise) -> { - this.r = (CRTSInt) noise.out(); + this.r = (CRTSInt) noise.getNoisePair().out(); return seq.numeric().add(qPrime, value); }).seq((seq, value) -> { diff --git a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractDummyCRTTest.java b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractDummyCRTTest.java index ccc3962d8..89604ae6d 100644 --- a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractDummyCRTTest.java +++ b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractDummyCRTTest.java @@ -17,7 +17,7 @@ import dk.alexandra.fresco.lib.field.integer.BasicNumericContext; import dk.alexandra.fresco.suite.ProtocolSuiteNumeric; import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTDataSupplier; -import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTDummyDataSupplier; +import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTCovertDummyDataSupplier; import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTResourcePool; import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTResourcePoolImpl; import dk.alexandra.fresco.suite.crt.protocols.framework.CRTSequentialStrategy; @@ -41,6 +41,7 @@ public class AbstractDummyCRTTest { MersennePrimeFieldDefinition.find(64); protected static final FieldDefinition DEFAULT_FIELD_RIGHT = new BigIntegerFieldDefinition( new BigInteger(152 + 40, new Random(1234)).nextProbablePrime()); + protected static final int STATISTICAL_SEC = 40; public void runTest( TestThreadRunner.TestThreadFactory, ProtocolBuilderNumeric> f, @@ -71,9 +72,9 @@ public void runTest( ProtocolSuiteNumeric> ps = new CRTProtocolSuite<>( new DummyArithmeticBuilderFactory(new BasicNumericContext(DEFAULT_FIELD_LEFT.getBitLength() - 24, - playerId, noOfParties, DEFAULT_FIELD_LEFT, 16, 40)), + playerId, noOfParties, DEFAULT_FIELD_LEFT, 16, STATISTICAL_SEC)), new DummyArithmeticBuilderFactory(new BasicNumericContext(DEFAULT_FIELD_RIGHT.getBitLength()- 40, - playerId, noOfParties, DEFAULT_FIELD_RIGHT, 16, 40))); + playerId, noOfParties, DEFAULT_FIELD_RIGHT, 16, STATISTICAL_SEC))); ProtocolEvaluator> evaluator = new BatchedProtocolEvaluator<>(batchEvaluationStrategy, ps); @@ -81,8 +82,8 @@ public void runTest( SecureComputationEngine, ProtocolBuilderNumeric> sce = new SecureComputationEngineImpl<>(ps, evaluator); - CRTDataSupplier dataSupplier = new CRTDummyDataSupplier<>(playerId, noOfParties, - DEFAULT_FIELD_LEFT, DEFAULT_FIELD_RIGHT, + CRTDataSupplier dataSupplier = new CRTCovertDummyDataSupplier<>(playerId, noOfParties, STATISTICAL_SEC + , DEFAULT_FIELD_LEFT, DEFAULT_FIELD_RIGHT, x -> new DummyArithmeticSInt(DEFAULT_FIELD_LEFT.createElement(x)), y -> new DummyArithmeticSInt(DEFAULT_FIELD_RIGHT.createElement(y))); diff --git a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSpdzCRTTest.java b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSpdzCRTTest.java index 9f3984f87..1f5d93928 100644 --- a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSpdzCRTTest.java +++ b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSpdzCRTTest.java @@ -26,7 +26,7 @@ import dk.alexandra.fresco.framework.value.SInt; import dk.alexandra.fresco.lib.field.integer.BasicNumericContext; import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTDataSupplier; -import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTDummyDataSupplier; +import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTCovertDummyDataSupplier; import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTResourcePool; import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTResourcePoolImpl; import dk.alexandra.fresco.suite.crt.protocols.framework.CRTSequentialStrategy; @@ -58,7 +58,7 @@ public class AbstractSpdzCRTTest { protected static final FieldDefinition DEFAULT_FIELD_LEFT = MersennePrimeFieldDefinition.find(64); - + protected static final int STATISTICAL_SEC = 40; // q = p^3 + 139p + 1 where q = DEFAULT_FIELD_RIGHT and p = DEFAULT_FIELD_LEFT. protected static final FieldDefinition DEFAULT_FIELD_RIGHT = new BigIntegerFieldDefinition( new BigInteger("6277101735386680703605810478201558570393289253487848005721")); //152 + 40, new Random(1234)).nextProbablePrime()); @@ -113,9 +113,9 @@ public void runTest( CRTProtocolSuite ps = new CRTProtocolSuite<>( new SpdzBuilder(new BasicNumericContext(DEFAULT_FIELD_LEFT.getBitLength() - 24, - playerId, noOfParties, DEFAULT_FIELD_LEFT, 16, 40)), + playerId, noOfParties, DEFAULT_FIELD_LEFT, 16, STATISTICAL_SEC)), new SpdzBuilder(new BasicNumericContext(DEFAULT_FIELD_RIGHT.getBitLength() - 40, - playerId, noOfParties, DEFAULT_FIELD_RIGHT, 16, 40))); + playerId, noOfParties, DEFAULT_FIELD_RIGHT, 16, STATISTICAL_SEC))); ProtocolEvaluator> evaluator = new BatchedProtocolEvaluator>(strategy, ps); @@ -123,8 +123,8 @@ public void runTest( SecureComputationEngine, ProtocolBuilderNumeric> sce = new SecureComputationEngineImpl<>(ps, evaluator); - CRTDataSupplier dataSupplier = new CRTDummyDataSupplier<>(playerId, noOfParties, - DEFAULT_FIELD_LEFT, DEFAULT_FIELD_RIGHT, + CRTDataSupplier dataSupplier = new CRTCovertDummyDataSupplier<>(playerId, noOfParties, STATISTICAL_SEC + , DEFAULT_FIELD_LEFT, DEFAULT_FIELD_RIGHT, x -> toSpdzSInt(x, playerId, noOfParties, DEFAULT_FIELD_LEFT, new Random(1234), new BigInteger(DEFAULT_FIELD_LEFT.getModulus().bitLength(), new Random(0)) .mod(DEFAULT_FIELD_LEFT.getModulus())), diff --git a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/BasicCRTTests.java b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/BasicCRTTests.java index 8d9579c5f..ee56fbc97 100644 --- a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/BasicCRTTests.java +++ b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/BasicCRTTests.java @@ -57,7 +57,7 @@ public TestThread next() { @Override public void test() { Application app = producer -> producer - .seq(new CorrelatedNoiseProtocol<>()).seq((seq, r) -> seq.numeric().open(r)); + .seq(new CorrelatedNoiseProtocol<>()).seq((seq, r) -> seq.numeric().open(r.getNoisePair())); BigInteger output = runApplication(app); CRTRingDefinition ring = (CRTRingDefinition) this.getFieldDefinition(); From d383db0e277a8e78d80271748a5ed59a0af7917c Mon Sep 17 00:00:00 2001 From: Tore Frederiksen Date: Tue, 9 May 2023 13:13:50 +0300 Subject: [PATCH 23/38] fix: fixed general runtime issue --- .../fresco/suite/crt/datatypes/CRTCombinedPad.java | 10 ++-------- .../fresco/suite/crt/datatypes/CRTNoise.java | 13 +++++++++++++ .../crt/datatypes/resource/CRTDataSupplier.java | 3 ++- .../resource/CRTSemiHonestDataSupplier.java | 4 ++-- .../resource/CRTSemiHonestDummyDataSupplier.java | 9 +++++---- .../datatypes/resource/CovertNoiseGenerator.java | 6 ++++-- .../crt/datatypes/resource/NoiseGenerator.java | 3 ++- .../resource/SemiHonestNoiseGenerator.java | 9 +++++---- .../crt/protocols/CorrelatedNoiseProtocol.java | 7 ++++--- 9 files changed, 39 insertions(+), 25 deletions(-) create mode 100644 suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/CRTNoise.java diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/CRTCombinedPad.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/CRTCombinedPad.java index a11a1075c..c075c8c25 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/CRTCombinedPad.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/CRTCombinedPad.java @@ -3,22 +3,16 @@ import dk.alexandra.fresco.framework.DRes; import dk.alexandra.fresco.framework.value.SInt; -import java.util.List; - -public class CRTCombinedPad { - private final CRTSInt noisePair; +public class CRTCombinedPad extends CRTNoise { private final DRes rho; private final DRes psi; public CRTCombinedPad(CRTSInt noisePair, DRes rho, DRes psi) { - this.noisePair = noisePair; + super(noisePair); this.rho = rho; this.psi = psi; } - public CRTSInt getNoisePair() { - return noisePair; - } public DRes getRho() { return rho; diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/CRTNoise.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/CRTNoise.java new file mode 100644 index 000000000..cf5e38ae6 --- /dev/null +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/CRTNoise.java @@ -0,0 +1,13 @@ +package dk.alexandra.fresco.suite.crt.datatypes; + +public class CRTNoise { + private final CRTSInt noisePair; + + public CRTNoise(CRTSInt noisePair) { + this.noisePair = noisePair; + } + + public CRTSInt getNoisePair() { + return noisePair; + } +} diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDataSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDataSupplier.java index 62fa58580..805b1cbb5 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDataSupplier.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDataSupplier.java @@ -6,11 +6,12 @@ import dk.alexandra.fresco.framework.builder.numeric.field.FieldDefinition; import dk.alexandra.fresco.framework.util.Pair; import dk.alexandra.fresco.suite.crt.datatypes.CRTCombinedPad; +import dk.alexandra.fresco.suite.crt.datatypes.CRTNoise; import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; import java.util.ArrayDeque; -public abstract class CRTDataSupplier { +public abstract class CRTDataSupplier { private final ArrayDeque noisePairs = new ArrayDeque<>(); private final NoiseGenerator noiseGenerator; diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java index 77ae44dfe..931c19e7b 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java @@ -1,11 +1,11 @@ package dk.alexandra.fresco.suite.crt.datatypes.resource; import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool; -import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; +import dk.alexandra.fresco.suite.crt.datatypes.CRTNoise; public class CRTSemiHonestDataSupplier - extends CRTDataSupplier { + extends CRTDataSupplier { public CRTSemiHonestDataSupplier(CRTResourcePool resourcePool) { super(new SemiHonestNoiseGenerator<>(10), resourcePool); diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDummyDataSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDummyDataSupplier.java index fbe90b8ff..4077407e4 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDummyDataSupplier.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDummyDataSupplier.java @@ -7,13 +7,14 @@ import dk.alexandra.fresco.framework.util.Pair; import dk.alexandra.fresco.framework.value.SInt; import dk.alexandra.fresco.suite.crt.Util; +import dk.alexandra.fresco.suite.crt.datatypes.CRTNoise; import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; import java.math.BigInteger; import java.util.Random; import java.util.function.Function; -public class CRTSemiHonestDummyDataSupplier extends CRTDataSupplier { +public class CRTSemiHonestDummyDataSupplier extends CRTDataSupplier { private final FieldDefinition fp, fq; private final int players; @@ -35,12 +36,12 @@ public CRTSemiHonestDummyDataSupplier(int myId, int players, FieldDefinition lef } @Override - public DRes getCorrelatedNoise(ProtocolBuilderNumeric builder) { + public DRes getCorrelatedNoise(ProtocolBuilderNumeric builder) { BigInteger r = Util.randomBigInteger(random, fp.getModulus()); BigInteger l = Util .randomBigInteger(random, BigInteger.valueOf(players)); - return DRes.of(new CRTSInt(wrapperLeft.apply(r), - wrapperRight.apply(r.add(l.multiply(fp.getModulus()))))); + return DRes.of(new CRTNoise(new CRTSInt(wrapperLeft.apply(r), + wrapperRight.apply(r.add(l.multiply(fp.getModulus())))))); } @Override diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CovertNoiseGenerator.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CovertNoiseGenerator.java index 28ae8688e..84c639a71 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CovertNoiseGenerator.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CovertNoiseGenerator.java @@ -10,6 +10,7 @@ import dk.alexandra.fresco.framework.value.SInt; import dk.alexandra.fresco.suite.crt.CRTNumericContext; import dk.alexandra.fresco.suite.crt.datatypes.CRTCombinedPad; +import dk.alexandra.fresco.suite.crt.datatypes.CRTNoise; import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; import dk.alexandra.fresco.tools.commitment.CoinTossingComputation; import dk.alexandra.fresco.tools.commitment.HashBasedCommitmentSerializer; @@ -52,7 +53,7 @@ public CovertNoiseGenerator(int batchSize, int deterrenceFactor, int securityPar public DRes> buildComputation(ProtocolBuilderNumeric builder, CRTNumericContext context) { return builder.par(par -> { - DRes> semiHonestNoise = noiseGenerator.buildComputation(par, context); + DRes> semiHonestNoise = noiseGenerator.buildComputation(par, context); DRes> rhoPad = padGenerator.buildComputation(par, context); DRes> psiPad = padGenerator.buildComputation(par, context); // Sample batchsize random noise pairs to keep, mark all other to be checked/selected @@ -70,7 +71,7 @@ public DRes> buildComputation(ProtocolBuilderNumeric builde List combinedPads = new ArrayList<>(securityParam * batchSize); for (int i = 0; i < deterrenceFactor * batchSize; i++) { combinedPads.add(new CRTCombinedPad( - ((DRes>) data[0]).out().get(i), + ((DRes>) data[0]).out().get(i).getNoisePair(), ((DRes>) data[1]).out().get(i).getRight(), ((DRes>) data[2]).out().get(i).getRight())); } @@ -134,6 +135,7 @@ public DRes> buildComputation(ProtocolBuilderNumeric builde throw new MaliciousException("Cheating in the noise pair"); } } + // TODO identify which party is malicious for (int i = 0; i < 2*batchSize*(deterrenceFactor-1); i++) { if (padTestSet.get(i).out().compareTo(modulo) >= 0) { throw new MaliciousException("Cheating in the size of psi or rho"); diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/NoiseGenerator.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/NoiseGenerator.java index 3b484d830..09bc4ca3c 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/NoiseGenerator.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/NoiseGenerator.java @@ -4,12 +4,13 @@ import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool; import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; import dk.alexandra.fresco.suite.crt.CRTNumericContext; +import dk.alexandra.fresco.suite.crt.datatypes.CRTNoise; import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; import dk.alexandra.fresco.suite.crt.protocols.framework.CRTComputation; import java.util.List; -public abstract class NoiseGenerator +public abstract class NoiseGenerator extends CRTComputation, ResourcePoolL, ResourcePoolR> { @Override diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/SemiHonestNoiseGenerator.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/SemiHonestNoiseGenerator.java index ce27fc2d5..04b570639 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/SemiHonestNoiseGenerator.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/SemiHonestNoiseGenerator.java @@ -6,13 +6,14 @@ import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; import dk.alexandra.fresco.framework.value.SInt; import dk.alexandra.fresco.suite.crt.CRTNumericContext; +import dk.alexandra.fresco.suite.crt.datatypes.CRTNoise; import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; import java.util.ArrayList; import java.util.List; public class SemiHonestNoiseGenerator - extends NoiseGenerator { + extends NoiseGenerator { private final int batchSize; @@ -21,15 +22,15 @@ public SemiHonestNoiseGenerator(int batchSize) { } @Override - public DRes> buildComputation(ProtocolBuilderNumeric builder, + public DRes> buildComputation(ProtocolBuilderNumeric builder, CRTNumericContext context) { return builder.par(par -> { Numeric left = context.leftNumeric(par); - List list = new ArrayList<>(batchSize); + List list = new ArrayList<>(batchSize); for (int i = 0; i < batchSize; i++) { DRes r = left.randomElement(); CRTSInt noisePair = new CRTSInt(r, r); - list.add(noisePair); + list.add(new CRTNoise(noisePair)); } return DRes.of(list); }); diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/CorrelatedNoiseProtocol.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/CorrelatedNoiseProtocol.java index 2f16ff541..49c3547a8 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/CorrelatedNoiseProtocol.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/CorrelatedNoiseProtocol.java @@ -8,17 +8,18 @@ import dk.alexandra.fresco.framework.value.SInt; import dk.alexandra.fresco.suite.crt.CRTNumericContext; import dk.alexandra.fresco.suite.crt.datatypes.CRTCombinedPad; +import dk.alexandra.fresco.suite.crt.datatypes.CRTNoise; 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.CRTComputation; import dk.alexandra.fresco.suite.crt.protocols.framework.CRTNativeProtocol; /** Generate a pair of correlated noise, eg (r, r + ep) for some 0 ≤ e ≤ n. */ -public class CorrelatedNoiseProtocol extends - CRTComputation { +public class CorrelatedNoiseProtocol extends + CRTComputation { @Override - public DRes buildComputation(ProtocolBuilderNumeric builder, CRTNumericContext context) { + public DRes buildComputation(ProtocolBuilderNumeric builder, CRTNumericContext context) { return context.getResourcePool().getDataSupplier().getCorrelatedNoise(builder); } } From f15565b9062cd8a4d67847e932092d97a639b2b3 Mon Sep 17 00:00:00 2001 From: Tore Frederiksen Date: Tue, 9 May 2023 15:12:05 +0300 Subject: [PATCH 24/38] incremental: semihonest working --- .../suite/crt/datatypes/CRTCombinedPad.java | 10 +++- .../fresco/suite/crt/datatypes/CRTNoise.java | 13 ----- .../resource/CRTCovertDataSupplier.java | 2 +- .../resource/CRTCovertDummyDataSupplier.java | 2 +- .../datatypes/resource/CRTDataSupplier.java | 13 ++--- .../resource/CRTSemiHonestDataSupplier.java | 5 +- .../CRTSemiHonestDummyDataSupplier.java | 18 ++++-- .../resource/CovertNoiseGenerator.java | 11 ++-- .../datatypes/resource/NoiseGenerator.java | 9 ++- .../resource/SemiHonestNoiseGenerator.java | 55 +++++++++++++++++-- .../protocols/CorrelatedNoiseProtocol.java | 13 +---- .../suite/crt/AbstractDummyCRTTest.java | 2 +- .../fresco/suite/crt/AbstractSpdzCRTTest.java | 2 +- 13 files changed, 93 insertions(+), 62 deletions(-) delete mode 100644 suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/CRTNoise.java diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/CRTCombinedPad.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/CRTCombinedPad.java index c075c8c25..a11a1075c 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/CRTCombinedPad.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/CRTCombinedPad.java @@ -3,16 +3,22 @@ import dk.alexandra.fresco.framework.DRes; import dk.alexandra.fresco.framework.value.SInt; -public class CRTCombinedPad extends CRTNoise { +import java.util.List; + +public class CRTCombinedPad { + private final CRTSInt noisePair; private final DRes rho; private final DRes psi; public CRTCombinedPad(CRTSInt noisePair, DRes rho, DRes psi) { - super(noisePair); + this.noisePair = noisePair; this.rho = rho; this.psi = psi; } + public CRTSInt getNoisePair() { + return noisePair; + } public DRes getRho() { return rho; diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/CRTNoise.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/CRTNoise.java deleted file mode 100644 index cf5e38ae6..000000000 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/CRTNoise.java +++ /dev/null @@ -1,13 +0,0 @@ -package dk.alexandra.fresco.suite.crt.datatypes; - -public class CRTNoise { - private final CRTSInt noisePair; - - public CRTNoise(CRTSInt noisePair) { - this.noisePair = noisePair; - } - - public CRTSInt getNoisePair() { - return noisePair; - } -} diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTCovertDataSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTCovertDataSupplier.java index 465d2b0c3..19bbabf1a 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTCovertDataSupplier.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTCovertDataSupplier.java @@ -4,7 +4,7 @@ import dk.alexandra.fresco.suite.crt.datatypes.CRTCombinedPad; public class CRTCovertDataSupplier - extends CRTDataSupplier { + extends CRTDataSupplier { public CRTCovertDataSupplier(CRTResourcePool resourcePool) { diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTCovertDummyDataSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTCovertDummyDataSupplier.java index 9d0943d33..070a07893 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTCovertDummyDataSupplier.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTCovertDummyDataSupplier.java @@ -13,7 +13,7 @@ import java.util.Random; import java.util.function.Function; -public class CRTCovertDummyDataSupplier extends CRTDataSupplier { +public class CRTCovertDummyDataSupplier extends CRTDataSupplier { private final FieldDefinition fp, fq; private final int players; diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDataSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDataSupplier.java index 805b1cbb5..c653b260d 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDataSupplier.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDataSupplier.java @@ -6,19 +6,18 @@ import dk.alexandra.fresco.framework.builder.numeric.field.FieldDefinition; import dk.alexandra.fresco.framework.util.Pair; import dk.alexandra.fresco.suite.crt.datatypes.CRTCombinedPad; -import dk.alexandra.fresco.suite.crt.datatypes.CRTNoise; import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; import java.util.ArrayDeque; -public abstract class CRTDataSupplier { +public abstract class CRTDataSupplier { - private final ArrayDeque noisePairs = new ArrayDeque<>(); - private final NoiseGenerator noiseGenerator; + private final ArrayDeque noisePairs = new ArrayDeque<>(); + private final NoiseGenerator noiseGenerator; private final CRTResourcePool resourcePool; - protected CRTDataSupplier(NoiseGenerator noiseGenerator, CRTResourcePool resourcePool) { + protected CRTDataSupplier(NoiseGenerator noiseGenerator, CRTResourcePool resourcePool) { this.noiseGenerator = noiseGenerator; this.resourcePool = resourcePool; } @@ -28,11 +27,11 @@ protected CRTDataSupplier(NoiseGenerator noiseGenerator, CRTResour * * @return r */ - public DRes getCorrelatedNoise(ProtocolBuilderNumeric builder) { + public DRes getCorrelatedNoise(ProtocolBuilderNumeric builder) { if (noisePairs.isEmpty()) { return builder.seq(noiseGenerator).seq((seq, noise) -> { noisePairs.addAll(noise); - NoiseT out = noisePairs.pop(); + CRTCombinedPad out = noisePairs.pop(); return DRes.of(out); }); } else { diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java index 931c19e7b..ac2552ded 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java @@ -1,13 +1,12 @@ package dk.alexandra.fresco.suite.crt.datatypes.resource; import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool; -import dk.alexandra.fresco.suite.crt.datatypes.CRTNoise; public class CRTSemiHonestDataSupplier - extends CRTDataSupplier { + extends CRTDataSupplier { public CRTSemiHonestDataSupplier(CRTResourcePool resourcePool) { - super(new SemiHonestNoiseGenerator<>(10), resourcePool); + super(new SemiHonestNoiseGenerator<>(10, 40), resourcePool); } } diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDummyDataSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDummyDataSupplier.java index 4077407e4..647d56548 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDummyDataSupplier.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDummyDataSupplier.java @@ -7,27 +7,29 @@ import dk.alexandra.fresco.framework.util.Pair; import dk.alexandra.fresco.framework.value.SInt; import dk.alexandra.fresco.suite.crt.Util; -import dk.alexandra.fresco.suite.crt.datatypes.CRTNoise; +import dk.alexandra.fresco.suite.crt.datatypes.CRTCombinedPad; import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; import java.math.BigInteger; import java.util.Random; import java.util.function.Function; -public class CRTSemiHonestDummyDataSupplier extends CRTDataSupplier { +public class CRTSemiHonestDummyDataSupplier extends CRTDataSupplier { private final FieldDefinition fp, fq; private final int players; + private final int securityParam; private final Random random; private final Function wrapperLeft, wrapperRight; - public CRTSemiHonestDummyDataSupplier(int myId, int players, FieldDefinition leftField, + public CRTSemiHonestDummyDataSupplier(int myId, int players, int securityParam, FieldDefinition leftField, FieldDefinition rightField, Function wrapperLeft, Function wrapperRight) { super(null, null); this.players = players; this.fp = leftField; this.fq = rightField; + this.securityParam = securityParam; this.wrapperLeft = wrapperLeft; this.wrapperRight = wrapperRight; @@ -36,12 +38,16 @@ public CRTSemiHonestDummyDataSupplier(int myId, int players, FieldDefinition lef } @Override - public DRes getCorrelatedNoise(ProtocolBuilderNumeric builder) { + public DRes getCorrelatedNoise(ProtocolBuilderNumeric builder) { BigInteger r = Util.randomBigInteger(random, fp.getModulus()); BigInteger l = Util .randomBigInteger(random, BigInteger.valueOf(players)); - return DRes.of(new CRTNoise(new CRTSInt(wrapperLeft.apply(r), - wrapperRight.apply(r.add(l.multiply(fp.getModulus())))))); + BigInteger rho = Util.randomBigInteger(random, BigInteger.valueOf(2).pow(securityParam)); + BigInteger psi = Util.randomBigInteger(random, BigInteger.valueOf(2).pow(securityParam)); + return DRes.of(new CRTCombinedPad(new CRTSInt(wrapperLeft.apply(r), + wrapperRight.apply(r.add(l.multiply(fp.getModulus())))), + new CRTSInt(null, wrapperRight.apply(rho)), + new CRTSInt(null, wrapperRight.apply(psi)))); } @Override diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CovertNoiseGenerator.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CovertNoiseGenerator.java index 84c639a71..5eb7f8d88 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CovertNoiseGenerator.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CovertNoiseGenerator.java @@ -10,7 +10,6 @@ import dk.alexandra.fresco.framework.value.SInt; import dk.alexandra.fresco.suite.crt.CRTNumericContext; import dk.alexandra.fresco.suite.crt.datatypes.CRTCombinedPad; -import dk.alexandra.fresco.suite.crt.datatypes.CRTNoise; import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; import dk.alexandra.fresco.tools.commitment.CoinTossingComputation; import dk.alexandra.fresco.tools.commitment.HashBasedCommitmentSerializer; @@ -20,7 +19,7 @@ import java.util.List; public class CovertNoiseGenerator - extends NoiseGenerator { + extends NoiseGenerator { private final int batchSize; private final int deterrenceFactor; @@ -30,7 +29,7 @@ public class CovertNoiseGenerator(this.deterrenceFactor * batchSize); + this.noiseGenerator = new SemiHonestNoiseGenerator<>(this.deterrenceFactor * batchSize, securityParam); this.padGenerator = new PaddingGenerator(this.deterrenceFactor * batchSize, securityParam, localDrbg); this.coinToss = new CoinTossingComputation(32, commitmentSerializer, localDrbg); } @@ -53,7 +52,7 @@ public CovertNoiseGenerator(int batchSize, int deterrenceFactor, int securityPar public DRes> buildComputation(ProtocolBuilderNumeric builder, CRTNumericContext context) { return builder.par(par -> { - DRes> semiHonestNoise = noiseGenerator.buildComputation(par, context); + DRes> semiHonestNoise = noiseGenerator.buildComputation(par, context); DRes> rhoPad = padGenerator.buildComputation(par, context); DRes> psiPad = padGenerator.buildComputation(par, context); // Sample batchsize random noise pairs to keep, mark all other to be checked/selected @@ -71,7 +70,7 @@ public DRes> buildComputation(ProtocolBuilderNumeric builde List combinedPads = new ArrayList<>(securityParam * batchSize); for (int i = 0; i < deterrenceFactor * batchSize; i++) { combinedPads.add(new CRTCombinedPad( - ((DRes>) data[0]).out().get(i).getNoisePair(), + ((DRes>) data[0]).out().get(i).getNoisePair(), ((DRes>) data[1]).out().get(i).getRight(), ((DRes>) data[2]).out().get(i).getRight())); } diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/NoiseGenerator.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/NoiseGenerator.java index 09bc4ca3c..945539e8d 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/NoiseGenerator.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/NoiseGenerator.java @@ -4,16 +4,15 @@ import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool; import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; import dk.alexandra.fresco.suite.crt.CRTNumericContext; -import dk.alexandra.fresco.suite.crt.datatypes.CRTNoise; -import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; +import dk.alexandra.fresco.suite.crt.datatypes.CRTCombinedPad; import dk.alexandra.fresco.suite.crt.protocols.framework.CRTComputation; import java.util.List; -public abstract class NoiseGenerator - extends CRTComputation, ResourcePoolL, ResourcePoolR> { +public abstract class NoiseGenerator + extends CRTComputation, ResourcePoolL, ResourcePoolR> { @Override - abstract public DRes> buildComputation(ProtocolBuilderNumeric builder, + abstract public DRes> buildComputation(ProtocolBuilderNumeric builder, CRTNumericContext context); } diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/SemiHonestNoiseGenerator.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/SemiHonestNoiseGenerator.java index 04b570639..51a99b7a4 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/SemiHonestNoiseGenerator.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/SemiHonestNoiseGenerator.java @@ -4,36 +4,79 @@ import dk.alexandra.fresco.framework.builder.numeric.Numeric; import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool; import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; +import dk.alexandra.fresco.framework.util.AesCtrDrbg; +import dk.alexandra.fresco.framework.util.Drbg; import dk.alexandra.fresco.framework.value.SInt; import dk.alexandra.fresco.suite.crt.CRTNumericContext; -import dk.alexandra.fresco.suite.crt.datatypes.CRTNoise; +import dk.alexandra.fresco.suite.crt.datatypes.CRTCombinedPad; import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; +import java.math.BigInteger; import java.util.ArrayList; import java.util.List; public class SemiHonestNoiseGenerator - extends NoiseGenerator { + extends NoiseGenerator { private final int batchSize; + private final int securityParam; + private final Drbg localDrbg; - public SemiHonestNoiseGenerator(int batchSize) { + public SemiHonestNoiseGenerator(int batchSize, int securityParam) { + this(batchSize, securityParam, new AesCtrDrbg()); + } + + public SemiHonestNoiseGenerator(int batchSize, int securityParam, Drbg localDrbg) { this.batchSize = batchSize; + this.securityParam = securityParam; + this.localDrbg = localDrbg; } @Override - public DRes> buildComputation(ProtocolBuilderNumeric builder, + public DRes> buildComputation(ProtocolBuilderNumeric builder, CRTNumericContext context) { return builder.par(par -> { Numeric left = context.leftNumeric(par); - List list = new ArrayList<>(batchSize); + Numeric right = context.rightNumeric(par); + List list = new ArrayList<>(batchSize); for (int i = 0; i < batchSize; i++) { DRes r = left.randomElement(); CRTSInt noisePair = new CRTSInt(r, r); - list.add(new CRTNoise(noisePair)); +// List partyShares = new ArrayList<>(batchSize); +// for (int j = 1; j <= par.getBasicNumericContext().getNoOfParties(); j++) { +// BigInteger modulo = BigInteger.valueOf(2).pow(securityParam); +// byte[] sample = new byte[1 + (securityParam / 8)]; +// localDrbg.nextBytes(sample); +// BigInteger rho = new BigInteger(1, sample).mod(modulo); +// DRes sharedRho = right.input(rho, par.getBasicNumericContext().getMyId()); +// localDrbg.nextBytes(sample); +// BigInteger psi = new BigInteger(1, sample).mod(modulo); +// DRes sharedPsi = right.input(psi, par.getBasicNumericContext().getMyId()); +// } + DRes rho = getStatisticalShares(right, par.getBasicNumericContext().getMyId(), par.getBasicNumericContext().getNoOfParties()); + DRes psi = getStatisticalShares(right, par.getBasicNumericContext().getMyId(), par.getBasicNumericContext().getNoOfParties()); + list.add(new CRTCombinedPad(noisePair, rho, psi)); } return DRes.of(list); }); } + private DRes getStatisticalShares(Numeric right, int myId, int parties) { + DRes sharedRand = right.known(0); + for (int j = 1; j <= parties; j++) { + DRes curRand; + if (j == myId) { + BigInteger modulo = BigInteger.valueOf(2).pow(securityParam); + byte[] sample = new byte[1 + (securityParam / 8)]; + localDrbg.nextBytes(sample); + BigInteger rand = new BigInteger(1, sample).mod(modulo); + curRand = right.input(rand, myId); + } else { + curRand = right.input(null, j); + } + sharedRand = right.add(sharedRand, curRand); + } + return sharedRand; + } + } diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/CorrelatedNoiseProtocol.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/CorrelatedNoiseProtocol.java index 49c3547a8..8d0435a49 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/CorrelatedNoiseProtocol.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/CorrelatedNoiseProtocol.java @@ -3,23 +3,16 @@ import dk.alexandra.fresco.framework.DRes; import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool; import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; -import dk.alexandra.fresco.framework.network.Network; -import dk.alexandra.fresco.framework.sce.SecureComputationEngine; -import dk.alexandra.fresco.framework.value.SInt; import dk.alexandra.fresco.suite.crt.CRTNumericContext; import dk.alexandra.fresco.suite.crt.datatypes.CRTCombinedPad; -import dk.alexandra.fresco.suite.crt.datatypes.CRTNoise; -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.CRTComputation; -import dk.alexandra.fresco.suite.crt.protocols.framework.CRTNativeProtocol; /** Generate a pair of correlated noise, eg (r, r + ep) for some 0 ≤ e ≤ n. */ -public class CorrelatedNoiseProtocol extends - CRTComputation { +public class CorrelatedNoiseProtocol extends + CRTComputation { @Override - public DRes buildComputation(ProtocolBuilderNumeric builder, CRTNumericContext context) { + public DRes buildComputation(ProtocolBuilderNumeric builder, CRTNumericContext context) { return context.getResourcePool().getDataSupplier().getCorrelatedNoise(builder); } } diff --git a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractDummyCRTTest.java b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractDummyCRTTest.java index 89604ae6d..5126758c8 100644 --- a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractDummyCRTTest.java +++ b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractDummyCRTTest.java @@ -82,7 +82,7 @@ public void runTest( SecureComputationEngine, ProtocolBuilderNumeric> sce = new SecureComputationEngineImpl<>(ps, evaluator); - CRTDataSupplier dataSupplier = new CRTCovertDummyDataSupplier<>(playerId, noOfParties, STATISTICAL_SEC + CRTDataSupplier dataSupplier = new CRTCovertDummyDataSupplier<>(playerId, noOfParties, STATISTICAL_SEC , DEFAULT_FIELD_LEFT, DEFAULT_FIELD_RIGHT, x -> new DummyArithmeticSInt(DEFAULT_FIELD_LEFT.createElement(x)), y -> new DummyArithmeticSInt(DEFAULT_FIELD_RIGHT.createElement(y))); diff --git a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSpdzCRTTest.java b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSpdzCRTTest.java index 1f5d93928..924e4c9c6 100644 --- a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSpdzCRTTest.java +++ b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSpdzCRTTest.java @@ -123,7 +123,7 @@ public void runTest( SecureComputationEngine, ProtocolBuilderNumeric> sce = new SecureComputationEngineImpl<>(ps, evaluator); - CRTDataSupplier dataSupplier = new CRTCovertDummyDataSupplier<>(playerId, noOfParties, STATISTICAL_SEC + CRTDataSupplier dataSupplier = new CRTCovertDummyDataSupplier<>(playerId, noOfParties, STATISTICAL_SEC , DEFAULT_FIELD_LEFT, DEFAULT_FIELD_RIGHT, x -> toSpdzSInt(x, playerId, noOfParties, DEFAULT_FIELD_LEFT, new Random(1234), new BigInteger(DEFAULT_FIELD_LEFT.getModulus().bitLength(), new Random(0)) From a58d2eb4593de06eab68528dac64f2ec000a7366 Mon Sep 17 00:00:00 2001 From: Tore Frederiksen Date: Tue, 9 May 2023 16:08:04 +0300 Subject: [PATCH 25/38] refactor: improved code quality of change --- .../fresco/suite/crt/CRTBuilderFactory.java | 9 ++- .../dk/alexandra/fresco/suite/crt/Util.java | 1 + .../suite/crt/datatypes/CRTPadPair.java | 22 ------- .../resource/CRTCovertDataSupplier.java | 3 +- .../datatypes/resource/CRTDataSupplier.java | 3 + ...upplier.java => CRTDummyDataSupplier.java} | 8 +-- .../resource/CRTSemiHonestDataSupplier.java | 2 +- .../CRTSemiHonestDummyDataSupplier.java | 64 ------------------- .../resource/CovertNoiseGenerator.java | 21 +++--- .../datatypes/resource/PaddingGenerator.java | 42 ++++-------- .../resource/PairShareGenerator.java | 61 ++++++++++++++++++ .../resource/SemiHonestNoiseGenerator.java | 25 ++------ .../suite/crt/AbstractDummyCRTTest.java | 4 +- .../fresco/suite/crt/AbstractSpdzCRTTest.java | 4 +- 14 files changed, 110 insertions(+), 159 deletions(-) delete mode 100644 suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/CRTPadPair.java rename suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/{CRTCovertDummyDataSupplier.java => CRTDummyDataSupplier.java} (83%) delete mode 100644 suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDummyDataSupplier.java create mode 100644 suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/PairShareGenerator.java diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTBuilderFactory.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTBuilderFactory.java index 9cb03e1e1..ea82365b7 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTBuilderFactory.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTBuilderFactory.java @@ -9,6 +9,7 @@ import dk.alexandra.fresco.framework.value.SInt; import dk.alexandra.fresco.lib.field.integer.BasicNumericContext; import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; +import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTDataSupplier; import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTResourcePool; import dk.alexandra.fresco.suite.crt.protocols.framework.ProtocolBuilderNumericWrapper; @@ -27,6 +28,12 @@ public class CRTBuilderFactory resourcePool, BuilderFactoryNumeric left, BuilderFactoryNumeric right) { + this(resourcePool, left, right, CRTDataSupplier.DEFAULT_STATSECURITY); + } + public CRTBuilderFactory(CRTResourcePool resourcePool, + BuilderFactoryNumeric left, + BuilderFactoryNumeric right, + int statisticalSec) { if (resourcePool.getSubResourcePools().getFirst().getMyId() != resourcePool.getSubResourcePools().getSecond().getMyId() || resourcePool.getSubResourcePools().getFirst().getNoOfParties() != resourcePool.getSubResourcePools().getSecond().getNoOfParties()) { @@ -41,7 +48,7 @@ public CRTBuilderFactory(CRTResourcePool resourceP this.p = resourcePoolLeft.getModulus(); this.q = resourcePoolRight.getModulus(); this.context = new CRTNumericContext<>( - p.bitLength() + q.bitLength() - 40, //TODO + p.bitLength() + q.bitLength() - statisticalSec, //TODO @jonas is this right? resourcePoolLeft.getMyId(), resourcePoolLeft.getNoOfParties(), left, right, p, q, resourcePool); } diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/Util.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/Util.java index 004ab54c9..32a36e184 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/Util.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/Util.java @@ -1,5 +1,6 @@ package dk.alexandra.fresco.suite.crt; +import dk.alexandra.fresco.framework.util.Drbg; import dk.alexandra.fresco.framework.util.Pair; import java.math.BigInteger; import java.util.Objects; diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/CRTPadPair.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/CRTPadPair.java deleted file mode 100644 index 127d94d38..000000000 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/CRTPadPair.java +++ /dev/null @@ -1,22 +0,0 @@ -package dk.alexandra.fresco.suite.crt.datatypes; - -import dk.alexandra.fresco.framework.DRes; -import dk.alexandra.fresco.framework.value.SInt; - -public class CRTPadPair { - private final SInt rhoPad; - private final SInt psiPad; - - public CRTPadPair(SInt rhoPad, SInt psiPad) { - this.rhoPad = rhoPad; - this.psiPad = psiPad; - } - - public SInt getRho() { - return rhoPad; - } - - public SInt getPsi() { - return psiPad; - } -} diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTCovertDataSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTCovertDataSupplier.java index 19bbabf1a..c5daedae5 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTCovertDataSupplier.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTCovertDataSupplier.java @@ -1,14 +1,13 @@ package dk.alexandra.fresco.suite.crt.datatypes.resource; import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool; -import dk.alexandra.fresco.suite.crt.datatypes.CRTCombinedPad; public class CRTCovertDataSupplier extends CRTDataSupplier { public CRTCovertDataSupplier(CRTResourcePool resourcePool) { - this(resourcePool, 8, 2, 40); + this(resourcePool, DEFAULT_BATCH_SIZE, DEFAULT_DETERRENCE_FACTOR, DEFAULT_STATSECURITY); } public CRTCovertDataSupplier(CRTResourcePool resourcePool, diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDataSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDataSupplier.java index c653b260d..84030acc0 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDataSupplier.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDataSupplier.java @@ -11,6 +11,9 @@ import java.util.ArrayDeque; public abstract class CRTDataSupplier { + public static final int DEFAULT_BATCH_SIZE = 8; + public static final int DEFAULT_DETERRENCE_FACTOR = 2; + public static final int DEFAULT_STATSECURITY = 60; private final ArrayDeque noisePairs = new ArrayDeque<>(); private final NoiseGenerator noiseGenerator; diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTCovertDummyDataSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDummyDataSupplier.java similarity index 83% rename from suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTCovertDummyDataSupplier.java rename to suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDummyDataSupplier.java index 070a07893..edd40b5d1 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTCovertDummyDataSupplier.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDummyDataSupplier.java @@ -13,7 +13,7 @@ import java.util.Random; import java.util.function.Function; -public class CRTCovertDummyDataSupplier extends CRTDataSupplier { +public class CRTDummyDataSupplier extends CRTDataSupplier { private final FieldDefinition fp, fq; private final int players; @@ -22,9 +22,9 @@ public class CRTCovertDummyDataSupplier wrapperLeft, wrapperRight; // todo could aggregate the semihonest Dummy data supplier to avoid code-copy - public CRTCovertDummyDataSupplier(int myId, int players, int statisticalSecurity, FieldDefinition leftField, - FieldDefinition rightField, Function wrapperLeft, - Function wrapperRight) { + public CRTDummyDataSupplier(int myId, int players, int statisticalSecurity, FieldDefinition leftField, + FieldDefinition rightField, Function wrapperLeft, + Function wrapperRight) { super(null, null); this.players = players; this.statisticalSecurity = statisticalSecurity; diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java index ac2552ded..0f9a5e38a 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java @@ -7,6 +7,6 @@ public class CRTSemiHonestDataSupplier { public CRTSemiHonestDataSupplier(CRTResourcePool resourcePool) { - super(new SemiHonestNoiseGenerator<>(10, 40), resourcePool); + super(new SemiHonestNoiseGenerator<>(DEFAULT_BATCH_SIZE, DEFAULT_STATSECURITY), resourcePool); } } diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDummyDataSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDummyDataSupplier.java deleted file mode 100644 index 647d56548..000000000 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDummyDataSupplier.java +++ /dev/null @@ -1,64 +0,0 @@ -package dk.alexandra.fresco.suite.crt.datatypes.resource; - -import dk.alexandra.fresco.framework.DRes; -import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool; -import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; -import dk.alexandra.fresco.framework.builder.numeric.field.FieldDefinition; -import dk.alexandra.fresco.framework.util.Pair; -import dk.alexandra.fresco.framework.value.SInt; -import dk.alexandra.fresco.suite.crt.Util; -import dk.alexandra.fresco.suite.crt.datatypes.CRTCombinedPad; -import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; - -import java.math.BigInteger; -import java.util.Random; -import java.util.function.Function; - -public class CRTSemiHonestDummyDataSupplier extends CRTDataSupplier { - - private final FieldDefinition fp, fq; - private final int players; - private final int securityParam; - private final Random random; - private final Function wrapperLeft, wrapperRight; - - public CRTSemiHonestDummyDataSupplier(int myId, int players, int securityParam, FieldDefinition leftField, - FieldDefinition rightField, Function wrapperLeft, - Function wrapperRight) { - super(null, null); - this.players = players; - this.fp = leftField; - this.fq = rightField; - this.securityParam = securityParam; - this.wrapperLeft = wrapperLeft; - this.wrapperRight = wrapperRight; - - this.random = new Random(1234); - - } - - @Override - public DRes getCorrelatedNoise(ProtocolBuilderNumeric builder) { - BigInteger r = Util.randomBigInteger(random, fp.getModulus()); - BigInteger l = Util - .randomBigInteger(random, BigInteger.valueOf(players)); - BigInteger rho = Util.randomBigInteger(random, BigInteger.valueOf(2).pow(securityParam)); - BigInteger psi = Util.randomBigInteger(random, BigInteger.valueOf(2).pow(securityParam)); - return DRes.of(new CRTCombinedPad(new CRTSInt(wrapperLeft.apply(r), - wrapperRight.apply(r.add(l.multiply(fp.getModulus())))), - new CRTSInt(null, wrapperRight.apply(rho)), - new CRTSInt(null, wrapperRight.apply(psi)))); - } - - @Override - public CRTSInt getRandomBit() { - BigInteger bit = Util.randomBigInteger(random, BigInteger.valueOf(2)); - return new CRTSInt(wrapperLeft.apply(bit), - wrapperRight.apply(bit)); - } - - @Override - public Pair getFieldDefinitions() { - return new Pair<>(fp, fq); - } -} diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CovertNoiseGenerator.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CovertNoiseGenerator.java index 5eb7f8d88..ebfac4054 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CovertNoiseGenerator.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CovertNoiseGenerator.java @@ -4,9 +4,7 @@ import dk.alexandra.fresco.framework.MaliciousException; import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool; import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; -import dk.alexandra.fresco.framework.util.AesCtrDrbg; -import dk.alexandra.fresco.framework.util.Drbg; -import dk.alexandra.fresco.framework.util.Pair; +import dk.alexandra.fresco.framework.util.*; import dk.alexandra.fresco.framework.value.SInt; import dk.alexandra.fresco.suite.crt.CRTNumericContext; import dk.alexandra.fresco.suite.crt.datatypes.CRTCombinedPad; @@ -25,11 +23,11 @@ public class CovertNoiseGenerator noiseGenerator; + private final PairShareGenerator pairShareGenerator; private final PaddingGenerator padGenerator; private final CoinTossingComputation coinToss; private AesCtrDrbg jointDrbg; - private final Drbg localDrbg; + private final Drng localDrng; public CovertNoiseGenerator(int batchSize, int deterrenceFactor, int securityParam) { @@ -41,10 +39,10 @@ public CovertNoiseGenerator(int batchSize, int deterrenceFactor, int securityPar this.deterrenceFactor = deterrenceFactor; this.securityParam = securityParam; - this.localDrbg = localDrbg; + this.localDrng = new DrngImpl(localDrbg); HashBasedCommitmentSerializer commitmentSerializer = new HashBasedCommitmentSerializer(); - this.noiseGenerator = new SemiHonestNoiseGenerator<>(this.deterrenceFactor * batchSize, securityParam); - this.padGenerator = new PaddingGenerator(this.deterrenceFactor * batchSize, securityParam, localDrbg); + this.pairShareGenerator = new PairShareGenerator<>(this.deterrenceFactor * batchSize, securityParam, localDrng); + this.padGenerator = new PaddingGenerator(this.deterrenceFactor * batchSize, securityParam, localDrng); this.coinToss = new CoinTossingComputation(32, commitmentSerializer, localDrbg); } @@ -52,7 +50,7 @@ public CovertNoiseGenerator(int batchSize, int deterrenceFactor, int securityPar public DRes> buildComputation(ProtocolBuilderNumeric builder, CRTNumericContext context) { return builder.par(par -> { - DRes> semiHonestNoise = noiseGenerator.buildComputation(par, context); + DRes> pairShares = pairShareGenerator.buildComputation(par, context); DRes> rhoPad = padGenerator.buildComputation(par, context); DRes> psiPad = padGenerator.buildComputation(par, context); // Sample batchsize random noise pairs to keep, mark all other to be checked/selected @@ -64,13 +62,13 @@ public DRes> buildComputation(ProtocolBuilderNumeric builde seed = null; } return () -> new Object[] { - semiHonestNoise, rhoPad, psiPad, seed, + pairShares, rhoPad, psiPad, seed, }; }).par((par, data) -> { List combinedPads = new ArrayList<>(securityParam * batchSize); for (int i = 0; i < deterrenceFactor * batchSize; i++) { combinedPads.add(new CRTCombinedPad( - ((DRes>) data[0]).out().get(i).getNoisePair(), + ((DRes>) data[0]).out().get(i), ((DRes>) data[1]).out().get(i).getRight(), ((DRes>) data[2]).out().get(i).getRight())); } @@ -128,7 +126,6 @@ public DRes> buildComputation(ProtocolBuilderNumeric builde BigInteger modulo = BigInteger.valueOf(2).pow(securityParam); for (int i = 0; i < batchSize*(deterrenceFactor-1); i++) { BigInteger rp = noiseTestSet.get(i).getFirst().out(); - // TODO there was a bug here previously BigInteger rq = noiseTestSet.get(i).getSecond().out(); if (rp.compareTo(rq) != 0) { throw new MaliciousException("Cheating in the noise pair"); diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/PaddingGenerator.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/PaddingGenerator.java index bde4e7c35..cd1c9f25d 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/PaddingGenerator.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/PaddingGenerator.java @@ -1,17 +1,15 @@ package dk.alexandra.fresco.suite.crt.datatypes.resource; import dk.alexandra.fresco.framework.DRes; -import dk.alexandra.fresco.framework.builder.Computation; -import dk.alexandra.fresco.framework.builder.ProtocolBuilder; import dk.alexandra.fresco.framework.builder.numeric.Numeric; import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool; import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; import dk.alexandra.fresco.framework.util.AesCtrDrbg; import dk.alexandra.fresco.framework.util.Drbg; -import dk.alexandra.fresco.framework.util.Pair; +import dk.alexandra.fresco.framework.util.Drng; +import dk.alexandra.fresco.framework.util.DrngImpl; import dk.alexandra.fresco.framework.value.SInt; import dk.alexandra.fresco.suite.crt.CRTNumericContext; -import dk.alexandra.fresco.suite.crt.datatypes.CRTPadPair; import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; import dk.alexandra.fresco.suite.crt.protocols.framework.CRTComputation; @@ -25,16 +23,16 @@ public class PaddingGenerator //implements Computation>, Protoco private final int batchSize; private final int securityParam; - private final Drbg localDrbg; + private final Drng localDrng; public PaddingGenerator(int batchSize, int securityParam) { - this(batchSize, securityParam, new AesCtrDrbg()); + this(batchSize, securityParam, new DrngImpl(new AesCtrDrbg())); } - public PaddingGenerator(int batchSize, int securityParam, Drbg localDrbg) { + public PaddingGenerator(int batchSize, int securityParam, Drng localDrng) { this.batchSize = batchSize; this.securityParam = securityParam; - this.localDrbg = localDrbg; + this.localDrng = localDrng; } @Override @@ -43,37 +41,19 @@ public DRes> buildComputation(ProtocolBuilderNumeric builder, CRTN Numeric right = context.rightNumeric(par); List res = new ArrayList<>(batchSize); for (int i = 0; i < batchSize; i++) { -// DRes sharedRho = par.numeric().known(0); for (int j = 1; j <= par.getBasicNumericContext().getNoOfParties(); j++) { - DRes curRho; + DRes curPad; if (j == par.getBasicNumericContext().getMyId()) { BigInteger modulo = BigInteger.valueOf(2).pow(securityParam); - byte[] sample = new byte[1 + (securityParam / 8)]; - localDrbg.nextBytes(sample); - BigInteger myRhoShare = new BigInteger(1, sample).mod(modulo); - curRho = right.input(myRhoShare, par.getBasicNumericContext().getMyId()); + BigInteger myRhoShare = localDrng.nextBigInteger(modulo); + curPad = right.input(myRhoShare, par.getBasicNumericContext().getMyId()); } else { - curRho = right.input(null, j); + curPad = right.input(null, j); } - res.add(new CRTSInt(null, curRho)); -// res.put(j, curRho); -// sharedRho = par.numeric().add(sharedRho, curRho); + res.add(new CRTSInt(null, curPad)); } -// res.add(sharedRho); } return () -> res; -// return () -> res.stream().map(DRes::out).collect(Collectors.toList()); -// }).par((par, data) -> { -// Numeric right = context.rightNumeric(par); -// List res = new ArrayList<>(batchSize); -// for (int i = 0; i < batchSize; i++) { -// DRes sharedRho = right.known(0); -// for (DRes cur: data.get(i)) { -// sharedRho = right.add(sharedRho, cur); -// } -// res.add(new CRTSInt(null, sharedRho)); -// } -// return ()-> res; }); } } \ No newline at end of file diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/PairShareGenerator.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/PairShareGenerator.java new file mode 100644 index 000000000..e73f36561 --- /dev/null +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/PairShareGenerator.java @@ -0,0 +1,61 @@ +package dk.alexandra.fresco.suite.crt.datatypes.resource; + +import dk.alexandra.fresco.framework.DRes; +import dk.alexandra.fresco.framework.builder.numeric.Numeric; +import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool; +import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; +import dk.alexandra.fresco.framework.util.AesCtrDrbg; +import dk.alexandra.fresco.framework.util.Drbg; +import dk.alexandra.fresco.framework.util.Drng; +import dk.alexandra.fresco.framework.util.DrngImpl; +import dk.alexandra.fresco.framework.value.SInt; +import dk.alexandra.fresco.suite.crt.CRTNumericContext; +import dk.alexandra.fresco.suite.crt.Util; +import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; +import dk.alexandra.fresco.suite.crt.protocols.framework.CRTComputation; + +import java.math.BigInteger; +import java.util.ArrayList; +import java.util.List; + +public class PairShareGenerator + + extends CRTComputation, ResourcePoolL, ResourcePoolR> { + private final int batchSize; + private final int securityParam; + private final Drng localDrng; + + public PairShareGenerator(int batchSize, int securityParam) { + this(batchSize, securityParam, new DrngImpl(new AesCtrDrbg())); + } + + public PairShareGenerator(int batchSize, int securityParam, Drng localDrng) { + this.batchSize = batchSize; + this.securityParam = securityParam; + this.localDrng = localDrng; + } + + @Override + public DRes> buildComputation(ProtocolBuilderNumeric builder, CRTNumericContext context) { + return builder.par(par -> { + Numeric left = context.leftNumeric(par); + Numeric right = context.rightNumeric(par); + List res = new ArrayList<>(batchSize); + for (int i = 0; i < batchSize; i++) { + for (int j = 1; j <= context.getNoOfParties(); j++) { + DRes curLeft, curRight; + if (j == context.getMyId()) { + BigInteger r = localDrng.nextBigInteger(context.getLeftModulus()); + curLeft = left.input(r, context.getMyId()); + curRight = right.input(r, context.getMyId()); + } else { + curLeft = left.input(null, j); + curRight = right.input(null, j); + } + res.add(new CRTSInt(curLeft, curRight)); + } + } + return () -> res; + }); + } +} diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/SemiHonestNoiseGenerator.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/SemiHonestNoiseGenerator.java index 51a99b7a4..7869585d7 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/SemiHonestNoiseGenerator.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/SemiHonestNoiseGenerator.java @@ -6,6 +6,8 @@ import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; import dk.alexandra.fresco.framework.util.AesCtrDrbg; import dk.alexandra.fresco.framework.util.Drbg; +import dk.alexandra.fresco.framework.util.Drng; +import dk.alexandra.fresco.framework.util.DrngImpl; import dk.alexandra.fresco.framework.value.SInt; import dk.alexandra.fresco.suite.crt.CRTNumericContext; import dk.alexandra.fresco.suite.crt.datatypes.CRTCombinedPad; @@ -20,16 +22,16 @@ public class SemiHonestNoiseGenerator> buildComputation(ProtocolBuilderNumeric builde for (int i = 0; i < batchSize; i++) { DRes r = left.randomElement(); CRTSInt noisePair = new CRTSInt(r, r); -// List partyShares = new ArrayList<>(batchSize); -// for (int j = 1; j <= par.getBasicNumericContext().getNoOfParties(); j++) { -// BigInteger modulo = BigInteger.valueOf(2).pow(securityParam); -// byte[] sample = new byte[1 + (securityParam / 8)]; -// localDrbg.nextBytes(sample); -// BigInteger rho = new BigInteger(1, sample).mod(modulo); -// DRes sharedRho = right.input(rho, par.getBasicNumericContext().getMyId()); -// localDrbg.nextBytes(sample); -// BigInteger psi = new BigInteger(1, sample).mod(modulo); -// DRes sharedPsi = right.input(psi, par.getBasicNumericContext().getMyId()); -// } DRes rho = getStatisticalShares(right, par.getBasicNumericContext().getMyId(), par.getBasicNumericContext().getNoOfParties()); DRes psi = getStatisticalShares(right, par.getBasicNumericContext().getMyId(), par.getBasicNumericContext().getNoOfParties()); list.add(new CRTCombinedPad(noisePair, rho, psi)); @@ -67,9 +58,7 @@ private DRes getStatisticalShares(Numeric right, int myId, int parties) { DRes curRand; if (j == myId) { BigInteger modulo = BigInteger.valueOf(2).pow(securityParam); - byte[] sample = new byte[1 + (securityParam / 8)]; - localDrbg.nextBytes(sample); - BigInteger rand = new BigInteger(1, sample).mod(modulo); + BigInteger rand = localDrng.nextBigInteger(modulo); curRand = right.input(rand, myId); } else { curRand = right.input(null, j); diff --git a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractDummyCRTTest.java b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractDummyCRTTest.java index 5126758c8..00492ee9f 100644 --- a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractDummyCRTTest.java +++ b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractDummyCRTTest.java @@ -17,7 +17,7 @@ import dk.alexandra.fresco.lib.field.integer.BasicNumericContext; import dk.alexandra.fresco.suite.ProtocolSuiteNumeric; import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTDataSupplier; -import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTCovertDummyDataSupplier; +import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTDummyDataSupplier; import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTResourcePool; import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTResourcePoolImpl; import dk.alexandra.fresco.suite.crt.protocols.framework.CRTSequentialStrategy; @@ -82,7 +82,7 @@ public void runTest( SecureComputationEngine, ProtocolBuilderNumeric> sce = new SecureComputationEngineImpl<>(ps, evaluator); - CRTDataSupplier dataSupplier = new CRTCovertDummyDataSupplier<>(playerId, noOfParties, STATISTICAL_SEC + CRTDataSupplier dataSupplier = new CRTDummyDataSupplier<>(playerId, noOfParties, STATISTICAL_SEC , DEFAULT_FIELD_LEFT, DEFAULT_FIELD_RIGHT, x -> new DummyArithmeticSInt(DEFAULT_FIELD_LEFT.createElement(x)), y -> new DummyArithmeticSInt(DEFAULT_FIELD_RIGHT.createElement(y))); diff --git a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSpdzCRTTest.java b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSpdzCRTTest.java index 924e4c9c6..f3c4a8fc1 100644 --- a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSpdzCRTTest.java +++ b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSpdzCRTTest.java @@ -26,7 +26,7 @@ import dk.alexandra.fresco.framework.value.SInt; import dk.alexandra.fresco.lib.field.integer.BasicNumericContext; import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTDataSupplier; -import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTCovertDummyDataSupplier; +import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTDummyDataSupplier; import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTResourcePool; import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTResourcePoolImpl; import dk.alexandra.fresco.suite.crt.protocols.framework.CRTSequentialStrategy; @@ -123,7 +123,7 @@ public void runTest( SecureComputationEngine, ProtocolBuilderNumeric> sce = new SecureComputationEngineImpl<>(ps, evaluator); - CRTDataSupplier dataSupplier = new CRTCovertDummyDataSupplier<>(playerId, noOfParties, STATISTICAL_SEC + CRTDataSupplier dataSupplier = new CRTDummyDataSupplier<>(playerId, noOfParties, STATISTICAL_SEC , DEFAULT_FIELD_LEFT, DEFAULT_FIELD_RIGHT, x -> toSpdzSInt(x, playerId, noOfParties, DEFAULT_FIELD_LEFT, new Random(1234), new BigInteger(DEFAULT_FIELD_LEFT.getModulus().bitLength(), new Random(0)) From f6d2236723f9deb03d458a37be2daceb7ad58af6 Mon Sep 17 00:00:00 2001 From: Tore Frederiksen Date: Tue, 9 May 2023 16:30:32 +0300 Subject: [PATCH 26/38] fix: ensured correct and simpler openeing --- .../suite/crt/protocols/LiftPQProtocol.java | 10 ++------- .../suite/crt/protocols/LiftQPProtocol.java | 21 +++++++++---------- 2 files changed, 12 insertions(+), 19 deletions(-) diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftPQProtocol.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftPQProtocol.java index 541ebea95..bbcc6560a 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftPQProtocol.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftPQProtocol.java @@ -32,17 +32,11 @@ public DRes buildComputation(ProtocolBuilderNumeric builder, // Add noise to the left value. The right is ignored. DRes xBar = context.leftNumeric(seq).add(((CRTSInt) value.out()).getLeft(), r.getLeft()); - CRTSInt output = new CRTSInt(xBar, context.rightNumeric(seq).known(0)); - return seq.numeric().open(output); // TODO: We only need to open the left, so we should create an openLeft function - + return context.leftNumeric(seq).open(xBar); }).seq((seq, xBarOpen) -> { Numeric right = context.rightNumeric(seq); - - // Extract the left value from xBar - DRes xBarRight = right.known(Util.mapToCRT(xBarOpen, context.getLeftModulus(), context.getRightModulus()).getFirst()); - // Remove the noise and return - DRes adjusted = right.sub(xBarRight, r.getRight()); + DRes adjusted = right.sub(xBarOpen, r.getRight()); return new CRTSInt(((CRTSInt) value.out()).getLeft(), adjusted); }); } diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftQPProtocol.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftQPProtocol.java index 6e7613ec1..59aa0816e 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftQPProtocol.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftQPProtocol.java @@ -20,6 +20,8 @@ public class LiftQPProtocol value; private CRTSInt r; + private DRes rho; + private DRes psi; public LiftQPProtocol(DRes value) { this.value = value; @@ -33,24 +35,21 @@ public DRes buildComputation(ProtocolBuilderNumeric builder, return builder.seq(new CorrelatedNoiseProtocol<>()).seq((seq, noise) -> { this.r = (CRTSInt) noise.getNoisePair().out(); + this.rho = noise.getRho(); + this.psi = noise.getPsi(); return seq.numeric().add(qPrime, value); }).seq((seq, value) -> { // Add noise to the right value. The left is ignored. - DRes xBar = context.rightNumeric(seq).add(((CRTSInt) value.out()).getRight(), r.getRight()); - CRTSInt output = new CRTSInt(context.leftNumeric(seq).known(0), xBar); - return seq.numeric().open(output); // TODO: We only need to open the right, so we should create an openRight function - - // TODO: We haven't added the noise (c from the protocol) - + DRes temp = context.rightNumeric(seq).add(((CRTSInt) value.out()).getRight(), r.getRight()); + // Add statistical noise drowning + DRes xBar = context.rightNumeric(seq).add(temp, + context.rightNumeric(seq).mult(context.getLeftModulus(), rho)); + return context.rightNumeric(seq).open(xBar); }).seq((seq, xBarOpen) -> { Numeric left = context.leftNumeric(seq); - - // Extract the right value from xBar - DRes xBarLeft = left.known(Util.mapToCRT(xBarOpen, context.getLeftModulus(), context.getRightModulus()).getSecond()); - // Remove the noise and return - DRes adjusted = left.sub(xBarLeft, r.getLeft()); + DRes adjusted = left.sub(xBarOpen, r.getLeft()); return new CRTSInt(adjusted, ((CRTSInt) value.out()).getRight()); }); } From c04e645655cca3266be46ad728bc77fcc68bf746 Mon Sep 17 00:00:00 2001 From: Tore Frederiksen Date: Tue, 9 May 2023 17:39:01 +0300 Subject: [PATCH 27/38] feat: started the steps of single bit error. --- .../suite/crt/fixed/CRTFixedNumeric.java | 11 ++++ .../fresco/suite/crt/protocols/TruncOne.java | 57 +++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/TruncOne.java diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/fixed/CRTFixedNumeric.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/fixed/CRTFixedNumeric.java index 46943c04d..965d7fc07 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/fixed/CRTFixedNumeric.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/fixed/CRTFixedNumeric.java @@ -6,6 +6,7 @@ import dk.alexandra.fresco.lib.fixed.DefaultFixedNumeric; import dk.alexandra.fresco.lib.fixed.truncations.Truncation; import dk.alexandra.fresco.suite.crt.CRTNumericContext; +import dk.alexandra.fresco.suite.crt.protocols.TruncOne; import dk.alexandra.fresco.suite.crt.protocols.Truncp; import java.math.BigInteger; @@ -21,10 +22,20 @@ public BigInteger getDivisor() { return ((CRTNumericContext) builder.getBasicNumericContext()).getLeftModulus(); } + /** + * Truncation with error bound in the amount of parties + */ @Override public DRes truncate(DRes value, ProtocolBuilderNumeric scope) { return new Truncp(value).buildComputation(scope); } + + /** + * Truncation with at most a single bit error + */ + public DRes truncateOne(DRes value, ProtocolBuilderNumeric scope) { + return new TruncOne(value).buildComputation(scope); + } }); this.p = ((CRTNumericContext) builder.getBasicNumericContext()).getLeftModulus(); } diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/TruncOne.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/TruncOne.java new file mode 100644 index 000000000..7d27e05b4 --- /dev/null +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/TruncOne.java @@ -0,0 +1,57 @@ +package dk.alexandra.fresco.suite.crt.protocols; + +import dk.alexandra.fresco.framework.DRes; +import dk.alexandra.fresco.framework.builder.numeric.Numeric; +import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool; +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.suite.crt.CRTNumericContext; +import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; +import dk.alexandra.fresco.suite.crt.protocols.framework.CRTComputation; + +import java.math.BigInteger; + +public class TruncOne extends CRTComputation { + + private final DRes value; + + public TruncOne(DRes value) { + this.value = value; + } + + @Override + public DRes buildComputation(ProtocolBuilderNumeric builder, + CRTNumericContext context) { + + // The multiplicative inverse of p mod q + BigInteger n2 = context.getLeftModulus().modInverse(context.getRightModulus()); + + return builder.seq(seq -> { + BigInteger U = BigInteger.valueOf(context.getNoOfParties()); // TODO is it right that it is exactly number of parties? + DRes y = new Truncp<>(value).buildComputation(seq); + // (U + 1)([x] - p[y] + pU) + DRes temp = seq.numeric().sub(value, + seq.numeric().mult(context.getLeftModulus(), y)); + temp = seq.numeric().add(context.getLeftModulus().multiply(U), temp); + DRes xPrime = seq.numeric().mult(U.add(BigInteger.ONE), temp); + DRes w = new Truncp<>(xPrime).buildComputation(seq); + temp = seq.numeric().sub(y, U); + return seq.numeric().sub(temp, poly(seq.numeric(), context.getModulus(), U, w)); + }); + } + + private DRes poly(Numeric seq, BigInteger m, BigInteger U, DRes w) { + long L = U.longValueExact()+1; + long M = L+1; + long iterations = M+M*L+L-1; + for (int i = 0; i < iterations; i++) { +//todo!! + } + return null; + } + + private DRes lagrange(Numeric seq, BigInteger m, DRes x, int i, int j) { + return seq.mult(BigInteger.valueOf(i-j).modInverse(m), seq.sub(x, i)); + } +} From 5bbf85d2f2fcb6551b9ea3506aa0621be9a9aa61 Mon Sep 17 00:00:00 2001 From: Tore Frederiksen Date: Thu, 18 May 2023 10:55:10 +0200 Subject: [PATCH 28/38] refactor: refactored to remove some unused variables and support greater granularity in setup --- .../datatypes/resource/CRTCovertDataSupplier.java | 10 ++++------ .../crt/datatypes/resource/CRTDataSupplier.java | 12 +++--------- .../crt/datatypes/resource/CRTDummyDataSupplier.java | 2 +- .../resource/CRTSemiHonestDataSupplier.java | 8 ++++++-- .../fresco/suite/crt/AbstractCovertDummyCRTTest.java | 2 +- .../suite/crt/AbstractSemiHonestDummyCRTTest.java | 2 +- 6 files changed, 16 insertions(+), 20 deletions(-) diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTCovertDataSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTCovertDataSupplier.java index c5daedae5..093064ebd 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTCovertDataSupplier.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTCovertDataSupplier.java @@ -5,13 +5,11 @@ public class CRTCovertDataSupplier extends CRTDataSupplier { - public CRTCovertDataSupplier(CRTResourcePool resourcePool) { - this(resourcePool, DEFAULT_BATCH_SIZE, DEFAULT_DETERRENCE_FACTOR, DEFAULT_STATSECURITY); + public CRTCovertDataSupplier() { + this(DEFAULT_BATCH_SIZE, DEFAULT_DETERRENCE_FACTOR, DEFAULT_STATSECURITY); } - public CRTCovertDataSupplier(CRTResourcePool resourcePool, - int batchSize, int deterrenceFactor, int securityParam) { - super(new CovertNoiseGenerator<>(batchSize, deterrenceFactor, securityParam), resourcePool); + public CRTCovertDataSupplier(int batchSize, int deterrenceFactor, int securityParam) { + super(new CovertNoiseGenerator<>(batchSize, deterrenceFactor, securityParam)); } } diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDataSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDataSupplier.java index 84030acc0..3bcef2e3b 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDataSupplier.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDataSupplier.java @@ -17,12 +17,10 @@ public abstract class CRTDataSupplier noisePairs = new ArrayDeque<>(); private final NoiseGenerator noiseGenerator; - private final CRTResourcePool resourcePool; - protected CRTDataSupplier(NoiseGenerator noiseGenerator, CRTResourcePool resourcePool) { + protected CRTDataSupplier(NoiseGenerator noiseGenerator) { this.noiseGenerator = noiseGenerator; - this.resourcePool = resourcePool; } /** @@ -41,20 +39,16 @@ public DRes getCorrelatedNoise(ProtocolBuilderNumeric builder) { return DRes.of(noisePairs.pop()); } } - /** * Supply the next random bit * todo * @return b */ public CRTSInt getRandomBit() { - return null; + throw new IllegalArgumentException("Not implemented yet"); } public Pair getFieldDefinitions() { - return new Pair<>(resourcePool.getSubResourcePools().getFirst().getFieldDefinition(), - resourcePool.getSubResourcePools().getSecond().getFieldDefinition() - ); + throw new IllegalArgumentException("Not implemented yet"); } - } diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDummyDataSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDummyDataSupplier.java index edd40b5d1..5e592d107 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDummyDataSupplier.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTDummyDataSupplier.java @@ -25,7 +25,7 @@ public class CRTDummyDataSupplier wrapperLeft, Function wrapperRight) { - super(null, null); + super(null); this.players = players; this.statisticalSecurity = statisticalSecurity; this.fp = leftField; diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java index 0f9a5e38a..709ca7319 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CRTSemiHonestDataSupplier.java @@ -6,7 +6,11 @@ public class CRTSemiHonestDataSupplier extends CRTDataSupplier { - public CRTSemiHonestDataSupplier(CRTResourcePool resourcePool) { - super(new SemiHonestNoiseGenerator<>(DEFAULT_BATCH_SIZE, DEFAULT_STATSECURITY), resourcePool); + public CRTSemiHonestDataSupplier() { + super(new SemiHonestNoiseGenerator<>(DEFAULT_BATCH_SIZE, DEFAULT_STATSECURITY)); + } + + public CRTSemiHonestDataSupplier(int batchSize, int statSec) { + super(new SemiHonestNoiseGenerator<>(batchSize, statSec)); } } diff --git a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractCovertDummyCRTTest.java b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractCovertDummyCRTTest.java index c734d7756..932538952 100644 --- a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractCovertDummyCRTTest.java +++ b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractCovertDummyCRTTest.java @@ -88,7 +88,7 @@ public void runTest( Supplier networkSupplier = () -> new SocketNetwork(partyNetConf); CRTDataSupplier dataSupplier = new CRTCovertDataSupplier( - rp); + ); TestThreadRunner.TestThreadConfiguration< CRTResourcePool, diff --git a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSemiHonestDummyCRTTest.java b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSemiHonestDummyCRTTest.java index b876ca3ed..f8f0276f6 100644 --- a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSemiHonestDummyCRTTest.java +++ b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractSemiHonestDummyCRTTest.java @@ -86,7 +86,7 @@ public void runTest( new SecureComputationEngineImpl<>(ps, evaluator); Supplier networkSupplier = () -> new SocketNetwork(partyNetConf); - CRTDataSupplier dataSupplier = new CRTSemiHonestDataSupplier<>(rp); + CRTDataSupplier dataSupplier = new CRTSemiHonestDataSupplier<>(); TestThreadRunner.TestThreadConfiguration< CRTResourcePool, From 7777ce3ef865b86d0d2c76c3a9915a3672d1c5d6 Mon Sep 17 00:00:00 2001 From: Tore Frederiksen Date: Fri, 19 May 2023 18:50:46 +0200 Subject: [PATCH 29/38] feat: added support for batching --- .../framework/CRTBatchedStrategy.java | 39 +++++++++++++++++++ .../suite/crt/AbstractCovertDummyCRTTest.java | 3 +- 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/framework/CRTBatchedStrategy.java diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/framework/CRTBatchedStrategy.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/framework/CRTBatchedStrategy.java new file mode 100644 index 000000000..605d57802 --- /dev/null +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/framework/CRTBatchedStrategy.java @@ -0,0 +1,39 @@ +package dk.alexandra.fresco.suite.crt.protocols.framework; + +import dk.alexandra.fresco.framework.NativeProtocol; +import dk.alexandra.fresco.framework.ProtocolCollection; +import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool; +import dk.alexandra.fresco.framework.network.Network; +import dk.alexandra.fresco.framework.sce.evaluator.BatchEvaluationStrategy; +import dk.alexandra.fresco.framework.sce.evaluator.NetworkBatchDecorator; +import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTResourcePool; + +import java.util.Iterator; + +public class CRTBatchedStrategy implements + BatchEvaluationStrategy> { + + @Override + public void processBatch(ProtocolCollection> nativeProtocols, CRTResourcePool resourcePool, NetworkBatchDecorator network) { + int round = 0; + while (nativeProtocols.size() > 0) { + evaluateCurrentRound(nativeProtocols, network, resourcePool, round); + network.flush(); + round++; + } + } + + private void evaluateCurrentRound( + ProtocolCollection> protocols, Network sceNetwork, + CRTResourcePool rp, int round) { + Iterator>> iterator = protocols.iterator(); + while (iterator.hasNext()) { + NativeProtocol> protocol = iterator.next(); + NativeProtocol.EvaluationStatus status = protocol.evaluate(round, rp, sceNetwork); + if (status.equals(NativeProtocol.EvaluationStatus.IS_DONE)) { + iterator.remove(); + } + } + } +} + diff --git a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractCovertDummyCRTTest.java b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractCovertDummyCRTTest.java index 932538952..6871043d0 100644 --- a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractCovertDummyCRTTest.java +++ b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/AbstractCovertDummyCRTTest.java @@ -20,6 +20,7 @@ import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTDataSupplier; import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTResourcePool; import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTResourcePoolImpl; +import dk.alexandra.fresco.suite.crt.protocols.framework.CRTBatchedStrategy; import dk.alexandra.fresco.suite.crt.protocols.framework.CRTSequentialStrategy; import dk.alexandra.fresco.suite.dummy.arithmetic.DummyArithmeticBuilderFactory; import dk.alexandra.fresco.suite.dummy.arithmetic.DummyArithmeticResourcePool; @@ -62,7 +63,7 @@ public void runTest( for (int playerId : netConf.keySet()) { BatchEvaluationStrategy> batchEvaluationStrategy = - new CRTSequentialStrategy<>(); + new CRTBatchedStrategy<>(); DummyArithmeticResourcePool rpLeft = new DummyArithmeticResourcePoolImpl(playerId, noOfParties, DEFAULT_FIELD_LEFT); DummyArithmeticResourcePool rpRight = new DummyArithmeticResourcePoolImpl(playerId, From a8a0a172e0530da18c4dac5537b2098df8802ef3 Mon Sep 17 00:00:00 2001 From: Tore Frederiksen Date: Fri, 19 May 2023 18:53:28 +0200 Subject: [PATCH 30/38] temp: outcommented code to allow for larger domains --- .../sce/evaluator/NetworkBatchDecorator.java | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/dk/alexandra/fresco/framework/sce/evaluator/NetworkBatchDecorator.java b/core/src/main/java/dk/alexandra/fresco/framework/sce/evaluator/NetworkBatchDecorator.java index cb8317810..80f80cd2f 100644 --- a/core/src/main/java/dk/alexandra/fresco/framework/sce/evaluator/NetworkBatchDecorator.java +++ b/core/src/main/java/dk/alexandra/fresco/framework/sce/evaluator/NetworkBatchDecorator.java @@ -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); } From 61fbe4a4ab53fb2e24b995963f33eedb1e10f408 Mon Sep 17 00:00:00 2001 From: Tore Frederiksen Date: Fri, 19 May 2023 18:56:49 +0200 Subject: [PATCH 31/38] temp: started code to do gate evaluation properly --- .../fresco/suite/crt/CRTBuilderFactory.java | 9 ++- .../alexandra/fresco/suite/crt/CRTKnown.java | 53 ++++++++++++++++ .../alexandra/fresco/suite/crt/CRTMult.java | 63 +++++++++++++++++++ .../alexandra/fresco/suite/crt/CRTOpen.java | 61 ++++++++++++++++++ 4 files changed, 184 insertions(+), 2 deletions(-) create mode 100644 suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTKnown.java create mode 100644 suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTMult.java create mode 100644 suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTOpen.java diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTBuilderFactory.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTBuilderFactory.java index ea82365b7..1f71ba10c 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTBuilderFactory.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTBuilderFactory.java @@ -12,6 +12,7 @@ import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTDataSupplier; import dk.alexandra.fresco.suite.crt.datatypes.resource.CRTResourcePool; import dk.alexandra.fresco.suite.crt.protocols.framework.ProtocolBuilderNumericWrapper; +import dk.alexandra.fresco.suite.spdz.gates.SpdzMultProtocol; import java.math.BigInteger; @@ -64,7 +65,6 @@ public Numeric createNumeric(ProtocolBuilderNumeric builder) { @Override public DRes add(DRes a, DRes b) { - return builder.par(par -> { CRTSInt aOut = (CRTSInt) a.out(); @@ -134,6 +134,8 @@ public DRes sub(DRes a, BigInteger b) { @Override public DRes mult(DRes a, DRes b) { +// CRTMult crtMult = new CRTMult(a, b); +// return builder.append(crtMult); return builder.par(par -> { CRTSInt aOut = (CRTSInt) a.out(); DRes aLeft = aOut.getLeft(); @@ -188,7 +190,8 @@ public DRes randomElement() { @Override public DRes known(BigInteger value) { Pair crt = Util.mapToCRT(value, p, q); - +// CRTKnown crtMult = new CRTKnown(crt); +// return builder.append(crtMult); return builder.par(par -> { Numeric l = context.leftNumeric(par); Numeric r = context.rightNumeric(par); @@ -216,6 +219,8 @@ public DRes input(BigInteger value, int inputParty) { @Override public DRes open(DRes secretShare) { +// CRTOpen crtOpen = new CRTOpen(secretShare); +// return builder.append(crtOpen); return builder.par(par -> { CRTSInt crtsInt = (CRTSInt) secretShare.out(); diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTKnown.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTKnown.java new file mode 100644 index 000000000..ab690abf7 --- /dev/null +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTKnown.java @@ -0,0 +1,53 @@ +package dk.alexandra.fresco.suite.crt; + +import dk.alexandra.fresco.framework.DRes; +import dk.alexandra.fresco.framework.Party; +import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool; +import dk.alexandra.fresco.framework.network.Network; +import dk.alexandra.fresco.framework.util.Pair; +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.SpdzKnownSIntProtocol; +import dk.alexandra.fresco.suite.spdz.gates.SpdzMultProtocol; + +import java.math.BigInteger; + +public class CRTKnown extends CRTNativeProtocol { + + private final BigInteger left; + private final BigInteger right; + private CRTSInt out; + private EvaluationStatus status = EvaluationStatus.HAS_MORE_ROUNDS; + private SpdzKnownSIntProtocol spdzLeft; + private SpdzKnownSIntProtocol spdzRight; + + public CRTKnown(Pair input) { + this.left = input.getFirst(); + this.right = input.getSecond(); + } + + @Override + public CRTSInt out() { + return out; + } + + @Override + public EvaluationStatus evaluate(int round, CRTResourcePool CRTResourcePool, + Network network) { + if (round == 0) { + spdzLeft = new SpdzKnownSIntProtocol(left); + spdzRight = new SpdzKnownSIntProtocol(right); + } + spdzLeft.evaluate(round, (SpdzResourcePool) CRTResourcePool.getSubResourcePools().getFirst(), network); + status = spdzRight.evaluate(round, (SpdzResourcePool) CRTResourcePool.getSubResourcePools().getSecond(), network); + if (status == EvaluationStatus.IS_DONE) { + this.out = new CRTSInt(spdzLeft.out(), spdzRight.out()); + return EvaluationStatus.IS_DONE; + } else { + return EvaluationStatus.HAS_MORE_ROUNDS; + } + } +} diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTMult.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTMult.java new file mode 100644 index 000000000..3408609b2 --- /dev/null +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTMult.java @@ -0,0 +1,63 @@ +package dk.alexandra.fresco.suite.crt; + +import dk.alexandra.fresco.framework.DRes; +import dk.alexandra.fresco.framework.builder.numeric.Numeric; +import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool; +import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; +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.datatypes.SpdzSInt; +import dk.alexandra.fresco.suite.spdz.gates.SpdzKnownSIntProtocol; +import dk.alexandra.fresco.suite.spdz.gates.SpdzMultProtocol; +import dk.alexandra.fresco.suite.spdz.gates.SpdzNativeProtocol; + +import java.math.BigInteger; + +public class CRTMult extends CRTNativeProtocol { + + private final DRes a; + private final DRes b; + private CRTSInt out; + private EvaluationStatus status = EvaluationStatus.HAS_MORE_ROUNDS; + private SpdzMultProtocol spdzMultProtocolLeft; + private SpdzMultProtocol spdzMultProtocolRight; + + public CRTMult(DRes a, DRes 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 aLeft = aOut.getLeft(); + DRes aRight = aOut.getRight(); + + CRTSInt bOut = (CRTSInt) b.out(); + DRes bLeft = bOut.getLeft(); + DRes bRight = bOut.getRight(); + + spdzMultProtocolLeft = new SpdzMultProtocol(aLeft, bLeft); + spdzMultProtocolRight = new SpdzMultProtocol(aRight, bRight); + } + spdzMultProtocolLeft.evaluate(round, (SpdzResourcePool) CRTResourcePool.getSubResourcePools().getFirst(), network); + status = spdzMultProtocolRight.evaluate(round, (SpdzResourcePool) CRTResourcePool.getSubResourcePools().getSecond(), network); + if (status == EvaluationStatus.IS_DONE) { + this.out = new CRTSInt(spdzMultProtocolLeft.out(), spdzMultProtocolRight.out()); + return EvaluationStatus.IS_DONE; + } else { + return EvaluationStatus.HAS_MORE_ROUNDS; + } + } +} diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTOpen.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTOpen.java new file mode 100644 index 000000000..4374a7459 --- /dev/null +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTOpen.java @@ -0,0 +1,61 @@ +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.builder.numeric.field.FieldDefinition; +import dk.alexandra.fresco.framework.network.Network; +import dk.alexandra.fresco.framework.util.AesCtrDrbg; +import dk.alexandra.fresco.framework.util.OpenedValueStore; +import dk.alexandra.fresco.framework.util.OpenedValueStoreImpl; +import dk.alexandra.fresco.framework.util.Pair; +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.dummy.arithmetic.DummyArithmeticResourcePool; +import dk.alexandra.fresco.suite.dummy.arithmetic.DummyArithmeticResourcePoolImpl; +import dk.alexandra.fresco.suite.spdz.SpdzResourcePool; +import dk.alexandra.fresco.suite.spdz.SpdzResourcePoolImpl; +import dk.alexandra.fresco.suite.spdz.gates.SpdzKnownSIntProtocol; +import dk.alexandra.fresco.suite.spdz.gates.SpdzOutputToAllProtocol; +import dk.alexandra.fresco.suite.spdz.storage.SpdzDataSupplier; +import dk.alexandra.fresco.suite.spdz.storage.SpdzDummyDataSupplier; + +import java.math.BigInteger; + +public class CRTOpen extends CRTNativeProtocol { + + private final DRes input; + private BigInteger out; + private EvaluationStatus status = EvaluationStatus.HAS_MORE_ROUNDS; + private SpdzOutputToAllProtocol spdzLeft; + private SpdzOutputToAllProtocol spdzRight; + + public CRTOpen(DRes input) { + this.input = input; + } + + @Override + public BigInteger out() { + return out; + } + + @Override + public EvaluationStatus evaluate(int round, CRTResourcePool CRTResourcePool, + Network network) { + if (round == 0) { + spdzLeft = new SpdzOutputToAllProtocol(((CRTSInt) input.out()).getLeft()); + spdzRight = new SpdzOutputToAllProtocol(((CRTSInt) input.out()).getRight()); + } + spdzLeft.evaluate(round, (SpdzResourcePool) CRTResourcePool.getSubResourcePools().getFirst(), network); + status = spdzRight.evaluate(round, (SpdzResourcePool) CRTResourcePool.getSubResourcePools().getSecond(), network); + if (status == EvaluationStatus.IS_DONE) { + Pair fieldDefs = CRTResourcePool.getFieldDefinitions(); + this.out = Util.mapToBigInteger(spdzLeft.out(), spdzRight.out(), fieldDefs.getFirst().getModulus(), fieldDefs.getSecond().getModulus()); + return EvaluationStatus.IS_DONE; + } else { + return EvaluationStatus.HAS_MORE_ROUNDS; + } + } +} + From d3edf74d0131ce31244816c88b93dac222b47ea6 Mon Sep 17 00:00:00 2001 From: Tore Frederiksen Date: Mon, 22 May 2023 12:03:29 +0200 Subject: [PATCH 32/38] fix: fixed semi-honest noise generation --- .../dk/alexandra/fresco/suite/crt/CRTAdd.java | 57 +++++++++++++++++ .../fresco/suite/crt/CRTBuilderFactory.java | 2 + .../resource/CovertNoiseGenerator.java | 2 +- .../datatypes/resource/PaddingGenerator.java | 3 +- .../resource/SemiHonestNoiseGenerator.java | 62 ++++++++----------- 5 files changed, 87 insertions(+), 39 deletions(-) create mode 100644 suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTAdd.java diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTAdd.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTAdd.java new file mode 100644 index 000000000..e59330d8e --- /dev/null +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTAdd.java @@ -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 { + + private final DRes a; + private final DRes b; + private CRTSInt out; + private EvaluationStatus status = EvaluationStatus.HAS_MORE_ROUNDS; + private SpdzAddProtocol spdzAddProtocolLeft; + private SpdzAddProtocol spdzAddProtocolRight; + + public CRTAdd(DRes a, DRes 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 aLeft = aOut.getLeft(); + DRes aRight = aOut.getRight(); + + CRTSInt bOut = (CRTSInt) b.out(); + DRes bLeft = bOut.getLeft(); + DRes 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; + } + } +} \ No newline at end of file diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTBuilderFactory.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTBuilderFactory.java index 1f71ba10c..0093114ef 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTBuilderFactory.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTBuilderFactory.java @@ -65,6 +65,8 @@ public Numeric createNumeric(ProtocolBuilderNumeric builder) { @Override public DRes add(DRes a, DRes b) { +// CRTAdd crtAdd = new CRTAdd(a, b); +// return builder.append(crtAdd); return builder.par(par -> { CRTSInt aOut = (CRTSInt) a.out(); diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CovertNoiseGenerator.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CovertNoiseGenerator.java index ebfac4054..1cf9df139 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CovertNoiseGenerator.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/CovertNoiseGenerator.java @@ -65,7 +65,7 @@ public DRes> buildComputation(ProtocolBuilderNumeric builde pairShares, rhoPad, psiPad, seed, }; }).par((par, data) -> { - List combinedPads = new ArrayList<>(securityParam * batchSize); + List combinedPads = new ArrayList<>(this.deterrenceFactor * batchSize); for (int i = 0; i < deterrenceFactor * batchSize; i++) { combinedPads.add(new CRTCombinedPad( ((DRes>) data[0]).out().get(i), diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/PaddingGenerator.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/PaddingGenerator.java index cd1c9f25d..51825121d 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/PaddingGenerator.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/PaddingGenerator.java @@ -17,8 +17,7 @@ import java.util.ArrayList; import java.util.List; -public class PaddingGenerator //implements Computation>, ProtocolBuilderNumeric> { - +public class PaddingGenerator extends CRTComputation, ResourcePoolL, ResourcePoolR> { private final int batchSize; diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/SemiHonestNoiseGenerator.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/SemiHonestNoiseGenerator.java index 7869585d7..709abaaf5 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/SemiHonestNoiseGenerator.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/datatypes/resource/SemiHonestNoiseGenerator.java @@ -1,19 +1,15 @@ package dk.alexandra.fresco.suite.crt.datatypes.resource; import dk.alexandra.fresco.framework.DRes; -import dk.alexandra.fresco.framework.builder.numeric.Numeric; import dk.alexandra.fresco.framework.builder.numeric.NumericResourcePool; import dk.alexandra.fresco.framework.builder.numeric.ProtocolBuilderNumeric; -import dk.alexandra.fresco.framework.util.AesCtrDrbg; -import dk.alexandra.fresco.framework.util.Drbg; -import dk.alexandra.fresco.framework.util.Drng; -import dk.alexandra.fresco.framework.util.DrngImpl; -import dk.alexandra.fresco.framework.value.SInt; +import dk.alexandra.fresco.framework.util.*; import dk.alexandra.fresco.suite.crt.CRTNumericContext; import dk.alexandra.fresco.suite.crt.datatypes.CRTCombinedPad; import dk.alexandra.fresco.suite.crt.datatypes.CRTSInt; +import dk.alexandra.fresco.tools.commitment.CoinTossingComputation; +import dk.alexandra.fresco.tools.commitment.HashBasedCommitmentSerializer; -import java.math.BigInteger; import java.util.ArrayList; import java.util.List; @@ -23,49 +19,43 @@ public class SemiHonestNoiseGenerator pairShareGenerator; + private final PaddingGenerator padGenerator; + private final CoinTossingComputation coinToss; public SemiHonestNoiseGenerator(int batchSize, int securityParam) { - this(batchSize, securityParam, new DrngImpl(new AesCtrDrbg())); + this(batchSize, securityParam, new AesCtrDrbg()); } - public SemiHonestNoiseGenerator(int batchSize, int securityParam, Drng localDrng) { + public SemiHonestNoiseGenerator(int batchSize, int securityParam, Drbg localDrbg) { this.batchSize = batchSize; this.securityParam = securityParam; - this.localDrng = localDrng; + this.localDrng = new DrngImpl(localDrbg); + HashBasedCommitmentSerializer commitmentSerializer = new HashBasedCommitmentSerializer(); + this.pairShareGenerator = new PairShareGenerator<>( batchSize, securityParam, new DrngImpl(localDrbg)); + this.padGenerator = new PaddingGenerator(batchSize, securityParam, new DrngImpl(localDrbg)); + this.coinToss = new CoinTossingComputation(32, commitmentSerializer, localDrbg); } @Override public DRes> buildComputation(ProtocolBuilderNumeric builder, CRTNumericContext context) { return builder.par(par -> { - Numeric left = context.leftNumeric(par); - Numeric right = context.rightNumeric(par); - List list = new ArrayList<>(batchSize); + DRes> pairShares = pairShareGenerator.buildComputation(par, context); + DRes> rhoPad = padGenerator.buildComputation(par, context); + DRes> psiPad = padGenerator.buildComputation(par, context); + return () -> new Object[] { + pairShares, rhoPad, psiPad + }; + }).par((par, data) -> { + List combinedPads = new ArrayList<>(batchSize); for (int i = 0; i < batchSize; i++) { - DRes r = left.randomElement(); - CRTSInt noisePair = new CRTSInt(r, r); - DRes rho = getStatisticalShares(right, par.getBasicNumericContext().getMyId(), par.getBasicNumericContext().getNoOfParties()); - DRes psi = getStatisticalShares(right, par.getBasicNumericContext().getMyId(), par.getBasicNumericContext().getNoOfParties()); - list.add(new CRTCombinedPad(noisePair, rho, psi)); + combinedPads.add(new CRTCombinedPad( + ((DRes>) data[0]).out().get(i), + ((DRes>) data[1]).out().get(i).getRight(), + ((DRes>) data[2]).out().get(i).getRight())); } - return DRes.of(list); + return ()->combinedPads; }); } - - private DRes getStatisticalShares(Numeric right, int myId, int parties) { - DRes sharedRand = right.known(0); - for (int j = 1; j <= parties; j++) { - DRes curRand; - if (j == myId) { - BigInteger modulo = BigInteger.valueOf(2).pow(securityParam); - BigInteger rand = localDrng.nextBigInteger(modulo); - curRand = right.input(rand, myId); - } else { - curRand = right.input(null, j); - } - sharedRand = right.add(sharedRand, curRand); - } - return sharedRand; - } - } From 3f5c502198bde7b7ff20216efdd31a218fd96316 Mon Sep 17 00:00:00 2001 From: Tore Frederiksen Date: Fri, 26 May 2023 09:15:09 +0200 Subject: [PATCH 33/38] test: outcommented tests not working due to missing framework implementations for CRT --- .../fresco/lib/common/compare/RandomAdditiveMask.java | 1 - .../test/java/dk/alexandra/fresco/suite/crt/TestCRT.java | 7 +++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/common/src/main/java/dk/alexandra/fresco/lib/common/compare/RandomAdditiveMask.java b/lib/common/src/main/java/dk/alexandra/fresco/lib/common/compare/RandomAdditiveMask.java index 1f7479dd0..cbb69b8ca 100644 --- a/lib/common/src/main/java/dk/alexandra/fresco/lib/common/compare/RandomAdditiveMask.java +++ b/lib/common/src/main/java/dk/alexandra/fresco/lib/common/compare/RandomAdditiveMask.java @@ -14,7 +14,6 @@ public class RandomAdditiveMask implements Computation { private final int noOfBits; - private List> bits; private DRes value; diff --git a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/TestCRT.java b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/TestCRT.java index 449e9b5f4..9d92a2151 100644 --- a/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/TestCRT.java +++ b/suite/crt/src/test/java/dk/alexandra/fresco/suite/crt/TestCRT.java @@ -39,6 +39,7 @@ import dk.alexandra.fresco.suite.dummy.arithmetic.BasicArithmeticTests.TestSumAndMult; import dk.alexandra.fresco.suite.spdz.configuration.PreprocessingStrategy; import org.junit.Before; +import org.junit.Ignore; import org.junit.Test; public class TestCRT { @@ -211,6 +212,8 @@ public void testFixedPointDivision() { .runTest(new TestFixedPointDivision<>(), EvaluationStrategy.SEQUENTIAL, PreprocessingStrategy.DUMMY, 2); } + // TODO needs random bit and other framework things implemented + @Ignore @Test public void testFixedPointSecretDivision() { new AbstractSpdzCRTTest() @@ -224,6 +227,8 @@ public void testSort() { PreprocessingStrategy.DUMMY, 2); } + // TODO needs random bit and other framework things implemented + @Ignore @Test public void testSortDifferentValueLength() { new AbstractSpdzCRTTest() @@ -231,6 +236,8 @@ public void testSortDifferentValueLength() { PreprocessingStrategy.DUMMY, 2); } + // TODO needs random bit and other framework things implemented + @Ignore @Test public void testExp() { new AbstractSpdzCRTTest() From a85d14d2579660a7cf73ac479c9ab0b67321d208 Mon Sep 17 00:00:00 2001 From: Tore Frederiksen Date: Fri, 26 May 2023 09:31:00 +0200 Subject: [PATCH 34/38] refactor: removed personal reference --- .../java/dk/alexandra/fresco/suite/crt/CRTBuilderFactory.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTBuilderFactory.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTBuilderFactory.java index 0093114ef..ced76fa05 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTBuilderFactory.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/CRTBuilderFactory.java @@ -49,7 +49,7 @@ public CRTBuilderFactory(CRTResourcePool resourceP this.p = resourcePoolLeft.getModulus(); this.q = resourcePoolRight.getModulus(); this.context = new CRTNumericContext<>( - p.bitLength() + q.bitLength() - statisticalSec, //TODO @jonas is this right? + p.bitLength() + q.bitLength() - statisticalSec, //TODO update based on new detail resourcePoolLeft.getMyId(), resourcePoolLeft.getNoOfParties(), left, right, p, q, resourcePool); } From c58e08708b44567385e355d9adfa1df8361aa3f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Lindstr=C3=B8m?= Date: Wed, 30 Aug 2023 10:30:24 +0200 Subject: [PATCH 35/38] Create bits in parallel --- .../common/compare/RandomAdditiveMask.java | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/common/src/main/java/dk/alexandra/fresco/lib/common/compare/RandomAdditiveMask.java b/lib/common/src/main/java/dk/alexandra/fresco/lib/common/compare/RandomAdditiveMask.java index cbb69b8ca..6101247fa 100644 --- a/lib/common/src/main/java/dk/alexandra/fresco/lib/common/compare/RandomAdditiveMask.java +++ b/lib/common/src/main/java/dk/alexandra/fresco/lib/common/compare/RandomAdditiveMask.java @@ -23,20 +23,20 @@ public RandomAdditiveMask(int noOfBits) { @Override public DRes buildComputation( - ProtocolBuilderNumeric builder) { - Numeric numericBuilder = builder.numeric(); - bits = new ArrayList<>(); - for (int i = 0; i < noOfBits; i++) { - DRes randomBit = numericBuilder.randomBit(); - bits.add(randomBit); - } - - MiscBigIntegerGenerators oIntGenerators = new MiscBigIntegerGenerators(builder.getBasicNumericContext().getModulus()); - AdvancedNumeric advancedNumeric = AdvancedNumeric.using(builder); - List 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(); + bits = new ArrayList<>(); + for (int i = 0; i < noOfBits; i++) { + DRes randomBit = numericBuilder.randomBit(); + bits.add(randomBit); + } + return DRes.of(bits); + }).seq((seq, bits) -> { + MiscBigIntegerGenerators oIntGenerators = new MiscBigIntegerGenerators(seq.getBasicNumericContext().getModulus()); + List twoPows = oIntGenerators.getTwoPowersList(noOfBits); + value = AdvancedNumeric.using(seq).innerProductWithPublicPart(twoPows, bits); + return DRes.of(new AdvancedNumeric.RandomAdditiveMask(bits, value.out())); + }); } } From 28c5fc481977f3c64bbb129e5ab7187697057bac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Lindstr=C3=B8m?= Date: Tue, 5 Sep 2023 23:02:54 +0200 Subject: [PATCH 36/38] Fix bit generation --- .../fresco/lib/common/compare/RandomAdditiveMask.java | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/lib/common/src/main/java/dk/alexandra/fresco/lib/common/compare/RandomAdditiveMask.java b/lib/common/src/main/java/dk/alexandra/fresco/lib/common/compare/RandomAdditiveMask.java index 6101247fa..7d07537f8 100644 --- a/lib/common/src/main/java/dk/alexandra/fresco/lib/common/compare/RandomAdditiveMask.java +++ b/lib/common/src/main/java/dk/alexandra/fresco/lib/common/compare/RandomAdditiveMask.java @@ -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; @@ -14,9 +15,6 @@ public class RandomAdditiveMask implements Computation { private final int noOfBits; - private List> bits; - private DRes value; - public RandomAdditiveMask(int noOfBits) { this.noOfBits = noOfBits; } @@ -26,7 +24,7 @@ public DRes buildComputation( ProtocolBuilderNumeric builder) { return builder.par(par -> { Numeric numericBuilder = par.numeric(); - bits = new ArrayList<>(); + List> bits = new ArrayList<>(); for (int i = 0; i < noOfBits; i++) { DRes randomBit = numericBuilder.randomBit(); bits.add(randomBit); @@ -35,8 +33,7 @@ public DRes buildComputation( }).seq((seq, bits) -> { MiscBigIntegerGenerators oIntGenerators = new MiscBigIntegerGenerators(seq.getBasicNumericContext().getModulus()); List twoPows = oIntGenerators.getTwoPowersList(noOfBits); - value = AdvancedNumeric.using(seq).innerProductWithPublicPart(twoPows, bits); - return DRes.of(new AdvancedNumeric.RandomAdditiveMask(bits, value.out())); - }); + return Pair.lazy(bits, AdvancedNumeric.using(seq).innerProductWithPublicPart(twoPows, bits)); + }).seq((seq, bitsAndValue) -> DRes.of(new AdvancedNumeric.RandomAdditiveMask(bitsAndValue.getFirst(), bitsAndValue.getSecond()))); } } From 759bb0aafc49970b6384e6a8180db2eee890f673 Mon Sep 17 00:00:00 2001 From: Tore Frederiksen Date: Wed, 13 Sep 2023 15:27:10 +0200 Subject: [PATCH 37/38] refactor: making sure par is used whereever possible --- .../common/compare/RandomAdditiveMask.java | 10 ++++----- .../common/math/DefaultAdvancedNumeric.java | 3 ++- .../common/math/integer/binary/Truncate.java | 22 ++++++++++--------- .../math/integer/linalg/InnerProductOpen.java | 2 +- .../fixed/DefaultAdvancedFixedNumeric.java | 4 ++-- 5 files changed, 22 insertions(+), 19 deletions(-) diff --git a/lib/common/src/main/java/dk/alexandra/fresco/lib/common/compare/RandomAdditiveMask.java b/lib/common/src/main/java/dk/alexandra/fresco/lib/common/compare/RandomAdditiveMask.java index 7d07537f8..1a031ebaf 100644 --- a/lib/common/src/main/java/dk/alexandra/fresco/lib/common/compare/RandomAdditiveMask.java +++ b/lib/common/src/main/java/dk/alexandra/fresco/lib/common/compare/RandomAdditiveMask.java @@ -30,10 +30,10 @@ public DRes buildComputation( bits.add(randomBit); } return DRes.of(bits); - }).seq((seq, bits) -> { - MiscBigIntegerGenerators oIntGenerators = new MiscBigIntegerGenerators(seq.getBasicNumericContext().getModulus()); + }).par((par, bits) -> { + MiscBigIntegerGenerators oIntGenerators = new MiscBigIntegerGenerators(par.getBasicNumericContext().getModulus()); List twoPows = oIntGenerators.getTwoPowersList(noOfBits); - return Pair.lazy(bits, AdvancedNumeric.using(seq).innerProductWithPublicPart(twoPows, bits)); - }).seq((seq, bitsAndValue) -> DRes.of(new AdvancedNumeric.RandomAdditiveMask(bitsAndValue.getFirst(), bitsAndValue.getSecond()))); + return Pair.lazy(bits, AdvancedNumeric.using(par).innerProductWithPublicPart(twoPows, bits)); + }).par((par, bitsAndValue) -> DRes.of(new AdvancedNumeric.RandomAdditiveMask(bitsAndValue.getFirst(), bitsAndValue.getSecond()))); } -} +} \ No newline at end of file diff --git a/lib/common/src/main/java/dk/alexandra/fresco/lib/common/math/DefaultAdvancedNumeric.java b/lib/common/src/main/java/dk/alexandra/fresco/lib/common/math/DefaultAdvancedNumeric.java index 00cc0e023..e09cedbe0 100644 --- a/lib/common/src/main/java/dk/alexandra/fresco/lib/common/math/DefaultAdvancedNumeric.java +++ b/lib/common/src/main/java/dk/alexandra/fresco/lib/common/math/DefaultAdvancedNumeric.java @@ -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; @@ -178,7 +179,7 @@ public DRes, List>>>> keyedCompareAndSwap( public DRes bitsToInteger(List> 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)); } } diff --git a/lib/common/src/main/java/dk/alexandra/fresco/lib/common/math/integer/binary/Truncate.java b/lib/common/src/main/java/dk/alexandra/fresco/lib/common/math/integer/binary/Truncate.java index f0efeb0c4..66da05beb 100644 --- a/lib/common/src/main/java/dk/alexandra/fresco/lib/common/math/integer/binary/Truncate.java +++ b/lib/common/src/main/java/dk/alexandra/fresco/lib/common/math/integer/binary/Truncate.java @@ -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 @@ -37,20 +41,18 @@ public DRes 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 result = seq.numeric().add(input, randomAdditiveMask.value); - DRes open = seq.numeric().open(result); + DRes result = par.numeric().add(input, randomAdditiveMask.value); + DRes open = par.numeric().open(result); return Pair.lazy(open, randomAdditiveMask); }).seq((seq, maskedInput) -> { @@ -62,10 +64,10 @@ public DRes buildComputation(ProtocolBuilderNumeric builder) { * rBottom = r (mod 2^shifts). */ final DRes 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 rTop = seq.numeric().sub(mask.value, rBottom); /* diff --git a/lib/common/src/main/java/dk/alexandra/fresco/lib/common/math/integer/linalg/InnerProductOpen.java b/lib/common/src/main/java/dk/alexandra/fresco/lib/common/math/integer/linalg/InnerProductOpen.java index 96fed8b16..76368429d 100644 --- a/lib/common/src/main/java/dk/alexandra/fresco/lib/common/math/integer/linalg/InnerProductOpen.java +++ b/lib/common/src/main/java/dk/alexandra/fresco/lib/common/math/integer/linalg/InnerProductOpen.java @@ -36,6 +36,6 @@ public DRes 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)); } } diff --git a/lib/fixed/src/main/java/dk/alexandra/fresco/lib/fixed/DefaultAdvancedFixedNumeric.java b/lib/fixed/src/main/java/dk/alexandra/fresco/lib/fixed/DefaultAdvancedFixedNumeric.java index 4bedc3462..fc574d98e 100644 --- a/lib/fixed/src/main/java/dk/alexandra/fresco/lib/fixed/DefaultAdvancedFixedNumeric.java +++ b/lib/fixed/src/main/java/dk/alexandra/fresco/lib/fixed/DefaultAdvancedFixedNumeric.java @@ -31,9 +31,9 @@ public DefaultAdvancedFixedNumeric(ProtocolBuilderNumeric builder) { @Override public DRes random() { - return builder.seq(seq -> { + return builder.par(par -> { DRes 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); From dbbedcd86b3264ed2b9b2ff4c16dd91db81b9970 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Lindstr=C3=B8m?= Date: Tue, 26 Sep 2023 11:39:33 +0200 Subject: [PATCH 38/38] Remove constant --- .../dk/alexandra/fresco/suite/crt/protocols/LiftQPProtocol.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftQPProtocol.java b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftQPProtocol.java index 59aa0816e..3f797e1a0 100644 --- a/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftQPProtocol.java +++ b/suite/crt/src/main/java/dk/alexandra/fresco/suite/crt/protocols/LiftQPProtocol.java @@ -31,7 +31,7 @@ public LiftQPProtocol(DRes value) { public DRes buildComputation(ProtocolBuilderNumeric builder, CRTNumericContext context) { //q' = q / 2 is divisible by p, so adding it to the input only affects the output by q mod p = 1 if there's an overflow. - BigInteger qPrime = new BigInteger("3138550867693340351802905239100779285196644626743924002860"); + BigInteger qPrime = context.getRightModulus().subtract(BigInteger.ONE).divide(BigInteger.valueOf(2)); return builder.seq(new CorrelatedNoiseProtocol<>()).seq((seq, noise) -> { this.r = (CRTSInt) noise.getNoisePair().out();