<commit_before>/**
  * JAIST - Visiting Student 2014
  * <NAME> <EMAIL>
  *
  */

#include "boolean.h"
#include "helper.h"
#include "point.h"
#include "j_point.h"
#include "ecc.h"

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <fcntl.h>
#include <time.h>
#include <dirent.h>
#include <ctype.h>
#include <string.h>
#include <gmp.h>

// Define your TEST here
#define TEST_SCALAR_MULTIPLICATION true
#define TEST_ENCRYPT_DECRYPT true
#define TEST_SIMPLIFIED_ECIES true

long long max_iteration = 1;

// ECC Parameters (P-256 NIST)
char*a_v="-3";
char*b_v="5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b";
char*p_v="115792089210356248762697446949407573530086143415290314195533631308867097853951";
char*r_v="115792089210356248762697446949407573529996955224135760342422259061068512044369";
char*gx_v="5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b";
char*gy_v="4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5";
char*s_v="c49d360886e704936a6678e1139d26b7819f7e90";
char*c_v="7efba1662985be9403cb055c75d4f7e0ce8d84a9c5114abcaf3177680104fa0d";

static struct timeval tm1;

static inline void start() {
	gettimeofday(&tm1, NULL);
}

static inline void stop() {
	struct timeval tm2;
	gettimeofday(&tm2, NULL);

	double t = 1000 * (tm2.tv_sec - tm1.tv_sec) + (tm2.tv_usec - tm1.tv_usec) / 1000;
	printf("Average elapsed time: %.2f ms\n", (double) t / max_iteration);
}

