|
@@ -7,46 +7,52 @@
|
|
|
|
|
|
#include <gmp.h>
|
|
#include <gmp.h>
|
|
|
|
|
|
|
|
+#include "ddlog.h"
|
|
#include "elgamal.h"
|
|
#include "elgamal.h"
|
|
#include "entropy.h"
|
|
#include "entropy.h"
|
|
#include "rms.h"
|
|
#include "rms.h"
|
|
#include "hss.h"
|
|
#include "hss.h"
|
|
#include "timeit.h"
|
|
#include "timeit.h"
|
|
|
|
|
|
-#define strip_size 16
|
|
|
|
-uint32_t naif_convert(mpz_t n)
|
|
|
|
-{
|
|
|
|
- uint32_t i;
|
|
|
|
- mpz_t t;
|
|
|
|
- mpz_init_set_ui(t, 1);
|
|
|
|
- mpz_mul_2exp(t, t, 1536-strip_size);
|
|
|
|
-
|
|
|
|
|
|
|
|
- for (i = 0; mpz_cmp(n, t) > -1; i++) {
|
|
|
|
- mpz_mul_ui(n, n, 2);
|
|
|
|
- mpz_mod(n, n, p);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- mpz_clear(t);
|
|
|
|
- return i;
|
|
|
|
|
|
+static inline
|
|
|
|
+void fbpowm(mpz_t rop, const_fbptable_t T, const uint32_t exp)
|
|
|
|
+{
|
|
|
|
+ const uint8_t *e = (uint8_t *) &exp;
|
|
|
|
+
|
|
|
|
+ mpz_mul(rop, T[0][e[0]], T[1][e[1]]);
|
|
|
|
+ mpz_mod(rop, rop, p);
|
|
|
|
+ mpz_mul(rop, rop, T[2][e[2]]);
|
|
|
|
+ mpz_mod(rop, rop, p);
|
|
|
|
+ mpz_mul(rop, rop, T[3][e[3]]);
|
|
|
|
+ mpz_mod(rop, rop, p);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
static inline
|
|
static inline
|
|
uint32_t __mul_single(mpz_t op1,
|
|
uint32_t __mul_single(mpz_t op1,
|
|
mpz_t op2,
|
|
mpz_t op2,
|
|
const mpz_t c1,
|
|
const mpz_t c1,
|
|
const mpz_t c2,
|
|
const mpz_t c2,
|
|
- const mpz_t x,
|
|
|
|
|
|
+ const_fbptable_t T,
|
|
|
|
+ const uint32_t x,
|
|
const mpz_t cx)
|
|
const mpz_t cx)
|
|
{
|
|
{
|
|
|
|
|
|
mpz_powm(op1, c1, cx, p);
|
|
mpz_powm(op1, c1, cx, p);
|
|
mpz_invert(op1, op1, p);
|
|
mpz_invert(op1, op1, p);
|
|
|
|
|
|
- mpz_powm(op2, c2, x, p);
|
|
|
|
|
|
+ //mpz_t test; mpz_init(test);
|
|
|
|
+ //mpz_powm_ui(test, c2, x, p);
|
|
|
|
+ fbpowm(op2, T, x);
|
|
|
|
+ //if (mpz_cmp(test, op2)) gmp_printf("base: %Zx\nexp: %x\npcomp: %Zx\nreal: %Zd\n", c2, x, op2, test);
|
|
|
|
+ //mpz_clear(test);
|
|
|
|
+
|
|
|
|
+ mpz_powm_ui(op2, c2, x, p);
|
|
mpz_mul(op2, op2, op1);
|
|
mpz_mul(op2, op2, op1);
|
|
mpz_mod(op2, op2, p);
|
|
mpz_mod(op2, op2, p);
|
|
- return naif_convert(op2);
|
|
|
|
|
|
+ const uint32_t converted = convert(op2->_mp_d);
|
|
|
|
+ return converted;
|
|
}
|
|
}
|
|
|
|
|
|
void hss_mul(ssl2_t rop, const ssl1_t sl1, const ssl2_t sl2)
|
|
void hss_mul(ssl2_t rop, const ssl1_t sl1, const ssl2_t sl2)
|
|
@@ -56,13 +62,21 @@ void hss_mul(ssl2_t rop, const ssl1_t sl1, const ssl2_t sl2)
|
|
mpz_inits(op1, op2, NULL);
|
|
mpz_inits(op1, op2, NULL);
|
|
|
|
|
|
converted = __mul_single(op1, op2,
|
|
converted = __mul_single(op1, op2,
|
|
- sl1->w->c1, sl1->w->c2, sl2->x, sl2->cx);
|
|
|
|
- mpz_set_ui(rop->x, converted);
|
|
|
|
|
|
+ sl1->w->c1,
|
|
|
|
+ sl1->w->c2,
|
|
|
|
+ sl1->T,
|
|
|
|
+ sl2->x,
|
|
|
|
+ sl2->cx);
|
|
|
|
+ rop->x = converted;
|
|
|
|
|
|
mpz_set_ui(rop->cx, 0);
|
|
mpz_set_ui(rop->cx, 0);
|
|
for (size_t t = 0; t < 160; t++) {
|
|
for (size_t t = 0; t < 160; t++) {
|
|
converted = __mul_single(op1, op2,
|
|
converted = __mul_single(op1, op2,
|
|
- sl1->cw[t]->c1, sl1->cw[t]->c2, sl2->x, sl2->cx);
|
|
|
|
|
|
+ sl1->cw[t]->c1,
|
|
|
|
+ sl1->cw[t]->c2,
|
|
|
|
+ sl1->T,
|
|
|
|
+ sl2->x,
|
|
|
|
+ sl2->cx);
|
|
mpz_add_ui(rop->cx, rop->cx, converted);
|
|
mpz_add_ui(rop->cx, rop->cx, converted);
|
|
mpz_mul_2exp(rop->cx, rop->cx, 1);
|
|
mpz_mul_2exp(rop->cx, rop->cx, 1);
|
|
}
|
|
}
|
|
@@ -75,6 +89,7 @@ int main()
|
|
{
|
|
{
|
|
mpz_entropy_init();
|
|
mpz_entropy_init();
|
|
hss_init();
|
|
hss_init();
|
|
|
|
+ dlog_precompute();
|
|
|
|
|
|
mpz_t test;
|
|
mpz_t test;
|
|
mpz_init(test);
|
|
mpz_init(test);
|
|
@@ -96,7 +111,6 @@ int main()
|
|
ssl2_init(t1);
|
|
ssl2_init(t1);
|
|
ssl2_init(t2);
|
|
ssl2_init(t2);
|
|
|
|
|
|
-
|
|
|
|
INIT_TIMEIT();
|
|
INIT_TIMEIT();
|
|
for (int i = 0; i < (int) 1e1; i++) {
|
|
for (int i = 0; i < (int) 1e1; i++) {
|
|
|
|
|
|
@@ -119,7 +133,7 @@ int main()
|
|
hss_mul(t2, r2, s2);
|
|
hss_mul(t2, r2, s2);
|
|
#ifndef NDEBUG
|
|
#ifndef NDEBUG
|
|
gmp_printf("%Zx %Zx\n", x, y);
|
|
gmp_printf("%Zx %Zx\n", x, y);
|
|
- gmp_printf("%Zx %Zx\n", s1->x, s2->x);
|
|
|
|
|
|
+ gmp_printf("%d %d\n", s1->x, s2->x);
|
|
#endif
|
|
#endif
|
|
|
|
|
|
mpz_mul(xy, x, y);
|
|
mpz_mul(xy, x, y);
|