/**
 *--------------------------------------------------------------------\n
 *          HSLU T&A Hochschule Luzern Technik+Architektur            \n
 *--------------------------------------------------------------------\n
 *
 * \brief         model solution for ASYD assignment crypto 02
 * \file
 * \author        Stefano Nicora, stefano.nicora@hslu.ch
 * \date          20.10.22
 *
 *--------------------------------------------------------------------
 */

#include <stdint.h>
#include <stdio.h>
#include "DHKE.h"
#include "encryptionArithmetic.h"

/* based on the square and multiply algorithm */
/* https://www.biancahoegel.de/mathe/zahl/primitivwurzel.html */
/* https://www.practicalnetworking.net/stand-alone/square-and-multiply/ */
/* https://math.stackexchange.com/questions/1472480/learning-square-and-multiply-algorithm */
/* https://www.dcode.fr/diffie-hellman-key-exchange */

void moduloOperation(t_encryptionArithmetic* result, t_encryptionArithmetic* modulo, uint32_t keyLength);

void squareAndMultiply(t_encryptionArithmetic *base, t_encryptionArithmetic *exponent, t_encryptionArithmetic* modulo, t_encryptionArithmetic* result, uint32_t keyLength) {

	/* assign base to result as the first step defined in the square & multply algorithm */
	for (uint8_t i = 0; i < (keyLength / 32); i++) {
		*(result->number + i) = *(base->number + i);
	}

	/* get amount of bits representing the exponent */
	uint16_t numberOfBits = encryptionArithmetic_numberSize(exponent->number, keyLength);

	/* execute square and multiply algorithm */
	for (int i = 0; i < numberOfBits - 1; i++) {
		/* -2 as we already did store the base inside our number and therefore skip the MSB */
		if (*(exponent->number) >> (numberOfBits - i - 2) & 1) /* if bit is 1 => multiply number with itself as well as with base */
		{
			encryptionArithmetic(result->number, result->number, result, keyLength, MUL);	/* square */										
			encryptionArithmetic(result->number, base->number, result, keyLength, MUL);		/* multiply */
		}
		else /* if bit is 0 => multiply number with itself */
		{
			encryptionArithmetic(result->number, result->number, result, keyLength, MUL);	/* square */							
		}
		moduloOperation(result, modulo, keyLength);
	}
	printf("Square and Multiply result: %u\n", *result->number);
}

/* calculate modulo w/o the built in modulo operator */
void moduloOperation(t_encryptionArithmetic* number, t_encryptionArithmetic* moduloValue, uint32_t keyLength) {
	/* number % moduloValue */
	/* https://www.geeksforgeeks.org/program-to-find-remainder-without-using-modulo-or-operator/ */

	/* allocate memory */
	t_encryptionArithmetic* mulRes, * divRes, mulRes_2, divRes_2;
	mulRes = &mulRes_2;
	divRes = &divRes_2;
	encryptionArithmetic_Init(mulRes, keyLength);
	encryptionArithmetic_Init(divRes, keyLength);

	/* perform modulo operation */
	encryptionArithmetic(number->number, moduloValue->number, divRes, keyLength, DIV);
	encryptionArithmetic(moduloValue->number, divRes->number, mulRes, keyLength, MUL);
	encryptionArithmetic(number->number, mulRes->number, number, keyLength, SUB);

	/* free memory */
	encryptionArithmetic_DeInit(mulRes);
	encryptionArithmetic_DeInit(divRes);
}