Skip to content

Commit 2661739

Browse files
committed
add preprocessor define to use an alternate MontyHalfRange add and sub
1 parent 821729c commit 2661739

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

montgomery_arithmetic/include/hurchalla/montgomery_arithmetic/detail/MontyHalfRange.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,17 @@ class MontyHalfRange final :
356356
{
357357
HPBC_PRECONDITION2(isValid(x));
358358
HPBC_PRECONDITION2(isValid(y));
359+
#ifdef HURCHALLA_MONTYHALFRANGE_USE_ALT_ADDSUBS
360+
T tx = static_cast<T>(static_cast<T>(x.get()) + n_);
361+
T ty = static_cast<T>(static_cast<T>(y.get()) + n_);
362+
T n2 = static_cast<T>(n_ + n_);
363+
HPBC_ASSERT2(tx < n2);
364+
HPBC_ASSERT2(ty < n2);
365+
T modsum = ::hurchalla::modular_addition_prereduced_inputs(tx, ty, n2);
366+
return V(static_cast<S>(modsum - n_));
367+
#else
359368
return add(x, getCanonicalValue(y));
369+
#endif
360370
}
361371
HURCHALLA_FORCE_INLINE C add(C cx, C cy) const
362372
{
@@ -381,10 +391,20 @@ class MontyHalfRange final :
381391
{
382392
HPBC_PRECONDITION2(isValid(x));
383393
HPBC_PRECONDITION2(isValid(y));
394+
#ifdef HURCHALLA_MONTYHALFRANGE_USE_ALT_ADDSUBS
395+
T tx = static_cast<T>(static_cast<T>(x.get()) + n_);
396+
T ty = static_cast<T>(static_cast<T>(y.get()) + n_);
397+
T n2 = static_cast<T>(n_ + n_);
398+
HPBC_ASSERT2(tx < n2);
399+
HPBC_ASSERT2(ty < n2);
400+
T diff = ::hurchalla::modular_subtraction_prereduced_inputs(tx, ty, n2);
401+
S result = static_cast<S>(diff - n_);
402+
#else
384403
C cx = getCanonicalValue(x);
385404
C cy = getCanonicalValue(y);
386405
S result = static_cast<S>(
387406
static_cast<S>(cx.get()) - static_cast<S>(cy.get()));
407+
#endif
388408
HPBC_POSTCONDITION2(isValid(V(result)));
389409
return V(result);
390410
}

0 commit comments

Comments
 (0)