Пример #1
0
  ExecStatus
  Pow<A,B>::propagate(Space& home, const ModEventDelta&) {
    if ((x0.min() == 0.0) && (x0.max() == 0.0)) return ES_FAILED;
    GECODE_ME_CHECK(x1.eq(home,pow(x0.domain(),m_n)));

    if ((x1.min() == 0.0) && (x1.max() == 0.0)) {
      GECODE_ME_CHECK(x1.eq(home,0.0));
      return home.ES_SUBSUMED(*this);
    }
    
    if ((m_n % 2) == 0)
    {
      if (x0.min() >= 0)
        GECODE_ME_CHECK(x0.eq(home,nroot(x1.domain(),m_n)));
      else if (x0.max() <= 0)
        GECODE_ME_CHECK(x0.eq(home,-nroot(x1.domain(),m_n)));
      else
        GECODE_ME_CHECK(x0.eq(home,
                              hull(
                                  nroot(x1.domain(),m_n),
                                  -nroot(x1.domain(),m_n)
                              )
                        ));
    } else
      GECODE_ME_CHECK(x0.eq(home,nroot(x1.domain(),m_n)));
    return x0.assigned() ? home.ES_SUBSUMED(*this) : ES_FIX;
  }
Пример #2
0
  ExecStatus
  Pow<A,B>::post(Home home, A x0, B x1, int n) {
    if (n == 0) {
      if ((x0.min() == 0.0) && (x0.max() == 0.0)) return ES_FAILED;
      GECODE_ME_CHECK(x1.eq(home,1.0));
      return ES_OK;
    }

    GECODE_ME_CHECK(x1.eq(home,pow(x0.domain(),n)));
    if ((x1.min() == 0.0) && (x1.max() == 0.0)) {
      GECODE_ME_CHECK(x1.eq(home,0.0));
      return ES_OK;
    }
    
    if ((n % 2) == 0)
    {
      if (x0.min() >= 0)
        GECODE_ME_CHECK(x0.eq(home,nroot(x1.domain(),n)));
      else if (x0.max() <= 0)
        GECODE_ME_CHECK(x0.eq(home,-nroot(x1.domain(),n)));
      else
        GECODE_ME_CHECK(x0.eq(home,
                              hull(
                                  nroot(x1.domain(),n),
                                  -nroot(x1.domain(),n)
                              )
                        ));
    } else
      GECODE_ME_CHECK(x0.eq(home,nroot(x1.domain(),n)));

    if (!x0.assigned()) (void) new (home) Pow<A,B>(home,x0,x1,n);
    return ES_OK;
  }
Пример #3
0
/*
 * __BT_OPEN -- Open a btree.
 *
 * Creates and fills a DB struct, and calls the routine that actually
 * opens the btree.
 *
 * Parameters:
 *	fname:	filename (NULL for in-memory trees)
 *	flags:	open flag bits
 *	mode:	open permission bits
 *	b:	BTREEINFO pointer
 *
 * Returns:
 *	NULL on failure, pointer to DB on success.
 *
 */
