void share(char*fn) { int i; mpz_t x,k,t,e; byte rnd[16]; char buf[1024]; FILE *f; f=fopen(fn,"w+"); if(f==NULL){ fprintf(stderr,"Unable to open file %s.\n",fn); return; } /*generate random x*/ mpz_init(x); for(i=0;i<16;i++)rnd[i]=randbyte(); setbin(x,rnd,16); /*calculate k*/ mpz_init(k); mpz_init(t); mpz_init(e); for(i=0;i<n;i++){ /*k=k + kvec[i]*x^i*/ mpz_powm(t,x,e,p); mpz_mul(t,t,kvec[i]); mpz_add(k,k,t); mpz_mod(k,k,p); /*incr e so that it matches the next i*/ mpz_add_ui(e,e,1); } fprintf(f,"%s\n",mpz_get_str(buf,16,p)); fprintf(f,"%s\n",mpz_get_str(buf,16,x)); fprintf(f,"%s\n",mpz_get_str(buf,16,k)); mpz_set_ui(e,0); mpz_set_ui(k,0); for(i=0;i<n;i++){ /*k=k + hvec[i]*x^i*/ mpz_powm(t,x,e,p); mpz_mul(t,t,hvec[i]); mpz_add(k,k,t); mpz_mod(k,k,p); /*incr e so that it matches the next i*/ mpz_add_ui(e,e,1); } fprintf(f,"%s\n",mpz_get_str(buf,16,k)); fclose(f); mpz_clear(x); mpz_clear(k); mpz_clear(t); mpz_clear(e); }
void sharevector(int n_) { int i,j; byte hdl[17]; n=n_; /*intialize vector and p*/ kvec=malloc(n*sizeof(mpz_t)); hvec=malloc(n*sizeof(mpz_t)); for(i=0;i<n;i++){ mpz_init(kvec[i]); mpz_init(hvec[i]); if(i==0){ setbin(kvec[0],key,16); setbin(hvec[0],hashv,16); printf("Setting key: %s\n",key_prints(0,key)); printf("Setting hash: %s\n",key_prints(0,hashv)); }else{ for(j=0;j<16;j++) hdl[j]=randbyte(); setbin(kvec[i],hdl,16); for(j=0;j<16;j++) hdl[j]=randbyte(); setbin(hvec[i],hdl,16); } } mpz_init(p); /*generate random prime*/ hdl[0]=1; for(i=0;i<500;i++){ for(j=1;j<17;j++) hdl[j]=randbyte(); hdl[16]|=1; setbin(p,hdl,17); j=mpz_probab_prime_p(p,100); if(j)break; } if(!j){ fprintf(stderr,"Unable to generate a good prime. Giving up.\n"); exit(1); } }
/* * Find the best relation op for matching the two trees it has. * This is a sub-version of the function findops() above. * The instruction with the lowest grading is emitted. * * Level assignment for priority: * left right prio * - - - * direct direct 1 * direct OREG 2 # make oreg * OREG direct 2 # make oreg * OREG OREG 2 # make both oreg * direct REG 3 # put in reg * OREG REG 3 # put in reg, make oreg * REG direct 3 # put in reg * REG OREG 3 # put in reg, make oreg * REG REG 4 # put both in reg */ int relops(NODE *p) { extern int *qtable[]; struct optab *q; int i, shl = 0, shr = 0, sh; NODE *l, *r; int *ixp, idx = 0; int lvl = 10, gol = 0, gor = 0; F2DEBUG(("relops tree:\n")); F2WALK(p); l = getlr(p, 'L'); r = getlr(p, 'R'); ixp = qtable[p->n_op]; for (i = 0; ixp[i] >= 0; i++) { q = &table[ixp[i]]; F2DEBUG(("relops: ixp %d\n", ixp[i])); if (!acceptable(q)) /* target-dependent filter */ continue; if (ttype(l->n_type, q->ltype) == 0 || ttype(r->n_type, q->rtype) == 0) continue; /* Types must be correct */ F2DEBUG(("relops got types\n")); if ((shl = chcheck(l, q->lshape, 0)) == SRNOPE) continue; F2DEBUG(("relops lshape %d\n", shl)); F2WALK(p); if ((shr = chcheck(r, q->rshape, 0)) == SRNOPE) continue; F2DEBUG(("relops rshape %d\n", shr)); F2WALK(p); if (q->needs & REWRITE) break; /* Done here */ if (lvl <= (shl + shr)) continue; lvl = shl + shr; idx = ixp[i]; gol = shl; gor = shr; } if (lvl == 10) { F2DEBUG(("relops failed\n")); if (setbin(p)) return FRETRY; return FFAIL; } F2DEBUG(("relops entry %d(%s %s)\n", idx, srtyp[gol], srtyp[gor])); q = &table[idx]; (void)shswitch(-1, p->n_left, q->lshape, INREGS, q->rewrite & RLEFT, gol); (void)shswitch(-1, p->n_right, q->rshape, INREGS, q->rewrite & RRIGHT, gor); sh = 0; if (q->rewrite & RLEFT) sh = ffs(q->lshape & INREGS)-1; else if (q->rewrite & RRIGHT) sh = ffs(q->rshape & INREGS)-1; F2DEBUG(("relops: node %p\n", p)); p->n_su = MKIDX(idx, 0); SCLASS(p->n_su, sh); return 0; }
/* * Find the best instruction to evaluate the given tree. * Best is to match both subnodes directly, second-best is if * subnodes must be evaluated into OREGs, thereafter if nodes * must be put into registers. * Whether 2-op instructions or 3-op is preferred is depending on in * which order they are found in the table. * mtchno is set to the count of regs needed for its legs. */ int findops(NODE *p, int cookie) { extern int *qtable[]; struct optab *q, *qq = NULL; int i, shl, shr, *ixp, sh; int lvl = 10, idx = 0, gol = 0, gor = 0; NODE *l, *r; F2DEBUG(("findops node %p (%s)\n", p, prcook(cookie))); F2WALK(p); ixp = qtable[p->n_op]; l = getlr(p, 'L'); r = getlr(p, 'R'); for (i = 0; ixp[i] >= 0; i++) { q = &table[ixp[i]]; F2DEBUG(("findop: ixp %d str %s\n", ixp[i], q->cstring)); if (!acceptable(q)) /* target-dependent filter */ continue; if (ttype(l->n_type, q->ltype) == 0 || ttype(r->n_type, q->rtype) == 0) continue; /* Types must be correct */ if ((cookie & q->visit) == 0) continue; /* must get a result */ F2DEBUG(("findop got types\n")); if ((shl = chcheck(l, q->lshape, q->rewrite & RLEFT)) == SRNOPE) continue; F2DEBUG(("findop lshape %s\n", srtyp[shl])); F2WALK(l); if ((shr = chcheck(r, q->rshape, q->rewrite & RRIGHT)) == SRNOPE) continue; F2DEBUG(("findop rshape %s\n", srtyp[shr])); F2WALK(r); /* Help register assignment after SSA by preferring */ /* 2-op insns instead of 3-ops */ if (xssa && (q->rewrite & RLEFT) == 0 && shl == SRDIR) shl = SRREG; if (q->needs & REWRITE) break; /* Done here */ if (lvl <= (shl + shr)) continue; lvl = shl + shr; qq = q; idx = ixp[i]; gol = shl; gor = shr; } if (lvl == 10) { F2DEBUG(("findops failed\n")); if (setbin(p)) return FRETRY; return FFAIL; } F2DEBUG(("findops entry %d(%s,%s)\n", idx, srtyp[gol], srtyp[gor])); sh = -1; #ifdef mach_pdp11 if (cookie == FORCC && p->n_op != AND) /* XXX - fix */ cookie = INREGS; #else if (cookie == FORCC) cookie = INREGS; #endif sh = shswitch(sh, p->n_left, qq->lshape, cookie, qq->rewrite & RLEFT, gol); sh = shswitch(sh, p->n_right, qq->rshape, cookie, qq->rewrite & RRIGHT, gor); if (sh == -1) { if (cookie == FOREFF || cookie == FORCC) sh = 0; else sh = ffs(cookie & qq->visit & INREGS)-1; } F2DEBUG(("findops: node %p sh %d (%s)\n", p, sh, prcook(1 << sh))); p->n_su = MKIDX(idx, 0); SCLASS(p->n_su, sh); return sh; }