Skip to content

Commit

Permalink
feat(𝔾ₜ constant-time exponentiation): generalize ec_endomorphism_acc…
Browse files Browse the repository at this point in the history
…el to elliptic curve and 𝔾ₜ
  • Loading branch information
mratsim committed Jul 15, 2024
1 parent d8721a1 commit 776e906
Show file tree
Hide file tree
Showing 70 changed files with 375 additions and 393 deletions.
8 changes: 3 additions & 5 deletions benchmarks/bench_ec_g1_scalar_mul.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@

import
# Internals
constantine/named/algebras,
constantine/named/[algebras, zoo_endomorphisms],
constantine/math/arithmetic,
constantine/math/elliptic/[
ec_shortweierstrass_projective,
ec_shortweierstrass_jacobian],
constantine/math/ec_shortweierstrass,
# Helpers
./bench_elliptic_template

Expand Down Expand Up @@ -69,7 +67,7 @@ proc main() =
scalarMulVartimeWNAFBench(EC_ShortW_Jac[Fp[curve], G1], bits, window = 4, MulIters)
scalarMulVartimeWNAFBench(EC_ShortW_Jac[Fp[curve], G1], bits, window = 5, MulIters)
separator()
when bits >= 196: # All endomorphisms constants are below this threshold
when bits >= EndomorphismThreshold: # All endomorphisms constants are below this threshold
scalarMulVartimeEndoWNAFBench(EC_ShortW_Prj[Fp[curve], G1], bits, window = 2, MulIters)
scalarMulVartimeEndoWNAFBench(EC_ShortW_Prj[Fp[curve], G1], bits, window = 3, MulIters)
scalarMulVartimeEndoWNAFBench(EC_ShortW_Prj[Fp[curve], G1], bits, window = 4, MulIters)
Expand Down
5 changes: 1 addition & 4 deletions benchmarks/bench_ec_g2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ import
constantine/named/algebras,
constantine/math/arithmetic,
constantine/math/extension_fields,
constantine/math/elliptic/[
ec_shortweierstrass_projective,
ec_shortweierstrass_jacobian,
ec_shortweierstrass_jacobian_extended],
constantine/math/ec_shortweierstrass,
# Helpers
./bench_elliptic_template,
# Standard library
Expand Down
8 changes: 3 additions & 5 deletions benchmarks/bench_ec_g2_scalar_mul.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@

import
# Internals
constantine/named/algebras,
constantine/named/[algebras, zoo_endomorphisms],
constantine/math/arithmetic,
constantine/math/extension_fields,
constantine/math/elliptic/[
ec_shortweierstrass_projective,
ec_shortweierstrass_jacobian],
constantine/math/ec_shortweierstrass,
# Helpers
./bench_elliptic_template

Expand Down Expand Up @@ -68,7 +66,7 @@ proc main() =
scalarMulVartimeWNAFBench(EC_ShortW_Jac[Fp2[curve], G2], bits, window = 4, MulIters)
scalarMulVartimeWNAFBench(EC_ShortW_Jac[Fp2[curve], G2], bits, window = 5, MulIters)
separator()
when bits >= 196: # All endomorphisms constants are below this threshold
when bits >= EndomorphismThreshold: # All endomorphisms constants are below this threshold
scalarMulVartimeEndoWNAFBench(EC_ShortW_Prj[Fp2[curve], G2], bits, window = 2, MulIters)
scalarMulVartimeEndoWNAFBench(EC_ShortW_Prj[Fp2[curve], G2], bits, window = 3, MulIters)
scalarMulVartimeEndoWNAFBench(EC_ShortW_Prj[Fp2[curve], G2], bits, window = 4, MulIters)
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/bench_ec_msm_bandersnatch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import
# Internals
constantine/named/algebras,
constantine/math/arithmetic,
constantine/math/elliptic/ec_twistededwards_projective,
constantine/math/ec_twistededwards,
# Helpers
helpers/prng_unsafe,
./bench_elliptic_parallel_template
Expand Down
4 changes: 1 addition & 3 deletions benchmarks/bench_ec_msm_bls12_381_g1.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import
# Internals
constantine/named/algebras,
constantine/math/arithmetic,
constantine/math/elliptic/[
ec_shortweierstrass_projective,
ec_shortweierstrass_jacobian],
constantine/math/ec_shortweierstrass,
# Helpers
helpers/prng_unsafe,
./bench_elliptic_parallel_template
Expand Down
4 changes: 1 addition & 3 deletions benchmarks/bench_ec_msm_bls12_381_g2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import
# Internals
constantine/named/algebras,
constantine/math/extension_fields,
constantine/math/elliptic/[
ec_shortweierstrass_projective,
ec_shortweierstrass_jacobian],
constantine/math/ec_shortweierstrass,
# Helpers
helpers/prng_unsafe,
./bench_elliptic_parallel_template
Expand Down
4 changes: 1 addition & 3 deletions benchmarks/bench_ec_msm_bn254_snarks_g1.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import
# Internals
constantine/named/algebras,
constantine/math/arithmetic,
constantine/math/elliptic/[
ec_shortweierstrass_projective,
ec_shortweierstrass_jacobian],
constantine/math/ec_shortweierstrass,
# Helpers
./bench_elliptic_parallel_template

