-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathpb_copy.c
51 lines (43 loc) · 1.16 KB
/
pb_copy.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/* LibTomPoly, Polynomial Basis Math -- Tom St Denis
*
* LibTomPoly is a public domain library that provides
* polynomial basis arithmetic support. It relies on
* LibTomMath for large integer support.
*
* This library is free for all purposes without any
* express guarantee that it works.
*
* Tom St Denis, [email protected], http://poly.libtomcrypt.org
*/
#include <tompoly.h>
/* dest = src */
int pb_copy(pb_poly *src, pb_poly *dest)
{
int err, x;
/* avoid trivial copies */
if (src == dest) {
return MP_OKAY;
}
/* grow dest as required */
if (dest->alloc < src->used) {
if ((err = pb_grow(dest, src->used)) != MP_OKAY) {
return err;
}
}
/* set the characteristic */
if ((err = mp_copy(&(src->characteristic), &(dest->characteristic))) != MP_OKAY) {
return err;
}
/* copy digits */
for (x = 0; x < src->used; x++) {
if ((err = mp_copy(&(src->terms[x]), &(dest->terms[x]))) != MP_OKAY) {
return err;
}
}
/* zero excess digits */
for (x = src->used; x < dest->used; x++) {
mp_zero(&(dest->terms[x]));
}
dest->used = src->used;
return MP_OKAY;
}