Skip to content

Commit ecb4cef

Browse files
committed
RV64: Make cast from unsigned->signed int explicit in rej_uniform
This is required by `-Wconversion`, and is indeed worth a comment since a cast from `unsigned` to `int` can in theory overflow. Also, unify unsigned types used in `rej_uniform` to `unsigned`, and add explicit casts where the RVV intrinsics return `unsigned long`. Signed-off-by: Hanno Becker <[email protected]>
1 parent 9bf23a0 commit ecb4cef

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

mlkem/src/native/riscv64/meta.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ static MLK_INLINE int mlk_rej_uniform_native(int16_t *r, unsigned len,
6161
const uint8_t *buf,
6262
unsigned buflen)
6363
{
64-
return mlk_rv64v_rej_uniform(r, len, buf, buflen);
64+
/* The cast from unsigned to signed integer is safe
65+
* because the return value is <= len, which we asssume
66+
* to be bound by 4096 and hence <= INT_MAX. */
67+
return (int) mlk_rv64v_rej_uniform(r, len, buf, buflen);
6568
}
6669

6770
static MLK_INLINE int mlk_poly_reduce_native(int16_t data[MLKEM_N])

mlkem/src/native/riscv64/src/rv64v_poly.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ void mlk_rv64v_poly_reduce(int16_t *r)
734734
unsigned int mlk_rv64v_rej_uniform(int16_t *r, unsigned int len,
735735
const uint8_t *buf, unsigned int buflen)
736736
{
737-
size_t n, ctr, pos;
737+
unsigned n, ctr, pos;
738738
vuint16m1_t x, y;
739739
vbool16_t lt;
740740

@@ -743,8 +743,8 @@ unsigned int mlk_rv64v_rej_uniform(int16_t *r, unsigned int len,
743743

744744
while (ctr < len && pos < buflen)
745745
{
746-
const size_t vl = __riscv_vsetvl_e16m1((buflen - pos) * 8 / 12);
747-
const size_t vl23 = (vl * 24) / 32;
746+
const unsigned vl = (unsigned) __riscv_vsetvl_e16m1((buflen - pos) * 8 / 12);
747+
const unsigned vl23 = (vl * 24) / 32;
748748

749749
const vuint16m1_t vid = __riscv_vid_v_u16m1(vl);
750750
const vuint16m1_t srl12v = __riscv_vmul_vx_u16m1(vid, 12, vl);
@@ -766,7 +766,7 @@ unsigned int mlk_rv64v_rej_uniform(int16_t *r, unsigned int len,
766766

767767
lt = __riscv_vmsltu_vx_u16m1_b16(x, MLKEM_Q, vl);
768768
y = __riscv_vcompress_vm_u16m1(x, lt, vl);
769-
n = __riscv_vcpop_m_b16(lt, vl);
769+
n = (unsigned) __riscv_vcpop_m_b16(lt, vl);
770770

771771
if (ctr + n > len)
772772
{

0 commit comments

Comments
 (0)