[PATCH] Add mpi_set_cond.
NIIBE Yutaka
gniibe at fsij.org
Wed Feb 11 13:47:55 CET 2015
Hello,
For Curve25519, I added the internal function mpi_swap_cond last year.
I think that we should also have mpi_set_cond.
Adding mpi_set_cond, I revised the implementation of
_gcry_mpi_swap_cond.
OK to commit? I think that it's worth to have those functions in 1.6.
mpi: Add mpi_set_cond.
* mpi/mpiutil.c (_gcry_mpi_set_cond): New.
(_gcry_mpi_swap_cond): Fix types.
* src/mpi.h (mpi_set_cond): New.
diff --git a/mpi/mpiutil.c b/mpi/mpiutil.c
index f74dd91..e2e4db9 100644
--- a/mpi/mpiutil.c
+++ b/mpi/mpiutil.c
@@ -482,6 +482,31 @@ _gcry_mpi_set (gcry_mpi_t w, gcry_mpi_t u)
return w;
}
+gcry_mpi_t
+_gcry_mpi_set_cond (gcry_mpi_t w, const gcry_mpi_t u, unsigned long set)
+{
+ mpi_size_t i;
+ mpi_size_t nlimbs = u->alloced;
+ mpi_limb_t mask = 0UL - !!set;
+ mpi_limb_t x;
+
+ if (w->alloced != u->alloced)
+ log_bug ("mpi_set_cond: different sizes\n");
+
+ for (i = 0; i < nlimbs; i++)
+ {
+ x = mask & (w->d[i] ^ u->d[i]);
+ w->d[i] = w->d[i] ^ x;
+ }
+
+ x = mask & (w->nlimbs ^ u->nlimbs);
+ w->nlimbs = w->nlimbs ^ x;
+
+ x = mask & (w->sign ^ u->sign);
+ w->sign = w->sign ^ x;
+ return w;
+}
+
gcry_mpi_t
_gcry_mpi_set_ui (gcry_mpi_t w, unsigned long u)
@@ -545,10 +570,10 @@ _gcry_mpi_swap (gcry_mpi_t a, gcry_mpi_t b)
void
_gcry_mpi_swap_cond (gcry_mpi_t a, gcry_mpi_t b, unsigned long swap)
{
- size_t i;
- size_t nlimbs = a->alloced;
- unsigned long mask = 0UL - !!swap;
- unsigned long x;
+ mpi_size_t i;
+ mpi_size_t nlimbs = a->alloced;
+ mpi_limb_t mask = 0UL - !!swap;
+ mpi_limb_t x;
if (a->alloced != b->alloced)
log_bug ("mpi_swap_cond: different sizes\n");
diff --git a/src/mpi.h b/src/mpi.h
index 13b5117..0d19f46 100644
--- a/src/mpi.h
+++ b/src/mpi.h
@@ -120,8 +120,11 @@ void _gcry_mpi_immutable_failed (void);
#define mpi_m_check(a) _gcry_mpi_m_check ((a))
#define mpi_const(n) _gcry_mpi_const ((n))
#define mpi_swap_cond(a,b,sw) _gcry_mpi_swap_cond ((a),(b),(sw))
+#define mpi_set_cond(w,u,set) _gcry_mpi_set_cond ((w),(u),(set))
void _gcry_mpi_clear( gcry_mpi_t a );
+gcry_mpi_t _gcry_mpi_set_cond (gcry_mpi_t w, const gcry_mpi_t u,
+ unsigned long swap);
gcry_mpi_t _gcry_mpi_alloc_like( gcry_mpi_t a );
gcry_mpi_t _gcry_mpi_alloc_set_ui( unsigned long u);
void _gcry_mpi_m_check( gcry_mpi_t a );
--
More information about the Gcrypt-devel
mailing list