Exemplo n.º 1
0
void printkey(const unsigned char *k)
{
  printf("[Mod(");
  print16(k + 0);
  printf(",2^127-1),Mod(");
  print16(k + 16);
  printf(",2^127-1),Mod(");
  print16(k + 32);
  printf(",2^127-1)]");
}
Exemplo n.º 2
0
int main() {

	const int rate = 192000;
	const int length = 3840000;

	double dl;
	double dr;
	int n;
	short v;

	// write the wav header

	fputs("RIFF", stdout);      // magic
	print32(36 + length);       // chunksize
	fputs("WAVE", stdout);      // format

	fputs("fmt ", stdout);      // subchunk id
	print32(16);                // subchunk size
	print16(1);                 // audioformat (pcm)
	print16(1);                 // channels (mono)
	print32(rate);              // samplerate
	print32(rate * 1 * 2);      // byterate
	print16(1 * 2);             // blockalign
	print16(16);                // bitspersample

	fputs("data", stdout);      // subchunk id
	print32(length);            // subchunk size

	while(1) {
		n = read(0, &dl, sizeof(dl));
		if(n == 0) {
			break;
		} else if(n == -1) {
			perror("read()");
			exit(EXIT_FAILURE);
		}
		n = read(0, &dr, sizeof(dr));
		if(n == 0) {
			break;
		} else if(n == -1) {
			perror("read()");
			exit(EXIT_FAILURE);
		}
		v=(int)rint(dl*5.0*32767.0);
		n=fwrite(&v, sizeof(v), 1, stdout);
		if(n!=1) {
			perror("write()");
			exit(EXIT_FAILURE);
		}
	}

	exit(EXIT_SUCCESS);
}
Exemplo n.º 3
0
int main () {
   union union32 n32 = {0x12345678};
   print32 ("Original", &n32);
   n32.num = htonl (n32.num);
   print32 ("Network", &n32);
   n32.num = ntohl (n32.num);
   print32 ("Host", &n32);

   union union16 n16 = {0x1234};
   print16 ("Original", &n16);
   n16.num = htons (n16.num);
   print16 ("Network", &n16);
   n16.num = ntohs (n16.num);
   print16 ("Host", &n16);

   return 0;
}
static int32_t func_31(uint32_t p_32, uint16_t p_33, uint8_t p_34)
{
  uint16_t l_40[2];
  int32_t l_41 = 0x42CC3231L;
  int32_t l_42 = 0x642D9490L;
  int i;
  for (i = print13(0, 112); i < 2; i++)
    l_40[i] = print14(65535UL, 113);

  l_42 = print15(((uint16_t) (p_33 = print16((((l_41 = print17(l_40[1], 114)) ^ p_34) != 0x6104DBC0L) < 0x9B05L, 114))) + ((uint16_t) l_40[1]), 114);
  return l_40[1];
}
Exemplo n.º 5
0
void fp_add_test() {
    unsigned bias = 0b01111111;
    bit16 zero = construct(0b0, 0, 0);
    bit16 one = construct(0b0, 0b01111111, 0b0000000);
    bit16 onePointTwoFive = construct(0b0, 0b01111111, 0b0100000);
    bit16 threePointSevenFive = construct(0b0, 0b10000000, 0b1110000);
    bit16 twoTotheSeven = construct(0b0, 0b111 + bias, 0b0000000);
    bit16 twoTotheMinusSeven = construct(0b0, -0b111 + bias, 0b0000000);
    
    
    bit16 minusOnePointTwoFive = construct(0b0, 0b01111111, 0b0100000);
    bit16 minusThreePointSevenFive = construct(0b0, 0b10000000, 0b1110000);
    
    
    print16(SM_ADD(minusOnePointTwoFive, minusThreePointSevenFive));
    
    printf("1.25 + 3.75 = \n");
    print16(fp_add(onePointTwoFive, threePointSevenFive));
    printf("0 + 3.75 = \n");
    print16(fp_add(zero, threePointSevenFive));
    printf("0 + 0 = \n");
    print16(fp_add(zero, zero));
    
    
    printf("2^7 + 1 = \n");
    print16(fp_add(twoTotheSeven, 1));
    printf("2^7 + 2^-7 = \n");
    print16(fp_add(twoTotheSeven, twoTotheMinusSeven));
    
    return;
}
Exemplo n.º 6
0
main()
{
  int loop;
  int i;

  gaudry_init();

  for (loop = 0;loop < 100;++loop) {
    random16(k);
    printf("tk="); print16(k); printf(";\n");
    gaudry_todouble(x,k);
    printf("tx="); printdouble(x); printf(";\n");
    printf("(tx-tk)%(2^127-1)\n");

    randomdouble(x);
    randomdouble(y);
    printf("tx="); printdouble(x); printf(";\n");
    printf("ty="); printdouble(y); printf(";\n");
    gaudry_mult(z,x,y);
    printf("tz="); printdouble(z); printf(";\n");
    printf("(tx*ty-tz)%(2^127-1)\n");

    randomdouble(x);
    printf("tx="); printdouble(x); printf(";\n");
    gaudry_recip(z,x);
    printf("tz="); printdouble(z); printf(";\n");
    printf("(tx*tz-1)%(2^127-1)\n");
    gaudry_fromdouble(k,z);
    printf("tk="); print16(k); printf(";\n");
    printf("(tz-tk)%(2^127-1)\n");

    randomdouble(x); printf("x0="); printdouble(x); printf(";\n");
    randomdouble(x + 5); printf("x1="); printdouble(x + 5); printf(";\n");
    randomdouble(x + 10); printf("x2="); printdouble(x + 10); printf(";\n");
    randomdouble(x + 15); printf("x3="); printdouble(x + 15); printf(";\n");
    gaudry_hadamard(z,x);
    printf("z0="); printdouble(z); printf(";\n");
    printf("z1="); printdouble(z + 5); printf(";\n");
    printf("z2="); printdouble(z + 10); printf(";\n");
    printf("z3="); printdouble(z + 15); printf(";\n");
    printf("(x0+x1+x2+x3)-z0\n");
    printf("(x0+x1-x2-x3)-z1\n");
    printf("(x0-x1+x2-x3)-z2\n");
    printf("(x0-x1-x2+x3)-z3\n");
  }

  gaudry_surface_specify(a,b,c,d);
  printf("p=2^127-1;\n");
  printf("a=Mod("); print16(a); printf(",p);\n");
  printf("b=Mod("); print16(b); printf(",p);\n");
  printf("c=Mod("); print16(c); printf(",p);\n");
  printf("d=Mod("); print16(d); printf(",p);\n");
  printf("qx1=Mod("); print16(x1str); printf(",p);\n");
  printf("qy1=Mod("); print16(y1str); printf(",p);\n");
  printf("qz1=Mod("); print16(z1str); printf(",p);\n");
  printf("qt1=Mod("); print16(t1str); printf(",p);\n");
  printf("R1=[qx1,qy1,qz1,qt1];\n");
  printf("H(Q)=Q*[1,1,1,1;1,1,-1,-1;1,-1,1,-1;1,-1,-1,1]\n");
  printf("AA = H([a*a,b*b,c*c,d*d])[1]/4;\n");
  printf("BB = H([a*a,b*b,c*c,d*d])[2]/4;\n");
  printf("CC = H([a*a,b*b,c*c,d*d])[3]/4;\n");
  printf("DD = H([a*a,b*b,c*c,d*d])[4]/4;\n");
  printf("e = 256 * a*b*c*d *AA*BB*CC*DD / ((a*a*d*d-b*b*c*c)*(a*a*c*c-b*b*d*d)*(a*a*b*b-c*c*d*d));\n");
  printf("f = (a*a*a*a - b*b*b*b - c*c*c*c + d*d*d*d) / (a*a*d*d - b*b*c*c);\n");
  printf("g = (a*a*a*a - b*b*b*b + c*c*c*c - d*d*d*d) / (a*a*c*c - b*b*d*d);\n");
  printf("h = (a*a*a*a + b*b*b*b - c*c*c*c - d*d*d*d) / (a*a*b*b - c*c*d*d);\n");
  printf("C = x^4+y^4+z^4+t^4 + 2*e*x*y*z*t - f*(x^2*t^2+y^2*z^2) - g*(x^2*z^2+y^2*t^2) - h*(x^2*y^2+z^2*t^2);\n");
  printf("square(Q)=[Q[1]^2,Q[2]^2,Q[3]^2,Q[4]^2]\n");
  printf("ABC(Q)=[Q[1],Q[2]*AA/BB,Q[3]*AA/CC,Q[4]*AA/DD]\n");
  printf("mult(P,Q)=[P[1]*Q[1],P[2]*Q[2],P[3]*Q[3],P[4]*Q[4]]\n");
  printf("abc(Q)=[Q[1],Q[2]*a/b,Q[3]*a/c,Q[4]*a/d]\n");
  printf("double(Q)=abc(H(ABC(square(H(square(Q))))))\n");
  printf("divide(P,Q)=[P[1]/Q[1],P[2]/Q[2],P[3]/Q[3],P[4]/Q[4]]\n");
  printf("dividescaled(Q,Q1)=[Q[1],Q[2]*Q1[1]/Q1[2],Q[3]*Q1[1]/Q1[3],Q[4]*Q1[1]/Q1[4]]\n");
  printf("add(Q2,Q3,Q1)=dividescaled(H(ABC(mult(H(square(Q2)),H(square(Q3))))),Q1)\n");
  printf("onsurface(Q)=subst(subst(subst(subst(C,x,Q[1]),y,Q[2]),z,Q[3]),t,Q[4])\n");
  printf("doubleadd(Z,Q1)=[double(Z[1]),add(Z[1],Z[2],Q1)]\n");
  printf("flip(Z,bit)=[Z[2]*bit+Z[1]*(1-bit),Z[1]*bit+Z[2]*(1-bit)]\n");
  printf("doubleaddflip(Z,Q1,bit)=flip(doubleadd(flip(Z,bit),Q1),bit)\n");
  printf("ladder(Q1,n)=if(n==0,[[a,b,c,d],Q1],doubleaddflip(ladder(Q1,floor(n/2)),Q1,n%2))\n");
  printf("scalarmult(Q1,n)=ladder(Q1,n)[1]\n");

  gaudry_todouble(q1 + 0,x1str);
  gaudry_todouble(q1 + 5,y1str);
  gaudry_todouble(q1 + 10,z1str);
  gaudry_todouble(q1 + 15,t1str);

  printf("Q1="); printpoint(q1); printf(";\n");
  printf("lift(onsurface(Q1))\n");
  printf("lift((Q1-R1)[1])\n");
  printf("lift((Q1-R1)[2])\n");
  printf("lift((Q1-R1)[3])\n");
  printf("lift((Q1-R1)[4])\n");

  for (loop = 0;loop < 1000;++loop) {
    for (i = 0;i < 32;++i) alicesecret[i] = random();
    printf("AK="); print32(alicesecret); printf(";\n");
    gaudry(alicepublic,alicesecret,basepoint);
    printf("AP="); printkey(alicepublic); printf(";\n");
    printf("T=scalarmult(Q1,AK);\n");
    printf("lift(T[1]/T[2]-AP[1])\n");
    printf("lift(T[1]/T[3]-AP[2])\n");
    printf("lift(T[1]/T[4]-AP[3])\n");

    for (i = 0;i < 32;++i) bobsecret[i] = random();
    printf("BK="); print32(bobsecret); printf(";\n");
    gaudry(bobpublic,bobsecret,basepoint);
    printf("BP="); printkey(bobpublic); printf(";\n");
    printf("T=scalarmult(Q1,BK);\n");
    printf("lift(T[1]/T[2]-BP[1])\n");
    printf("lift(T[1]/T[3]-BP[2])\n");
    printf("lift(T[1]/T[4]-BP[3])\n");
  
    gaudry(aliceshared,alicesecret,bobpublic);
    gaudry(bobshared,bobsecret,alicepublic);
    printf("AS="); printkey(aliceshared); printf(";\n");
    printf("BS="); printkey(bobshared); printf(";\n");
    printf("lift(AS[1]-BS[1])\n");
    printf("lift(AS[2]-BS[2])\n");
    printf("lift(AS[3]-BS[3])\n");
    printf("T=scalarmult(Q1,AK*BK);\n");
    printf("lift(T[1]/T[2]-AS[1])\n");
    printf("lift(T[1]/T[3]-AS[2])\n");
    printf("lift(T[1]/T[4]-AS[3])\n");
  }

  return 0;
}
Exemplo n.º 7
0
/*
** Create a new delta.
**
** The delta is written into a preallocated buffer, zDelta, which 
** should be at least 60 bytes longer than the target file, zOut.
** The delta string will be NUL-terminated, but it might also contain
** embedded NUL characters if either the zSrc or zOut files are
** binary.  This function returns the length of the delta string
** in bytes, excluding the final NUL terminator character.
**
** Output Format:
**
** The delta begins with a base64 number followed by a newline.  This
** number is the number of bytes in the TARGET file.  Thus, given a
** delta file z, a program can compute the size of the output file
** simply by reading the first line and decoding the base-64 number
** found there.  The delta_output_size() routine does exactly this.
**
** After the initial size number, the delta consists of a series of
** literal text segments and commands to copy from the SOURCE file.  
** A copy command looks like this:
**
**     NNN@MMM,
**
** where NNN is the number of bytes to be copied and MMM is the offset
** into the source file of the first byte (both base-64).   If NNN is 0
** it means copy the rest of the input file.  Literal text is like this:
**
**     NNN:TTTTT
**
** where NNN is the number of bytes of text (base-64) and TTTTT is the text.
**
** The last term is of the form
**
**     NNN;
**
** In this case, NNN is a 32-bit bigendian checksum of the output file
** that can be used to verify that the delta applied correctly.  All
** numbers are in base-64.
**
** Pure text files generate a pure text delta.  Binary files generate a
** delta that may contain some binary data.
**
** Algorithm:
**
** The encoder first builds a hash table to help it find matching
** patterns in the source file.  16-byte chunks of the source file
** sampled at evenly spaced intervals are used to populate the hash
** table.
**
** Next we begin scanning the target file using a sliding 16-byte
** window.  The hash of the 16-byte window in the target is used to
** search for a matching section in the source file.  When a match
** is found, a copy command is added to the delta.  An effort is
** made to extend the matching section to regions that come before
** and after the 16-byte hash window.  A copy command is only issued
** if the result would use less space that just quoting the text
** literally. Literal text is added to the delta for sections that 
** do not match or which can not be encoded efficiently using copy
** commands.
*/
int delta_create(
  const char *zSrc,      /* The source or pattern file */
  unsigned int lenSrc,   /* Length of the source file */
  const char *zOut,      /* The target file */
  unsigned int lenOut,   /* Length of the target file */
  char *zDelta           /* Write the delta into this buffer */
){
  int i, base;
  char *zOrigDelta = zDelta;
  hash h;
  int nHash;                 /* Number of hash table entries */
  int *landmark;             /* Primary hash table */
  int *collide;              /* Collision chain */
  int lastRead = -1;         /* Last byte of zSrc read by a COPY command */

  /* Add the target file size to the beginning of the delta
  */
  putInt(lenOut, &zDelta);
  *(zDelta++) = '\n';

  /* If the source file is very small, it means that we have no
  ** chance of ever doing a copy command.  Just output a single
  ** literal segment for the entire target and exit.
  */
  if( lenSrc<=NHASH ){
    putInt(lenOut, &zDelta);
    *(zDelta++) = ':';
    memcpy(zDelta, zOut, lenOut);
    zDelta += lenOut;
    putInt(checksum(zOut, lenOut), &zDelta);
    *(zDelta++) = ';';
    return zDelta - zOrigDelta;
  }

  /* Compute the hash table used to locate matching sections in the
  ** source file.
  */
  nHash = lenSrc/NHASH;
  collide = fossil_malloc( nHash*2*sizeof(int) );
  landmark = &collide[nHash];
  memset(landmark, -1, nHash*sizeof(int));
  memset(collide, -1, nHash*sizeof(int));
  for(i=0; i<lenSrc-NHASH; i+=NHASH){
    int hv;
    hash_init(&h, &zSrc[i]);
    hv = hash_32bit(&h) % nHash;
    collide[i/NHASH] = landmark[hv];
    landmark[hv] = i/NHASH;
  }

  /* Begin scanning the target file and generating copy commands and
  ** literal sections of the delta.
  */
  base = 0;    /* We have already generated everything before zOut[base] */
  while( base+NHASH<lenOut ){
    int iSrc, iBlock;
    unsigned int bestCnt, bestOfst=0, bestLitsz=0;
    hash_init(&h, &zOut[base]);
    i = 0;     /* Trying to match a landmark against zOut[base+i] */
    bestCnt = 0;
    while( 1 ){
      int hv;
      int limit = 250;

      hv = hash_32bit(&h) % nHash;
      DEBUG2( printf("LOOKING: %4d [%s]\n", base+i, print16(&zOut[base+i])); )
      iBlock = landmark[hv];
      while( iBlock>=0 && (limit--)>0 ){
        /*
        ** The hash window has identified a potential match against 
        ** landmark block iBlock.  But we need to investigate further.
        ** 
        ** Look for a region in zOut that matches zSrc. Anchor the search
        ** at zSrc[iSrc] and zOut[base+i].  Do not include anything prior to
        ** zOut[base] or after zOut[outLen] nor anything after zSrc[srcLen].
        **
        ** Set cnt equal to the length of the match and set ofst so that
        ** zSrc[ofst] is the first element of the match.  litsz is the number
        ** of characters between zOut[base] and the beginning of the match.
        ** sz will be the overhead (in bytes) needed to encode the copy
        ** command.  Only generate copy command if the overhead of the
        ** copy command is less than the amount of literal text to be copied.
        */
        int cnt, ofst, litsz;
        int j, k, x, y;
        int sz;

        /* Beginning at iSrc, match forwards as far as we can.  j counts
        ** the number of characters that match */
        iSrc = iBlock*NHASH;
        for(j=0, x=iSrc, y=base+i; x<lenSrc && y<lenOut; j++, x++, y++){
          if( zSrc[x]!=zOut[y] ) break;
        }
        j--;

        /* Beginning at iSrc-1, match backwards as far as we can.  k counts
        ** the number of characters that match */
        for(k=1; k<iSrc && k<=i; k++){
          if( zSrc[iSrc-k]!=zOut[base+i-k] ) break;
        }
        k--;

        /* Compute the offset and size of the matching region */
        ofst = iSrc-k;
        cnt = j+k+1;
        litsz = i-k;  /* Number of bytes of literal text before the copy */
        DEBUG2( printf("MATCH %d bytes at %d: [%s] litsz=%d\n",
                        cnt, ofst, print16(&zSrc[ofst]), litsz); )
        /* sz will hold the number of bytes needed to encode the "insert"
        ** command and the copy command, not counting the "insert" text */
        sz = digit_count(i-k)+digit_count(cnt)+digit_count(ofst)+3;
        if( cnt>=sz && cnt>bestCnt ){
          /* Remember this match only if it is the best so far and it
          ** does not increase the file size */
          bestCnt = cnt;
          bestOfst = iSrc-k;
          bestLitsz = litsz;
          DEBUG2( printf("... BEST SO FAR\n"); )
        }

        /* Check the next matching block */
        iBlock = collide[iBlock];
      }