#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "poly.h"
#include "params.h"
#include "byte.h"
#include "sig.h"
#include "fips202.h"
#include "ntt.h"
#include "rand.h"

#define NTESTS 10000

static void pol_naivemul(uint16_t *c, uint16_t  *a, uint16_t  *b)
{
	int i, j;
	uint16_t r[2 * N];

	for (i = 0; i < 2 * N; i++)
		r[i] = 0;

	for (i = 0; i < N; i++)
		for (j = 0; j < N; j++)
		{
			r[i + j] += a[i] * b[j] % Q;
			r[i + j] %= Q;
		}

	for (i = N; i < 2 * N - 1; i++) {
		r[i - N] = r[i - N] + Q - r[i];
		r[i - N] %= Q;
	}

	for (i = 0; i < N; i++)
		c[i] = r[i];
}


int main()
{
	FILE *fpt;
	clock_t start,finish;
	double t1=0,t2=0,t3=0;
	unsigned long long pk_size[1] = { 0 }, sk_size[1] = { 0 };
	int32_t arr[5] = { 805306368,2148139008,2148073472,65536,1460142080 };//{48,2688,2432,256,2135}
	int32_t a[N]={0},b[N]={0},c[N]={0},d[N]={1}, i,j=0;
	unsigned long long smlen[1];
	unsigned char  m[256], pk[N_BYTES],sk[N2+N_BYTES],temp[256],sm[ULEN + RLEN + L_BYTES],j1,rb[N];
	unsigned char rd[48] = { 0 }, bg[48] = { 0,1,1};

	rand_init(bg, 48);
	//randombytes(temp, 256);
	randombytes(rd, 48);
	rand_init(rd, 256);
	//fwrite(arr, sizeof(arr), 1, fpt);
	for (i = 0; i < NTESTS; i++) {
		randombytes(temp, 256);
		start = clock();
		sig_keygen(pk, pk_size, sk, sk_size);
		finish = clock();
		t1 = t1+(double)(finish - start) / CLOCKS_PER_SEC;
		//printf("%d\n", i);
		start = clock();
		sig_sign(sk, sk_size, temp, MLEN, sm, smlen);
		finish = clock();
		t2 = t2+(double)(finish - start) / CLOCKS_PER_SEC;
		//printf("*\n");
		start = clock();
		j = sig_verf(pk, pk_size, sm, smlen[0], temp, MLEN);
		finish = clock();
		t3 = t3+(double)(finish - start) / CLOCKS_PER_SEC;

//		if (j == 1) printf("success\n");
		if (j != 1) printf("wrong\n");
	}
	printf("keygen time  :%f us\n", (t1 * 1000000 / NTESTS));
	printf("sign time  :%f us\n", (t2 * 1000000 / NTESTS));
	printf("verify time  :%f us\n", (t3 * 1000000 / NTESTS));
    printf("\n");

	/*unsigned char rd[48] = { 0 }, bg[48] = { 1,2,1,4,5,1,4,12,1,1};

	rand_init(bg, 48);
	randombytes(temp, 256);
	randombytes(rd, 48);
	rand_init(rd, 48);
	fopen_s(&fpt, "SIG_VEC_INFO(10).dat", "wb+");
	fwrite(arr, 4, 1, fpt);
	fwrite(rd, sizeof(rd), 1, fpt);
	
	sig_keygen(pk, pk_size, sk, sk_size);

	fwrite(arr + 1, 4, 1, fpt);
	fwrite(sk, sizeof(sk), 1, fpt);
	fwrite(arr + 2, 4, 1, fpt);
	fwrite(pk, sizeof(pk), 1, fpt);

	sig_sign(sk, sk_size, temp, MLEN, sm, smlen);

	fwrite(arr + 3, 4, 1, fpt);
	fwrite(m, sizeof(m), 1, fpt);
	fwrite(arr + 4, 4, 1, fpt);
	fwrite(sm, sizeof(sm), 1, fpt);

	j = sig_verf(pk, pk_size, sm, smlen[0], temp, MLEN);
	if (j != 1) printf("wrong\n");
	j1 = j & 0xff;
	fwrite(&j1, sizeof(j1), 1, fpt);

	fclose(fpt);
*/

////读取测试向量并测试
//	fopen_s(&fpt, "SIG_VEC_INFO.dat", "rb+");
//
//	//读取种子的长度
//	fseek(fpt, 0, SEEK_SET);
//	fread(sl, 4, 1, fpt);
//	for (i = 0; i < 4; i++) {
//		printf("s[%d] is %x \n", i, sl[i]);
//		seed_len += (sl[i] << ((3 - i) << 3));
//	}
//	//读种子
//	printf("the length of seed is %d \n", seed_len);
//	rseed=()

	return 0;
}