Implement mpi_{add,sub}() in terms of mpi_{add,sub}_abs().

This commit is contained in:
Dag-Erling Smørgrav 2015-01-07 18:59:44 +00:00 committed by des
parent cddb6c0e02
commit d0fb2359f1
3 changed files with 57 additions and 0 deletions

View file

@ -55,7 +55,9 @@ int mpi_set(cryb_mpi *, int32_t);
int mpi_lshift(cryb_mpi *, unsigned int); int mpi_lshift(cryb_mpi *, unsigned int);
int mpi_rshift(cryb_mpi *, unsigned int); int mpi_rshift(cryb_mpi *, unsigned int);
int mpi_add_abs(cryb_mpi *, cryb_mpi *, cryb_mpi *); int mpi_add_abs(cryb_mpi *, cryb_mpi *, cryb_mpi *);
int mpi_add(cryb_mpi *, cryb_mpi *, cryb_mpi *);
int mpi_sub_abs(cryb_mpi *, cryb_mpi *, cryb_mpi *); int mpi_sub_abs(cryb_mpi *, cryb_mpi *, cryb_mpi *);
int mpi_sub(cryb_mpi *, cryb_mpi *, cryb_mpi *);
int mpi_cmp_abs(cryb_mpi *, cryb_mpi *); int mpi_cmp_abs(cryb_mpi *, cryb_mpi *);
int mpi_cmp(cryb_mpi *, cryb_mpi *); int mpi_cmp(cryb_mpi *, cryb_mpi *);
int mpi_eq_abs(cryb_mpi *, cryb_mpi *); int mpi_eq_abs(cryb_mpi *, cryb_mpi *);

View file

@ -97,3 +97,31 @@ mpi_add_abs(cryb_mpi *X, cryb_mpi *A, cryb_mpi *B)
X->msb += i * 32 + 1; X->msb += i * 32 + 1;
return (0); return (0);
} }
/*
* Add two numbers together.
*/
int
mpi_add(cryb_mpi *X, cryb_mpi *A, cryb_mpi *B)
{
if (A->neg && B->neg) {
if (mpi_add_abs(X, A, B) < 0)
return (-1);
X->neg = 1;
} else if (A->neg) {
if (mpi_sub_abs(X, A, B) < 0)
return (-1);
X->neg = (mpi_cmp_abs(A, B) < 0);
} else if (B->neg) {
if (mpi_sub_abs(X, A, B) < 0)
return (-1);
X->neg = (mpi_cmp_abs(A, B) > 0);
} else {
if (mpi_add_abs(X, A, B) < 0)
return (-1);
X->neg = 0;
}
return (0);
}

View file

@ -92,3 +92,30 @@ mpi_sub_abs(cryb_mpi *X, cryb_mpi *A, cryb_mpi *B)
X->msb += i * 32 + 1; X->msb += i * 32 + 1;
return (0); return (0);
} }
/*
* Subtract one number from another.
*/
int
mpi_sub(cryb_mpi *X, cryb_mpi *A, cryb_mpi *B)
{
if (A->neg && B->neg) {
if (mpi_sub_abs(X, A, B) < 0)
return (-1);
X->neg = (mpi_cmp_abs(A, B) > 0);
} else if (A->neg) {
if (mpi_add_abs(X, A, B) < 0)
return (-1);
X->neg = 1;
} else if (B->neg) {
if (mpi_add_abs(X, A, B) < 0)
return (-1);
X->neg = 0;
} else {
if (mpi_sub_abs(X, A, B) < 0)
return (-1);
X->neg = (mpi_cmp_abs(A, B) < 0);
}
return (0);
}