Esempio n. 1
0
void test_add_scale_bigint(void){
	bigint_t a, b, c;
	uint16_t scale;
	cli_putstr_P(PSTR("\r\nadd-scale test\r\n"));
	for(;;){
		cli_putstr_P(PSTR("\r\nenter a:"));
		if(bigint_read_hex_echo(&a)){
			cli_putstr_P(PSTR("\r\n end add-scale test"));
			return;
		}
		cli_putstr_P(PSTR("\r\nenter b:"));
		if(bigint_read_hex_echo(&b)){
			cli_putstr_P(PSTR("\r\n end add-scale test"));
			return;
		}
		cli_putstr_P(PSTR("\r\nenter scale:"));
		{
			char str[8];
			cli_getsn_cecho(str, 7);
			scale = atoi(str);
		}
	/*
		if(bigint_read_hex_echo(&scale)){
			free(scale.wordv);
			cli_putstr_P(PSTR("\r\n end add test"));
			return;
		}
	*/
		uint8_t *c_b;
		c_b = malloc(((a.length_B>(b.length_B+scale))?a.length_B:(b.length_B+scale))+2);
		if(c_b==NULL){
			cli_putstr_P(PSTR("\n\rERROR: Out of memory!"));
			free(a.wordv);
			free(b.wordv);
			continue;
		}
		c.wordv = c_b;
		bigint_copy(&c, &a);
		bigint_add_scale_u(&c, &b, scale);
		cli_putstr_P(PSTR("\r\n "));
		bigint_print_hex(&a);
		cli_putstr_P(PSTR(" + "));
		bigint_print_hex(&b);
		cli_putstr_P(PSTR("<<8*"));
		cli_hexdump_rev(&scale, 2);
		cli_putstr_P(PSTR(" = "));
		bigint_print_hex(&c);
		cli_putstr_P(PSTR("\r\n"));
		free(a.wordv);
		free(b.wordv);
		free(c_b);
	}
}
Esempio n. 2
0
/**
 * Add entropy from previous calls.
 */
static G_GNUC_COLD void
entropy_merge(sha1_t *digest)
{
	bigint_t older, newer;

	/*
	 * These big integers operate on the buffer space from ``digest'' and
	 * ``entropy_previous'' directly.
	 */

	bigint_use(&older, &entropy_previous, SHA1_RAW_SIZE);
	bigint_use(&newer, digest, SHA1_RAW_SIZE);
	bigint_add(&newer, &older);
	bigint_copy(&older, &newer);
}
Esempio n. 3
0
File: memory.c Progetto: jes/bnbf
/* Read input to the current cell */
void input(Memory *mem) {
  void *cell;
  bigint input;
  int c;
  char buf[1024];
  char *p;

  if(!(cell = get_cell(mem))) return;

  if(chario) {
    /* character io */
    fflush(stdout);
    c = fgetc(stdin);

    if(c == EOF) goto handle_eof;
    else {
      if(wrap) *(unsigned char *)cell = c;
      else bigint_from_int(cell, c);
    }
  } else {
    /* number io */
    bigint_init(&input);

    /* use bigint for input even if we're reading to wrapping cells */
    do {
      if(prompt) {
        fprintf(stderr, "Input: ");
        fflush(stderr);
      }

      if(!(fgets(buf, 1024, stdin))) goto handle_eof;
      if((p = strchr(buf, '\n'))) *p = '\0';
    } while(bigint_from_string(&input, buf) == BIGINT_ILLEGAL_PARAM);

    if(wrap) *(unsigned char *)cell = bigint_to_int(&input);
    else bigint_copy(cell, &input);

    bigint_release(&input);
  }

  return;

 handle_eof:
  if(strcasecmp(eof_value, "nochange") != 0) {
    bigint_from_string(cell, eof_value);
  }
}
Esempio n. 4
0
// [fail (c)]:  A862 % 2752 = 0D1A ; should a862 % 2752 = b1a
void test_reduce_simple(void){
	bigint_t a, b, c;

	uint8_t a_b[2] = {0x62, 0xA8};
	uint8_t b_b[2] = {0x52, 0x27};
	uint8_t c_b[2];
	a.wordv=a_b;
	a.length_B = 2;
	a.info=0x00;
	bigint_adjust(&a);
	b.wordv=b_b;
	b.length_B = 2;
	b.info=0x00;
	bigint_adjust(&b);
	c.wordv = c_b;
	bigint_copy(&c, &a);
	bigint_reduce(&c, &b);
	cli_putstr_P(PSTR("\r\n test: "));
	bigint_print_hex(&a);
	cli_putstr_P(PSTR(" % "));
	bigint_print_hex(&b);
	cli_putstr_P(PSTR(" = "));
	bigint_print_hex(&c);
}
Esempio n. 5
0
void testrun_performance_reduce_bigint(void){
    printf_P(PSTR("\n=== performance measurement (reduce) ===\n"));
    unsigned i, j;
    bigint_t a,b,v;
    bigint_word_t v_w[192 * 2 / BIGINT_WORD_SIZE];
    bigint_word_t a_w[192 * 2 / BIGINT_WORD_SIZE];
    bigint_word_t b_w[192 * 2 / BIGINT_WORD_SIZE];
    uint32_t time_a, time_b;
    int32_t time_diff;
    int16_t faster_percent;
    v.wordv = v_w;
    for(j = 0; j < 32; ++j){
        do{
            for(i = 0; i < 192 * 2 / BIGINT_WORD_SIZE; ++i){
                ((uint8_t*)v_w)[i] = random();
            }
            v.length_W = 192 * 2 / BIGINT_WORD_SIZE;
            v.info = 0;
            bigint_adjust(&v);
        }while(0);

    //    printf_P(PSTR("candidate:\n"));
    //    bigint_print_hex(&v);
        a.wordv = a_w;
        b.wordv = b_w;
        calibrateTimer();

    //    printf_P(PSTR("\n  going to test optimized version: ...\n"));
        uart0_flush();
        time_a = 0;
        for(i = 0; i < 16; ++i){
            bigint_copy(&a, &v);
            startTimer(1);
            START_TIMER;
            bigint_reduce_p192(&a);
            STOP_TIMER;
            time_a += stopTimer();
        }
    //    printf_P(PSTR("    took: %"PRIu32" cycles\nresult:"), time);
    //    bigint_print_hex(&a);


    //    printf_P(PSTR("\n  going to test not-optimized version: ...\n"));
    //    uart0_flush();
        time_b = 0;
        for(i = 0; i < 16; ++i){
            bigint_copy(&b, &v);
            startTimer(1);
            START_TIMER;
            bigint_reduce(&b, &nist_curve_p192_p);
            STOP_TIMER;
            time_b += stopTimer();
        }
    //    printf_P(PSTR("    took: %"PRIu32" cycles\nresult:"), time);
    //    bigint_print_hex(&b);

        time_diff = time_b - time_a;
        faster_percent = (time_diff * 100) / time_b;

        printf_P(PSTR("  delta: %7"PRId32"  (%3"PRId16"%%)  :-"), time_diff, faster_percent);
        if(bigint_cmp_u(&a, &b)){
            printf_P(PSTR("(\n"));
        } else {
            printf_P(PSTR(")\n"));
        }
        uart0_flush();
    }
}