From 5e8b1870a69a1eba88013ea780f0a4c68150e718 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mamy=20Andr=C3=A9-Ratsimbazafy?= Date: Fri, 24 Jul 2020 23:08:00 +0200 Subject: [PATCH] Rename files --- constantine/arithmetic/bigints.nim | 12 ++- constantine/arithmetic/finite_fields.nim | 7 +- ..._asm_x86.nim => limbs_asm_modular_x86.nim} | 45 +----------- ..._mul_x86.nim => limbs_asm_montmul_x86.nim} | 2 +- ...nim => limbs_asm_montmul_x86_adx_bmi2.nim} | 4 +- constantine/arithmetic/limbs_asm_x86.nim | 73 +++++++++++++++++++ .../{limbs.nim => limbs_generic.nim} | 0 ..._modular.nim => limbs_generic_modular.nim} | 2 +- constantine/arithmetic/limbs_montgomery.nim | 6 +- 9 files changed, 93 insertions(+), 58 deletions(-) rename constantine/arithmetic/{finite_fields_asm_x86.nim => limbs_asm_modular_x86.nim} (88%) rename constantine/arithmetic/{finite_fields_asm_mul_x86.nim => limbs_asm_montmul_x86.nim} (99%) rename constantine/arithmetic/{finite_fields_asm_mul_x86_adx_bmi2.nim => limbs_asm_montmul_x86_adx_bmi2.nim} (99%) create mode 100644 constantine/arithmetic/limbs_asm_x86.nim rename constantine/arithmetic/{limbs.nim => limbs_generic.nim} (100%) rename constantine/arithmetic/{limbs_modular.nim => limbs_generic_modular.nim} (99%) diff --git a/constantine/arithmetic/bigints.nim b/constantine/arithmetic/bigints.nim index da2106a..302e3a1 100644 --- a/constantine/arithmetic/bigints.nim +++ b/constantine/arithmetic/bigints.nim @@ -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 diff --git a/constantine/arithmetic/finite_fields.nim b/constantine/arithmetic/finite_fields.nim index d85d822..ce157d8 100644 --- a/constantine/arithmetic/finite_fields.nim +++ b/constantine/arithmetic/finite_fields.nim @@ -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 diff --git a/constantine/arithmetic/finite_fields_asm_x86.nim b/constantine/arithmetic/limbs_asm_modular_x86.nim similarity index 88% rename from constantine/arithmetic/finite_fields_asm_x86.nim rename to constantine/arithmetic/limbs_asm_modular_x86.nim index 1265e4d..40108c0 100644 --- a/constantine/arithmetic/finite_fields_asm_x86.nim +++ b/constantine/arithmetic/limbs_asm_modular_x86.nim @@ -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 # ------------------------------------------------------------ diff --git a/constantine/arithmetic/finite_fields_asm_mul_x86.nim b/constantine/arithmetic/limbs_asm_montmul_x86.nim similarity index 99% rename from constantine/arithmetic/finite_fields_asm_mul_x86.nim rename to constantine/arithmetic/limbs_asm_montmul_x86.nim index f943875..7be67c8 100644 --- a/constantine/arithmetic/finite_fields_asm_mul_x86.nim +++ b/constantine/arithmetic/limbs_asm_montmul_x86.nim @@ -12,7 +12,7 @@ import # Internal ../config/common, ../primitives, - ./limbs + ./limbs_generic # ############################################################ # diff --git a/constantine/arithmetic/finite_fields_asm_mul_x86_adx_bmi2.nim b/constantine/arithmetic/limbs_asm_montmul_x86_adx_bmi2.nim similarity index 99% rename from constantine/arithmetic/finite_fields_asm_mul_x86_adx_bmi2.nim rename to constantine/arithmetic/limbs_asm_montmul_x86_adx_bmi2.nim index 8c41e23..f9d7d57 100644 --- a/constantine/arithmetic/finite_fields_asm_mul_x86_adx_bmi2.nim +++ b/constantine/arithmetic/limbs_asm_montmul_x86_adx_bmi2.nim @@ -12,8 +12,8 @@ import # Internal ../config/common, ../primitives, - ./limbs, - ./finite_fields_asm_mul_x86 + ./limbs_generic, + ./limbs_asm_montmul_x86 # ############################################################ # diff --git a/constantine/arithmetic/limbs_asm_x86.nim b/constantine/arithmetic/limbs_asm_x86.nim new file mode 100644 index 0000000..e0921ba --- /dev/null +++ b/constantine/arithmetic/limbs_asm_x86.nim @@ -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) diff --git a/constantine/arithmetic/limbs.nim b/constantine/arithmetic/limbs_generic.nim similarity index 100% rename from constantine/arithmetic/limbs.nim rename to constantine/arithmetic/limbs_generic.nim diff --git a/constantine/arithmetic/limbs_modular.nim b/constantine/arithmetic/limbs_generic_modular.nim similarity index 99% rename from constantine/arithmetic/limbs_modular.nim rename to constantine/arithmetic/limbs_generic_modular.nim index c0b0586..ac73d6c 100644 --- a/constantine/arithmetic/limbs_modular.nim +++ b/constantine/arithmetic/limbs_generic_modular.nim @@ -9,7 +9,7 @@ import ../config/common, ../primitives, - ./limbs + ./limbs_generic # No exceptions allowed {.push raises: [].} diff --git a/constantine/arithmetic/limbs_montgomery.nim b/constantine/arithmetic/limbs_montgomery.nim index 6098d30..93d8396 100644 --- a/constantine/arithmetic/limbs_montgomery.nim +++ b/constantine/arithmetic/limbs_montgomery.nim @@ -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 # ############################################################ #