chsvlib
chsv helper source code

◆ memsub_LE()

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

Computes a difference between two arbitrary-precision integers specified with vectors in the Little-Endian byte order and returns the borrow bit.

Parameters
[out]pResultA pointer to a buffer receiving the difference. 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 subtrahend. The buffer can be the same as one pointed to by pResult and/or pY.
cbXSize of the subtrahend in bytes.
[in]pYA pointer to the minuend.
cbYSize of the minuend in bytes.
Returns
A value of a borrow bit which results from the subtraction.

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 subtraction of the integer \(Y=\sum_{i=0}^{n_y}y_i\cdot b^i\) from the integer \(X = \sum_{i=0}^{n_x}x_i\cdot b^i\). Furthermore, the difference 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 subtract 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 subtracts them writing the difference into the destination buffer pResult and returning the borrow 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 difference of \(Y'\) from \(X'\) that is written to the destination buffer and produces the resulting borrow bit.

To subtract Big-Endian integers use memsub_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 memsub_LE_copy, memsub_LE_inplace and memnegsub_LE_inplace.

See also
memsub_BE;
memsub_LE_copy;
memsub_LE_inplace;
memnegsub_LE_inplace;
memdecr_LE;
memneg_LE;
memadd_LE;
memmul_LE;
memfmsub_LE;
memdiv_LE.