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

chore: Pippenger implementation with batch addition (per bucket) #106

Draft
wants to merge 2 commits into
base: kw/add-pippenger-remove-parallel-execute
Choose a base branch
from
Draft
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
47 changes: 31 additions & 16 deletions internal/multiexp/multiexp.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,46 +68,50 @@ func MultiExpG1Pippenger(scalars []fr.Element, points []bls12381.G1Affine, _nbTh
scalarsBytes := scalarsToBytes(scalars)

// First need to compute a window length
// TODO: We should test and see if these numbers work for us
var c int
if len(points) < 4 {
c = 1
} else if len(points) < 32 {
c = 3
} else {
c = int(math.Ceil(math.Log(float64(len(points)))))
}
var c = computeWindowSize(len(points))

// Compute number of windows needed
numWindows := (fr.Bits / c) + 1
result := bls12381.G1Jac{}
windowSums := make([]bls12381.G1Jac, numWindows)

// Create all of the buckets for window size
buckets := make([][]bls12381.G1Affine, 1<<(c-1))

for currentWindow := 0; currentWindow < numWindows; currentWindow++ {
// Create all of the buckets for this window
buckets := make([]bls12381.G1Jac, 1<<(c-1))

// Clear all buckets (but keep capacity)
for i := range buckets {
buckets[i] = buckets[i][:0]
}

for i := 0; i < len(scalars); i++ {
scalarBytes := scalarsBytes[i]
point := points[i]

digit := getBoothIndex(currentWindow, c, scalarBytes)
if digit > 0 {
buckets[digit-1].AddMixed(&point)
buckets[digit-1] = append(buckets[digit-1], point)
} else if digit < 0 {
var negPoint bls12381.G1Affine
negPoint.Neg(&point)
buckets[uint(-digit)-1].AddMixed(&negPoint)
buckets[uint(-digit)-1] = append(buckets[uint(-digit)-1], negPoint)
}
}

summedBuckets := make([]bls12381.G1Jac, 1<<(c-1))
for i := 0; i < len(buckets); i++ {
summedBuckets[i] = BatchAdditionBinaryTreeStride(buckets[i])
}

runningSum := bls12381.G1Jac{}
for i := len(buckets) - 1; i >= 0; i-- {
runningSum.AddAssign(&buckets[i])
for i := len(summedBuckets) - 1; i >= 0; i-- {
runningSum.AddAssign(&summedBuckets[i])
windowSums[currentWindow].AddAssign(&runningSum)
}
}

result := bls12381.G1Jac{}

result.Set(&windowSums[numWindows-1]) // Set the accumulator to the last point
for currentWindow := numWindows - 2; currentWindow >= 0; currentWindow-- {
for i := 0; i < c; i++ {
Expand All @@ -121,3 +125,14 @@ func MultiExpG1Pippenger(scalars []fr.Element, points []bls12381.G1Affine, _nbTh

return &resultAff, nil
}
func computeWindowSize(numPoints int) int {
if numPoints < 8 {
return 2
} else if numPoints < 16 {
return 3
} else if numPoints < 32 {
return 4
} else {
return int(math.Ceil(math.Log(float64(numPoints)))) + 1
}
}