mirror of
https://github.com/codex-storage/constantine.git
synced 2025-01-11 19:44:10 +00:00
Rename files
This commit is contained in:
parent
d97bc9b61c
commit
5e8b1870a6
@ -9,7 +9,12 @@
|
|||||||
import
|
import
|
||||||
../config/[common, type_bigint],
|
../config/[common, type_bigint],
|
||||||
../primitives,
|
../primitives,
|
||||||
./limbs, ./limbs_montgomery, ./limbs_modular
|
./limbs_generic,
|
||||||
|
./limbs_generic_modular,
|
||||||
|
./limbs_montgomery
|
||||||
|
|
||||||
|
when UseX86ASM:
|
||||||
|
import ./limbs_asm_x86
|
||||||
|
|
||||||
export BigInt
|
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 true: b is copied into a
|
||||||
## if ctl is false: b is not copied and a is untouched
|
## 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
|
## 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) =
|
func cswap*(a, b: var BigInt, ctl: CTBool) =
|
||||||
## Swap ``a`` and ``b`` if ``ctl`` is true
|
## Swap ``a`` and ``b`` if ``ctl`` is true
|
||||||
|
@ -30,7 +30,7 @@ import
|
|||||||
./bigints, ./limbs_montgomery
|
./bigints, ./limbs_montgomery
|
||||||
|
|
||||||
when UseX86ASM:
|
when UseX86ASM:
|
||||||
import ./finite_fields_asm_x86
|
import ./limbs_asm_modular_x86
|
||||||
|
|
||||||
export Fp
|
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 true: b is copied into a
|
||||||
## if ctl is false: b is not copied and a is unmodified
|
## 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
|
## Time and memory accesses are the same whether a copy occurs or not
|
||||||
when UseX86ASM:
|
ccopy(a.mres, b.mres, ctl)
|
||||||
ccopy_asm(a.mres.limbs, b.mres.limbs, ctl)
|
|
||||||
else:
|
|
||||||
ccopy(a.mres, b.mres, ctl)
|
|
||||||
|
|
||||||
func cswap*(a, b: var Fp, ctl: CTBool) {.inline.} =
|
func cswap*(a, b: var Fp, ctl: CTBool) {.inline.} =
|
||||||
## Swap ``a`` and ``b`` if ``ctl`` is true
|
## Swap ``a`` and ``b`` if ``ctl`` is true
|
||||||
|
@ -12,7 +12,7 @@ import
|
|||||||
# Internal
|
# Internal
|
||||||
../config/common,
|
../config/common,
|
||||||
../primitives,
|
../primitives,
|
||||||
./limbs
|
./limbs_generic
|
||||||
|
|
||||||
# ############################################################
|
# ############################################################
|
||||||
#
|
#
|
||||||
@ -29,49 +29,6 @@ static: doAssert UseX86ASM
|
|||||||
|
|
||||||
{.localPassC:"-fomit-frame-pointer".} # Needed so that the compiler finds enough registers
|
{.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
|
# Field addition
|
||||||
# ------------------------------------------------------------
|
# ------------------------------------------------------------
|
||||||
|
|
@ -12,7 +12,7 @@ import
|
|||||||
# Internal
|
# Internal
|
||||||
../config/common,
|
../config/common,
|
||||||
../primitives,
|
../primitives,
|
||||||
./limbs
|
./limbs_generic
|
||||||
|
|
||||||
# ############################################################
|
# ############################################################
|
||||||
#
|
#
|
@ -12,8 +12,8 @@ import
|
|||||||
# Internal
|
# Internal
|
||||||
../config/common,
|
../config/common,
|
||||||
../primitives,
|
../primitives,
|
||||||
./limbs,
|
./limbs_generic,
|
||||||
./finite_fields_asm_mul_x86
|
./limbs_asm_montmul_x86
|
||||||
|
|
||||||
# ############################################################
|
# ############################################################
|
||||||
#
|
#
|
73
constantine/arithmetic/limbs_asm_x86.nim
Normal file
73
constantine/arithmetic/limbs_asm_x86.nim
Normal 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)
|
@ -9,7 +9,7 @@
|
|||||||
import
|
import
|
||||||
../config/common,
|
../config/common,
|
||||||
../primitives,
|
../primitives,
|
||||||
./limbs
|
./limbs_generic
|
||||||
|
|
||||||
# No exceptions allowed
|
# No exceptions allowed
|
||||||
{.push raises: [].}
|
{.push raises: [].}
|
@ -12,12 +12,12 @@ import
|
|||||||
# Internal
|
# Internal
|
||||||
../config/common,
|
../config/common,
|
||||||
../primitives,
|
../primitives,
|
||||||
./limbs
|
./limbs_generic
|
||||||
|
|
||||||
when UseX86ASM:
|
when UseX86ASM:
|
||||||
import
|
import
|
||||||
./finite_fields_asm_mul_x86,
|
./limbs_asm_montmul_x86,
|
||||||
./finite_fields_asm_mul_x86_adx_bmi2
|
./limbs_asm_montmul_x86_adx_bmi2
|
||||||
|
|
||||||
# ############################################################
|
# ############################################################
|
||||||
#
|
#
|
||||||
|
Loading…
x
Reference in New Issue
Block a user