From 782be63ce7a40f83441a9715395018e39c860d21 Mon Sep 17 00:00:00 2001 From: "Diego F. Aranha" Date: Mon, 30 Dec 2024 02:11:31 +0100 Subject: [PATCH] More robust GMP backends. --- src/low/gmp-sec/relic_bn_shift_low.c | 18 +++++++++++++++--- ...lic_bn_shift_low.c => relic_bn_shift_low.c} | 17 +++++++++++++++-- 2 files changed, 30 insertions(+), 5 deletions(-) rename src/low/gmp/{_relic_bn_shift_low.c => relic_bn_shift_low.c} (89%) diff --git a/src/low/gmp-sec/relic_bn_shift_low.c b/src/low/gmp-sec/relic_bn_shift_low.c index 57ceff773..459f99b5c 100644 --- a/src/low/gmp-sec/relic_bn_shift_low.c +++ b/src/low/gmp-sec/relic_bn_shift_low.c @@ -1,6 +1,6 @@ /* * RELIC is an Efficient LIbrary for Cryptography - * Copyright (c) 2015 RELIC Authors + * Copyright (c) 2009 RELIC Authors * * This file is part of RELIC. RELIC is legal property of its developers, * whose names are not listed here. Please refer to the COPYRIGHT file @@ -34,8 +34,10 @@ #include #include +#include "relic_dv.h" #include "relic_bn.h" #include "relic_bn_low.h" +#include "relic_alloc.h" /*============================================================================*/ /* Public definitions */ @@ -46,7 +48,12 @@ dig_t bn_lsh1_low(dig_t *c, const dig_t *a, size_t size) { } dig_t bn_lshb_low(dig_t *c, const dig_t *a, size_t size, uint_t bits) { - return mpn_lshift(c, a, size, bits); + dig_t carry, *t = (dig_t *)RLC_ALLOCA(dig_t, size); + carry = mpn_lshift(t, a, size, bits); + dv_copy(c, a, size); + dv_copy_sec(c, t, size, bits > 0); + RLC_FREE(t); + return RLC_SEL(0, carry, bits > 0); } dig_t bn_rsh1_low(dig_t *c, const dig_t *a, size_t size) { @@ -54,7 +61,12 @@ dig_t bn_rsh1_low(dig_t *c, const dig_t *a, size_t size) { } dig_t bn_rshb_low(dig_t *c, const dig_t *a, size_t size, uint_t bits) { - return mpn_rshift(c, a, size, bits); + dig_t carry, *t = (dig_t *)RLC_ALLOCA(dig_t, size); + carry = mpn_rshift(t, a, size, bits); + dv_copy(c, a, size); + dv_copy_sec(c, t, size, bits > 0); + RLC_FREE(t); + return RLC_SEL(0, carry, bits > 0); } dig_t bn_rshs_low(dig_t *c, const dig_t *a, size_t size, uint_t bits) { diff --git a/src/low/gmp/_relic_bn_shift_low.c b/src/low/gmp/relic_bn_shift_low.c similarity index 89% rename from src/low/gmp/_relic_bn_shift_low.c rename to src/low/gmp/relic_bn_shift_low.c index b65acc1dd..2954ca709 100644 --- a/src/low/gmp/_relic_bn_shift_low.c +++ b/src/low/gmp/relic_bn_shift_low.c @@ -34,6 +34,7 @@ #include #include +#include "relic_dv.h" #include "relic_bn.h" #include "relic_bn_low.h" @@ -46,7 +47,13 @@ dig_t bn_lsh1_low(dig_t *c, const dig_t *a, size_t size) { } dig_t bn_lshb_low(dig_t *c, const dig_t *a, size_t size, uint_t bits) { - return mpn_lshift(c, a, size, bits); + dig_t carry = 0; + if (bits > 0) { + carry = mpn_lshift(c, a, size, bits); + } else { + dv_copy(c, a, size); + } + return carry; } dig_t bn_rsh1_low(dig_t *c, const dig_t *a, size_t size) { @@ -54,7 +61,13 @@ dig_t bn_rsh1_low(dig_t *c, const dig_t *a, size_t size) { } dig_t bn_rshb_low(dig_t *c, const dig_t *a, size_t size, uint_t bits) { - return mpn_rshift(c, a, size, bits); + dig_t carry = 0; + if (bits > 0) { + carry = mpn_rshift(c, a, size, bits); + } else { + dv_copy(c, a, size); + } + return carry; } dig_t bn_rshs_low(dig_t *c, const dig_t *a, size_t size, uint_t bits) {