internal proc renaming
This commit is contained in:
parent
3e9a03fc02
commit
08e12df4ed
|
@ -45,12 +45,12 @@ type BaseType* = uint64 # Exported type for conversion in "normal integers"
|
||||||
const WordBitSize* = sizeof(Word) * 8 - 1
|
const WordBitSize* = sizeof(Word) * 8 - 1
|
||||||
## Limbs are 63-bit by default
|
## Limbs are 63-bit by default
|
||||||
|
|
||||||
func words_required(bits: int): int {.compileTime.}=
|
func wordsRequired(bits: int): int {.compileTime.}=
|
||||||
(bits + WordBitSize - 1) div WordBitSize
|
(bits + WordBitSize - 1) div WordBitSize
|
||||||
|
|
||||||
type
|
type
|
||||||
BigInt*[bits: static int] = object
|
BigInt*[bits: static int] = object
|
||||||
limbs*: array[bits.words_required, Word]
|
limbs*: array[bits.wordsRequired, Word]
|
||||||
|
|
||||||
const MaxWord* = (not Ct[uint64](0)) shr 1
|
const MaxWord* = (not Ct[uint64](0)) shr 1
|
||||||
## This represents 0x7F_FF_FF_FF__FF_FF_FF_FF
|
## This represents 0x7F_FF_FF_FF__FF_FF_FF_FF
|
||||||
|
|
|
@ -69,8 +69,9 @@ func `+`*(a, b: Fp): Fp =
|
||||||
ctl = ctl or not sub(result, Fp.P, CtFalse)
|
ctl = ctl or not sub(result, Fp.P, CtFalse)
|
||||||
sub(result, Fp.P, ctl)
|
sub(result, Fp.P, ctl)
|
||||||
|
|
||||||
template scaleadd_impl(a: var Fp, c: Word) =
|
template shiftAddImpl(a: var Fp, c: Word) =
|
||||||
## Scale-accumulate
|
## Shift-accumulate
|
||||||
|
## Shift input a by a word and add c.
|
||||||
##
|
##
|
||||||
## With a word W = 2^WordBitSize and a field Fp
|
## With a word W = 2^WordBitSize and a field Fp
|
||||||
## Does a <- a * W + c (mod p)
|
## 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
|
# (hi, lo) = a * 2^63 + c
|
||||||
let hi = a[0] shr 1 # 64 - 63 = 1
|
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
|
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:
|
else:
|
||||||
## Multiple limbs
|
## Multiple limbs
|
||||||
|
@ -111,7 +112,7 @@ template scaleadd_impl(a: var Fp, c: Word) =
|
||||||
a_hi = a0 shr 1 # 64 - 63 = 1
|
a_hi = a0 shr 1 # 64 - 63 = 1
|
||||||
a_lo = (a0 shl WordBitSize) or a1
|
a_lo = (a0 shl WordBitSize) or a1
|
||||||
var q, r: Word
|
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
|
q = mux( # If n_hi == divisor
|
||||||
a0 == b0, MaxWord, # Quotient == MaxWord (0b0111...1111)
|
a0 == b0, MaxWord, # Quotient == MaxWord (0b0111...1111)
|
||||||
mux(
|
mux(
|
||||||
|
@ -129,7 +130,7 @@ template scaleadd_impl(a: var Fp, c: Word) =
|
||||||
|
|
||||||
block: # q*p
|
block: # q*p
|
||||||
qp_hi: Word
|
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 qp_lo.isMsbSet.not
|
||||||
assert carry.isMsbSet.not
|
assert carry.isMsbSet.not
|
||||||
qp_lo += carry # Add carry from previous limb
|
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)
|
add(a, Fp.P, neg)
|
||||||
sub(a, Fp.P, tooBig)
|
sub(a, Fp.P, tooBig)
|
||||||
|
|
||||||
func scaleadd*(a: var Fp, c: Word) =
|
func shiftAdd*(a: var Fp, c: Word) =
|
||||||
## Scale-accumulate modulo P
|
## 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
|
## With a word W = 2^WordBitSize and a field Fp
|
||||||
## Does a <- a * W + c (mod p)
|
## 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
|
## 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
|
## With a word W = 2^WordBitSize and a field Fp
|
||||||
## Does a <- a * W + c (mod p)
|
## Does a <- a * W + c (mod p)
|
||||||
scaleadd_impl(a, c)
|
shiftAddImpl(a, c)
|
||||||
|
|
|
@ -77,4 +77,4 @@ func toMonty*[P: static BigInt](a: Fp[P]): Montgomery[P] =
|
||||||
|
|
||||||
result = a
|
result = a
|
||||||
for i in static(countdown(P.limbs.high, 0)):
|
for i in static(countdown(P.limbs.high, 0)):
|
||||||
scaleadd(result, 0)
|
shiftAdd(result, 0)
|
||||||
|
|
|
@ -36,7 +36,7 @@ func asm_x86_64_extMul(hi, lo: var uint64, a, b: uint64) {.inline.}=
|
||||||
: // no clobbered registers
|
: // 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
|
## Extended precision multiplication uint64 * uint64 --> uint128
|
||||||
##
|
##
|
||||||
## TODO, at the moment only x86_64 architecture are supported
|
## 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
|
: // 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
|
## Division uint128 by uint64
|
||||||
## Warning ⚠️ :
|
## Warning ⚠️ :
|
||||||
## - if n_hi == d, quotient does not fit in an uint64
|
## - if n_hi == d, quotient does not fit in an uint64
|
||||||
|
|
Loading…
Reference in New Issue