void ExampleAnnealKnapsack(int argIndex, int argc, char **argv) {
	KNAPSACK_PARAMS *params;
	TRAIN *train;
	unsigned char x0[NUM_ITEMS_TO_CHOOSE],*x;
	unsigned int size, n;
	const int kMax = 100;

	params = CreateKnapsack();
	size = NUM_ITEMS_TO_CHOOSE * sizeof(unsigned char);
	
	RandomFill(x0);
	train = TrainCreateAnneal(score_function,x0,size,400,0.0001,500,kMax,params);
	((TRAIN_ANNEAL*)train)->anneal_randomize = KnapsackRandomize;
	TrainRun(train,kMax,0.01,1);

	/* print results */
	x = (unsigned char*)train->best_position;
    printf("item\tprofit\tweight\n");
    for (n = 0; n < NUM_ITEMS_TO_CHOOSE; n++) {
		if(x[n]) {
			printf("%i\t%.2f\t%.2f\n", (n + 1), params->profit[n], params->weight[n]);
		}
    }
	
	free(params);
}
Beispiel #2
0
void test_sha1()
{
    const int N = 50000;
    const int M = 50000;

    int wit_failed = 0;
    u8 h1[100], h2[100], source[1000-21];
    printf("\n*** test SHA1 ***\n\n");

    int i;

    u32 t1 = GetTimerMSec();
    for ( i=0; i<M; i++ )
	SHA1(source,sizeof(source),h1);
    t1 = GetTimerMSec() - t1;

    u32 t2 = GetTimerMSec();
    for ( i=0; i<M; i++ )
	WIT_SHA1(source,sizeof(source),h2);
    t2 = GetTimerMSec() - t2;

    printf("WIT_SHA1: %8u msec / %u = %6llu nsec\n",t2,M,(u64)t2*1000000/M);
    printf("SHA1:     %8u msec / %u = %6llu nsec\n",t1,M,(u64)t1*1000000/M);

    for ( i = 0; i < N; i++ )
    {
	RandomFill(h1,sizeof(h1));
	memcpy(h2,h1,sizeof(h2));
	RandomFill(source,sizeof(source));

	SHA1(source,sizeof(source),h1);
	WIT_SHA1(source,sizeof(source),h2);

	if (memcmp(h2,h1,sizeof(h2)))
	    wit_failed++;
    }
    printf("WWT failed:%7u/%u\n\n",wit_failed,N);

    HexDump(stdout,0,0,0,24,h2,24);
    HexDump(stdout,0,0,0,24,h1,24);
}
Beispiel #3
0
int main() {

    int m=10,n=100;
    char** a;
    a=CreateArray(n,m);
    RandomFill (a,n,m);
    PrintArray (a,n,m);
    DeleteArray(a,n);

    return 0;

}
Beispiel #4
0
int main(){

    char* array;
    int n = 4 , m = 6;
    char a;

    array = CreateArray(n,m);
    RandomFill(array,n,m);
    PrintArray(array,n,m);


    a = GetElement(array,n,m,2,5);
    printf("\n%c\n",a);

    SetElement(array,n,m,2,5,'A');

    a = GetElement(array,n,m,2,5);
    printf("\n%c\n",a);

    DeleteArray(array);

    return 0;

}
Beispiel #5
0
void CopyIn(void *thread_data) {
  ThreadData *g = (ThreadData *) thread_data;
  RandomFill(g->vector_a, g->element_count);
  RandomFill(g->vector_b, g->element_count);
}
Beispiel #6
0
/*Returns 1 on success.
Assumes files are open.  Closes them when done.
OSx has built-in function "encrypt", use different name.
The characters are picked in natural order from the input buffer,
but written to the output buffer using BufSequ to mix the order.
After encryption, the character is remaped to the new CharMap.
At periodic intervals the CharMap is renewed.
*/
int Encrypt(FILE *src, FILE *dst) {
  int CharCount, bytesread, byteswrit, bufpos, Unchanged;
  int k=0;	//k = current byte position within the 4 bytes of the random number.
  uint8_t *buf_in, *buf_out;		//File contents buffers
  uint8_t CharMap[256];			//A charset used to remap chars
  uint16_t MapFreq = 0;			//Create CharMap immediately
  uint16_t *BufSequ;			//out-file sequence
  uint8_t ch, origCh;
  uint8_t *p=NULL;	//Ptr for final buffer to use for start of padding.

  buf_in  = malloc(bufsize);
  buf_out = malloc(bufsize);
  BufSequ = malloc(bufsize * sizeof(BufSequ[0]));

  Unchanged = 0;
  CharCount = 0;
  do {
    bytesread = fread(buf_in, 1, bufsize, source);	//fill the buffer
    if (bytesread == 0) break;		//All done?
    DesyncInDoc(k);
    if (bytesread < bufsize) {
      p = &buf_in[bytesread];		//Point to last input char+1.
      RandomFill(p, FilePadLen, PadCharsBinary); //Fill here to end of buffer.
      bytesread = bytesread+FilePadLen; //Adjust bytesread so entire buf is encrypted.
    }	//if bytesread

    //The output sequence must be generated using the length of buffer in use.
    //If MixOut is 0 then there is no mixing.
    GenerateSequence16(BufSequ, bytesread, MixOut);
    Next4Rands();
    if (Debug == dg_Sequ4)
      DebugOutSequence2File(SeqDebugF, BufSequ, bytesread, MixOut);

    //Process the buffer
    for (bufpos=0; bufpos<bytesread; bufpos++) {
      origCh = buf_in[bufpos];
      ch = origCh;
      if ((CharCount & 3) == 0) {	//CharCount mod 4 (every 4th char)
        k = 0;				//Reset to use 1st byte of layers.
        Next4Rands();
        FastDesync(k, FastDesyncLayers, FastDesyncScale);
      }	//if mod 4      
      if (MapFreq == 0) {  //New Charset at 0
        Next4Rands();      
        GenerateSequence8(CharMap, 256, k, (LayerRandByte(CharMapLayer, k)&15)+1);
        Next4Rands();
        //Change the layers each time the charset is renewed
        CharMapLayer = PickALayer(CharMapLayer);  
        CharMapFreqLay = PickALayer(CharMapFreqLay);   
        //Reset the freq counter to a new number
        MapFreq = LayerRandByte(CharMapFreqLay, k) + MapFreqMin;
#ifdef MixGens         
        MixLayerGens(k, (LayerRandByte(MapFreq, k)&15)+1);
#endif
      } 
      else
        MapFreq--;                   

      Progress(CharCount);

      ch = CryptChar(ch, k);		//encrypt the char.
      if (origCh == ch)			//Check if unchanged.
        Unchanged++;
      ch = CharMap[ch];		//When encrypting remap char just after decrypting
      buf_out[BufSequ[bufpos]] = ch;	//Write char to scrambled position in output buf
      k++;
      CharCount++;
    }	//for bufpos

    byteswrit = fwrite(buf_out, 1, bytesread, dest);
    if (byteswrit < bytesread) 
      msgi("ERROR Encrypt: bytes written incorrect", byteswrit);
#ifdef SubKeyRenew
      NewSubKey(1);
      Desync(1);
#endif        
   
  } while (bytesread == bufsize);
  msg("", 0);
  fclose(source);
  fclose(dest);
  if (! QuietMode)
    printf("%d bytes encrypted\n", CharCount);
  if (Unchanged > 0) {
    printf("warning: %d characters unchanged during encryption.\n", Unchanged);
    warnings++;
    exitcode = ex_unchg;
  }
  else
    msg("All charcters changed", 0);
  free(buf_in);
  free(buf_out);
  free(BufSequ);
  return 1;
}  //Encrypt