DB *
__bt_open(const char *fname, int flags, mode_t mode, const BTREEINFO *openinfo,
    int dflags)
{
	struct stat sb;
	BTMETA m;
	BTREE *t;
	BTREEINFO b;
	DB *dbp;
	pgno_t ncache;
	ssize_t nr;
	size_t temp;
	int machine_lorder;

	t = NULL;

	/*
	 * Intention is to make sure all of the user's selections are okay
	 * here and then use them without checking.  Can't be complete, since
	 * we don't know the right page size, lorder or flags until the backing
	 * file is opened.  Also, the file's page size can cause the cachesize
	 * to change.
	 */
	machine_lorder = byteorder();
	if (openinfo) {
		b = *openinfo;

		/* Flags: R_DUP. */
		if (b.flags & ~(R_DUP))
			goto einval;

		/*
		 * Page size must be indx_t aligned and >= MINPSIZE.  Default
		 * page size is set farther on, based on the underlying file
		 * transfer size.
		 */
		if (b.psize &&
		    (b.psize < MINPSIZE || b.psize > MAX_PAGE_OFFSET + 1 ||
		    b.psize & (sizeof(indx_t) - 1)))
			goto einval;

		/* Minimum number of keys per page; absolute minimum is 2. */
		if (b.minkeypage) {
			if (b.minkeypage < 2)
				goto einval;
		} else
			b.minkeypage = DEFMINKEYPAGE;

		/* If no comparison, use default comparison and prefix. */
		if (b.compare == NULL) {
			b.compare = __bt_defcmp;
			if (b.prefix == NULL)
				b.prefix = __bt_defpfx;
		}

		if (b.lorder == 0)
			b.lorder = machine_lorder;
	} else {
		b.compare = __bt_defcmp;
		b.cachesize = 0;
		b.flags = 0;
		b.lorder = machine_lorder;
		b.minkeypage = DEFMINKEYPAGE;
		b.prefix = __bt_defpfx;
		b.psize = 0;
	}

	/* Check for the ubiquitous PDP-11. */
	if (b.lorder != BIG_ENDIAN && b.lorder != LITTLE_ENDIAN)
		goto einval;

	/* Allocate and initialize DB and BTREE structures. */
	if ((t = (BTREE *)malloc(sizeof(BTREE))) == NULL)
		goto err;
	memset(t, 0, sizeof(BTREE));
	t->bt_fd = -1;			/* Don't close unopened fd on error. */
	t->bt_lorder = b.lorder;
	t->bt_order = NOT;
	t->bt_cmp = b.compare;
	t->bt_pfx = b.prefix;
	t->bt_rfd = -1;

	if ((t->bt_dbp = dbp = (DB *)malloc(sizeof(DB))) == NULL)
		goto err;
	memset(t->bt_dbp, 0, sizeof(DB));
	if (t->bt_lorder != machine_lorder)
		F_SET(t, B_NEEDSWAP);

	dbp->type = DB_BTREE;
	dbp->internal = t;
	dbp->close = __bt_close;
	dbp->del = __bt_delete;
	dbp->fd = __bt_fd;
	dbp->get = __bt_get;
	dbp->put = __bt_put;
	dbp->seq = __bt_seq;
	dbp->sync = __bt_sync;

	/*
	 * If no file name was supplied, this is an in-memory btree and we
	 * open a backing temporary file.  Otherwise, it's a disk-based tree.
	 */
	if (fname) {
		switch (flags & O_ACCMODE) {
		case O_RDONLY:
			F_SET(t, B_RDONLY);
			break;
		case O_RDWR:
			break;
		case O_WRONLY:
		default:
			goto einval;
		}
		
		if ((t->bt_fd = open(fname, flags, mode)) == -1)
			goto err;
		if (fcntl(t->bt_fd, F_SETFD, FD_CLOEXEC) == -1)
			goto err;
	} else {
		if ((flags & O_ACCMODE) != O_RDWR)
			goto einval;
		if ((t->bt_fd = tmp()) == -1)
			goto err;
		F_SET(t, B_INMEM);
	}

	if (fcntl(t->bt_fd, F_SETFD, FD_CLOEXEC) == -1)
		goto err;

	if (fstat(t->bt_fd, &sb))
		goto err;
	if (sb.st_size) {
		if ((nr = read(t->bt_fd, &m, sizeof(BTMETA))) < 0)
			goto err;
		if (nr != sizeof(BTMETA))
			goto eftype;

		/*
		 * Read in the meta-data.  This can change the notion of what
		 * the lorder, page size and flags are, and, when the page size
		 * changes, the cachesize value can change too.  If the user
		 * specified the wrong byte order for an existing database, we
		 * don't bother to return an error, we just clear the NEEDSWAP
		 * bit.
		 */
		if (m.magic == BTREEMAGIC)
			F_CLR(t, B_NEEDSWAP);
		else {
			F_SET(t, B_NEEDSWAP);
			M_32_SWAP(m.magic);
			M_32_SWAP(m.version);
			M_32_SWAP(m.psize);
			M_32_SWAP(m.free);
			M_32_SWAP(m.nrecs);
			M_32_SWAP(m.flags);
		}
		if (m.magic != BTREEMAGIC || m.version != BTREEVERSION)
			goto eftype;
		if (m.psize < MINPSIZE || m.psize > MAX_PAGE_OFFSET + 1 ||
		    m.psize & (sizeof(indx_t) - 1))
			goto eftype;
		if (m.flags & ~SAVEMETA)
			goto eftype;
		b.psize = m.psize;
		F_SET(t, m.flags);
		t->bt_free = m.free;
		t->bt_nrecs = m.nrecs;
	} else {
		/*
		 * Set the page size to the best value for I/O to this file.
		 * Don't overflow the page offset type.
		 */
		if (b.psize == 0) {
			b.psize = sb.st_blksize;
			if (b.psize < MINPSIZE)
				b.psize = MINPSIZE;
			if (b.psize > MAX_PAGE_OFFSET + 1)
				b.psize = MAX_PAGE_OFFSET + 1;
		}

		/* Set flag if duplicates permitted. */
		if (!(b.flags & R_DUP))
			F_SET(t, B_NODUPS);

		t->bt_free = P_INVALID;
		t->bt_nrecs = 0;
		F_SET(t, B_METADIRTY);
	}

	t->bt_psize = b.psize;

	/* Set the cache size; must be a multiple of the page size. */
	if (b.cachesize && b.cachesize & (b.psize - 1))
		b.cachesize += (~b.cachesize & (b.psize - 1)) + 1;
	if (b.cachesize < b.psize * MINCACHE)
		b.cachesize = b.psize * MINCACHE;

	/* Calculate number of pages to cache. */
	ncache = (b.cachesize + t->bt_psize - 1) / t->bt_psize;

	/*
	 * The btree data structure requires that at least two keys can fit on
	 * a page, but other than that there's no fixed requirement.  The user
	 * specified a minimum number per page, and we translated that into the
	 * number of bytes a key/data pair can use before being placed on an
	 * overflow page.  This calculation includes the page header, the size
	 * of the index referencing the leaf item and the size of the leaf item
	 * structure.  Also, don't let the user specify a minkeypage such that
	 * a key/data pair won't fit even if both key and data are on overflow
	 * pages.
	 */
	temp = (t->bt_psize - BTDATAOFF) / b.minkeypage -
	    (sizeof(indx_t) + NBLEAFDBT(0, 0));
	_DBFIT(temp, indx_t);
	t->bt_ovflsize = (indx_t)temp;
	if (t->bt_ovflsize < NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(indx_t))
		t->bt_ovflsize =
		    NBLEAFDBT(NOVFLSIZE, NOVFLSIZE) + sizeof(indx_t);

	/* Initialize the buffer pool. */
	if ((t->bt_mp =
	    mpool_open(NULL, t->bt_fd, t->bt_psize, ncache)) == NULL)
		goto err;
	if (!F_ISSET(t, B_INMEM))
		mpool_filter(t->bt_mp, __bt_pgin, __bt_pgout, t);

	/* Create a root page if new tree. */
	if (nroot(t) == RET_ERROR)
		goto err;

	/* Global flags. */
	if (dflags & DB_LOCK)
		F_SET(t, B_DB_LOCK);
	if (dflags & DB_SHMEM)
		F_SET(t, B_DB_SHMEM);
	if (dflags & DB_TXN)
		F_SET(t, B_DB_TXN);

	return (dbp);

einval:	errno = EINVAL;
	goto err;

eftype:	errno = EFTYPE;
	goto err;

err:	if (t) {
		if (t->bt_dbp)
			free(t->bt_dbp);
		if (t->bt_fd != -1)
			(void)close(t->bt_fd);
		free(t);
	}
	return (NULL);
}
Пример #4
0
 if (n<0)
 {
     minus=TRUE;
     n=(-n);
 }
 s=exsign(w);
 if (n%2==0 && s==MINUS)
 {
     mr_berror(_MIPP_ MR_ERR_NEG_ROOT);
     MR_OUT
     return FALSE;
 }
 insign(PLUS,w);
 numer(_MIPP_ w,mr_mip->w8);
 denom(_MIPP_ w,mr_mip->w9);
 rn=nroot(_MIPP_ mr_mip->w8,n,mr_mip->w8);
 rm=nroot(_MIPP_ mr_mip->w9,n,mr_mip->w9);
 if (rn && rm)
 {
     fpack(_MIPP_ mr_mip->w8,mr_mip->w9,w);
     if (minus) frecip(_MIPP_ w,w);
     insign(s,w);
     MR_OUT
     return TRUE;
 }
 nm=size(mr_mip->w8);
 dn=size(mr_mip->w9);
 if (n==2 && ((nm<MR_TOOBIG) || rn) && ((dn<MR_TOOBIG) || rm))
 {
     if (!rn && nm<MR_TOOBIG)
     {
Пример #5
0
int main()
{  /*  encode using public key  */
    big e,m,y,ke,mn,mx;
    FILE *ifile;
    FILE *ofile;
    static char line[500];
    static char buff[256];
    char ifname[13],ofname[13];
    BOOL fli,last;
    int i,ipt,klen;
    mip=mirsys(100,0);
    e=mirvar(0);
    m=mirvar(0);
    y=mirvar(0);
    ke=mirvar(0);
    mn=mirvar(0);
    mx=mirvar(0);
    if ((ifile=fopen("public.key","rt"))==NULL)
    {
        printf("Unable to open file public.key\n");
        return 0;
    }
    mip->IOBASE=16;
    cinnum(ke,ifile);
    fclose(ifile);
    nroot(ke,3,mn);
    multiply(mn,mn,m);
    multiply(mn,m,mx);
    subtract(mx,m,mx);
    klen=0;
    copy(mx,m);
    while (size(m)>0)
    { /* find key length in characters */
        klen++;
        subdiv(m,128,m);
    }
    klen--;
    printf("file to be encoded = ");
    gets(ifname);
    fli=FALSE;
    if (strlen(ifname)>0) fli=TRUE;
    if (fli)
    { /* set up input file */
        strcpy(ofname,ifname);
        strip(ofname);
        strcat(ofname,".rsa");
        if ((ifile=fopen(ifname,"rt"))==NULL)
        {
            printf("Unable to open file %s\n",ifname);
            return 0;
        }
        printf("encoding message\n");
    }
    else
    { /* accept input from keyboard */
        ifile=stdin;
        do
        {
            printf("output filename = ");
            gets(ofname); 
        } while (strlen(ofname)==0);
        strip(ofname);    
        strcat(ofname,".rsa");
        printf("input message - finish with cntrl z\n");
    }
    ofile=fopen(ofname,"wt");
    ipt=0;
    last=FALSE;
    while (!last)
    { /* encode line by line */
        if (fgets(&line[ipt],132,ifile)==NULL) last=TRUE;
        if (line[ipt]==EOF) last=TRUE;
        ipt=strlen(line);
        if (ipt<klen && !last) continue;
        while (ipt>=klen)
        { /* chop up into klen-sized chunks and encode */
            for (i=0;i<klen;i++)
                buff[i]=line[i];
            buff[klen]='\0';
            for (i=klen;i<=ipt;i++)
                line[i-klen]=line[i];
            ipt-=klen;
            mip->IOBASE=128;
            cinstr(m,buff);
            power(m,3,ke,e);
            mip->IOBASE=16;
            cotnum(e,ofile);
        }
        if (last && ipt>0)
        { /* now deal with left overs */
            mip->IOBASE=128;
            cinstr(m,line);
            if (compare(m,mn)<0)
            { /* pad out with random number if necessary */
                bigrand(mn,y);
                multiply(mn,mn,e);
                subtract(e,y,e);
                multiply(mn,e,y);
                add(m,y,m);
            }
            power(m,3,ke,e);
            mip->IOBASE=16;
            cotnum(e,ofile);
        }
    }
    fclose(ofile);
    if (fli) fclose(ifile);
    return 0;
}   
Пример #6
0
 void c_dd_nroot(const double *a, int n, double *b) {
     dd_real bb;
     bb = nroot(dd_real(a), n);
     TO_DOUBLE_PTR(bb, b);
 }
Пример #7
0
 ExecStatus
 NthRoot<A,B>::propagate(Space& home, const ModEventDelta&) {
   GECODE_ME_CHECK(x1.eq(home,nroot(x0.domain(),m_n)));
   GECODE_ME_CHECK(x0.eq(home,pow(x1.domain(),m_n)));
   return x0.assigned() ? home.ES_SUBSUMED(*this) : ES_FIX;
 }
Пример #8
0
int main()
{  /*  decode using private key  */
    int i;
    big e,ep[NP],m,ke,kd,p[NP],kp[NP],mn,mx;
    FILE *ifile;
    FILE *ofile;
    char ifname[13],ofname[13];
    BOOL flo;
    big_chinese ch;
    mip=mirsys(100,0);
    for (i=0;i<NP;i++)
    {
        p[i]=mirvar(0);
        ep[i]=mirvar(0);
        kp[i]=mirvar(0);
    }
    e=mirvar(0);
    m=mirvar(0);
    kd=mirvar(0);
    ke=mirvar(0);
    mn=mirvar(0);
    mx=mirvar(0);
    mip->IOBASE=60;
    if ((ifile=fopen("private.key","r"))==NULL)
    {
        printf("Unable to open file private.key\n");
        return 0;
    }
    for (i=0;i<NP;i++)
    {
        cinnum(p[i],ifile);
    }
    fclose(ifile);
 /* generate public and private keys */
    convert(1,ke);
    for (i=0;i<NP;i++)
    {
        multiply(ke,p[i],ke);
    }
    for (i=0;i<NP;i++)
    { /* kp[i]=(2*(p[i]-1)+1)/3 = 1/3 mod p[i]-1 */
        decr(p[i],1,kd);
        premult(kd,2,kd);
        incr(kd,1,kd);
        subdiv(kd,3,kp[i]);
    }
    crt_init(&ch,NP,p);
    nroot(ke,3,mn);
    multiply(mn,mn,m);
    multiply(mn,m,mx);
    subtract(mx,m,mx);
    do
    { /* get input file */
        printf("file to be decoded = ");
        gets(ifname);
    } while (strlen(ifname)==0);
    strip(ifname);
    strcat(ifname,".rsa");
    printf("output filename = ");
    gets(ofname);
    flo=FALSE;
    if (strlen(ofname)>0) 
    { /* set up output file */
        flo=TRUE;
        ofile=fopen(ofname,"w");
    }
    printf("decoding message\n");
    if ((ifile=fopen(ifname,"r"))==NULL)
    {
        printf("Unable to open file %s\n",ifname);
        return 0;
    }
    forever
    { /* decode line by line */
        mip->IOBASE=60;
        cinnum(m,ifile);
        if (size(m)==0) break;
        for (i=0;i<NP;i++)
            powmod(m,kp[i],p[i],ep[i]);
        crt(&ch,ep,e);    /* Chinese remainder thereom */
        if (compare(e,mx)>=0) divide(e,mn,mn);
        mip->IOBASE=128;
        if (flo) cotnum(e,ofile);
        cotnum(e,stdout);
    }
    crt_end(&ch);
    fclose(ifile);
    if (flo) fclose(ofile);
    printf("message ends\n");
    return 0;
}
Пример #9
0
int main()
{
    FILE *fp;
    big q,p,p1,h,t,g,low,high;
    big pool[POOL_SIZE];
    BOOL fail;
    int i,j,p1bits,np;
    long seed,m,permutation;
    miracl *mip=mirsys(100,0);
    q=mirvar(0);
    p=mirvar(0);
    h=mirvar(0);
    t=mirvar(0);
    g=mirvar(0);
    p1=mirvar(0);
    low=mirvar(0);
    high=mirvar(0);
    gprime(10000);

/* randomise */
    printf("Enter 9 digit random number seed  = ");
    scanf("%ld",&seed);
    getchar();
    irand(seed);
    
    p1bits=PBITS-QBITS-1;

/* find number of primes pa, pb, pc etc., that will be needed */

    np=1;
    while (p1bits/np >= OBITS) np++;
    np--;

/* find the high/low limits for these primes, so that 
   the generated prime p will be exactly PBITS in length */

    expb2(p1bits-1,t);
    nroot(t,np,low);      /* np-th integer root */
    incr(low,1,low);

    premult(t,2,t);
    decr(t,1,t);
    nroot(t,np,high);

    subtract(high,low,t);   /* raise low limit up to half-way...  */
    subdiv(t,2,t);
    subtract(high,t,low);

/* generate q  */
    forever
    { /* make sure leading two bits of q 11... */
        expb2(QBITS,q);
        bigbits(QBITS-2,t);
        subtract(q,t,q);
        nxprime(q,q);
        if (logb2(q)>QBITS) continue;
        break;
    }
    printf("q= (%d bits)\n",logb2(q));
    cotnum(q,stdout);

/* generate prime pool from which permutations of np 
   primes will be picked until a Lim-Lee prime is found */

    for (i=0;i<POOL_SIZE;i++)
    { /* generate the primes pa, pb, pc etc.. */
        pool[i]=mirvar(0);
        forever
        { 
            bigrand(high,p1);
            if (mr_compare(p1,low)<0) continue;
            nxprime(p1,p1);
            if (mr_compare(p1,high)>0) continue;
            copy(p1,pool[i]);  
            break;
        }
    }

/* The '1' bits in the permutation indicate which primes are 
   picked from the pool. If np=5, start at 11111, then 101111 etc */

    permutation=1L;
    for (i=0;i<np;i++) permutation<<=1;
    permutation-=1;     /* permuation = 2^np-1 */

/* generate p   */
    fail=FALSE;
    forever 
    {
        convert(1,p1);
        for (i=j=0,m=1L;j<np;i++,m<<=1)
        {
            if (i>=POOL_SIZE) 
            { /* ran out of primes... */
                fail=TRUE;
                break;
            }
            if (m&permutation) 
            {
                multiply(p1,pool[i],p1); 
                j++;
            }
        } 
        if (fail) break;
        printf(".");   
        premult(q,2,p);
        multiply(p,p1,p);
        incr(p,1,p);
        permutation=increment(permutation);
        if (logb2(p)!=PBITS) continue;
        if (isprime(p)) break; 
    } 

    if (fail)
    {
        printf("\nFailed - very unlikely! - try increasing POOL_SIZE\n");
        return 0;
    }

    printf("\np= (%d bits)\n",logb2(p));
    cotnum(p,stdout);

/* finally find g */
    do {
        decr(p,1,t);
        bigrand(t,h);
        divide(t,q,t);
        powmod(h,t,p,g);
    } while(size(g)==1);    
   
    printf("g= (%d bits)\n",logb2(g));
    cotnum(g,stdout);

    fp=fopen("common.dss","wt");
    fprintf(fp,"%d\n",PBITS);
    mip->IOBASE=16;
    cotnum(p,fp);
    cotnum(q,fp);
    cotnum(g,fp);
    fclose(fp);
    return 0;
}