@Override public DRes<SBool> xnor(DRes<SBool> left, boolean right) { if (right) { return left; } else { return builder.binary().not(left); } }
@Override public DRes<Void> buildComputation(ProtocolBuilderBinary builder) { return builder.seq(seq -> { output.println(message); return null; }); } }
@Override public DRes<List<List<DRes<SBool>>>> buildComputation(ProtocolBuilderBinary builder) { return builder.seq(seq -> { return seq.comparison().greaterThan(right, left); }).par((par, data) -> { List<DRes<SBool>> first = left.stream() .map(e -> {return par.advancedBinary().condSelect(data, e, right.get(left.indexOf(e)));}) .collect(Collectors.toList()); List<DRes<SBool>> second = right.stream() .map(e -> {return par.advancedBinary().condSelect(data, e, left.get(right.indexOf(e)));}) .collect(Collectors.toList()); List<List<DRes<SBool>>> result = new ArrayList<List<DRes<SBool>>>(); result.add(first); result.add(second); return () -> result; }); } }
private void compareAndSwapAtIndices(int i, int j, ProtocolBuilderBinary builder) { builder.seq(seq -> { return seq.advancedBinary().keyedCompareAndSwap(numbers.get(i), numbers.get(j)); }).seq((seq, res) -> { numbers.set(i, res.get(0)); numbers.set(j, res.get(1)); return null; }); }
@Override public DRes<List<DRes<SBool>>> buildComputation(ProtocolBuilderBinary builder) { return builder.seq(seq -> { List<DRes<SBool>> ors = new ArrayList<>(); ors.add(number.get(0)); for (int i = 1; i < number.size(); i++) { ors.add(seq.advancedBinary().or(number.get(i), ors.get(i - 1))); xors.add(seq.binary().xor(ors.get(0), seq.binary().known(false))); xors.add(seq.binary().xor(ors.get(i - 1), ors.get(i))); xors.add(seq.binary().known(false)); return () -> xors; }).seq((seq, xors) -> { List<DRes<SBool>> res = new ArrayList<>(); for (int j = 0; j < log2(number.size()) + 1; j++) { res.add(seq.binary().known(false)); DRes<SBool> tmp = seq.binary().and(xors.get(i), seq.binary().known(ithBit)); res.add(j, seq.binary().xor(tmp, res.remove(j)));
@Override public DRes<List<Boolean>> buildComputation(ProtocolBuilderBinary producer) { return producer.seq(seq -> { Binary bin = seq.binary(); List<DRes<SBool>> keyInputs = new ArrayList<>(); List<DRes<SBool>> plainInputs = new ArrayList<>(); if (this.id == 1) { for (boolean b : in) { keyInputs.add(bin.input(b, 1)); plainInputs.add(bin.input(false, 2)); } } else { // Receive inputs for (boolean b : in) { keyInputs.add(bin.input(false, 1)); plainInputs.add(bin.input(b, 2)); } } DRes<List<SBool>> res = seq.bristol().AES(plainInputs, keyInputs); return res; }).seq((seq, aesRes) -> { List<DRes<Boolean>> outs = new ArrayList<>(); for (SBool toOpen : aesRes) { outs.add(seq.binary().open(toOpen)); } return () -> outs; }).seq((seq, opened) -> () -> opened.stream().map(DRes::out).collect(Collectors.toList())); }
@Override public DRes<SBool> buildComputation(ProtocolBuilderBinary builder) { return builder.seq(seq -> { int round = 0; DRes<SBool> xor = seq.binary().xor(inA.get(length - 1), inB.get(length - 1)); round++; DRes<SBool> outC = seq.binary().and(inA.get(length - 1), xor); round++; return new IterationState(round, outC); }).whileLoop( (state) -> state.round <= length, (seq, state) -> { int i = length - state.round; DRes<SBool> xor = seq.binary().xor(inA.get(i), inB.get(i)); DRes<SBool> tmp = seq.binary().xor(inA.get(i), state.value); tmp = seq.binary().and(tmp, xor); DRes<SBool> outC = seq.binary().xor(state.value, tmp); return new IterationState(state.round + 1, outC); }).seq((state, seq) -> { return seq.value; }); }
@Override public DRes<Void> buildComputation(ProtocolBuilderBinary builder) { return builder.seq(seq -> { List<SBool> unfolded = this.string.stream().map(DRes::out).collect(Collectors.toList()); List<DRes<Boolean>> bools = new ArrayList<>(); for (SBool b : unfolded) { bools.add(seq.binary().open(b)); } return () -> bools; }).seq((seq, res) -> { StringBuilder sb = new StringBuilder(); sb.append(label); sb.append('\n'); for (DRes<Boolean> entry : res) { if (entry.out()) { sb.append(1); } else { sb.append(0); } } seq.debug().marker(sb.toString(), output); return null; }); } }
@Override public void test() throws Exception { Boolean[] comp1 = new Boolean[] {false, true, false, true, false}; Boolean[] comp2 = new Boolean[] {false, true, true}; Application<List<Boolean>, ProtocolBuilderBinary> app = producer -> producer.seq(seq -> { List<DRes<SBool>> in1 = BooleanHelper.known(comp1, seq.binary()); List<DRes<SBool>> in2 = BooleanHelper.known(comp2, seq.binary()); DRes<SBool> res1 = seq.comparison().greaterThan(in1, in2); DRes<Boolean> open1 = seq.binary().open(res1); return () -> Collections.singletonList(open1); }).seq( (seq, opened) -> () -> opened.stream().map(DRes::out).collect(Collectors.toList())); try { runApplication(app); } catch (Exception e) { if (!(e.getCause() instanceof IllegalArgumentException)) { throw e; } } } };
@Override public DRes<List<Pair<List<DRes<SBool>>, List<DRes<SBool>>>>> buildComputation( ProtocolBuilderBinary builder) { return builder.par(seq -> { DRes<SBool> comparison = seq.comparison().greaterThan(leftKey, rightKey); xorKey = leftKey.stream().map(e -> seq.binary().xor(e, rightKey.get(leftKey.indexOf(e)))) .collect(Collectors.toList()); leftValue.stream().map(e -> seq.binary().xor(e, rightValue.get(leftValue.indexOf(e)))) .collect(Collectors.toList()); return () -> comparison; .map(e -> par.advancedBinary().condSelect(data, e, rightValue.get(leftValue.indexOf(e)))) .collect(Collectors.toList()); .map(e -> par.advancedBinary().condSelect(data, e, rightKey.get(leftKey.indexOf(e)))) .collect(Collectors.toList()); }).par((par, data) -> { List<DRes<SBool>> lastValue = xorValue.stream().map(e -> par.binary().xor(e, data.getSecond().get(xorValue.indexOf(e)))) .collect(Collectors.toList()); xorKey.stream().map(e -> par.binary().xor(e, data.getFirst().get(xorKey.indexOf(e)))) .collect(Collectors.toList());
@Override public DRes<SBool> buildComputation(ProtocolBuilderBinary builder) { return builder.par(par -> { AdvancedBinary bb = par.advancedBinary(); List<DRes<SBool>> xnors = new ArrayList<>(); for (int i = 0; i < length; i++) { int size = input.size() % 2 + input.size() / 2; IterationState is = new IterationState(size, seq.par(par -> { List<DRes<SBool>> ands = new ArrayList<>(); int idx = 0; while (idx < input.size() - 1) { ands.add(par.binary().and(input.get(idx), input.get(idx + 1))); idx += 2;
@Override public DRes<SBool> and(DRes<SBool> left, DRes<SBool> right) { TinyTablesANDProtocol p = new TinyTablesANDProtocol(getNextId(), left, right); builder.append(p); return p; } };
@Override public DRes<List<DRes<SBool>>> buildComputation(ProtocolBuilderBinary builder) { return builder.seq(seq -> { int idx = this.lefts.size() - 1; List<DRes<SBool>> res = new ArrayList<>(); for (DRes<SBool> right : rights) { res.add(seq.binary().and(lefts.get(idx), right)); res.add(seq.binary().and(lefts.get(idx), right)); res.add(seq.binary().known(false)); tmp.add(0, seq.binary().known(false)); seq.advancedBinary().fullAdder(state.value.out(), res, seq.binary().known(false))); return is;
@Override public DRes<List<List<Boolean>>> buildComputation(ProtocolBuilderBinary producer) { return producer.seq(seq -> { Binary bin = seq.binary(); List<DRes<List<SBool>>> aesResults = new ArrayList<>(); for (List<DRes<SBool>> set : inputs.set1) { aesResults.add(par.bristol().AES(set, inputs.commonKey)); aesResults.add(par.bristol().AES(set, inputs.commonKey)); output.add(innerOut); for (SBool b : bs) { innerOut.add(seq.binary().open(b));
@Override public void test() throws Exception { Application<Boolean, ProtocolBuilderBinary> app = producer -> producer.seq(seq -> { DRes<SBool> in = seq.binary().input(true, 2); DRes<Boolean> open = seq.binary().open(in); return open; }).seq((seq, out) -> { return () -> out; }); boolean output = runApplication(app); if (doAsserts) { Assert.assertEquals(true, output); } } };
@Override public void test() throws Exception { Application<Void, ProtocolBuilderBinary> app = producer -> producer.seq(seq -> { List<DRes<SBool>> toPrint = BooleanHelper.known(new Boolean[] {true, false, false, true}, seq.binary()); return () -> toPrint; }).seq((seq, inputs) -> { seq.debug().openAndPrint("test", inputs); seq.debug().marker("test"); return null; }); runApplication(app); } };
@Override public void test() throws Exception { Boolean[] comp1 = new Boolean[] {false, true, false, true, false}; Boolean[] comp2 = new Boolean[] {false, true, true, true, false}; Application<List<Boolean>, ProtocolBuilderBinary> app = producer -> producer.seq(seq -> { List<DRes<SBool>> in1 = BooleanHelper.known(comp1, seq.binary()); List<DRes<SBool>> in2 = BooleanHelper.known(comp2, seq.binary()); DRes<SBool> res1 = seq.comparison().equal(in1, in2); DRes<SBool> res2 = seq.comparison().equal(in1, in1); DRes<Boolean> open1 = seq.binary().open(res1); DRes<Boolean> open2 = seq.binary().open(res2); return () -> Arrays.asList(open1, open2); }).seq( (seq, opened) -> () -> opened.stream().map(DRes::out).collect(Collectors.toList())); List<Boolean> res = runApplication(app); if (doAsserts) { Assert.assertEquals(false, res.get(0)); Assert.assertEquals(true, res.get(1)); } } };
@Override public DRes<Pair<SBool, SBool>> buildComputation(ProtocolBuilderBinary builder) { return builder.par(par -> { xor1 = par.binary().xor(valueA, valueB); and1 = par.binary().and(valueA, valueB); return () -> (par); }).par((par, pair) -> { xor2 = par.binary().xor(xor1, carry); and2 = par.binary().and(xor1, carry); return () -> (par); }).par((par, pair) -> { xor3 = par.binary().xor(and2, and1); return () -> new Pair<SBool, SBool>(xor2.out(), xor3.out()); }); } }
@Override public DRes<List<DRes<SBool>>> buildComputation(ProtocolBuilderBinary builder) { List<DRes<SBool>> result = new ArrayList<DRes<SBool>>(); return builder.seq(seq -> { int idx = base.size() - 1; IterationState is = new IterationState(idx, seq.advancedBinary().oneBitHalfAdder(base.get(idx), increment)); return is; }).whileLoop((state) -> state.round >= 1, (seq, state) -> { int idx = state.round - 1; result.add(0, state.value.out().getFirst()); IterationState is = new IterationState(idx, seq.advancedBinary().oneBitHalfAdder(base.get(idx), state.value.out().getSecond())); return is; }).seq((seq, state) -> { result.add(0, state.value.out().getFirst()); result.add(0, state.value.out().getSecond()); return () -> result; }); }