int main(int c, char **a) { OE oe = OperatingEnvironment_LinuxNew(); init_polynomial(); if (oe) { MR mr = 0; int i = 0; MiniMacs mm = setup_generic_minimacs(oe, a[1]); if (!mm) return -42; mm->get_id(); mr = mm->connect("127.0.0.1",2020); mm->init_heap(6); C(mm->secret_input(0,0,0)); C(mm->open(0)); C(mm->secret_input(0,1,Data_shallow("\001\001",1))); C(mm->secret_input(0,2,Data_shallow("\002\002",1))); for(i = 0; i < 256;++i) C(mm->mul(3,2,1)); C(mm->add(3,3,3)); C(mm->open(3)); oe->p(mm->heap_get(0)->codeword); _p("Mul res", mm->heap_get(3)->codeword,8,8); } failure: return 0; }
static int tokenize_file(OE oe) { uint lbuffer = 1060365; uint ands = 0, xors = 0, invs = 0, nums = 0, tokens = 0; Tokenizer tk = 0; Token tok = {0}; byte * buffer = oe->getmem(lbuffer); uint fp = 0; oe->open("file ../test/AES",&fp); oe->read(fp,buffer,&lbuffer); oe->close(fp); tk = FunCallTokenizer_New(oe); tk->start(buffer,lbuffer); do { tk->nextToken(&tok); if (tok.type == INV) invs += 1; if (tok.type == AND) ands +=1; if (tok.type == XOR) xors += 1; if (tok.type == NUM) nums += 1; tokens++; } while(tok.type != DONE); DBG_P(oe,"\nANDS: %u\nXORS: %u\nINVS: %u\nNUMS: %u\nTOKENS: %u\n",ands,xors,invs,nums,tokens); oe->putmem(buffer); return ands == 6800 && xors == 24448 && nums == 139136 && tokens == 172076 && invs == 1691; }
static XORcommitResult xor_commit(OE oe, Rnd rnd, CommitmentScheme cs, byte * circuit_string, uint lcircuit_string) { XORcommitResult res = oe->getmem(sizeof(*res)); uint i = 0; Data m0cmt = 0, m1cmt = 0; res->m0 = oe->getmem(lcircuit_string); res->m1 = oe->getmem(lcircuit_string); rnd->rand(res->m0,lcircuit_string); UserReport(oe,"m0 at random: "); print_bit_string(oe,Data_shallow(res->m0,lcircuit_string)); res->lm0 = lcircuit_string; res->lm1 = lcircuit_string; for(i = 0; i < lcircuit_string; ++i) { res->m1[i] = circuit_string[i] ^ res->m0[i]; } // white box usage of commit we use 64 bits per commitment (sha512 with random value of length 16 bytes) res->commitment = oe->getmem(128); res->lcommitment = 128; m0cmt = cs->commit(Data_shallow(res->m0,res->lm0)); m1cmt = cs->commit(Data_shallow(res->m1,res->lm1)); mcpy(m0cmt->data,res->commitment,m0cmt->ldata > 64 ? 64 : m0cmt->ldata); mcpy(m1cmt->data,res->commitment+64,m1cmt->ldata > 64 ? 64 : m1cmt->ldata); xor_commit_end: Data_destroy(oe,&m0cmt); Data_destroy(oe,&m1cmt); return res; }
void MapEntry_destroy(MapEntry * entry) { OE oe = 0; if (!entry) return; if (!*entry) return; oe = (*entry)->oe; oe->putmem(*entry); }
// ------------------------------------------------------------ Cmaphore Cmaphore_new(OE oe, uint count) { Cmaphore res = (Cmaphore)oe->getmem(sizeof(*res)); if (!res) return 0; oe->newmutex(&(res->lock)); res->count = count; res->oe = oe; return res; };
int main(int c, char **a) { OE oe = OperatingEnvironment_New(); OE tmp = 0; Map hmap = HashMap_new(oe,hfn,cfn,5); hmap->put( (void*)(ull)1,oe); if (hmap->size() == 1) oe->p("Testing size one [ok]"); else oe->p("Testing size one [fail]"); tmp = (OE)hmap->get( (void *)(ull)1 ); if (tmp == oe) oe->p("Testing get [ok]"); else oe->p("Testing get [fail]"); tmp = hmap->rem( (void*)(ull) 1); if (tmp == oe) oe->p("Testing rem return [ok]"); else oe->p("Testing rem return [fail]"); if (hmap->size() == 0) oe->p("Testing rem size [ok]"); else {oe->p("Testing rem size [fail]"); } OperatingEnvironment_Destroy( &oe ); return 0; }
/*! * {n} is the nth root number and f has that degree. * * {f} is an n vector of gf2^8 elements * * {omega} is root of unity of length n with 1, omega, omega^2 etc... * * TODO(rwl): * * I) Optimize the 1 of the omega list no need to have it there. * II) */ void txt_book_fft(OE oe, uint n, byte * f, byte * omegas, byte * result) { byte * r0 = 0; byte * r1star = 0; byte * new_omegas = 0; byte * r0_result = 0; byte * r1star_result = 0; uint idx = 0; byte omega = omegas[1]; byte q = 0; // base case if (n == 1) { result[0] = f[0]; return; } r0 = oe->getmem(n/2+1); r1star = oe->getmem(n/2+1); // divide for(idx = 0;idx<n/2;++idx) { q = add(f[idx],f[idx+n/2]); r0[idx] = q; r1star[idx] = multiply(q,omegas[idx]); } if (n%2) { r0[n/2] = f[n/2]; r1star[n/2]=multiply(f[n/2],omegas[n/2]); } new_omegas = oe->getmem(n); r0_result = oe->getmem(n); r1star_result = oe->getmem(n); for(idx = 1;idx<n;++idx) { new_omegas[idx] = multiply(omegas[idx],omega); } new_omegas[idx] = 1; txt_book_fft(oe, n/2, r0, new_omegas, r0_result); txt_book_fft(oe, n/2, r1star, new_omegas,r1star_result); for(idx = 0;idx < 2*(n/2);idx+=2) { result[idx] = eval_pol(r0_result,n/2,omegas[idx/2]); result[idx+1]= eval_pol(r1star_result,n/2,omegas[idx/2]); } oe->putmem(r0); oe->putmem(r1star); oe->putmem(r0_result); oe->putmem(r1star_result); oe->putmem(new_omegas); return; }
int main(int c, char **a) { char * material = 0, * bdt_material=0; char * ip = "127.0.0.1"; uint count = 0, i = 0; OE oe = OperatingEnvironment_LinuxNew(); MiniMacs mm = 0; int * pids = 0; InitStats(oe); init_polynomial(); if (c < 3 || c > 5) { printf("multirun <material> <bdt_material> <count> [< server >]\n"); return -1; } if ( c >= 3 ) { material = a[1]; bdt_material = a[2]; } if (c >= 4) { count = atoi(a[3]); } if (c >= 5) { ip =a[4]; } // No bit encoder mm=BitWiseMulPar2MiniMacs_DefaultLoadNew(oe, material, bdt_material, False); if (!mm) { printf("Unable to create MiniMacs, see errors above.\n"); return -1; } printf("Multirun CAES\n"); printf("material taken from: %s\n",material); printf("ip: %s\n", ip); printf("count: %u\n",count); pids = (int*)oe->getmem(sizeof(int)*count); for( i = 0; i < count; ++i) { pids[i] = fork(); if (pids[i] == 0) { return run(ip,i,count,oe,mm); } } CHECK_POINT_S("TOTAL"); for(i = 0;i < count;++i) { wait(pids[i]); } CHECK_POINT_E("TOTAL"); PrintMeasurements(oe); }
int main(int c, char **a) { char * material = 0, * bdt_material=0; char * ip = "127.0.0.1"; uint count = 0, i = 0; OE oe = OperatingEnvironment_LinuxNew(); MiniMacs mm = 0; int * pids = 0; InitStats(oe); init_polynomial(); if (c < 3 || c > 5) { printf("multirun <material> <bdt_material> <count> [< server >]\n"); return -1; } if ( c >= 3 ) { material = a[1]; bdt_material = a[2]; } if (c >= 4) { count = atoi(a[3]); } if (c >= 5) { ip =a[4]; } // loads the preprocessing material making MiniMac ready mm=BitWiseMulPar2MiniMacs_DefaultLoadFFTNew(oe, material, bdt_material, True); printf("Multirun CAES\n"); printf("material taken from: %s\n",material); printf("ip: %s\n", ip); printf("count: %u\n",count); pids = (int*)oe->getmem(sizeof(int)*count); // create processes for parallel execution ({count} of them) for( i = 0; i < count; ++i) { pids[i] = fork(); if (pids[i] == 0) { return run(ip,i,count,oe,mm); } } // wait for everybody to complete CHECK_POINT_S("TOTAL"); for(i = 0;i < count;++i) { wait(pids[i]); } CHECK_POINT_E("TOTAL"); PrintMeasurements(oe); }
static int test_listen(OE oe) { uint fd = 0; oe->open("listen 2020",&fd); if (!fd) { oe->p("Unable to listen on port 2020"); return 0; } else { oe->p("Accepting connections on 2020"); return 1; } }
static int test_open_file(OE oe) { uint fd = 0; oe->open("file C:\\Users\\rwl\\test.txt",&fd); if (!fd) { oe->p("unable to open file test.txt"); return 0; } else { oe->p("test.txt successfully opened"); oe->close(fd); return 1; } }
int main(int c, char **a) { char * material = 0; char * ip = "127.0.0.1"; uint count = 0, i = 0; OE oe = OperatingEnvironment_LinuxNew(); MiniMacs mm = 0; int * pids = 0; InitStats(oe); init_polynomial(); if (c < 2 || c > 4) { printf("multirun <material> <count> [< server >]\n"); return -1; } if ( c >= 2 ) { material = a[1]; } if (c >= 3) { count = atoi(a[2]); } if (c >= 4) { ip =a[3]; } mm=GenericFFTMiniMacs_new(oe,a[1]); printf("Multirun CAES\n"); printf("material taken from: %s\n",material); printf("ip: %s\n", ip); printf("count: %u\n",count); pids = (int*)oe->getmem(sizeof(int)*count); for( i = 0; i < count; ++i) { pids[i] = fork(); if (pids[i] == 0) { return run(material,ip,i,oe,mm); } } CHECK_POINT_S("TOTAL"); for(i = 0;i < count;++i) { wait(pids[i]); } CHECK_POINT_E("TOTAL"); PrintMeasurements(oe); }
static int run(char * material, char * ip, uint count, OE oe, MiniMacs mm) { CArena mc = CArena_new(oe); MpcPeer mission_control = 0; mc->connect("87.104.238.146", 65000); mission_control = mc->get_peer(0); if (!mission_control) { oe->p("Failed connection to mission control. aborting.\n"); return -1; } if (mm->get_id() == 0) { mm->invite(1,2020+count); } else { if (mm->connect(ip,2020+count) != 0) { return 0; } } { byte key[128] = {0}; byte ptxt[128] = {0}; mpc_aes(mm,ptxt, key,mission_control); CArena_destroy(&mc); } PrintMeasurements(oe); return 0; }
void Aes_destroy(OE oe, Aes * aes) { if (!aes) return; if (!*aes) return; oe->putmem(*aes); *aes = 0; }
static void print_bit_string(OE oe, Data bs) { char buf[512] = {0}; uint idx = 0; uint i = 0; uint length = (sizeof(buf)-2) > bs->ldata*8 ? bs->ldata*8 : sizeof(buf)-2; AddCh(buf,'\n',idx); for(i = 0; i < bs->ldata*8 && idx < sizeof(buf)-2;++i) { byte bit = get_bit(bs->data,i); // add white space if (i > 0 && i % 24 == 0) { AddCh(buf,'\n',idx); } else { if (i > 0 && i % 8 == 0) { AddCh(buf,' ',idx); } } if (bit == 1) { AddCh(buf,'1',idx); } else { AddCh(buf,'0',idx); } } AddCh(buf,'\n',idx); AddCh(buf,0,idx); oe->p(buf); }
Observer Observer_DefaultNew( OE oe, void (*fn)(void *data) ) { Observer res = (Observer)oe->getmem(sizeof(*res)); if (!res) return 0; res->notify = fn; return res; }
int main(int c, char **a) { OE oe = (OE)OperatingEnvironment_LinuxNew(); MiniMacs mm = 0; Data input = 0; uint count=0,i=0; init_polynomial(); mm = GenericMiniMacs_DefaultLoadNew(oe,a[1]); InitStats(oe); if (!mm) { oe->p("Error could not create instance of MiniMacs"); OperatingEnvironment_LinuxDestroy(&oe); return -1; } { CliArg arg = (CliArg)oe->getmem(sizeof(*arg)); arg->file = a[2]; arg->oe = oe; oe->newthread(client,arg); } mm->init_heap(2); mm->invite(1,8080); printf("Got client ... \n"); input = Data_new(oe, mm->get_ltext()); for(i = 0; i < mm->get_ltext(); ++i) { input->data[i] = 'r'; } mm->secret_input(0,0,input); for(count = 0; count < COUNT; ++count) { CHECK_POINT_S("Mul server"); mm->mul(1,0,0); CHECK_POINT_E("Mul server"); } usleep(5); PrintMeasurements(oe); return 0; }
static int run(char * ip, uint myid, uint count, OE oe, MiniMacs mm) { CArena mc = CArena_new(oe); MpcPeer mission_control = 0; if (mc->connect("87.104.238.146", 65000).rc != 0) { oe->syslog(OSAL_LOGLEVEL_FATAL,"Failed to connect to the performance monitor."); return -1; }; mission_control = mc->get_peer(0); if (!mission_control) { oe->p("Failed connection to mission control. aborting.\n"); return -1; } if (mm->get_id() == 0) { if (mm->invite(1,2020+myid) != 0) { byte d[256] = {0}; char m[128] = {0}; osal_sprintf(m,"Failed to invite %u peers on port %u",1,2020+myid); oe->p(m); i2b(myid, d); osal_sprintf(d+4,"error"); mission_control->send(Data_shallow(d,128)); return 0; } } else { if (mm->connect(ip,2020+myid) != 0) { char m[128] = {0}; osal_sprintf(m,"Failed to connect to peer %s:%u",ip,2020+myid); oe->p(m); return 0; } } { byte key[128] = {0}; byte ptxt[128] = {0}; mpc_aes(mm,ptxt, key,myid,count,mission_control); CArena_destroy(&mc); } PrintMeasurements(oe); return 0; }
void Cmaphore_destroy(Cmaphore * c) { OE oe = 0; Cmaphore s = 0; if (!c) return; if (!*c) return; s = *c; oe = s->oe; // inform all down's that we are destroying this semaphore oe->lock(s->lock); s->oe = 0; oe->unlock(s->lock); // give all other threads a chance to run. oe->yieldthread(); // Really now we destroy it ! oe->lock(s->lock); s->count = 0; oe->destroymutex(&s->lock); zeromem(s, sizeof(*s)); *c = 0; oe->putmem(s); }
static int openip(OE oe) { byte buf[64] = { 0 }; uint lbuf = sizeof(buf); uint fd = 0; RC rc = RC_OK; oe->open("ip 87.104.238.146 80",&fd); if (!fd) { oe->p("Failed to connect to IP-address 87.104.238.146 on port 80."); return 0; } else { uint written = 19; oe->write(fd, (byte*)"GET / HTTP/1.0\n\n\n", &written); if (written != 19) { oe->p("Failed to write 19 bytes to server at 87.104.238.146 port 80."); return 0; } rc = oe->read(fd, buf, &lbuf); oe->close(fd); if (rc != RC_OK) { if (lbuf != sizeof(buf)) { oe->p("Read too few bytes."); return 0; } } return 1; } }
static void FDEntry_destroy(OE oe, FDEntry * ent) { FDEntry e = 0; if (!ent) return; if (!*ent) return; e = *ent; oe->putmem(e); *ent = 0; }
int main(int c, char **a) { OE oe = OperatingEnvironment_LinuxNew(); CArena arena = CArena_new(oe); arena->listen(2020); while(!arena->get_no_peers()) { char m[32] = {0}; sprintf(m,"peer %d\n",arena->get_no_peers()); oe->p(m); sleep(1); } CArena_destroy( & arena ); OperatingEnvironment_LinuxDestroy(&oe); return 0; }
static Node NodeNew(OE oe, void * element, Node next) { Node res = oe->getmem(sizeof(*res)); if (!res) return res; res->element = element; res->next = next; return res; }
static uint FDEntry_new(OE oe, List entries, int osfd) { FDEntry r = (FDEntry)oe->getmem(sizeof(*r)); if (!r) return 0; r->osfd = osfd; r->fd = ++fd_pool; entries->add_element(r); return r->fd; }
static int test_large_file(OE oe) { _Bool ok = 1; uint lbuffer = 1060365; uint ands = 0, xors = 0, nums = 0, tokens = 0; Tokenizer tk = 0;CircuitParser cp = 0; List ast = 0; byte * buffer = oe->getmem(lbuffer); uint fp = 0, i = 0; CircuitVisitor cv = 0; Map input_gates = 0; List aoig = 0; DateTime clock = 0; ull start = 0; oe->open("file ../test/AES",&fp); cv = InputGateVisitor_New(oe); clock = DateTime_New(oe); start = clock->getMicroTime(); oe->read(fp,buffer,&lbuffer); oe->close(fp); DBG_P(oe,"reading file took %u microseconds",clock->getMicroTime()-start); tk = FunCallTokenizer_New(oe); cp = CircuitParser_New(oe,tk); start = clock->getMicroTime(); ast = cp->parseSource(buffer,lbuffer); DBG_P(oe,"parsing circuit took %u microseconds.",clock->getMicroTime()-start); oe->putmem(buffer); start = clock->getMicroTime(); input_gates = cv->visit(ast); AssertTrue(input_gates != 0) aoig = input_gates->get_keys(); if (aoig) { DBG_P(oe,"#Input: %u analysis took %u microseconds",aoig->size(),clock->getMicroTime()-start); } AssertTrue( aoig != 0 ); test_end: return ok; }
MapEntry MapEntry_new(OE oe, void * key, void * elm) { MapEntry ent = (MapEntry)oe->getmem(sizeof(*ent)); if (!ent) return 0; if (!oe) return 0; ent->key = key; ent->elm = elm; ent->oe = oe; return ent; }
bool run_test_suit(OE oe, TestSuit suit) { bool success = True; uint i = 0; uint j = 0; // indent suit name print out for (j = 0; j < indent_level; ++j) { oe->print(" "); } PRINT(oe,"[SUIT] %s",suit.name); // for each inferior test suit run it for(i = 0;i < suit.lsubs;++i) { __get_suit__ fn = suit.subs[i]; TestSuit * subsuit = (TestSuit *)fn(oe); if (subsuit) { indent_level++; run_test_suit(oe, *subsuit); indent_level--; } } // for each test in this suit run it. for(i = 0; i < suit.ltest;++i) { uint j = 0; Test t = suit.tests[i]; bool r = t.f(oe); for (j = 0; j < indent_level; ++j) { oe->print(" "); } if (r == True) { PRINT(oe, "[TEST] %s [OK]",suit.tests[i].name); } else { PRINT(oe, "[TEST] %s [FAILED]",suit.tests[i].name); success = False; } } return success; }
static void * client(void * a) { CliArg arg = (CliArg)a; OE oe = arg->oe; MiniMacs mm = GenericMiniMacs_DefaultLoadNew(oe,arg->file); uint count = 0; if (!mm) { oe->p("Client: Error could not create instance of MiniMacs"); return 0; } mm->init_heap(2); mm->connect("127.0.0.1",8080); mm->secret_input(0,0,0); for(count = 0; count < COUNT; ++count) { CHECK_POINT_S("\033[01mMul Client\033[00m"); mm->mul(1,0,0); CHECK_POINT_E("\033[01mMul Client\033[00m"); } }
static void print_aes(OE oe, Aes aes) { uint i = 0, j = 0; byte b[5*16+2] = {0}; b[0] = '\n'; b[5*16+1] = '\n'; for(i = 0;i < 4;++i) { for(j = 0; j < 4;++j) { if (j == 3) osal_sprintf(b+16*i+j*4+1," %2x\n",aes->state[j+4*i]); else osal_sprintf(b+16*i+j*4+1," %2x ",aes->state[j+4*i]); } } oe->p(b); }
static int test_read(OE oe) { byte buf[10] = { 0 }; uint lbuf = sizeof(buf); uint fd = 0; oe->open("file .\\test.txt",&fd); if (oe->read(fd, buf, &lbuf) == RC_OK && lbuf == sizeof(buf)) { oe->p("Successfully read 10 bytes"); oe->close(fd); return 1; } else { oe->p("Failed to read 10 bytes from test.txt"); oe->close(fd); return 0; } }