Esempio n. 1
0
void sign(int argc, char **argv) {
  ecc_int256_t secret, hash, k, krecip, r, s, tmp;
  ecc_25519_work_t kG;

  if (argc != 2)
    exit_error(1, 0, "Usage: ecdsautil sign file (secret is read from stdin)");

  if (!sha256_file(argv[1], tmp.p))
    exit_error(1, 0, "Error while hashing file");

  char secret_string[65];

  if (fgets(secret_string, sizeof(secret_string), stdin) == NULL)
    exit_error(1, 0, "Error reading secret");

  if (!parsehex(secret.p, secret_string, 32))
    exit_error(1, 0, "Error reading secret");

  // Reduce hash (instead of clearing 3 bits)
  ecc_25519_gf_reduce(&hash, &tmp);

  // Generate k
  generate_k(k.p, secret.p, tmp.p);
  ecc_25519_gf_sanitize_secret(&k, &k);

  // calculate k^(-1)
  ecc_25519_gf_recip(&krecip, &k);

  // calculate kG = k * base point
  ecc_25519_scalarmult_base(&kG, &k);

  // store x coordinate of kG in r
  ecc_25519_store_xy(&tmp, NULL, &kG);
  ecc_25519_gf_reduce(&r, &tmp);

  if (ecc_25519_gf_is_zero(&r))
    exit_error(1, 0, "Error: r is zero (this should never happen)");

  // tmp = r * secret
  ecc_25519_gf_mult(&tmp, &r, &secret);

  // s = hash + tmp = hash + r * secret
  ecc_25519_gf_add(&s, &hash, &tmp);

  // tmp = krecip * s = k^(-1) * s
  ecc_25519_gf_mult(&tmp, &krecip, &s);

  // mod n (order of G)
  ecc_25519_gf_reduce(&s, &tmp);

  if (ecc_25519_gf_is_zero(&s))
    exit_error(1, 0, "Error: s is zero (this should never happen)");

  hexdump(stdout, r.p, 32);
  hexdump(stdout, s.p, 32);
  puts("");
}
Esempio n. 2
0
int main(int argc, char** argv){
	int cases, case_no;
	long a, b, c, r, k, n;
	node* k_list, *tail;
	long output;
	float start_time, end_time;
	char* filename = "sample.txt";
	if(argc>=2){
		filename = argv[1];
	}

	FILE* infile = fopen(filename,"r");
	FILE* outfile = fopen("out.txt","w");
	fscanf(infile,"%d",&cases);
	for(case_no=1;case_no<=cases;case_no++){
		//input
		fscanf(infile,"%ld %ld",&n,&k);
		fscanf(infile,"%ld %ld %ld %ld",&a,&b,&c,&r);
		printf("Case #%d: %ld/%ld: %ld %ld %ld %ld\n",case_no,k,n,a,b,c,r);
		start_time = (float) clock()/CLOCKS_PER_SEC;

		//generate first k-values
		k_list = generate_k(a,b,c,r,k);
		//print_arr(k_list,k);

		//execution
		k_list = solve(k_list, a, b, c, r, k, n);
		tail=get_tail(k_list,k);
		output = tail->val;
		//print_arr(k_list,k);

		//post-execution
		clean_list(k_list,k);
		end_time = (float) clock()/CLOCKS_PER_SEC;
		printf("Case #%d: Output: %ld\n",case_no,output);
		fprintf(outfile,"Case #%d: %ld\n",case_no,output);
		printf("Time %f sec\n",end_time-start_time);
	}
	fclose(infile);
	fclose(outfile);

	return 0;
}