Skip to content

Commit 79a6b61

Browse files
hanno-beckermkannwischer
authored andcommitted
RV64: Avoid carry-in during Montgomery reduction
Use high subtraction rather than addition in the Montgomery reduction to avoid the possibility of a carry-in from the low half. cf https://eprint.iacr.org/2018/039. Signed-off-by: Hanno Becker <beckphan@amazon.co.uk>
1 parent 0229e67 commit 79a6b61

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
#include "rv64v_debug.h"
1717

1818
/* Montgomery reduction constants */
19-
/* check-magic: 3327 == unsigned_mod(-pow(MLKEM_Q,-1,2^16), 2^16) */
20-
#define MLK_RVV_QI 3327
19+
/* check-magic: -3327 == signed_mod(pow(MLKEM_Q,-1,2^16), 2^16) */
20+
#define MLK_RVV_QI -3327
2121

2222
/* check-magic: 2285 == unsigned_mod(2^16, MLKEM_Q) */
2323
#define MLK_RVV_MONT_R1 2285
@@ -31,12 +31,10 @@
3131
static inline vint16m1_t fq_redc(vint16m1_t rh, vint16m1_t rl, size_t vl)
3232
{
3333
vint16m1_t t;
34-
vbool16_t c;
3534

36-
t = __riscv_vmul_vx_i16m1(rl, MLK_RVV_QI, vl); /* t = l * -Q^-1 */
35+
t = __riscv_vmul_vx_i16m1(rl, MLK_RVV_QI, vl); /* t = l * Q^-1 */
3736
t = __riscv_vmulh_vx_i16m1(t, MLKEM_Q, vl); /* t = (t*Q) / R */
38-
c = __riscv_vmsne_vx_i16m1_b16(rl, 0, vl); /* c = (l != 0) */
39-
t = __riscv_vadc_vvm_i16m1(t, rh, c, vl); /* t += h + c */
37+
t = __riscv_vsub_vv_i16m1(rh, t, vl); /* t = h - t */
4038

4139
return t;
4240
}
@@ -48,9 +46,9 @@ static inline vint16m1_t fq_redc2(vint32m2_t z, size_t vl)
4846
vint16m1_t t;
4947

5048
t = __riscv_vmul_vx_i16m1(__riscv_vncvt_x_x_w_i16m1(z, vl), MLK_RVV_QI,
51-
vl); /* t = l * -Q^-1 */
52-
z = __riscv_vadd_vv_i32m2(z, __riscv_vwmul_vx_i32m2(t, MLKEM_Q, vl),
53-
vl); /* x = (x + (t*Q)) */
49+
vl); /* t = l * Q^-1 */
50+
z = __riscv_vsub_vv_i32m2(z, __riscv_vwmul_vx_i32m2(t, MLKEM_Q, vl),
51+
vl); /* x = (x - (t*Q)) */
5452
t = __riscv_vnsra_wx_i16m1(z, 16, vl);
5553

5654
return t;

0 commit comments

Comments
 (0)