internal proc renaming

This commit is contained in:
Mamy André-Ratsimbazafy 2019-04-28 14:05:13 +02:00
parent 3e9a03fc02
commit 08e12df4ed
No known key found for this signature in database
GPG Key ID: 7B88AD1FE79492E1
4 changed files with 18 additions and 15 deletions

View File

@ -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

View File

@ -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)

View File

@ -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)

View File

@ -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