Replace HighLimb by MaxWord as well

This commit is contained in:
mratsim 2018-12-02 19:32:52 +01:00
parent 9b1dc4ca30
commit b496f57c68
2 changed files with 9 additions and 9 deletions

View File

@ -50,7 +50,7 @@ type
BigInt*[bits: static int] = object
limbs*: array[bits.words_required, Word]
const HighLimb* = (not Ct[uint64](0)) shr 1
const MaxWord* = (not Ct[uint64](0)) shr 1
## This represents 0x7F_FF_FF_FF__FF_FF_FF_FF
## also 0b0111...1111
## This biggest representable number in our limbs.
@ -81,7 +81,7 @@ template addImpl[bits](result: CTBool[Word], a: var BigInt[bits], b: BigInt[bits
for i in static(0 ..< a.limbs.len):
let new_a = a.limbs[i] + b.limbs[i] + Word(result)
result = new_a.isMsbSet()
a[i] = ctl.mux(new_a and HighLimb, a)
a[i] = ctl.mux(new_a and MaxWord, a)
func add*[bits](a: var BigInt[bits], b: BigInt[bits], ctl: CTBool[Word]): CTBool[Word] =
## Constant-time big integer in-place addition
@ -99,7 +99,7 @@ template subImpl[bits](result: CTBool[Word], a: var BigInt[bits], b: BigInt[bits
for i in static(0 ..< a.limbs.len):
let new_a = a.limbs[i] - b.limbs[i] - Word(result)
result = new_a.isMsbSet()
a[i] = ctl.mux(new_a and HighLimb, a)
a[i] = ctl.mux(new_a and MaxWord, a)
func sub*[bits](a: var BigInt[bits], b: BigInt[bits], ctl: CTBool[Word]): CTBool[Word] =
## Constant-time big integer in-place addition

View File

@ -97,11 +97,11 @@ template scaleadd_impl(a: var Fp, c: Word) =
a[0] = c # and replace the first one by c
const p0 = Fp.P[^1]
else: # Need to deal with partial word shifts at the edge.
let a1 = ((a[^2] shl (WordBitSize-R)) or (a[^3] shr R)) and HighLimb
let a0 = ((a[^1] shl (WordBitSize-R)) or (a[^2] shr R)) and HighLimb
let a1 = ((a[^2] shl (WordBitSize-R)) or (a[^3] shr R)) and MaxWord
let a0 = ((a[^1] shl (WordBitSize-R)) or (a[^2] shr R)) and MaxWord
moveMem(a[1], a[0], (len-1) * Word.sizeof)
a[0] = c
const p0 = ((Fp.P[^1] shl (WordBitSize-R)) or (Fp.P[^2] shr R)) and HighLimb
const p0 = ((Fp.P[^1] shl (WordBitSize-R)) or (Fp.P[^2] shr R)) and MaxWord
# p0 has its high bit set. (a0, a1)/p0 fits in a limb.
# Get a quotient q, at most we will be 2 iterations off
@ -113,7 +113,7 @@ template scaleadd_impl(a: var Fp, c: Word) =
var q, r: Word
q = unsafe_div2n1n(q, r, a_hi, a_lo, p0) # Estimate quotient
q = mux( # If n_hi == divisor
a0 == b0, HighLimb, # Quotient == HighLimb (0b0111...1111)
a0 == b0, MaxWord, # Quotient == MaxWord (0b0111...1111)
mux(
q == 0, 0, # elif q == 0, true quotient = 0
q - 1 # else instead of being of by 0, 1 or 2
@ -136,12 +136,12 @@ template scaleadd_impl(a: var Fp, c: Word) =
let qp_carry = qp_lo.isMsbSet
carry = mux(qp_carry, qp_hi + Word(1), qp_hi) # New carry
qp_lo = qp_lo and HighLimb # Normalize to u63
qp_lo = qp_lo and MaxWord # Normalize to u63
block: # a*2^63 - q*p
a[i] -= qp_lo
carry += Word(a[i].isMsbSet) # Adjust if borrow
a[i] = a[i] and HighLimb # Normalize to u63
a[i] = a[i] and MaxWord # Normalize to u63
over_p = mux(
a[i] == Fp.P[i], over_p,