Skip to content

Commit 03ca7bc

Browse files
czurniedensjaeckel
authored andcommitted
added tests for Karatsuba and Toom-Cook 3-way
1 parent 75d3c57 commit 03ca7bc

File tree

1 file changed

+136
-1
lines changed

1 file changed

+136
-1
lines changed

demo/test.c

Lines changed: 136 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1883,6 +1883,137 @@ static int test_s_mp_balance_mul(void)
18831883
return EXIT_FAILURE;
18841884
}
18851885

1886+
#define s_mp_mul(a, b, c) s_mp_mul_digs(a, b, c, (a)->used + (b)->used + 1)
1887+
static int test_s_mp_karatsuba_mul(void)
1888+
{
1889+
mp_int a, b, c, d;
1890+
int size, err;
1891+
1892+
if ((err = mp_init_multi(&a, &b, &c, &d, NULL)) != MP_OKAY) {
1893+
goto LTM_ERR;
1894+
}
1895+
for (size = MP_KARATSUBA_MUL_CUTOFF; size < MP_KARATSUBA_MUL_CUTOFF + 20; size++) {
1896+
if ((err = mp_rand(&a, size)) != MP_OKAY) {
1897+
goto LTM_ERR;
1898+
}
1899+
if ((err = mp_rand(&b, size)) != MP_OKAY) {
1900+
goto LTM_ERR;
1901+
}
1902+
if ((err = s_mp_karatsuba_mul(&a, &b, &c)) != MP_OKAY) {
1903+
goto LTM_ERR;
1904+
}
1905+
if ((err = s_mp_mul(&a,&b,&d)) != MP_OKAY) {
1906+
goto LTM_ERR;
1907+
}
1908+
if (mp_cmp(&c, &d) != MP_EQ) {
1909+
fprintf(stderr, "Karatsuba multiplication failed at size %d\n", size);
1910+
goto LTM_ERR;
1911+
}
1912+
}
1913+
1914+
mp_clear_multi(&a, &b, &c, &d, NULL);
1915+
return EXIT_SUCCESS;
1916+
LTM_ERR:
1917+
mp_clear_multi(&a, &b, &c, &d, NULL);
1918+
return EXIT_FAILURE;
1919+
}
1920+
1921+
static int test_s_mp_karatsuba_sqr(void)
1922+
{
1923+
mp_int a, b, c;
1924+
int size, err;
1925+
1926+
if ((err = mp_init_multi(&a, &b, &c, NULL)) != MP_OKAY) {
1927+
goto LTM_ERR;
1928+
}
1929+
for (size = MP_KARATSUBA_SQR_CUTOFF; size < MP_KARATSUBA_SQR_CUTOFF + 20; size++) {
1930+
if ((err = mp_rand(&a, size)) != MP_OKAY) {
1931+
goto LTM_ERR;
1932+
}
1933+
if ((err = s_mp_karatsuba_sqr(&a, &b)) != MP_OKAY) {
1934+
goto LTM_ERR;
1935+
}
1936+
if ((err = s_mp_sqr(&a, &c)) != MP_OKAY) {
1937+
goto LTM_ERR;
1938+
}
1939+
if (mp_cmp(&b, &c) != MP_EQ) {
1940+
fprintf(stderr, "Karatsuba squaring failed at size %d\n", size);
1941+
goto LTM_ERR;
1942+
}
1943+
}
1944+
1945+
mp_clear_multi(&a, &b, &c, NULL);
1946+
return EXIT_SUCCESS;
1947+
LTM_ERR:
1948+
mp_clear_multi(&a, &b, &c, NULL);
1949+
return EXIT_FAILURE;
1950+
}
1951+
1952+
static int test_s_mp_toom_mul(void)
1953+
{
1954+
mp_int a, b, c, d;
1955+
int size, err;
1956+
1957+
if ((err = mp_init_multi(&a, &b, &c, &d, NULL)) != MP_OKAY) {
1958+
goto LTM_ERR;
1959+
}
1960+
for (size = MP_TOOM_MUL_CUTOFF; size < MP_TOOM_MUL_CUTOFF + 20; size++) {
1961+
if ((err = mp_rand(&a, size)) != MP_OKAY) {
1962+
goto LTM_ERR;
1963+
}
1964+
if ((err = mp_rand(&b, size)) != MP_OKAY) {
1965+
goto LTM_ERR;
1966+
}
1967+
if ((err = s_mp_toom_mul(&a, &b, &c)) != MP_OKAY) {
1968+
goto LTM_ERR;
1969+
}
1970+
if ((err = s_mp_mul(&a,&b,&d)) != MP_OKAY) {
1971+
goto LTM_ERR;
1972+
}
1973+
if (mp_cmp(&c, &d) != MP_EQ) {
1974+
fprintf(stderr, "Toom-Cook 3-way multiplication failed at size %d\n", size);
1975+
goto LTM_ERR;
1976+
}
1977+
}
1978+
1979+
mp_clear_multi(&a, &b, &c, &d, NULL);
1980+
return EXIT_SUCCESS;
1981+
LTM_ERR:
1982+
mp_clear_multi(&a, &b, &c, &d, NULL);
1983+
return EXIT_FAILURE;
1984+
}
1985+
1986+
static int test_s_mp_toom_sqr(void)
1987+
{
1988+
mp_int a, b, c;
1989+
int size, err;
1990+
1991+
if ((err = mp_init_multi(&a, &b, &c, NULL)) != MP_OKAY) {
1992+
goto LTM_ERR;
1993+
}
1994+
for (size = MP_TOOM_SQR_CUTOFF; size < MP_TOOM_SQR_CUTOFF + 20; size++) {
1995+
if ((err = mp_rand(&a, size)) != MP_OKAY) {
1996+
goto LTM_ERR;
1997+
}
1998+
if ((err = s_mp_toom_sqr(&a, &b)) != MP_OKAY) {
1999+
goto LTM_ERR;
2000+
}
2001+
if ((err = s_mp_sqr(&a, &c)) != MP_OKAY) {
2002+
goto LTM_ERR;
2003+
}
2004+
if (mp_cmp(&b, &c) != MP_EQ) {
2005+
fprintf(stderr, "Toom-Cook 3-way squaring failed at size %d\n", size);
2006+
goto LTM_ERR;
2007+
}
2008+
}
2009+
2010+
mp_clear_multi(&a, &b, &c, NULL);
2011+
return EXIT_SUCCESS;
2012+
LTM_ERR:
2013+
mp_clear_multi(&a, &b, &c, NULL);
2014+
return EXIT_FAILURE;
2015+
}
2016+
18862017
int unit_tests(int argc, char **argv)
18872018
{
18882019
static const struct {
@@ -1921,7 +2052,11 @@ int unit_tests(int argc, char **argv)
19212052
T(mp_tc_or),
19222053
T(mp_tc_xor),
19232054
T(s_mp_balance_mul),
1924-
T(s_mp_jacobi)
2055+
T(s_mp_jacobi),
2056+
T(s_mp_karatsuba_mul),
2057+
T(s_mp_karatsuba_sqr),
2058+
T(s_mp_toom_mul),
2059+
T(s_mp_toom_sqr)
19252060
#undef T
19262061
};
19272062
unsigned long i;

0 commit comments

Comments
 (0)