Rename files

This commit is contained in:
Mamy André-Ratsimbazafy 2020-07-24 23:08:00 +02:00
parent d97bc9b61c
commit 5e8b1870a6
No known key found for this signature in database
GPG Key ID: 7B88AD1FE79492E1
9 changed files with 93 additions and 58 deletions

View File

@ -9,7 +9,12 @@
import
../config/[common, type_bigint],
../primitives,
./limbs, ./limbs_montgomery, ./limbs_modular
./limbs_generic,
./limbs_generic_modular,
./limbs_montgomery
when UseX86ASM:
import ./limbs_asm_x86
export BigInt
@ -80,7 +85,10 @@ func ccopy*(a: var BigInt, b: BigInt, ctl: SecretBool) =
## If ctl is true: b is copied into a
## if ctl is false: b is not copied and a is untouched
## Time and memory accesses are the same whether a copy occurs or not
ccopy(a.limbs, b.limbs, ctl)
when UseX86ASM:
ccopy_asm(a.limbs, b.limbs, ctl)
else:
ccopy(a.limbs, b.limbs, ctl)
func cswap*(a, b: var BigInt, ctl: CTBool) =
## Swap ``a`` and ``b`` if ``ctl`` is true

View File

@ -30,7 +30,7 @@ import
./bigints, ./limbs_montgomery
when UseX86ASM:
import ./finite_fields_asm_x86
import ./limbs_asm_modular_x86
export Fp
@ -65,10 +65,7 @@ func ccopy*(a: var Fp, b: Fp, ctl: SecretBool) {.inline.} =
## If ctl is true: b is copied into a
## if ctl is false: b is not copied and a is unmodified
## Time and memory accesses are the same whether a copy occurs or not
when UseX86ASM:
ccopy_asm(a.mres.limbs, b.mres.limbs, ctl)
else:
ccopy(a.mres, b.mres, ctl)
ccopy(a.mres, b.mres, ctl)
func cswap*(a, b: var Fp, ctl: CTBool) {.inline.} =
## Swap ``a`` and ``b`` if ``ctl`` is true

View File

@ -12,7 +12,7 @@ import
# Internal
../config/common,
../primitives,
./limbs
./limbs_generic
# ############################################################
#
@ -29,49 +29,6 @@ static: doAssert UseX86ASM
{.localPassC:"-fomit-frame-pointer".} # Needed so that the compiler finds enough registers
# Copy
# ------------------------------------------------------------
macro ccopy_gen[N: static int](a: var Limbs[N], b: Limbs[N], ctl: SecretBool): untyped =
## Generate an optimized conditional copy kernel
result = newStmtList()
var ctx = init(Assembler_x86, BaseType)
let
arrA = init(OperandArray, nimSymbol = a, N, PointerInReg, InputOutput)
arrB = init(OperandArray, nimSymbol = b, N, PointerInReg, Input)
# If N is too big, we need to spill registers. TODO.
arrT = init(OperandArray, nimSymbol = ident"t", N, ElemsInReg, Output_EarlyClobber)
control = Operand(
desc: OperandDesc(
asmId: "[ctl]",
nimSymbol: ctl,
rm: Reg,
constraint: Input,
cEmit: "ctl"
)
)
ctx.test control, control
for i in 0 ..< N:
ctx.mov arrT[i], arrA[i]
ctx.cmovnz arrT[i], arrB[i]
ctx.mov arrA[i], arrT[i]
let t = arrT.nimSymbol
let c = control.desc.nimSymbol
result.add quote do:
var `t` {.noInit.}: typeof(`a`)
result.add ctx.generate()
func ccopy_asm*(a: var Limbs, b: Limbs, ctl: SecretBool) {.inline.}=
## Constant-time conditional copy
## If ctl is true: b is copied into a
## if ctl is false: b is not copied and a is untouched
## Time and memory accesses are the same whether a copy occurs or not
ccopy_gen(a, b, ctl)
# Field addition
# ------------------------------------------------------------

View File

@ -12,7 +12,7 @@ import
# Internal
../config/common,
../primitives,
./limbs
./limbs_generic
# ############################################################
#

View File

@ -12,8 +12,8 @@ import
# Internal
../config/common,
../primitives,
./limbs,
./finite_fields_asm_mul_x86
./limbs_generic,
./limbs_asm_montmul_x86
# ############################################################
#

View File

@ -0,0 +1,73 @@
# Constantine
# Copyright (c) 2018-2019 Status Research & Development GmbH
# Copyright (c) 2020-Present Mamy André-Ratsimbazafy
# Licensed and distributed under either of
# * MIT license (license terms in the root directory or at http://opensource.org/licenses/MIT).
# * Apache v2 license (license terms in the root directory or at http://www.apache.org/licenses/LICENSE-2.0).
# at your option. This file may not be copied, modified, or distributed except according to those terms.
import
# Standard library
std/macros,
# Internal
../config/common,
../primitives,
./limbs_generic
# ############################################################
#
# Assembly implementation of bigints
#
# ############################################################
# Note: We can refer to at most 30 registers in inline assembly
# and "InputOutput" registers count double
# They are nice to let the compiler deals with mov
# but too constraining so we move things ourselves.
static: doAssert UseX86ASM
{.localPassC:"-fomit-frame-pointer".} # Needed so that the compiler finds enough registers
# Copy
# ------------------------------------------------------------
macro ccopy_gen[N: static int](a: var Limbs[N], b: Limbs[N], ctl: SecretBool): untyped =
## Generate an optimized conditional copy kernel
result = newStmtList()
var ctx = init(Assembler_x86, BaseType)
let
arrA = init(OperandArray, nimSymbol = a, N, PointerInReg, InputOutput)
arrB = init(OperandArray, nimSymbol = b, N, PointerInReg, Input)
# If N is too big, we need to spill registers. TODO.
arrT = init(OperandArray, nimSymbol = ident"t", N, ElemsInReg, Output_EarlyClobber)
control = Operand(
desc: OperandDesc(
asmId: "[ctl]",
nimSymbol: ctl,
rm: Reg,
constraint: Input,
cEmit: "ctl"
)
)
ctx.test control, control
for i in 0 ..< N:
ctx.mov arrT[i], arrA[i]
ctx.cmovnz arrT[i], arrB[i]
ctx.mov arrA[i], arrT[i]
let t = arrT.nimSymbol
let c = control.desc.nimSymbol
result.add quote do:
var `t` {.noInit.}: typeof(`a`)
result.add ctx.generate()
func ccopy_asm*(a: var Limbs, b: Limbs, ctl: SecretBool) {.inline.}=
## Constant-time conditional copy
## If ctl is true: b is copied into a
## if ctl is false: b is not copied and a is untouched
## Time and memory accesses are the same whether a copy occurs or not
ccopy_gen(a, b, ctl)

View File

@ -9,7 +9,7 @@
import
../config/common,
../primitives,
./limbs
./limbs_generic
# No exceptions allowed
{.push raises: [].}

View File

@ -12,12 +12,12 @@ import
# Internal
../config/common,
../primitives,
./limbs
./limbs_generic
when UseX86ASM:
import
./finite_fields_asm_mul_x86,
./finite_fields_asm_mul_x86_adx_bmi2
./limbs_asm_montmul_x86,
./limbs_asm_montmul_x86_adx_bmi2
# ############################################################
#