Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weā€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constant-time š”¾ā‚œ exponentiation with endomorphism acceleration #431

Merged
merged 3 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
5 changes: 4 additions & 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 All @@ -59,6 +59,9 @@ proc main() =
gtExp_endo_wNAF_vartimeBench(Fp12[curve], window = 3, ExpIters)
gtExp_endo_wNAF_vartimeBench(Fp12[curve], window = 4, ExpIters)
separator()
gtExpEndo_constanttimeBench(Fp12[curve], ExpIters)
separator()


main()
notes()
16 changes: 12 additions & 4 deletions benchmarks/bench_gt_template.nim
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import
constantine/math/pairings/[
pairings_generic,
cyclotomic_subgroups,
gt_exponentiations,
gt_exponentiations_vartime
],
# Helpers
Expand Down Expand Up @@ -145,23 +146,30 @@ 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)

proc gtExpEndo_constanttimeBench*(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 (endomorphism, constant-time)", T, iters):
r.gtExpEndo(x, exponent)
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
Loading
Loading