Expand Down
4 changes: 1 addition & 3 deletions benchmarks/bench_ec_msm_pasta.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import
# Internals
constantine/named/algebras,
constantine/math/arithmetic,
constantine/math/elliptic/[
ec_shortweierstrass_projective,
ec_shortweierstrass_jacobian],
constantine/math/ec_shortweierstrass,
# Helpers
helpers/prng_unsafe,
./bench_elliptic_parallel_template
Expand Down
10 changes: 5 additions & 5 deletions benchmarks/bench_elliptic_template.nim
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import
ec_shortweierstrass_jacobian,
ec_shortweierstrass_jacobian_extended,
ec_shortweierstrass_batch_ops,
ec_scalar_mul, ec_endomorphism_accel],
ec_scalar_mul],
constantine/named/zoo_subgroups,
# Helpers
helpers/prng_unsafe,
Expand Down Expand Up @@ -210,7 +210,7 @@ proc scalarMulVartimeMinHammingWeightRecodingBench*(EC: typedesc, bits: static i

bench("EC ScalarMul " & $bits & "-bit " & $EC.G & " (vartime min Hamming Weight recoding)", EC, iters):
r = P
r.scalarMul_minHammingWeight_vartime(exponent)
r.scalarMul_jy00_vartime(exponent)

proc scalarMulVartimeWNAFBench*(EC: typedesc, bits, window: static int, iters: int) =
var r {.noInit.}: EC
Expand All @@ -221,7 +221,7 @@ proc scalarMulVartimeWNAFBench*(EC: typedesc, bits, window: static int, iters: i

bench("EC ScalarMul " & $bits & "-bit " & $EC.G & " (vartime wNAF-" & $window & ")", EC, iters):
r = P
r.scalarMul_minHammingWeight_windowed_vartime(exponent, window)
r.scalarMul_wNAF_vartime(exponent, window)

proc scalarMulVartimeEndoWNAFBench*(EC: typedesc, bits, window: static int, iters: int) =
var r {.noInit.}: EC
Expand All @@ -232,7 +232,7 @@ proc scalarMulVartimeEndoWNAFBench*(EC: typedesc, bits, window: static int, iter

bench("EC ScalarMul " & $bits & "-bit " & $EC.G & " (vartime endomorphism + wNAF-" & $window & ")", EC, iters):
r = P
r.scalarMulEndo_minHammingWeight_windowed_vartime(exponent, window)
r.scalarMulEndo_wNAF_vartime(exponent, window)

proc subgroupCheckBench*(EC: typedesc, iters: int) =
var P = rng.random_unsafe(EC)
Expand All @@ -251,7 +251,7 @@ proc subgroupCheckScalarMulVartimeEndoWNAFBench*(EC: typedesc, bits, window: sta
bench("EC subgroup check + ScalarMul " & $bits & "-bit " & $EC.G & " (vartime endo + wNAF-" & $window & ")", EC, iters):
r = P
discard r.isInSubgroup()
r.scalarMulEndo_minHammingWeight_windowed_vartime(exponent, window)
r.scalarMulEndo_wNAF_vartime(exponent, window)

proc multiAddBench*(EC: typedesc, numPoints: int, useBatching: bool, iters: int) =
var points = newSeq[EC_ShortW_Aff[EC.F, EC.G]](numPoints)
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/bench_eth_eip2537_subgroup_checks_impact.nim
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import
constantine/named/algebras,
constantine/math/arithmetic,
constantine/math/extension_fields,
constantine/math/elliptic/ec_shortweierstrass_jacobian,
constantine/math/ec_shortweierstrass,
# Helpers
./bench_elliptic_template

Expand Down
2 changes: 1 addition & 1 deletion benchmarks/bench_gt.nim
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ proc main() =
powVartimeBench(Fp12[curve], window = 4, ExpIters)
separator()
gtExp_sqrmul_vartimeBench(Fp12[curve], ExpIters)
gtExp_minHammingWeight_vartimeBench(Fp12[curve], ExpIters)
gtExp_jy00_vartimeBench(Fp12[curve], ExpIters)
separator()
gtExp_wNAF_vartimeBench(Fp12[curve], window = 2, ExpIters)
gtExp_wNAF_vartimeBench(Fp12[curve], window = 3, ExpIters)
Expand Down
8 changes: 4 additions & 4 deletions benchmarks/bench_gt_template.nim
Original file line number Diff line number Diff line change
Expand Up @@ -145,23 +145,23 @@ proc gtExp_sqrmul_vartimeBench*(T: typedesc, iters: int) =
bench("𝔾ₜ Exponentiation " & $exponent.bits & "-bit (cyclotomic square-multiply, vartime)", T, iters):
r.gtExp_sqrmul_vartime(x, exponent)

proc gtExp_minHammingWeight_vartimeBench*(T: typedesc, iters: int) =
proc gtExp_jy00_vartimeBench*(T: typedesc, iters: int) =
let x = rng.random_gt(T)
let exponent = rng.random_unsafe(BigInt[Fr[T.Name].bits()])
var r {.noInit.}: T
bench("𝔾ₜ Exponentiation " & $exponent.bits & "-bit (signed recoding, vartime)", T, iters):
r.gtExp_minHammingWeight_vartime(x, exponent)
r.gtExp_jy00_vartime(x, exponent)

proc gtExp_wNAF_vartimeBench*(T: typedesc, window: static int, iters: int) =
let x = rng.random_gt(T)
let exponent = rng.random_unsafe(BigInt[Fr[T.Name].bits()])
var r {.noInit.}: T
bench("𝔾ₜ Exponentiation " & $exponent.bits & "-bit (wNAF-" & $window & ", vartime)", T, iters):
r.gtExp_minHammingWeight_windowed_vartime(x, exponent, window)
r.gtExp_wNAF_vartime(x, exponent, window)

proc gtExp_endo_wNAF_vartimeBench*(T: typedesc, window: static int, iters: int) =
let x = rng.random_gt(T)
let exponent = rng.random_unsafe(BigInt[Fr[T.Name].bits()])
var r {.noInit.}: T
bench("𝔾ₜ Exponentiation " & $exponent.bits & "-bit (endomorphism, wNAF-" & $window & ", vartime)", T, iters):
r.gtExpEndo_minHammingWeight_windowed_vartime(x, exponent, window)
r.gtExpEndo_wNAF_vartime(x, exponent, window)
8 changes: 1 addition & 7 deletions benchmarks/bench_summary_bls12_377.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@
# * 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
# Internals
constantine/named/algebras,
constantine/math/arithmetic,
constantine/math/extension_fields,
# Helpers
./bench_summary_template
import ./bench_summary_template

# ############################################################
#
Expand Down
8 changes: 1 addition & 7 deletions benchmarks/bench_summary_bls12_381.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@
# * 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
# Internals
constantine/named/algebras,
constantine/math/arithmetic,
constantine/math/extension_fields,
# Helpers
./bench_summary_template
import ./bench_summary_template

# ############################################################
#
Expand Down
8 changes: 1 addition & 7 deletions benchmarks/bench_summary_bn254_nogami.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@
# * 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
# Internals
constantine/named/algebras,
constantine/math/arithmetic,
constantine/math/extension_fields,
# Helpers
./bench_summary_template
import ./bench_summary_template

# ############################################################
#
Expand Down
8 changes: 1 addition & 7 deletions benchmarks/bench_summary_bn254_snarks.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@
# * 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
# Internals
constantine/named/algebras,
constantine/math/arithmetic,
constantine/math/extension_fields,
# Helpers
./bench_summary_template
import ./bench_summary_template

# ############################################################
#
Expand Down
8 changes: 1 addition & 7 deletions benchmarks/bench_summary_pasta.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@
# * 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
# Internals
constantine/named/algebras,
constantine/math/arithmetic,
constantine/math/extension_fields,
# Helpers
./bench_summary_template
import ./bench_summary_template

# ############################################################
#
Expand Down
11 changes: 4 additions & 7 deletions benchmarks/bench_summary_template.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ import
constantine/platforms/abstractions,
constantine/named/algebras,
constantine/math/[arithmetic, extension_fields],
constantine/math/elliptic/[
ec_shortweierstrass_affine,
ec_shortweierstrass_projective,
ec_shortweierstrass_jacobian,
ec_scalar_mul, ec_scalar_mul_vartime, ec_endomorphism_accel],
constantine/math/ec_shortweierstrass,
constantine/named/zoo_subgroups,
constantine/math/pairings/[
cyclotomic_subgroups,
Expand All @@ -36,8 +32,9 @@ import
./bench_blueprint

export
ec_shortweierstrass_projective,
ec_shortweierstrass_jacobian
algebras,
arithmetic, extension_fields,
ec_shortweierstrass

export abstractions # generic sandwich on SecretBool and SecretBool in Jacobian sum
export zoo_pairings # generic sandwich https://github.com/nim-lang/Nim/issues/11225
Expand Down
4 changes: 2 additions & 2 deletions constantine/boolean_hypercube/multilinear_extensions.nim
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func evalMultilinearExtensionAt_BE[F](
# e ∈ {0,1}ˢ hence each factor is either:
# (1-xᵢ) or xᵢ
#
# See the algorithm in ec_endomorphism_accel to build
# See the algorithm in split_scalars to build
# a binary lookup table for O(n) evaluations
#
# Algorithm:
Expand Down Expand Up @@ -231,7 +231,7 @@ func evalMultilinearExtensionAt_LE[F](
# e ∈ {0,1}ˢ hence each factor is either:
# (1-xᵢ) or xᵢ
#
# See the algorithm in ec_endomorphism_accel to build
# See the algorithm in split_scalars to build
# a binary lookup table for O(n) evaluations
#
# Algorithm:
Expand Down
2 changes: 1 addition & 1 deletion constantine/lowlevel_extension_fields.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import
./platforms/abstractions,
./named/algebras,
./math/isogenies/frobenius,
./math/endomorphisms/frobenius,
./math/extension_fields,
./math/io/io_extfields

Expand Down
2 changes: 1 addition & 1 deletion constantine/lowlevel_pairing_curves.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import
./platforms/abstractions,
./named/algebras,
./named/zoo_pairings,
./math/isogenies/frobenius,
./math/endomorphisms/frobenius,
./math/pairings/[
cyclotomic_subgroups,
lines_eval,
Expand Down
6 changes: 5 additions & 1 deletion constantine/math/arithmetic/bigints.nim
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,10 @@ iterator recoding_l2r_signed_vartime*[bits: static int](a: BigInt[bits]): int8 =
##
## ⚠️ While the recoding is constant-time,
## usage of this recoding is intended vartime
##
## - Optimal Left-to-Right Binary Signed-Digit Recoding
## Joye, Yen, 2000
## https://marcjoye.github.io/papers/JY00sd2r.pdf

# As the caller is copy-pasted at each yield
# we rework the algorithm so that we have a single yield point
Expand All @@ -610,7 +614,7 @@ iterator recoding_l2r_signed_vartime*[bits: static int](a: BigInt[bits]): int8 =
var bi, bi1, ri, ri1, ri2: int8

var i = bits
while true: # JY00 outputs at mots bits+1 digits
while true: # JY00 outputs at most bits+1 digits
if i == bits: # We rely on compiler to hoist this branch out of the loop.
ri = 0
ri1 = int8 a.bit(bits-1)
Expand Down
2 changes: 2 additions & 0 deletions constantine/math/ec_shortweierstrass.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import
elliptic/[
ec_shortweierstrass_affine,
ec_shortweierstrass_jacobian,
ec_shortweierstrass_jacobian_extended,
ec_shortweierstrass_projective,
ec_shortweierstrass_batch_ops,
ec_scalar_mul, ec_scalar_mul_vartime,
Expand All @@ -25,6 +26,7 @@ import
../named/zoo_generators

export ec_shortweierstrass_affine, ec_shortweierstrass_jacobian, ec_shortweierstrass_projective,
ec_shortweierstrass_jacobian_extended,
ec_shortweierstrass_batch_ops, ec_scalar_mul, ec_scalar_mul_vartime,
ec_multi_scalar_mul

Expand Down
2 changes: 1 addition & 1 deletion constantine/math/elliptic/ec_multi_scalar_mul.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import constantine/named/algebras,
./ec_multi_scalar_mul_scheduler,
./ec_endomorphism_accel,
constantine/math/endomorphisms/split_scalars,
constantine/math/extension_fields,
constantine/named/zoo_endomorphisms,
constantine/platforms/abstractions
Expand Down
2 changes: 1 addition & 1 deletion constantine/math/elliptic/ec_multi_scalar_mul_parallel.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import constantine/named/algebras,
./ec_multi_scalar_mul_scheduler,
./ec_multi_scalar_mul,
./ec_endomorphism_accel,
constantine/math/endomorphisms/split_scalars,
constantine/math/extension_fields,
constantine/named/zoo_endomorphisms,
../../threadpool/[threadpool, partitioners]
Expand Down
Loading

0 comments on commit 776e906

Please sign in to comment.