@Override public DRes<SInt> buildComputation(ProtocolBuilderNumeric builder) { return builder.par((par) -> { int n = keys.size(); List<DRes<SInt>> index = new ArrayList<>(n); for (DRes<SInt> key : keys) { index.add(par.comparison().equals(lookUpKey, key)); } return () -> index; }).seq((seq, index) -> { DRes<SInt> outputValue = notFoundValue; for (int i = 0, valuesLength = values.size(); i < valuesLength; i++) { DRes<SInt> value = values.get(i); outputValue = seq.seq(new ConditionalSelect(index.get(i), value, outputValue)); } return outputValue; }); } }
@Override public DRes<SInt> buildComputation(ProtocolBuilderNumeric builder) { DRes<SInt> diff = builder.numeric().sub(left, right); return builder.comparison().compareZero(diff, bitLength); } }
public DRes<SInt> isSorted(ProtocolBuilderNumeric builder, List<DRes<SInt>> values) { return builder.par(par -> { Comparison comparison = par.comparison(); ArrayList<DRes<SInt>> comparisons = new ArrayList<>(); boolean first = true; DRes<SInt> previous = null; for (DRes<SInt> value : values) { if (!first) { comparisons.add(comparison.compareLEQ(previous, value)); } else { first = false; } previous = value; } return () -> comparisons; }).seq((seq, comparison) -> seq.advancedNumeric().product(comparison)); }
return rootBuilder.par((parallelBuilder) -> { List<DRes<SInt>> result = new ArrayList<>(); Comparison builder = parallelBuilder.comparison();
public void compareAndSwap(ProtocolBuilderNumeric builder, int a, int b, List<DRes<SInt>> values) { //Non splitting version Numeric numeric = builder.numeric(); DRes<SInt> valueA = values.get(a); DRes<SInt> valueB = values.get(b); DRes<SInt> comparison = builder.comparison().compareLEQ(valueA, valueB); DRes<SInt> sub = numeric.sub(valueA, valueB); DRes<SInt> c = numeric.mult(comparison, sub); DRes<SInt> d = numeric.mult(minusOne, c); //a = comparison*a+(1-comparison)*b ==> comparison*(a-b)+b //b = comparison*b+(1-comparison)*a ==> -comparison*(a-b)+a builder.par(par -> { values.set(a, par.numeric().add(c, valueB)); values.set(b, par.numeric().add(d, valueA)); return null; }); }
}).par((innerPar, basisValues) -> { List<DRes<SInt>> above = lpOutput.basis.stream().map(n -> innerPar.comparison().compareLEQ(firstPeer, n)) .collect(Collectors.toList()); List<DRes<SInt>> below = lpOutput.basis.stream().map(n -> innerPar.comparison().compareLEQ(n, lastPeer)) .collect(Collectors.toList()); List<List<DRes<SInt>>> newState = new ArrayList<>(3);
ArrayList<DRes<SInt>> signs = new ArrayList<>(updatedF.size()); for (DRes<SInt> f : updatedF) { signs.add(par.comparison().compareLEQ(f, negativeOne)); ArrayList<DRes<SInt>> enteringIndex = new ArrayList<>(); int bitlength = (int) Math.log(pairwiseSums.size()) * 2 + 1; Comparison comparison = par.comparison(); for (int i = 0; i < updatedF.size(); i++) { enteringIndex.add(comparison.equals(bitlength, pairwiseSums.get(i), one));
DRes<SInt> dividendSign = builder.comparison().sign(dividend); DRes<SInt> dividendAbs = numeric.mult(dividend, dividendSign);
@Override public DRes<SInt> leq(DRes<SReal> a, DRes<SReal> b) { return builder.seq(seq -> { SFixed floatA = (SFixed) a.out(); SFixed floatB = (SFixed) b.out(); int scale = Math.max(floatA.getPrecision(), (floatB.getPrecision())); DRes<SInt> unscaledA = unscaled(seq, floatA, scale); DRes<SInt> unscaledB = unscaled(seq, floatB, scale); return seq.comparison().compareLEQ(unscaledA, unscaledB); }); } }
@Override public DRes<SInt> buildComputation(ProtocolBuilderNumeric builder) { return builder.par(par -> { Numeric numeric = par.numeric(); return Pair.lazy( numeric.mult(d0, n1), numeric.mult(d1, n0) ); }).seq((seq, pair) -> seq.comparison().equals(pair.getFirst(), pair.getSecond()) ); }
ArrayList<DRes<SInt>> nonApps = new ArrayList<>(updatedB.size()); Comparison comparison = par.comparison(); for (int i = 0; i < updatedB.size(); i++) { nonApps.add(
/** * annotates list1 with duplicate marks. To annotate list 2 also, run with the lists switched If a * horisontal join is desired, make sure that both lists are ordered initially, annotate both * lists and update fields by going through tables in lockstep. */ public void findDuplicates(ProtocolBuilderNumeric builder, SIntListofTuples list1, SIntListofTuples list2) { for (int i = 0; i < list1.size(); i++) { int finalI = i; builder.seq(seq -> { Comparison comparison = seq.comparison(); for (int j = 0; j < list2.size(); j++) { DRes<SInt> equals = comparison.equals(list1.getId(finalI), list2.getId(j)); list1.setDuplicate(finalI, or(seq, list1.getDuplicate(finalI), equals)); } return null; }); } }
BigInteger one = BigInteger.ONE; if (this.size == 2) { Comparison comparison = builder.comparison(); Numeric numeric = builder.numeric(); DRes<SInt> firstValue = this.xs.get(0); minimum.out()); } else if (this.size == 3) { Comparison comparison = builder.comparison(); Numeric numeric = builder.numeric(); DRes<SInt> firstValue = this.xs.get(0); (seq, pair) -> seq.seq(new Minimum(pair.getSecond())) ).seq((seq, pair) -> { Comparison comparison = seq.comparison(); Numeric numeric = seq.numeric(); Pair<List<DRes<SInt>>, SInt> minimum1 = pair.getFirst();
@Override public void test() throws Exception { Application<List<BigInteger>, ProtocolBuilderNumeric> app = builder -> { Numeric input = builder.numeric(); DRes<SInt> x = input.input(BigInteger.valueOf(3), 1); DRes<SInt> y = input.input(BigInteger.valueOf(5), 1); Comparison comparison = builder.comparison(); DRes<SInt> compResult1 = comparison.compareLEQ(x, y); DRes<SInt> compResult2 = comparison.compareLEQ(y, x); DRes<SInt> compResult3 = comparison.compareLEQ(x, x); Numeric open = builder.numeric(); DRes<BigInteger> res1; DRes<BigInteger> res2; res1 = open.open(compResult1); res2 = open.open(compResult2); DRes<BigInteger> res3 = open.open(compResult3); return () -> Arrays.asList(res1.out(), res2.out(), res3.out()); }; List<BigInteger> output = runApplication(app); Assert.assertEquals(BigInteger.ONE, output.get(0)); Assert.assertEquals(BigInteger.ZERO, output.get(1)); Assert.assertEquals(BigInteger.ONE, output.get(2)); } };
/** * Creates a ProtocolProducer to compute the first half of a simplex iteration. * <p> * This finds the variable to enter the basis, based on the pivot rule of most negative entry in * the <i>F</i> vector. Also tests if no negative entry in the <i>F</i> vector is present. If this * is the case we should terminate the simplex method. * </p> * * @return a delayed result of the phaseOne computation of the first half of a simplex iteration */ private DRes<Pair<List<DRes<SInt>>, BigInteger>> phaseOneDanzig( ProtocolBuilderNumeric builder, LpState state, DRes<SInt> zero) { return builder .seq( // Compute potential entering variable index and corresponding value of // entry in F new EnteringVariable(state.tableau, state.updateMatrix)) .seq((seq, enteringAndMinimum) -> { List<DRes<SInt>> entering = enteringAndMinimum.getFirst(); SInt minimum = enteringAndMinimum.getSecond(); // Check if the entry in F is non-negative DRes<SInt> positive = seq.comparison().compareLEQLong(zero, () -> minimum); DRes<BigInteger> terminationOut = seq.numeric().open(positive); return () -> new Pair<>(entering, terminationOut.out()); }); }
Application<List<BigInteger>, ProtocolBuilderNumeric> app = builder -> { Numeric input = builder.numeric(); Comparison comparison = builder.comparison(); int maxBitLength = builder.getBasicNumericContext().getMaxBitLength();
Application<List<BigInteger>, ProtocolBuilderNumeric> app = builder -> { Numeric input = builder.numeric(); Comparison comparison = builder.comparison(); BigInteger modulus = builder.getBasicNumericContext().getModulus();
DRes<SInt> eqResult = seq.comparison().compareZero(dif, bitLengthTop); return () -> new Object[] {eqResult, rBottom, rTop, mBot, mTop, mBar, rBar, z}; }).seq((ProtocolBuilderNumeric seq, Object[] input) -> {
@Override public void test() throws Exception { Application<Pair<BigInteger, BigInteger>, ProtocolBuilderNumeric> app = builder -> { Numeric input = builder.numeric(); DRes<SInt> x = input.known(BigInteger.valueOf(3)); DRes<SInt> y = input.known(BigInteger.valueOf(5)); Comparison comparison = builder.comparison(); DRes<SInt> compResult1 = comparison.equals(x, x); DRes<SInt> compResult2 = comparison.equals(x, y); Numeric open = builder.numeric(); DRes<BigInteger> res1 = open.open(compResult1); DRes<BigInteger> res2 = open.open(compResult2); return () -> new Pair<>(res1.out(), res2.out()); }; Pair<BigInteger, BigInteger> output = runApplication(app); Assert.assertEquals(BigInteger.ONE, output.getFirst()); Assert.assertEquals(BigInteger.ZERO, output.getSecond()); } };
DRes<SInt> sign = seq.comparison().sign(numerator); DRes<SInt> sign = seq.comparison().sign(denominator);