Skip to content

Commit

Permalink
curves: get rid of goroutines in ML
Browse files Browse the repository at this point in the history
  • Loading branch information
yelhousni committed Jan 7, 2021
1 parent 4f7547c commit 105d1cd
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 54 deletions.
15 changes: 3 additions & 12 deletions bls377/pairing.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,21 +118,17 @@ func MillerLoop(P []G1Affine, Q []G2Affine) (GT, error) {
return GT{}, errors.New("invalid inputs sizes")
}

var (
ch = make([]chan struct{}, 0, nP)
evaluations = make([]*[69]lineEvaluation, 0, nP)
)
var evaluations = make([]*[69]lineEvaluation, 0, nP)

var countInf = 0
for k := 0; k < nP; k++ {
if P[k].IsInfinity() || Q[k].IsInfinity() {
countInf++
continue
}
ch = append(ch, make(chan struct{}, 10))
evaluations = append(evaluations, lineEvalPool.Get().(*[69]lineEvaluation))

go preCompute(evaluations[k-countInf], &Q[k], ch[k-countInf])
preCompute(evaluations[k-countInf], &Q[k])
}

nP = nP - countInf
Expand All @@ -145,14 +141,12 @@ func MillerLoop(P []G1Affine, Q []G2Affine) (GT, error) {

result.Square(&result)
for k := 0; k < nP; k++ {
<-ch[k]
lineEval(&result, &evaluations[k][j], &P[k])
}
j++

if loopCounter[i] == 1 {
for k := 0; k < nP; k++ {
<-ch[k]
lineEval(&result, &evaluations[k][j], &P[k])
}
j++
Expand All @@ -178,7 +172,7 @@ func lineEval(z *GT, l *lineEvaluation, P *G1Affine) *GT {
}

// precomputes the line evaluations used during the Miller loop.
func preCompute(evaluations *[69]lineEvaluation, Q *G2Affine, ch chan struct{}) {
func preCompute(evaluations *[69]lineEvaluation, Q *G2Affine) {

var Qproj g2Proj
Qproj.FromAffine(Q)
Expand All @@ -188,16 +182,13 @@ func preCompute(evaluations *[69]lineEvaluation, Q *G2Affine, ch chan struct{})
for i := len(loopCounter) - 2; i >= 0; i-- {

Qproj.DoubleStep(&evaluations[j])
ch <- struct{}{}

if loopCounter[i] != 0 {
j++
ch <- struct{}{}
Qproj.AddMixedStep(&evaluations[j], Q)
}
j++
}
close(ch)
}

// DoubleStep doubles a point in Homogenous projective coordinates, and evaluates the line in Miller loop
Expand Down
15 changes: 3 additions & 12 deletions bls381/pairing.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,7 @@ func MillerLoop(P []G1Affine, Q []G2Affine) (GT, error) {
return GT{}, errors.New("invalid inputs sizes")
}

var (
ch = make([]chan struct{}, 0, nP)
evaluations = make([]*[68]lineEvaluation, 0, nP)
)
var evaluations = make([]*[68]lineEvaluation, 0, nP)

var countInf = 0
for k := 0; k < nP; k++ {
Expand All @@ -122,10 +119,9 @@ func MillerLoop(P []G1Affine, Q []G2Affine) (GT, error) {
continue
}

ch = append(ch, make(chan struct{}, 10))
evaluations = append(evaluations, lineEvalPool.Get().(*[68]lineEvaluation))

go preCompute(evaluations[k-countInf], &Q[k], ch[k-countInf])
preCompute(evaluations[k-countInf], &Q[k])
}

nP = nP - countInf
Expand All @@ -138,14 +134,12 @@ func MillerLoop(P []G1Affine, Q []G2Affine) (GT, error) {

result.Square(&result)
for k := 0; k < nP; k++ {
<-ch[k]
lineEval(&result, &evaluations[k][j], &P[k])
}
j++

if loopCounter[i] == 1 {
for k := 0; k < nP; k++ {
<-ch[k]
lineEval(&result, &evaluations[k][j], &P[k])
}
j++
Expand All @@ -172,7 +166,7 @@ func lineEval(z *GT, l *lineEvaluation, P *G1Affine) *GT {
}

// precomputes the line evaluations used during the Miller loop.
func preCompute(evaluations *[68]lineEvaluation, Q *G2Affine, ch chan struct{}) {
func preCompute(evaluations *[68]lineEvaluation, Q *G2Affine) {

var Qproj g2Proj
Qproj.FromAffine(Q)
Expand All @@ -182,16 +176,13 @@ func preCompute(evaluations *[68]lineEvaluation, Q *G2Affine, ch chan struct{})
for i := len(loopCounter) - 2; i >= 0; i-- {

Qproj.DoubleStep(&evaluations[j])
ch <- struct{}{}

if loopCounter[i] != 0 {
j++
ch <- struct{}{}
Qproj.AddMixedStep(&evaluations[j], Q)
}
j++
}
close(ch)
}

// DoubleStep doubles a point in Homogenous projective coordinates, and evaluates the line in Miller loop
Expand Down
13 changes: 2 additions & 11 deletions bn256/pairing.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ func MillerLoop(P []G1Affine, Q []G2Affine) (GT, error) {
}

var (
ch = make([]chan struct{}, 0, nP)
evaluations = make([]*[86]lineEvaluation, 0, nP)
Qjac = make([]G2Jac, nP)
Q1 = make([]G2Jac, nP)
Expand All @@ -152,12 +151,11 @@ func MillerLoop(P []G1Affine, Q []G2Affine) (GT, error) {
continue
}

ch = append(ch, make(chan struct{}, 10))
evaluations = append(evaluations, lineEvalPool.Get().(*[86]lineEvaluation))

Qjac[k-countInf].FromAffine(&Q[k])
Paff[k-countInf].Set(&P[k])
go preCompute(evaluations[k-countInf], &Qjac[k-countInf], &Paff[k-countInf], ch[k-countInf])
preCompute(evaluations[k-countInf], &Qjac[k-countInf], &Paff[k-countInf])

//Q1[k] = Frob(Q[k])
Q1[k-countInf].X.Conjugate(&Q[k].X).MulByNonResidue1Power2(&Q1[k-countInf].X)
Expand All @@ -180,14 +178,12 @@ func MillerLoop(P []G1Affine, Q []G2Affine) (GT, error) {

result.Square(&result)
for k := 0; k < nP; k++ {
<-ch[k]
mulAssign(&result, &evaluations[k][j])
}
j++

if loopCounter[i] != 0 {
for k := 0; k < nP; k++ {
<-ch[k]
mulAssign(&result, &evaluations[k][j])
}
j++
Expand Down Expand Up @@ -253,7 +249,7 @@ func mulAssign(z *GT, l *lineEvaluation) *GT {
}

// precomputes the line evaluations used during the Miller loop.
func preCompute(evaluations *[86]lineEvaluation, Q *G2Jac, P *G1Affine, ch chan struct{}) {
func preCompute(evaluations *[86]lineEvaluation, Q *G2Jac, P *G1Affine) {

var Q1, Qbuf, Qneg G2Jac
Q1.Set(Q)
Expand All @@ -268,21 +264,16 @@ func preCompute(evaluations *[86]lineEvaluation, Q *G2Jac, P *G1Affine, ch chan
Q.Double(&Q1).Neg(Q)
lineEval(&Q1, Q, P, &evaluations[j]) // f(P), div(f) = 2(Q1)+(-2Q)-3(O)
Q.Neg(Q)
ch <- struct{}{}
j++

if loopCounter[i] == 1 {
lineEval(Q, &Qbuf, P, &evaluations[j]) // f(P), div(f) = (Q)+(Qbuf)+(-Q-Qbuf)-3(O)
Q.AddAssign(&Qbuf)
ch <- struct{}{}
j++
} else if loopCounter[i] == -1 {
lineEval(Q, &Qneg, P, &evaluations[j]) // f(P), div(f) = (Q)+(-Qbuf)+(-Q+Qbuf)-3(O)
Q.AddAssign(&Qneg)
ch <- struct{}{}
j++
}
}

close(ch)
}
23 changes: 4 additions & 19 deletions bw761/pairing.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,6 @@ func MillerLoop(P []G1Affine, Q []G2Affine) (GT, error) {
}

var (
ch1 = make([]chan struct{}, 0, nP)
ch2 = make([]chan struct{}, 0, nP)

evaluations1 = make([]*[69]lineEvaluation, 0, nP)
evaluations2 = make([]*[144]lineEvaluation, 0, nP)
Paff = make([]G1Affine, nP)
Expand All @@ -220,10 +217,9 @@ func MillerLoop(P []G1Affine, Q []G2Affine) (GT, error) {
xQjac[k-countInf].FromAffine(&Q[k])
QjacSaved[k-countInf].FromAffine(&Q[k])
Paff[k-countInf].Set(&P[k])
ch1 = append(ch1, make(chan struct{}, 10))
evaluations1 = append(evaluations1, lineEvalPool1.Get().(*[69]lineEvaluation))

go preCompute1(evaluations1[k-countInf], &xQjac[k-countInf], &Paff[k-countInf], ch1[k-countInf])
preCompute1(evaluations1[k-countInf], &xQjac[k-countInf], &Paff[k-countInf])
}

nP = nP - countInf
Expand All @@ -237,14 +233,12 @@ func MillerLoop(P []G1Affine, Q []G2Affine) (GT, error) {

result.Square(&result)
for k := 0; k < nP; k++ {
<-ch1[k]
mulAssign(&result, &evaluations1[k][j])
}
j++

if loopCounter1[i] != 0 {
for k := 0; k < nP; k++ {
<-ch1[k]
mulAssign(&result, &evaluations1[k][j])
}
j++
Expand All @@ -271,34 +265,30 @@ func MillerLoop(P []G1Affine, Q []G2Affine) (GT, error) {
lineEval(&xQjac[k], &QjacSaved[k], &Paff[k], &lEval[k])
mulAssign(&mxplusone, &lEval[k])

ch2 = append(ch2, make(chan struct{}, 10))
evaluations2 = append(evaluations2, lineEvalPool2.Get().(*[144]lineEvaluation))

// Miller loop part 2 (xQjac = [x]Q)
// computes f(P), div(f)=(x**3-x**2-x)(Q)-([x**3-x**2-x](Q)-(x**3-x**2-x-1)(O)
go preCompute2(evaluations2[k], &xQjac[k], &Paff[k], ch2[k])
preCompute2(evaluations2[k], &xQjac[k], &Paff[k])
}

j = 0
for i := len(loopCounter2) - 2; i >= 0; i-- {

result.Square(&result)
for k := 0; k < nP; k++ {
<-ch2[k]
mulAssign(&result, &evaluations2[k][j])
}
j++

if loopCounter2[i] == 1 {
for k := 0; k < nP; k++ {
<-ch2[k]
mulAssign(&result, &evaluations2[k][j]) // accumulate g(P), div(g)=x(Q)-([x]Q)-(x-1)(O)
}
result.MulAssign(&mx)
j++
} else if loopCounter2[i] == -1 {
for k := 0; k < nP; k++ {
<-ch2[k]
mulAssign(&result, &evaluations2[k][j]) // accumulate g(P), div(g)=x(Q)-([x]Q)-(x-1)(O)
}
result.MulAssign(&mxInv)
Expand Down Expand Up @@ -356,7 +346,7 @@ func mulAssign(z *GT, l *lineEvaluation) *GT {
}

// precomputes the line evaluations used during the Miller loop.
func preCompute1(evaluations *[69]lineEvaluation, Q *G2Jac, P *G1Affine, ch chan struct{}) {
func preCompute1(evaluations *[69]lineEvaluation, Q *G2Jac, P *G1Affine) {

var Q1, Qbuf G2Jac
Q1.Set(Q)
Expand All @@ -370,21 +360,19 @@ func preCompute1(evaluations *[69]lineEvaluation, Q *G2Jac, P *G1Affine, ch chan
Q.Double(&Q1).Neg(Q)
lineEval(&Q1, Q, P, &evaluations[j]) // f(P), div(f) = 2(Q1)+(-2Q)-3(O)
Q.Neg(Q)
ch <- struct{}{}
j++

if loopCounter1[i] == 1 {
lineEval(Q, &Qbuf, P, &evaluations[j]) // f(P), div(f) = (Q)+(Qbuf)+(-Q-Qbuf)-3(O)
Q.AddAssign(&Qbuf)
ch <- struct{}{}
j++
}
}

}

// precomputes the line evaluations used during the Miller loop.
func preCompute2(evaluations *[144]lineEvaluation, Q *G2Jac, P *G1Affine, ch chan struct{}) {
func preCompute2(evaluations *[144]lineEvaluation, Q *G2Jac, P *G1Affine) {

var Q1, Qbuf, Qneg G2Jac
Q1.Set(Q)
Expand All @@ -399,18 +387,15 @@ func preCompute2(evaluations *[144]lineEvaluation, Q *G2Jac, P *G1Affine, ch cha
Q.Double(&Q1).Neg(Q)
lineEval(&Q1, Q, P, &evaluations[j]) // f(P), div(f) = 2(Q1)+(-2Q)-3(O)
Q.Neg(Q)
ch <- struct{}{}
j++

if loopCounter2[i] == 1 {
lineEval(Q, &Qbuf, P, &evaluations[j]) // f(P), div(f) = (Q)+(Qbuf)+(-Q-Qbuf)-3(O)
Q.AddAssign(&Qbuf)
ch <- struct{}{}
j++
} else if loopCounter2[i] == -1 {
lineEval(Q, &Qneg, P, &evaluations[j]) // f(P), div(f) = (Q)+(-Qbuf)+(-Q+Qbuf)-3(O)
Q.AddAssign(&Qneg)
ch <- struct{}{}
j++
}
}
Expand Down

0 comments on commit 105d1cd

Please sign in to comment.