From 13ea02f16c9cdb87c74dd35c63e25ffa96718483 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Wed, 22 Apr 2015 19:54:20 -0700 Subject: add-mp-hash patch * mpi/mpi.c (mp_hash): New function. * mpi/mpi.c (mp_hash): Declared. --- mpi/mpi.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'mpi/mpi.c') diff --git a/mpi/mpi.c b/mpi/mpi.c index 26334854..48c7203f 100644 --- a/mpi/mpi.c +++ b/mpi/mpi.c @@ -1958,6 +1958,34 @@ int mp_iseven(mp_int *a) /* }}} */ +unsigned long mp_hash(mp_int *a) +{ +#if SIZEOF_LONG > MP_DIGIT_SIZE + unsigned long hash; + int ix; + + if (USED(a) >= 2 * SIZEOF_LONG / MP_DIGIT_SIZE) { + unsigned long omega = 0; + unsigned long alpha = 0; + for (ix = 0; ix < SIZEOF_LONG / MP_DIGIT_SIZE; ix++) + omega = (omega << MP_DIGIT_BIT) | DIGIT(a, ix); + for (ix = USED(a) - SIZEOF_LONG / MP_DIGIT_SIZE; ix < USED(a); ix++) + alpha = (alpha << MP_DIGIT_BIT) | DIGIT(a, ix); + hash = alpha + omega; + } else { + hash = 0; + + for (ix = 0; ix < USED(a); ix++) + hash = (hash << MP_DIGIT_BIT) | DIGIT(a, ix); + } +#else + mp_digit omega = DIGIT(a, 0); + mp_digit alpha = DIGIT(a, USED(a) - 1); + unsigned long hash = alpha + omega; +#endif + return SIGN(a) == MP_NEG ? ~hash : hash; +} + /*------------------------------------------------------------------------*/ /* {{{ Number theoretic functions */ -- cgit v1.2.3