void zzn2_sqr(_MIPD_ zzn2 *x,zzn2 *w) { #ifdef MR_COUNT_OPS fpa+=3; fpc+=2; #endif modadd(x->a->w,x->b->w,mr_mip->w1->w); modsub(x->a->w,x->b->w,mr_mip->w2->w); modmult(x->a->w,x->b->w,w->b->w); modmult(mr_mip->w1->w,mr_mip->w2->w,w->a->w); modadd(w->b->w,w->b->w,w->b->w); w->a->len=2; w->b->len=2; }
int main(int argc, char *argv[]) { setIO("sample"); int n = gi,k = gi; c[0][0] = 1; if((n-k)&1){ puts("0"); closeIO();return 0; } k = (n-k)/2+k; for(int i = 0;i<n;++i) a[i] = gi; sort(a,a+n); for(int i = 0;i<n;++i) b[i] = gi; sort(b,b+n); for(int i = 1;i<=MN;++i) for(int j = 0;j<=i;++j) if(j == 0) c[i][0] = 1; else modadd(c[i][j],c[i-1][j],c[i-1][j-1]); t[0] = 1; for(int i = 1;i<=n;++i) modmut(t[i],t[i-1],i); for(int i = 0;i<n;++i) { if(i != 0) r[i] = r[i-1]; while(r[i] < n && b[r[i]] < a[i]) ++ r[i]; } f[0][0] = 1; for(int i = 0;i<n;++i) for(int j = 0;j<=i;++j) { if(f[i][j] == 0) continue; int64 tmp = 0; modmut(tmp,f[i][j],max(0LL,r[i]-j)); modadd(f[i+1][j+1],tmp,f[i+1][j+1]); modadd(f[i+1][j],f[i][j],f[i+1][j]); } for(int i = n;i>=0;--i){ modmut(f[n][i],f[n][i],t[n-i]); for(int j = i+1;j<=n+1;++j) { int64 tmp = 0; modmut(tmp,f[n][j],c[j][i]); modadd(f[n][i],f[n][i],-tmp); } } printf("%d\n",f[n][k]); closeIO(); return EXIT_SUCCESS; }
void zzn2_mul(_MIPD_ zzn2 *x,zzn2 *y,zzn2 *w) { #ifdef MR_COUNT_OPS fpa+=5; fpc+=3; #endif modmult(x->a->w,y->a->w,mr_mip->w1->w); modmult(x->b->w,y->b->w,mr_mip->w2->w); modadd(x->a->w,x->b->w,mr_mip->w5->w); modadd(y->a->w,y->b->w,w->b->w); modmult(w->b->w,mr_mip->w5->w,w->b->w); modsub(w->b->w,mr_mip->w1->w,w->b->w); modsub(w->b->w,mr_mip->w2->w,w->b->w); modsub(mr_mip->w1->w,mr_mip->w2->w,w->a->w); w->a->len=2; w->b->len=2; }
function modmult(a,b,N) { if (a > N) a = a%N; if (b > N) b = b%N; if (a*b <= 9007199254740991) { return ((a*b)%N); } else { if (b > a) return modmult(b,a,N); // Right to left binary multiplication var t = 0; var f = a; while (b > 1) { if ((b & 1) == 1) t = modadd(t,f,N); b = Math.floor(b/2); f = modadd(f,f,N); }; t = modadd(t,f,N); return t; } }
void zzn2_add(_MIPD_ zzn2 *x,zzn2 *y,zzn2 *w) { #ifdef MR_OS_THREADS miracl *mr_mip=get_mip(); #endif if (mr_mip->ERNUM) return; MR_IN(159) #ifdef P64_1271 #ifdef MR_COUNT_OPS fpa+=2; #endif modadd(x->a->w,y->a->w,w->a->w); modadd(x->b->w,y->b->w,w->b->w); w->a->len=2; w->b->len=2; #else nres_modadd(_MIPP_ x->a,y->a,w->a); nres_modadd(_MIPP_ x->b,y->b,w->b); #endif MR_OUT }
bool CMTSumCheckVerifier:: doRound(MPZVector& newExpected, CMTSumCheckResults& results, int round, MPZVector& roundPoly, const MPZVector& expected) { mpz_t& newRand = results.rand[round]; prng.get_random(newRand, prime); requestPoly(roundPoly, newRand); results.totalCheckTime.begin_with_history(); bool success = true; #ifndef CMTGKR_DISABLE_CHECKS for (int b = 0; b < conf.batchSize(); b++) { modadd(tmp, roundPoly[b * polySize], roundPoly[b * polySize + 1], prime); success = mpz_cmp(expected[b], tmp) == 0; if (!success) { cout << "[Instance " << b << "] [SumCheck] Round " << round << " sumcheck failed!" << endl; gmp_printf("poly[0][0]+poly[0][1] != ri\n"); gmp_printf("poly[0] is: %Zd\n", roundPoly[b * polySize]); gmp_printf("poly[1] is: %Zd\n", roundPoly[b * polySize + 1]); gmp_printf("actual is: %Zd\n", tmp); gmp_printf("expected is: %Zd\n", expected[b]); cout << endl; break; } } #endif results.totalCheckTime.end(); if (success) { MPZVector weights(polySize); results.setupTime.begin_with_history(); bary_precompute_weights3(weights, newRand, prime); results.setupTime.end(); results.totalCheckTime.begin_with_history(); bary_extrap(newExpected, roundPoly, weights, prime); results.totalCheckTime.end(); } return success; }
int main(int argc, char *argv[]) { int incpus,ncpus,cpuid,modidx,width,y,x,nextx,tsizelen; uint64_t msize,modulus,nin; char c,*tsizearg,inbase[64]; uint64_t skipunder, nnewillcnt, newstates[3]; int i,nnew,noutfiles; statebuf *mb; statecnt sn; jtset *jts; goin *gin; goout *go; assert(sizeof(uint64_t)==8); if (argc!=9 && argc!=10) { printf("usage: %s width modulo_index y x incpus ncpus cpuid maxtreesize[kKmMgG] [lastfile]\n",argv[0]); exit(1); } setwidth(width = atoi(argv[1])); modidx = atoi(argv[2]); if (modidx < 0 || modidx >= NMODULI) { printf ("modulo_index %d not in range [0,%d)\n", modidx, NMODULI); exit(1); } modulus = -(uint64_t)modulusdeltas[modidx]; y = atoi(argv[3]); x = atoi(argv[4]); nextx = (x+1) % width; incpus = atoi(argv[5]); ncpus = atoi(argv[6]); if (ncpus < 1 || ncpus > MAXCPUS) { printf ("#cpus %d not in range [0,%d]\n", ncpus, MAXCPUS); exit(1); } cpuid = atoi(argv[7]); if (cpuid < 0 || cpuid >= ncpus) { printf("cpuid %d not in range [0,%d]\n", ncpus, ncpus-1); exit(1); } tsizelen = strlen(tsizearg = argv[8]); if (!isdigit(c = tsizearg[tsizelen-1])) tsizearg[tsizelen-1] = '\0'; msize = atol(tsizearg); if (c == 'k' || c == 'K') msize *= 1000LL; if (c == 'm' || c == 'M') msize *= 1000000LL; if (c == 'g' || c == 'G') msize *= 1000000000LL; if (msize < 1000000LL) { printf("memsize %jd too small for comfort.\n", (intmax_t)msize); exit(1); } if (argc > 9) { assert(sscanf(argv[9],"%d.%lo", &noutfiles, &skipunder) == 2); noutfiles++; printf("skipping %d output files and states up to %jo\n", noutfiles, (uintmax_t)skipunder); skipunder++; } else { noutfiles = 0; skipunder = 0L; } sprintf(inbase,"%d.%d/yx.%02d.%02d",width,modidx,y,x); gin = openstreams(inbase, incpus, ncpus, cpuid, modulus, skipunder); go = goinit(width, modidx, modulus, y+!nextx, nextx, ncpus, cpuid); nnewillcnt = nin = 0LL; jts = jtalloc(msize, modulus, LOCBITS); if (nstreams(gin)) { for (; (mb = minstream(gin))->state != FINALSTATE; nin++,deletemin(gin)) { sn.cnt = mb->cnt; // printf("expanding %jo\n", (uintmax_t)mb->state); nnew = expandstate(mb->state, x, newstates); for (i=0; i<nnew; i++) { sn.state = newstates[i]; //printf("inserting %jo\n", (uintmax_t)sn.state); jtinsert(jts, &sn); } if (nnew < 3) // nnew == 2 modadd(modulus, &nnewillcnt, mb->cnt); if (jtfull(jts)) dumpstates(go, jts, noutfiles++, mb->state); } } dumpstates(go, jts, noutfiles++, FINALSTATE); jtfree(jts); printf("(%d,%d) size %ju xsize %ju mod ",y,x,(uintmax_t)nin,(uintmax_t)totalread(gin)); if (modulus) printf("%ju",(uintmax_t)modulus); else printf("18446744073709551616"); printf("\nnewillegal %ju ", (uintmax_t)nnewillcnt); printf( "needy %ju ", (uintmax_t)needywritten(go)); printf( "legal %ju ", (uintmax_t)legalwritten(go)); printf("at (%d,%d)\n",y+(nextx==0),nextx); assert(cpuid != 0 || skipunder != 0L || nin > 0); // crash if cpu0 processes no states return 0; }