int main() {
	/** Fm, where modulo = m */
	mpz_t a, b, k, r, modulo;
	int i = 0; // loop variable
	Point p, next_p;
	p = init_point(p);
	mpz_init(a);
	mpz_init(b);
	mpz_init(k);
	mpz_init(r); // order
	mpz_init(modulo);

	/** Initialize parameters of ECC (F2p) */
	mpz_set_str(a, a_v, 10);
	mpz_set_str(b, b_v, 16);
	mpz_set_str(modulo, p_v, 10);
	mpz_set_str(r, r_v, 10);
	mpz_set_str(p.x, gx_v, 16);
	mpz_set_str(p.y, gy_v, 16);

	mpz_t zero_value;
	mpz_init(zero_value);

	// Convert Affine coordinate to Jacobian coordinate
	J_Point j_p, j_next_p;
	j_next_p = init_j_point(j_next_p);
	j_p = affine_to_jacobian(p); // Generator point


	if (TEST_SCALAR_MULTIPLICATION) {
		while (mpz_cmp(k, zero_value) == 0) {
			get_random(k, 32); // generate random test (256 bits)
			positive_modulo(k, k, modulo);
		}
		printf("Random k (in Binary): ");
		mpz_out_str(stdout, 2, k);
		printf("\n");

		/** Test Left-to-right binary algorithm */
		start(); // start operation
		while (i < max_iteration) {
			next_p = affine_left_to_right_binary(p, a, k, modulo); // Q = [k]P
			// gmp_printf("%Zd %Zd\n", next_p.x, next_p.y);
			i++;
		}
		printf("--[AFFINE] Left to right binary algorithm--\n");
		stop(); // stop operation

		start(); // start operation
		i = 0;
		while (i < max_iteration) {
			j_next_p = jacobian_left_to_right_binary(j_p, a, k, modulo); // Q = [k]P
			// gmp_printf("%Zd %Zd\n", j_next_p.X, j_next_p.Y);
			next_p = jacobian_to_affine(j_next_p, modulo);
			// gmp_printf("%Zd %Zd\n", next_p.x, next_p.y);
			i++;
		}
		printf("--[JACOBIAN] Left to right binary algorithm--\n");
		stop(); // stop operation

		start(); // start operation
		i = 0;
		while (i < max_iteration) {
			j_next_p = jacobian_affine_left_to_right_binary(j_p, p, a, k, modulo); // Q = [k]P
			// gmp_printf("%Zd %Zd\n", j_next_p.X, j_next_p.Y);
			next_p = jacobian_to_affine(j_next_p, modulo);
			gmp_printf("%Zd %Zd\n", next_p.x, next_p.y);
			i++;
		}
		printf("--[JACOBIAN-AFFINE] Left to right binary algorithm--\n");
		stop(); // stop operation

		start(); // start operation
		int w = 4; // windows size
		i = 0;
		while (i < max_iteration) {
			j_next_p = jacobian_affine_sliding_NAF(j_p, p, a, k, modulo, w); // Q = [k]P
			// gmp_printf("%Zd %Zd\n", j_next_p.X, j_next_p.Y);
			next_p = jacobian_to_affine(j_next_p, modulo);
			gmp_printf("%Zd %Zd\n", next_p.x, next_p.y);
			i++;
		}
		printf("--[JACOBIAN-AFFINE] Sliding NAF Left to right binary algorithm (w = 4)--\n");
		stop(); // stop operation

		/** Test Right-to-left binary algorithm */
		start(); // start operation
		i = 0;
		while (i < max_iteration) {
			next_p = affine_right_to_left_binary(p, a, k, modulo); // Q = [k]P
			// gmp_printf("%Zd %Zd\n", next_p.x, next_p.y);
			i++;
		}
		printf("--[AFFINE] Right to left binary algorithm--\n");
		stop(); // stop operation

		/** Test Montgomery ladder algorithm (Against time-based attack) */
		start(); // start operation
		i = 0;
		while (i < max_iteration) {
			j_next_p = jacobian_montgomery_ladder(j_p, a, k, modulo); // Q = [k]P
			// gmp_printf("%Zd %Zd\n", j_next_p.X, j_next_p.Y);
			next_p = jacobian_to_affine(j_next_p, modulo);
			// gmp_printf("%Zd %Zd\n", next_p.x, next_p.y);
			i++;
		}
		printf("--[JACOBIAN] Montgomery ladder algorithm--\n");<commit_msg>Fix Gx value, add new test<commit_after>/**
  * JAIST - Visiting Student 2014
  * <NAME> <EMAIL>
  *
  */

#include "boolean.h"
#include "helper.h"
#include "point.h"
#include "j_point.h"
#include "ecc.h"

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
#include <fcntl.h>
#include <time.h>
#include <dirent.h>
#include <ctype.h>
#include <string.h>
#include <gmp.h>

// Define your TEST here
#define TEST_SCALAR_MULTIPLICATION true
#define TEST_ENCRYPT_DECRYPT true
#define TEST_SIMPLIFIED_ECIES true

long long max_iteration = 100;

// ECC Parameters (P-256 NIST)
char*a_v="-3";
char*b_v="5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b";
char*p_v="115792089210356248762697446949407573530086143415290314195533631308867097853951";
char*r_v="115792089210356248762697446949407573529996955224135760342422259061068512044369";
char*gx_v="6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296";
char*gy_v="4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5";
char*s_v="c49d360886e704936a6678e1139d26b7819f7e90";
char*c_v="7efba1662985be9403cb055c75d4f7e0ce8d84a9c5114abcaf3177680104fa0d";

static struct timeval tm1;

static inline void start() {
	gettimeofday(&tm1, NULL);
}

static inline void stop() {
	struct timeval tm2;
	gettimeofday(&tm2, NULL);

	double t = 1000 * (tm2.tv_sec - tm1.tv_sec) + (tm2.tv_usec - tm1.tv_usec) / 1000;
	printf("Average elapsed time: %.2f ms\n", (double) t / max_iteration);
}

int main() {
	/** Fm, where modulo = m */
	mpz_t a, b, k, r, modulo;
	int i = 0; // loop variable
	Point p, next_p;
	p = init_point(p);
	mpz_init(a);
	mpz_init(b);
	mpz_init(k);
	mpz_init(r); // order
	mpz_init(modulo);

	/** Initialize parameters of ECC (F2p) */
	mpz_set_str(a, a_v, 10);
	mpz_set_str(b, b_v, 16);
	mpz_set_str(modulo, p_v, 10);
	mpz_set_str(r, r_v, 10);
	mpz_set_str(p.x, gx_v, 16);
	mpz_set_str(p.y, gy_v, 16);

	mpz_t zero_value;
	mpz_init(zero_value);

	// Convert Affine coordinate to Jacobian coordinate
	J_Point j_p, j_next_p;
	j_next_p = init_j_point(j_next_p);
	j_p = affine_to_jacobian(p); // Generator point


	if (TEST_SCALAR_MULTIPLICATION) {
		while (mpz_cmp(k, zero_value) == 0) {
			get_random(k, 32); // generate random test (256 bits)
			positive_modulo(k, k, modulo);
		}
		printf("Random k (in Binary): ");
		mpz_out_str(stdout, 2, k);
		printf("\n");

		/** Test Left-to-right binary algorithm */
		start(); // start operation
		while (i < max_iteration) {
			next_p = affine_left_to_right_binary(p, a, k, modulo); // Q = [k]P
			// gmp_printf("%Zd %Zd\n", next_p.x, next_p.y);
			i++;
		}
		printf("--[AFFINE] Left to right binary algorithm--\n");
		stop(); // stop operation

		start(); // start operation
		i = 0;
		while (i < max_iteration) {
			j_next_p = jacobian_left_to_right_binary(j_p, a, k, modulo); // Q = [k]P
			// gmp_printf("%Zd %Zd\n", j_next_p.X, j_next_p.Y);
			next_p = jacobian_to_affine(j_next_p, modulo);
			// gmp_printf("%Zd %Zd\n", next_p.x, next_p.y);
			i++;
		}
		printf("--[JACOBIAN] Left to right binary algorithm--\n");
		stop(); // stop operation

		start(); // start operation
		i = 0;
		while (i < max_iteration) {
			j_next_p = jacobian_affine_left_to_right_binary(j_p, p, a, k, modulo); // Q = [k]P
			// gmp_printf("%Zd %Zd\n", j_next_p.X, j_next_p.Y);
			next_p = jacobian_to_affine(j_next_p, modulo);
			// gmp_printf("%Zd %Zd\n", next_p.x, next_p.y);
			i++;
		}
		printf("--[JACOBIAN-AFFINE] Left to right binary algorithm--\n");
		stop(); // stop operation

		start(); // start operation
		int w = 4; // windows size
		i = 0;
		while (i < max_iteration) {
			j_next_p = jacobian_affine_sliding_NAF(j_p, p, a, k, modulo, w); // Q = [k]P
			// gmp_printf("%Zd %Zd\n", j_next_p.X, j_next_p.Y);
			next_p = jacobian_to_affine(j_next_p, modulo);
			// gmp_printf("%Zd %Zd\n", next_p.x, next_p.y);
			i++;
		}
		printf("--[JACOBIAN-AFFINE] Sliding NAF Left to right binary algorithm (w = 4)--\n");
		stop(); // stop operation

		start(); // start operation
		w = 5; // windows size
		i = 0;
		while (i < max_iteration) {
			j_next_p = jacobian_affine_sliding_NAF(j_p, p, a, k, modulo, w); // Q = [k]P
			// gmp_printf("%Zd %Zd\n", j_next_p.X, j_next_p.Y);
			next_p = jacobian_to_affine(j_next_p, modulo);
			// gmp_printf("%Zd %Zd\n", next_p.x, next_p.y);
			i++;
		}
		printf("--[JACOBIAN-AFFINE] Sliding NAF Left to right binary algorithm (w = 5)--\n");
		stop(); // stop operation

		/** Test Right-to-left binary algorithm */
		start(); // start operation
		i = 0;
		while (i < max_iteration) {
			next_p = affine_right_to_left_binary(p, a, k, modulo); // Q = [k]P
			// gmp_printf("%Zd %Zd\n", next_p.x, next_p.y);
			i++;
		}
		printf("--[AFFINE] Right to left binary algorithm--\n");
		stop(); // stop operation

		/** Test Montgomery ladder algorithm (Against time-based attack) */
		start(); // start operation
		i = 0;
		while (i < max_iteration) {
			j_next_p = jacobian_montgomery_ladder(j_p, a, k, modulo); // Q = [k]P
			// gmp_printf("%Zd %Zd\n", j_next_p.X, j_next_p.Y);
			next_p = jacobian_to_affine(j_next_p, modulo);
			// gmp_printf("%Zd %Zd\n", next_p.x, next_p.y);
			i++;
		}
		printf("--[JACOBIAN] Montgomery ladder algorithm--\n");
{'fraction_non_alphanumeric': 0.16351568785197104, 'fraction_numerical': 0.07934432823813355, 'mean_word_length': 4.0, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '282818', 'n_tokens_mistral': 5200, 'n_tokens_neox': 4449, 'n_words': 882}