diff --git a/constantine/bigints.nim b/constantine/bigints.nim index ac7a5a6..e64ca36 100644 --- a/constantine/bigints.nim +++ b/constantine/bigints.nim @@ -45,12 +45,12 @@ type BaseType* = uint64 # Exported type for conversion in "normal integers" const WordBitSize* = sizeof(Word) * 8 - 1 ## Limbs are 63-bit by default -func words_required(bits: int): int {.compileTime.}= +func wordsRequired(bits: int): int {.compileTime.}= (bits + WordBitSize - 1) div WordBitSize type BigInt*[bits: static int] = object - limbs*: array[bits.words_required, Word] + limbs*: array[bits.wordsRequired, Word] const MaxWord* = (not Ct[uint64](0)) shr 1 ## This represents 0x7F_FF_FF_FF__FF_FF_FF_FF diff --git a/constantine/field_fp.nim b/constantine/field_fp.nim index 12eceb1..9400cec 100644 --- a/constantine/field_fp.nim +++ b/constantine/field_fp.nim @@ -69,8 +69,9 @@ func `+`*(a, b: Fp): Fp = ctl = ctl or not sub(result, Fp.P, CtFalse) sub(result, Fp.P, ctl) -template scaleadd_impl(a: var Fp, c: Word) = - ## Scale-accumulate +template shiftAddImpl(a: var Fp, c: Word) = + ## Shift-accumulate + ## Shift input a by a word and add c. ## ## With a word W = 2^WordBitSize and a field Fp ## Does a <- a * W + c (mod p) @@ -83,7 +84,7 @@ template scaleadd_impl(a: var Fp, c: Word) = # (hi, lo) = a * 2^63 + c let hi = a[0] shr 1 # 64 - 63 = 1 let lo = a[0] shl WordBitSize or c # Assumes most-significant bit in c is not set - unsafe_div2n1n(q, a[0], hi, lo, Fp.P.limbs[0]) # (hi, lo) mod P + unsafeDiv2n1n(q, a[0], hi, lo, Fp.P.limbs[0]) # (hi, lo) mod P else: ## Multiple limbs @@ -111,7 +112,7 @@ template scaleadd_impl(a: var Fp, c: Word) = a_hi = a0 shr 1 # 64 - 63 = 1 a_lo = (a0 shl WordBitSize) or a1 var q, r: Word - q = unsafe_div2n1n(q, r, a_hi, a_lo, p0) # Estimate quotient + q = unsafeDiv2n1n(q, r, a_hi, a_lo, p0) # Estimate quotient q = mux( # If n_hi == divisor a0 == b0, MaxWord, # Quotient == MaxWord (0b0111...1111) mux( @@ -129,7 +130,7 @@ template scaleadd_impl(a: var Fp, c: Word) = block: # q*p qp_hi: Word - unsafe_extendedPrecMul(qp_hi, qp_lo, q, Fp.P[i]) # q * p + unsafeExtendedPrecMul(qp_hi, qp_lo, q, Fp.P[i]) # q * p assert qp_lo.isMsbSet.not assert carry.isMsbSet.not qp_lo += carry # Add carry from previous limb @@ -159,16 +160,18 @@ template scaleadd_impl(a: var Fp, c: Word) = add(a, Fp.P, neg) sub(a, Fp.P, tooBig) -func scaleadd*(a: var Fp, c: Word) = - ## Scale-accumulate modulo P +func shiftAdd*(a: var Fp, c: Word) = + ## Shift-accumulate modulo P + ## Shift input a by a word and add c modulo P ## ## With a word W = 2^WordBitSize and a field Fp ## Does a <- a * W + c (mod p) - scaleadd_impl(a, c) + shiftAddImpl(a, c) -func scaleadd*(a: var Fp, c: static Word) = +func shiftAdd*(a: var Fp, c: static Word) = ## Scale-accumulate modulo P + ## Shift input a by a word and add c modulo P ## ## With a word W = 2^WordBitSize and a field Fp ## Does a <- a * W + c (mod p) - scaleadd_impl(a, c) + shiftAddImpl(a, c) diff --git a/constantine/montgomery.nim b/constantine/montgomery.nim index b557db8..27e13a2 100644 --- a/constantine/montgomery.nim +++ b/constantine/montgomery.nim @@ -77,4 +77,4 @@ func toMonty*[P: static BigInt](a: Fp[P]): Montgomery[P] = result = a for i in static(countdown(P.limbs.high, 0)): - scaleadd(result, 0) + shiftAdd(result, 0) diff --git a/constantine/private/word_types_internal.nim b/constantine/private/word_types_internal.nim index 15eb1f1..dc132c8 100644 --- a/constantine/private/word_types_internal.nim +++ b/constantine/private/word_types_internal.nim @@ -36,7 +36,7 @@ func asm_x86_64_extMul(hi, lo: var uint64, a, b: uint64) {.inline.}= : // no clobbered registers """ -func unsafe_extendedPrecMul*(hi, lo: var Ct[uint64], a, b: Ct[uint64]) {.inline.}= +func unsafeExtendedPrecMul*(hi, lo: var Ct[uint64], a, b: Ct[uint64]) {.inline.}= ## Extended precision multiplication uint64 * uint64 --> uint128 ## ## TODO, at the moment only x86_64 architecture are supported @@ -81,7 +81,7 @@ func asm_x86_64_div2n1n(q, r: var uint64, n_hi, n_lo, d: uint64) {.inline.}= : // no register clobbered besides explicitly used RAX and RDX """ -func unsafe_div2n1n*(q, r: var Ct[uint64], n_hi, n_lo, d: Ct[uint64]) {.inline.}= +func unsafeDiv2n1n*(q, r: var Ct[uint64], n_hi, n_lo, d: Ct[uint64]) {.inline.}= ## Division uint128 by uint64 ## Warning ⚠️ : ## - if n_hi == d, quotient does not fit in an uint64