/**
 * @file hv_hdcp_BigDigits.h
 * @brief hdcp engine interface.
 * @verbatim
 * ==============================================================================
 *                        ##### How to use this driver #####
 * ==============================================================================
 * (+) Use hv_engine_Divide(...) to Divide Computes integer division of u by v such that u=qv+r .
 * (+) Use hv_engine_Multiply(...) to Computes product w = u * v.
 * (+) Use hv_engine_Modulo(...) to Computes remainder r = u mod v. 
 * @endverbatim
 *
 * @author HiView SoC Software Team
 * @version 0.0.1
 * @date 2022-08-22
 */

#include "hv_vos_Comm.h"

#ifndef HV_HDCP_BIGDIGITS_H_
#define HV_HDCP_BIGDIGITS_H_
#include "Common/hv_comm_DataType.h"


/** Computes w = u + v, returns carry */
UINT32 Hv_Engine_Add(UINT32 w[], const UINT32 u[], const UINT32 v[], size_t ndigits);

/** Computes w = u - v, returns borrow */
UINT32 Hv_Engine_Subtract(UINT32 w[], const UINT32 u[], const UINT32 v[], size_t ndigits);

/** Computes product w = u * v
@param[out] w To receive the product, an array of size 2 x `ndigits`
@param[in] u An array of size `ndigits`
@param[in] v An array of size `ndigits`
@param[in] ndigits size of arrays `u` and `v`
@warning The product must be of size 2 x `ndigits`
*/
int Hv_Engine_Multiply(UINT32 w[], const UINT32 u[], const UINT32 v[], size_t ndigits);

/** Computes integer division of u by v such that u=qv+r
@param[out] q to receive quotient = u div v, an array of size `udigits`
@param[out] r to receive divisor = u mod v, an array of size `udigits`
@param[in]  u dividend of size `udigits`
@param[in] udigits size of arrays `q` `r` and `u`
@param[in]  v divisor of size `vdigits`
@param[in] vdigits size of array `v`
@warning Trashes q and r first
*/
int Hv_Engine_Divide(UINT32 q[], UINT32 r[], const UINT32 u[],
	size_t udigits, UINT32 v[], size_t vdigits);


/** Computes remainder r = u mod v
@param[out] r to receive divisor = u mod v, an array of size `vdigits`
@param[in]  u dividend of size `udigits`
@param[in] udigits size of arrays `r` and `u`
@param[in]  v divisor of size `vdigits`
@param[in] vdigits size of array `v`
@remark Note that `r` is `vdigits` long here, but is `udigits` long in mpDivide().
*/
int Hv_Engine_Modulo(UINT32 r[], const UINT32 u[], size_t udigits, UINT32 v[], size_t vdigits);

/** Computes square w = x^2
@param[out] w array of size 2 x `ndigits` to receive square
@param[in] x array of size `ndigits`
@param[in] ndigits size of array `x`
@warning The product `w` must be of size 2 x `ndigits`
*/
int Hv_Engine_Square(UINT32 w[], const UINT32 x[], size_t ndigits);

/** Returns true if a is zero, else false, using constant-time algorithm
 *  @remark Constant-time with respect to `ndigits`
 */
int Hv_Engine_IsZero(const UINT32 a[], size_t ndigits);

/*************************/
/* COMPARISON OPERATIONS */
/*************************/
/* [v2.5] Changed to constant-time algorithms */

/** Returns true if a == b, else false, using constant-time algorithm
 *  @remark Constant-time with respect to `ndigits`
 */
int Hv_Engine_Equal(const UINT32 a[], const UINT32 b[], size_t ndigits);

/** Returns sign of `(a-b)` as `{-1,0,+1}` using constant-time algorithm
 *  @remark Constant-time with respect to `ndigits`
 */
int Hv_Engine_Compare(const UINT32 a[], const UINT32 b[], size_t ndigits);

/** Computes a = (x * y) mod m */
int Hv_Engine_ModMult(UINT32 a[], const UINT32 x[], const UINT32 y[], UINT32 m[], size_t ndigits);

/** Computes the inverse of `u` modulo `m`, inv = u^{-1} mod m */
int Hv_Engine_ModInv(UINT32 inv[], const UINT32 u[], const UINT32 m[], size_t ndigits);

/** Returns number of significant bits in a */
size_t Hv_Engine_BitLength(const UINT32 a[], size_t ndigits);

/** Computes a = b << x */
UINT32 Hv_Engine_ShiftLeft(UINT32 a[], const UINT32 b[], size_t x, size_t ndigits);

/** Computes a = b >> x */
UINT32 Hv_Engine_ShiftRight(UINT32 a[], const UINT32 b[], size_t x, size_t ndigits);

/** Sets bit n of a (0..nbits-1) with value 1 or 0 */
int mpSetBit(UINT32 a[], size_t ndigits, UINT32 n, int value);

/** Sets a = 0 */
volatile UINT32 Hv_Engine_SetZero(volatile UINT32 a[], size_t ndigits);

/** Sets a = d where d is a single digit */
void Hv_Engine_SetDigit(UINT32 a[], UINT32 d, size_t ndigits);

/** Sets a = b */
void Hv_Engine_SetEqual(UINT32 a[], const UINT32 b[], size_t ndigits);

/** Returns value 1 or 0 of bit n (0..nbits-1) */
int Hv_Engine_GetBit(UINT32 a[], size_t ndigits, size_t n);

/** Returns number of significant non-zero digits in a */
size_t Hv_Engine_Sizeof(const UINT32 a[], size_t ndigits);

/** Computes quotient q = u div d, returns remainder */
UINT32 Hv_Engine_ShortDiv(UINT32 q[], const UINT32 u[], UINT32 d, size_t ndigits);

/** Returns sign of (a - d) where d is a single digit */
int Hv_Engine_ShortCmp(const UINT32 a[], UINT32 d, size_t ndigits);

/** Computes p = x * y, where x and y are single digits */
int Hv_Engine_DualMultiply(UINT32 p[2], UINT32 x, UINT32 y);

/** Computes quotient q = u div v, remainder r = u mod v, where q, r and v are single digits */
UINT32 Hv_Engine_DualDivide(UINT32 *q, UINT32 *r, const UINT32 u[2], UINT32 v);

size_t Hv_Engine_ConvFromOctets(UINT32 a[], size_t ndigits, const unsigned char *c, size_t nbytes);
/** Converts big digit a into string of octets, in big-endian order, padding to nbytes or truncating if necessary.
@returns number of non-zero octets required. */
size_t Hv_Engine_ConvToOctets(const UINT32 a[], size_t ndigits, unsigned char *c, size_t nbytes);

/** Computes y = x^e mod m */
int Hv_Engine_ModExp(UINT32 y[], const UINT32 x[], const UINT32 e[], UINT32 m[], size_t ndigits);

/** @endcond */

#endif	/* BIGDIGITS_H_ */
