chsvlib
chsv helper source code

◆ memadd_LE()

bool Chusov::Memory::memadd_LE ( void *  pResult,
std::size_t  cbResult,
const void *  pX,
std::size_t  cbX,
const void *  pY,
std::size_t  cbY 
)
noexcept

Performs an addition of two Little-Endian arbitrary-precision integers and returns the carry flag.

Parameters
[out]pResultA pointer to a buffer receiving a result of the summation. The pointer can address the same as any of the input buffers or as both of them.
cbResultA capacity of the output buffer in bytes and the byte size of the output value, i.e. the result is calculated modulo bcbResult, where b = 2CHAR_BIT is a number of values that can be represented using one byte.
[in]pXA pointer to a buffer containing the first addend. The buffer can be the same as one pointed to by pResult and/or pY.
cbXSize of the first addend in bytes.
[in]pYA pointer to the second operand.
cbYSize of the second addend in bytes.
Returns
A value of a carry bit which results from the addition.

The function considers the byte vectors pointed to by pX and pY as well as the resulting vector to be Little-Endian ordered. That is, given that \(V_X=\{x_0, \dots, x_{n_x}\}\) and \(V_Y = \{y_0,\dots,y_{n_y}\}\) are the vectors pointed to by pX and pY respectively so that \(x_0\) and \(y_0\) are the first bytes of the respective vectors, nx = cbX - 1 and ny = cbY - 1, the function performs the addition of the integers \(X = \sum_{i=0}^{n_x}x_i\cdot b^i\) and \(Y=\sum_{i=0}^{n_y}y_i\cdot b^i\). Furthermore, the result of the summation is also written in the Little-Endian byte order, i.e. the least significant byte is written in place of the first element of pResult.

In order to perform the addition of the differently sized integers, the function first represents them as members of the factor-ring \(\mathbb{Z}_{N}\), where \(N = b^{n + 1}\), and n = cbResult - 1. That is, the function either contracts or zero-extends each of the operands to be represented with cbResult bytes and then adds them up writing the result into the destination buffer pResult and returning the carry bit. Thus, the input values \(X\) and \(Y\) given above are represented as members \(X' = \sum_{i=0}^{n}x'_i\cdot b^i\) and \(Y' = \sum_{i=0}^{n}y'_i\cdot b^i\) of the ring \(\mathbb{Z}_{N}\), where \(x'_i = x_i\) when \(0\le i\le n_x\) and \(x'_i = 0\) otherwise and, likewise, \(y'_i = y_i\) when \(0\le i\le n_y\) and \(y'_i = 0\) otherwise. It is the sum of \(X'\) and \(Y'\) that is written to the destination buffer and produces the resulting carry bit.

To add up Big-Endian integers use memadd_BE.

Any or all of the pointers pResult, pX and pY can address the same buffer as any of the other pointers. Other than that, the buffers must not overlap.

If the sizes of the input and output integers are equal, and it is known beforehand whether the buffers are different or same, the preferable faster functions for the operation are memadd_LE_copy and memadd_LE_inplace.

See also
memadd_BE;
memadd_LE_copy;
memadd_LE_inplace;
memincr_LE;
memsub_LE;
memmul_LE;
memfmadd_LE;
memdiv_LE.