Commit b2f3d2e8 authored by davxy's avatar davxy

MPI multiplication

Implementation is absolutelly inefficient, but works.
Used as a placeholder for future better implementations.
parent 6e11a99d
......@@ -29,6 +29,7 @@ Given a version number MAJOR.MINOR.PATCH
- LRC
- 8-bit trivial checksum
- CRC16-IBM
- MPI add, sub, mul
[0.0.1] - 2014-05-10
--------------------
......
......@@ -30,7 +30,8 @@ OBJS := src/version.o \
src/mpi/mpi_add.o \
src/mpi/mpi_sub.o \
src/mpi/mpi_bin.o \
src/mpi/mpi_list.o
src/mpi/mpi_list.o \
src/mpi/mpi_mul.o
.PHONY: all clean
......
......@@ -3,6 +3,7 @@ CRY(T_T)
A small crypto library
Block ciphers
-------------
......@@ -16,11 +17,13 @@ Block ciphers
- GCM
- CTR
Message authentication code
---------------------------
- CMAC
Hash
----
......@@ -39,6 +42,13 @@ Hash
- LRC
- 8-bit trivial checksum
Multiple precision integers
---------------------------
- Basic arithmentic
Utilities
---------
......
......@@ -88,8 +88,11 @@ int cry_mpi_add_abs(cry_mpi *r, const cry_mpi *a, const cry_mpi *b);
int cry_mpi_add(cry_mpi *r, const cry_mpi *a, const cry_mpi *b);
int cry_mpi_sub_abs(cry_mpi *r, const cry_mpi *a, const cry_mpi *b);
int cry_mpi_sub(cry_mpi *r, const cry_mpi *a, const cry_mpi *b);
int cry_mpi_mul(cry_mpi *r, const cry_mpi *a, const cry_mpi *b);
/*
* Utilities
*/
......@@ -110,6 +113,12 @@ size_t cry_mpi_count_bits(const cry_mpi *a);
#define cry_mpi_count_bytes(a) \
((cry_mpi_count_bits(a) + 7) / 8)
#define cry_mpi_swap(a, b) do { \
cry_mpi __t = *(a); \
*(a) = *(b); \
*(b) = __t; \
} while(0)
#ifdef __cplusplus
}
#endif
......
/*
* Copyright (c) 2013-2014, Davide Galassi. All rights reserved.
*
* This file is part of CRY software.
*
* CRY is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with CRY; if not, see <http://www.gnu/licenses/>.
*/
#include "mpi_pvt.h"
int cry_mpi_mul(cry_mpi *r, const cry_mpi *a, const cry_mpi *b)
{
int res;
int sign = a->sign ^ b->sign;
cry_mpi t, c, one;
if (a < b) {
const cry_mpi *tmp = a;
a = b;
b == tmp;
}
if ((res = cry_mpi_init_list(&t, &one, &c, NULL)) != 0)
return res;
if ((res = cry_mpi_copy(&c, b)) != 0)
goto e;
one.data[0] = 1;
one.used = 1;
one.sign = 0;
while (c.used != 0) {
if ((res = cry_mpi_add_abs(&t, &t, a)) != 0 ||
(res = cry_mpi_sub_abs(&c, &c, &one)) != 0)
goto e;
}
cry_mpi_swap(&t, r);
r->sign = sign;
e: cry_mpi_clear_list(&t, &one, &c, NULL);
return 0;
}
......@@ -158,7 +158,21 @@ static void sub_test(void)
cry_mpi_clear(&r);
}
static void mul_test(void)
{
cry_mpi a, b, r;
printf("> MPI sub-test\n");
cry_mpi_init_list(&a, &b, &r, NULL);
cry_mpi_set_int(&a, 0x1234);
cry_mpi_set_int(&b, 0x02);
cry_mpi_mul(&r, &a, &b);
MPI_PRINT(&a, "a");
MPI_PRINT(&b, "b");
MPI_PRINT(&r, "a * b");
cry_mpi_clear_list(&a, &b, &r, NULL);
}
int main(void)
{
......@@ -168,6 +182,7 @@ int main(void)
sub_test();
init_bin_test();
init_list_test();
mul_test();
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment