@Override public DRes<SInt> sub(DRes<SInt> a, DRes<SInt> b) { subCount++; return this.delegate.sub(a, b); }
@Override public DRes<SInt> sub(BigInteger a, DRes<SInt> b) { return this.delegate.sub(a, b); }
@Override public DRes<SInt> sub(DRes<SInt> a, BigInteger b) { return this.delegate.sub(a, b); }
/** * Convenience implementation of {@link #sub(BigInteger, DRes)} */ default DRes<SInt> sub(long a, DRes<SInt> b) { return sub(BigInteger.valueOf(a), b); }
/** * Convenience implementation of {@link #sub(DRes, BigInteger)} */ default DRes<SInt> sub(DRes<SInt> a, long b) { return sub(a, BigInteger.valueOf(b)); }
@Override public DRes<SInt> buildComputation(ProtocolBuilderNumeric builder) { Numeric numeric = builder.numeric(); DRes<SInt> sub = numeric.sub(left, right); DRes<SInt> mult = numeric.mult(condition, sub); return numeric.add(mult, right); } }
private DRes<SInt> or(ProtocolBuilderNumeric builder, DRes<SInt> a, DRes<SInt> b) { Numeric numeric = builder.numeric(); DRes<SInt> product = numeric.mult(a, b); DRes<SInt> add = numeric.add(a, b); return numeric.sub(add, product); }
@Override public DRes<SInt> buildComputation(ProtocolBuilderNumeric builder) { DRes<SInt> diff = builder.numeric().sub(left, right); return builder.comparison().compareZero(diff, bitLength); } }
@Override public DRes<SInt> sign(DRes<SInt> x) { Numeric input = builder.numeric(); // TODO create a compareLeqOrEqZero on comparison builder DRes<SInt> compare = compareLEQ(input.known(BigInteger.ZERO), x); BigInteger oint = BigInteger.valueOf(2); Numeric numericBuilder = builder.numeric(); DRes<SInt> twice = numericBuilder.mult(oint, compare); return numericBuilder.sub(twice, BigInteger.valueOf(1)); }
@Override public DRes<SInt> buildComputation(ProtocolBuilderNumeric builder) { return builder.par((par) -> { List<DRes<SInt>> terms = new ArrayList<>(data.size()); for (DRes<SInt> value : data) { DRes<SInt> term = par.seq((seq) -> { Numeric numeric = seq.numeric(); DRes<SInt> tmp = numeric.sub(value, mean); return numeric.mult(tmp, tmp); }); terms.add(term); } return () -> terms; }).seq((seq, terms) -> seq.seq(new Mean(terms, data.size() - 1)) ); }
@Override public DRes<SInt> buildComputation(ProtocolBuilderNumeric builder) { AdvancedNumeric advancedNumericBuilder = builder.advancedNumeric(); DRes<SInt> divisionResult = advancedNumericBuilder.div(dividend, divisor); Numeric numeric = builder.numeric(); return numeric.sub(dividend, numeric.mult(divisor, divisionResult)); } }
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; }); }
@Override public DRes<SReal> sub(DRes<SReal> a, BigDecimal b) { return builder.seq(seq -> { SFixed floatA = (SFixed) a.out(); int precision = Math.max(defaultPrecision, floatA.getPrecision()); DRes<SInt> unscaledA = unscaled(seq, floatA, precision); BigInteger unscaledB = unscaled(b, precision); return new SFixed(seq.numeric().sub(unscaledA, unscaledB), precision); }); }
@Override public DRes<SReal> sub(BigDecimal a, DRes<SReal> b) { return builder.seq(seq -> { SFixed floatB = (SFixed) b.out(); int precision = Math.max(defaultPrecision, floatB.getPrecision()); BigInteger scaledA = unscaled(a, precision); DRes<SInt> unscaledB = unscaled(seq, floatB, precision); return new SFixed(seq.numeric().sub(scaledA, unscaledB), precision); }); }
@Override public DRes<SReal> sub(DRes<SReal> a, DRes<SReal> b) { return builder.seq(seq -> { SFixed floatA = (SFixed) a.out(); SFixed floatB = (SFixed) b.out(); int precision = Math.max(floatA.getPrecision(), floatB.getPrecision()); DRes<SInt> unscaledA = unscaled(seq, floatA, precision); DRes<SInt> unscaledB = unscaled(seq, floatB, precision); return new SFixed(seq.numeric().sub(unscaledA, unscaledB), precision); }); }
@Override public DRes<SInt> buildComputation(ProtocolBuilderNumeric builder) { /* * ln(2) = 45426 >> 16; */ BigInteger ln2 = BigInteger.valueOf(45426); int shifts = 16; /* * Find the bit length of the input. Note that bit length - 1 is the floor of the the logartihm * with base 2 of the input. */ DRes<SInt> bitLength = builder.advancedNumeric().bitLength(input, maxInputLength); DRes<SInt> log2 = builder.numeric().sub(bitLength, BigInteger.ONE); /* * ln(x) = log_2(x) * ln(2), and we use 45426 >> 16 as an approximation of ln(2). */ DRes<SInt> scaledLog = builder.numeric().mult(ln2, log2); return builder.advancedNumeric().rightShift(scaledLog, shifts); } }
@Override public void test() { Application<BigInteger, ProtocolBuilderNumeric> app = producer -> { Numeric numeric = producer.numeric(); DRes<SInt> leftClosed = numeric.input(left, 1); DRes<SInt> result = numeric.sub(leftClosed, right); return numeric.open(result); }; BigInteger actual = runApplication(app); BigInteger subtract = conf.getResourcePool().getModulus().add(BigInteger.valueOf(-6)); Assert.assertEquals(subtract, actual); } };
@Override public void test() { Application<BigInteger, ProtocolBuilderNumeric> app = producer -> { Numeric numeric = producer.numeric(); DRes<SInt> rightClosed = numeric.input(right, 1); DRes<SInt> result = numeric.sub(left, rightClosed); return numeric.open(result); }; BigInteger actual = runApplication(app); BigInteger subtract = conf.getResourcePool().getModulus().add(BigInteger.valueOf(-6)); Assert.assertEquals(subtract, actual); } };
@Override public void test() { Application<BigInteger, ProtocolBuilderNumeric> app = producer -> { Numeric numeric = producer.numeric(); DRes<SInt> leftClosed = numeric.input(left, 1); DRes<SInt> rightClosed = numeric.input(right, 1); DRes<SInt> result = numeric.sub(leftClosed, rightClosed); return numeric.open(result); }; BigInteger actual = runApplication(app); Assert.assertEquals(BigInteger.valueOf(6), actual); } };
@Override public void test() { Application<BigInteger, ProtocolBuilderNumeric> app = producer -> { Numeric numeric = producer.numeric(); DRes<SInt> leftClosed = numeric.input(left, 1); DRes<SInt> rightClosed = numeric.input(right, 1); DRes<SInt> result = numeric.sub(leftClosed, rightClosed); return numeric.open(result); }; BigInteger actual = runApplication(app); BigInteger subtract = conf.getResourcePool().getModulus().add(BigInteger.valueOf(-6)); Assert.assertEquals(subtract, actual); } };