Skip to content

Commit

Permalink
Even more cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
xvzcf committed Oct 29, 2024
1 parent a7a7983 commit fc04fdb
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 146 deletions.
71 changes: 37 additions & 34 deletions ml_dsa_65/ref/arithmetic/modular.jinc
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,59 @@ param int INVERSE_OF_MODULUS_MOD_MONTGOMERY_R = 58_728_449;
inline
fn montgomery_reduce(reg u64 a) -> reg u32
{
reg u32 a32;
reg u64 v64;
reg u32 t;

/* t = (int64_t)(int32_t)a*QINV; */
a32 = (32s) a;
v64 = (64s) a32;
v64 *= INVERSE_OF_MODULUS_MOD_MONTGOMERY_R;
t = (32s) v64;

/* t = (a - (int64_t)t*Q) >> 32; */
v64 = (64s) t;
v64 *= -MODULUS;
v64 += a;
v64 >>= 32;
t = (32s) v64;

return t;
reg u32 a32;
reg u64 v64;
reg u32 t;

// t = (int64_t)(int32_t)a*QINV;
a32 = (32s) a;
v64 = (64s) a32;
v64 *= INVERSE_OF_MODULUS_MOD_MONTGOMERY_R;
t = (32s) v64;

// t = (a - (int64_t)t*Q) >> 32;
v64 = (64s) t;
v64 *= -MODULUS;
v64 += a;
v64 >>= 32;
t = (32s) v64;

return t;
}


namespace coefficient {
inline
fn reduce32(reg u32 a) -> reg u32
fn reduce32(reg u32 coefficient) -> reg u32
{
reg u32 t v32;
reg u32 t quotient;

//t = (a + (1 << 22)) >> 23;
t = a;
// t = (coefficient + (1 << 22)) >> 23;
t = coefficient;
t += (1 << 22);
t = t >>s 23;

//t = a - t*Q;
v32 = t;
v32 *= MODULUS;
t = a;
t -= v32;
// t = coefficient - t*Q;
quotient = t;
quotient *= MODULUS;
t = coefficient;
t -= quotient;

return t;
}

inline
fn conditionally_add_modulus(reg u32 a) -> reg u32 {

reg u32 a_add_q;
fn conditionally_add_modulus(reg u32 coefficient) -> reg u32 {
reg u32 add_by;
reg u32 result;

a_add_q = a;
a_add_q += MODULUS;
add_by = coefficient;
add_by >>s= 31;
add_by &= MODULUS;

a = a_add_q if(a <s 0);
result = coefficient;
result += add_by;

return a;
return result;
}
}
23 changes: 8 additions & 15 deletions ml_dsa_65/ref/arithmetic/rounding.jinc
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
require "../parameters.jinc"

namespace coefficient {
fn power2round(reg u32 r)
-> reg u32, reg u32
{
fn power2round(reg u32 r) -> reg u32, reg u32 {
/*
int32_t a1; //high bits

Expand Down Expand Up @@ -86,23 +84,22 @@ namespace coefficient {
}

inline
fn check_norm(reg u32 coefficient, inline int threshold)
-> stack u8
fn check_norm(reg u32 coefficient, inline int threshold) -> stack u8
{
reg u32 sign;
reg u32 sign_mask;
stack u32 c;

stack u8 result;

result = 0;

sign = coefficient;
sign >>s= 31;
sign_mask = coefficient;
sign_mask >>s= 31;

// Compute coefficient = coefficient - (sign & (2 * coefficient));
// Compute coefficient = coefficient - (sign_mask & (2 * coefficient));
c = coefficient;
c <<= 1;
c &= sign;
c &= sign_mask;
coefficient = coefficient - c;

if(coefficient >= threshold) {
Expand All @@ -111,11 +108,7 @@ namespace coefficient {
return result;
}



fn use_hint(reg u32 a hint)
-> reg u32
{
fn use_hint(reg u32 a hint) -> reg u32 {
reg u32 a0;
reg u32 a1;
a0, a1 = decompose(a);
Expand Down
Loading

0 comments on commit fc04fdb

Please sign in to comment.