@Override public DRes<SInt> known(BigInteger value) { return this.delegate.known(value); }
/** * Convenience implementation of {@link #known(BigInteger)} */ default DRes<SInt> known(long value) { return known(BigInteger.valueOf(value)); }
@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<SReal> known(BigDecimal value) { return builder.seq(seq -> { DRes<SInt> input = seq.numeric().known(unscaled(value, defaultPrecision)); return new SFixed(input, defaultPrecision); }); }
@Override public void test() { Application<BigInteger, ProtocolBuilderNumeric> app = producer -> { Numeric numeric = producer.numeric(); DRes<SInt> firstClosed = numeric.known(first); DRes<SInt> secondClosed = numeric.known(second); DRes<SInt> add = numeric.add(firstClosed, secondClosed); DRes<SInt> mult = numeric.mult(firstClosed, add); return numeric.open(mult); }; BigInteger output = runApplication(app); Assert.assertEquals(first.add(second).multiply(first), output); } };
@Override public void test() { Application<BigInteger, ProtocolBuilderNumeric> app = producer -> { Numeric numeric = producer.numeric(); DRes<SInt> exponent = numeric.known(BigInteger.valueOf(exp)); DRes<SInt> result = producer.advancedNumeric().exp(input, exponent, 5); return numeric.open(result); }; BigInteger result = runApplication(app); Assert.assertEquals(BigInteger.valueOf(input).pow(exp), result); } };
@Test public void testRunApplication() { Application<BigInteger, ProtocolBuilderNumeric> app = builder -> { DRes<SInt> a = builder.numeric().known(BigInteger.valueOf(10)); DRes<SInt> b = builder.numeric().known(BigInteger.valueOf(10)); return builder.numeric().open(builder.numeric().add(a, b)); }; DummyArithmeticResourcePool rp = new DummyArithmeticResourcePoolImpl(0, 1, fieldDefinition); BigInteger b = sce.runApplication(app, rp, null); assertThat(b, is(BigInteger.valueOf(20))); }
@Override public void test() throws Exception { Application<SInt, ProtocolBuilderNumeric> app = (builder) -> { int maxLength = builder.getBasicNumericContext().getMaxBitLength(); BigInteger divisor = BigInteger.valueOf(2).pow(maxLength); DRes<SInt> dividend = builder.numeric().known(BigInteger.TEN); return builder.seq(new KnownDivisor(dividend, divisor)); }; runApplication(app); }
@Override public void test() throws Exception { Application<List<BigInteger>, ProtocolBuilderNumeric> app = producer -> producer.seq(builder -> { DRes<SInt> sharedInput = builder.numeric().known(input); return builder.advancedNumeric().toBits(sharedInput, max); }).seq((seq, result) -> { List<DRes<BigInteger>> outs = result.stream().map(seq.numeric()::open).collect(Collectors.toList()); return () -> outs.stream().map(DRes::out).collect(Collectors.toList()); }); List<BigInteger> result = runApplication(app); for (int i = 0; i < max; i++) { Assert.assertEquals(result.get(i).testBit(0), input.testBit(i)); } } };
@Override public void test() throws Exception { Application<BigInteger, ProtocolBuilderNumeric> app = builder -> { DRes<SInt> sharedInput = builder.numeric().known(input); AdvancedNumeric bitLengthBuilder = builder.advancedNumeric(); DRes<SInt> bitLength = bitLengthBuilder.bitLength(sharedInput, input.bitLength() * 2); return builder.numeric().open(bitLength); }; BigInteger result = runApplication(app); Assert.assertEquals(BigInteger.valueOf(input.bitLength()), result); } };
@Test public void testRunApplicationLong() { Application<BigInteger, ProtocolBuilderNumeric> app = builder -> { DRes<SInt> a = builder.numeric().known(10L); DRes<SInt> b = builder.numeric().known(10L); return builder.numeric().open(builder.numeric().add(a, b)); }; DummyArithmeticResourcePool rp = new DummyArithmeticResourcePoolImpl(0, 1, fieldDefinition); BigInteger b = sce.runApplication(app, rp, null); assertThat(b, is(BigInteger.valueOf(20))); }
@Override public void test() { Application<BigInteger, ProtocolBuilderNumeric> app = builder -> { DRes<SInt> p = builder.numeric().known(BigInteger.valueOf(numerator)); DRes<SInt> result = builder.advancedNumeric().mod(p, denominator); return builder.numeric().open(result); }; BigInteger result = runApplication(app); Assert.assertEquals(BigInteger.valueOf(numerator % denominator), result); } };
@Override public void test() { Application<BigInteger, ProtocolBuilderNumeric> app = builder -> { fieldDefinition = builder.getBasicNumericContext().getFieldDefinition(); DRes<SInt> p = builder.numeric().known(BigInteger.valueOf(numerator)); DRes<SInt> q = builder.numeric().known(BigInteger.valueOf(denominator)); DRes<SInt> result = builder.advancedNumeric().div(p, q); return builder.numeric().open(result); }; BigInteger result = runApplication(app); Assert.assertEquals( BigInteger.valueOf(numerator / denominator), fieldDefinition.convertToSigned(result)); } };
@Override public void test() throws Exception { BigInteger ten = BigInteger.TEN; Application<BigInteger, ProtocolBuilderNumeric> app = builder -> { Numeric intFactory = builder.numeric(); DRes<SInt> encryptionKey = intFactory.known(BigInteger.valueOf(527619)); DRes<SInt> plainText = intFactory.known(ten); DRes<SInt> cipherText = builder.seq(new MiMCEncryption(plainText, encryptionKey, 17)); DRes<SInt> decrypted = builder.seq(new MiMCDecryption(cipherText, encryptionKey, 17)); return builder.numeric().open(decrypted); }; BigInteger result = runApplication(app); Assert.assertEquals(ten, result); } };
@Override public void test() { Application<SInt, ProtocolBuilderNumeric> testApplication = root -> { DRes<SInt> left = root.numeric().known(BigInteger.ZERO); DRes<SInt> right = root.numeric().known(BigInteger.ONE); return root.numeric().mult(left, right); }; // this test verifies that the round synchronization logic works when the threshold for // open values is exceeded runApplication(testApplication); Assert.assertFalse( "There should be no unchecked opened values after the evaluation has finished", conf.getResourcePool().getOpenedValueStore().hasPendingValues()); } };
@Override public void test() { Application<SInt, ProtocolBuilderNumeric> testApplication = root -> { DRes<SInt> left = root.numeric().known(BigInteger.ZERO); DRes<SInt> right = root.numeric().known(BigInteger.ONE); return root.numeric().mult(left, right); }; // this tests verifies that the round synchronization logic works correctly when we // do not have output protocols in our application but still open values during // multiplication runApplication(testApplication); Assert.assertFalse( "There should be no unchecked opened values after the evaluation has finished", conf.getResourcePool().getOpenedValueStore().hasPendingValues()); } };
@Override public void test() { Application<SInt, ProtocolBuilderNumeric> testApplication = root -> { DRes<SInt> left = root.numeric().known(BigInteger.ZERO); DRes<SInt> right = root.numeric().known(BigInteger.ONE); return root.numeric().mult(left, right); }; // this tests verifies that the round synchronization logic works correctly when we // do not have output protocols in our application but still open values during // multiplication runApplication(testApplication); Assert.assertFalse( "There should be no unchecked opened values after the evaluation has finished", conf.getResourcePool().getOpenedValueStore().hasPendingValues()); } };
@Override public void test() { for (int partyId = 1; partyId <= conf.getResourcePool().getNoOfParties(); partyId++) { final int finalPartyId = partyId; Application<BigInteger, ProtocolBuilderNumeric> app = producer -> { Numeric numeric = producer.numeric(); DRes<SInt> input = numeric.known(value); return numeric.open(input, finalPartyId); }; BigInteger output = runApplication(app); if (conf.getMyId() == finalPartyId) { Assert.assertEquals(BigInteger.valueOf(10), output); } else { Assert.assertNull(output); } } } };
@Override public void test() { Application<BigInteger, ProtocolBuilderNumeric> app = builder -> { fieldDefinition = builder.getBasicNumericContext().getFieldDefinition(); DRes<SInt> p = builder.numeric().known(numerator); DRes<SInt> result = builder.advancedNumeric().div(p, (long) denominator); return builder.numeric().open(result); }; BigInteger result = runApplication(app); Assert.assertEquals( BigInteger.valueOf(numerator / denominator), fieldDefinition.convertToSigned(result)); } };
void inputTableau(ProtocolBuilderNumeric builder) { builder.par(par -> { Numeric numeric = par.numeric(); secretTableau = new LPTableau( new Matrix<>(constraints.getHeight(), constraints.getWidth(), (i) -> toArrayList(numeric, constraints.getRow(i))), toArrayList(numeric, b), toArrayList(numeric, f), numeric.known(BigInteger.ZERO)); secretUpdateMatrix = new Matrix<>(updateMatrix.getHeight(), updateMatrix.getWidth(), (i) -> toArrayList(numeric, updateMatrix.getRow(i))); return null; }); }