123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- package protocols;
- import java.math.BigInteger;
- import communication.Communication;
- import crypto.Crypto;
- import exceptions.NoSuchPartyException;
- import oram.Forest;
- import oram.Metadata;
- import protocols.precomputation.PrePermuteIndex;
- import protocols.struct.Party;
- import protocols.struct.PreData;
- import util.M;
- import util.P;
- import util.Timer;
- import util.Util;
- public class PermuteIndex extends Protocol {
- private int pid = P.PI;
- public PermuteIndex(Communication con1, Communication con2) {
- super(con1, con2);
- }
- public void runE() {
- }
- public int[] runD(PreData predata, boolean firstTree, byte[][] ti, int w, Timer timer) {
- if (firstTree)
- return null;
- timer.start(pid, M.online_comp);
- byte[][] z = Util.xor(ti, predata.pi_p);
- timer.start(pid, M.online_write);
- con2.write(pid, z);
- timer.stop(pid, M.online_write);
- timer.start(pid, M.online_read);
- byte[][] g = con2.readDoubleByteArray();
- timer.stop(pid, M.online_read);
- ti = Util.xor(predata.pi_a, g);
- int logW = (int) Math.ceil(Math.log(w + 1) / Math.log(2));
- int[] ti_pp = new int[ti.length];
- for (int i = 0; i < ti.length; i++)
- ti_pp[i] = Util.getSubBits(new BigInteger(ti[i]), logW, 0).intValue();
- timer.stop(pid, M.online_comp);
- return ti_pp;
- }
- public void runC(PreData predata, boolean firstTree, Timer timer) {
- if (firstTree)
- return;
- timer.start(pid, M.online_comp);
- timer.start(pid, M.online_read);
- byte[][] z = con2.readDoubleByteArray();
- timer.stop(pid, M.online_read);
- z = Util.xor(z, predata.pi_r);
- z = Util.permute(z, predata.evict_pi);
- byte[][] g = Util.xor(predata.evict_rho, z);
- timer.start(pid, M.online_write);
- con2.write(pid, g);
- timer.stop(pid, M.online_write);
- timer.stop(pid, M.online_comp);
- }
- // for testing correctness
- @Override
- public void run(Party party, Metadata md, Forest forest) {
- Timer timer = new Timer();
- for (int i = 0; i < 100; i++) {
- System.out.println("i=" + i);
- PreData predata = new PreData();
- PrePermuteIndex prepermuteindex = new PrePermuteIndex(con1, con2);
- if (party == Party.Eddie) {
- int d = Crypto.sr.nextInt(15) + 5;
- int w = Crypto.sr.nextInt(15) + 5;
- int logW = (int) Math.ceil(Math.log(w + 1) / Math.log(2));
- byte[][] ti = new byte[d][];
- predata.evict_pi = Util.randomPermutation(d, Crypto.sr);
- predata.evict_rho = new byte[d][];
- for (int j = 0; j < d; j++) {
- ti[j] = Util.nextBytes((logW + 7) / 8, Crypto.sr);
- predata.evict_rho[j] = Util.nextBytes((logW + 7) / 8, Crypto.sr);
- }
- con1.write(predata.evict_pi);
- con1.write(predata.evict_rho);
- con1.write(ti);
- con1.write(w);
- con2.write(predata.evict_pi);
- con2.write(predata.evict_rho);
- prepermuteindex.runE(predata, d, w, timer);
- runE();
- int[] ti_pp = con1.readIntArray();
- ti = Util.permute(ti, predata.evict_pi);
- int j = 0;
- for (; j < d; j++) {
- int tmp = Util.getSubBits(new BigInteger(Util.xor(predata.evict_rho[j], ti[j])), logW, 0)
- .intValue();
- if (tmp != ti_pp[j]) {
- System.err.println("PermuteIndex test failed");
- break;
- }
- }
- if (j == d)
- System.out.println("PermuteIndex test passed");
- } else if (party == Party.Debbie) {
- predata.evict_pi = con1.readIntArray();
- predata.evict_rho = con1.readDoubleByteArray();
- byte[][] ti = con1.readDoubleByteArray();
- int w = con1.readInt();
- prepermuteindex.runD(predata, timer);
- int[] ti_pp = runD(predata, false, ti, w, timer);
- con1.write(ti_pp);
- } else if (party == Party.Charlie) {
- predata.evict_pi = con1.readIntArray();
- predata.evict_rho = con1.readDoubleByteArray();
- prepermuteindex.runC(predata, timer);
- runC(predata, false, timer);
- } else {
- throw new NoSuchPartyException(party + "");
- }
- }
- // timer.print();
- }
- }
|