Skip to content

Commit

Permalink
faster hashToG2 by using sparsity
Browse files Browse the repository at this point in the history
  • Loading branch information
mratsim committed Jul 2, 2023
1 parent 2774f8b commit c7e2180
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
6 changes: 2 additions & 4 deletions constantine/math/elliptic/ec_shortweierstrass_jacobian.nim
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,7 @@ template sumImpl[F; G: static Subgroup](
a.square(HHH_or_Mpre, skipFinalSub = true)
a *= HHH_or_Mpre # a = 3X₁²
b.square(Z1Z1)
# b.mulCheckSparse(CoefA) # TODO: broken static compile-time type inference
b *= CoefA # b = αZZ, with α the "a" coefficient of the curve
b.mulCheckSparse(CoefA) # b = αZZ, with α the "a" coefficient of the curve

a += b
a.div2()
Expand Down Expand Up @@ -516,8 +515,7 @@ func madd*[F; G: static Subgroup](
a.square(HHH_or_Mpre, skipFinalSub = true)
a *= HHH_or_Mpre # a = 3X₁²
b.square(Z1Z1)
# b.mulCheckSparse(CoefA) # TODO: broken static compile-time type inference
b *= CoefA # b = αZZ, with α the "a" coefficient of the curve
b.mulCheckSparse(CoefA) # b = αZZ, with α the "a" coefficient of the curve

a += b
a.div2()
Expand Down
12 changes: 6 additions & 6 deletions constantine/math/extension_fields/towers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1440,11 +1440,11 @@ func mul_sparse_by_x0*(a: var QuadraticExt, sparseB: QuadraticExt) =
a.mul_sparse_by_x0(a, sparseB)

template mulCheckSparse*(a: var QuadraticExt, b: QuadraticExt) =
when b.isOne().bool:
when isOne(b).bool:
discard
elif b.isMinusOne().bool:
elif isMinusOne(b).bool:
a.neg()
elif b.c0.isZero().bool and b.c1.isOne().bool:
elif isZero(c0(b)).bool and isOne(c1(b)).bool:
var t {.noInit.}: type(a.c0)
when fromComplexExtension(b):
t.neg(a.c1)
Expand All @@ -1454,7 +1454,7 @@ template mulCheckSparse*(a: var QuadraticExt, b: QuadraticExt) =
t.prod(a.c1, NonResidue)
a.c1 = a.c0
a.c0 = t
elif b.c0.isZero().bool and b.c1.isMinusOne().bool:
elif isZero(c0(b)).bool and isMinusOne(c1(b)).bool:
var t {.noInit.}: type(a.c0)
when fromComplexExtension(b):
t = a.c1
Expand All @@ -1464,9 +1464,9 @@ template mulCheckSparse*(a: var QuadraticExt, b: QuadraticExt) =
t.prod(a.c1, NonResidue)
a.c1.neg(a.c0)
a.c0.neg(t)
elif b.c0.isZero().bool:
elif isZero(c0(b)).bool:
a.mul_sparse_by_0y(b)
elif b.c1.isZero().bool:
elif isZero(c1(b)).bool:
a.mul_sparse_by_x0(b)
else:
a *= b
Expand Down

0 comments on commit c7e2180

Please sign in to comment.