Skip to content

Commit

Permalink
bindings/go/blst_px.tgo: add multi-point validate.
Browse files Browse the repository at this point in the history
  • Loading branch information
dot-asm committed Jul 7, 2024
1 parent df27e60 commit 07df658
Show file tree
Hide file tree
Showing 2 changed files with 285 additions and 0 deletions.
190 changes: 190 additions & 0 deletions bindings/go/blst.go
Original file line number Diff line number Diff line change
Expand Up @@ -2320,6 +2320,101 @@ func (points P1Affines) Mult(scalarsIf interface{}, nbits int) *P1 {
func (points P1s) Mult(scalarsIf interface{}, nbits int) *P1 {
return points.ToAffine().Mult(scalarsIf, nbits)
}

//
// Group-check
//

func P1AffinesValidate(pointsIf interface{}) bool {
var npoints int
switch val := pointsIf.(type) {
case []*P1Affine:
npoints = len(val)
case []P1Affine:
npoints = len(val)
case P1Affines:
npoints = len(val)
default:
panic(fmt.Sprintf("unsupported type %T", val))
}

numCores := runtime.GOMAXPROCS(0)
numThreads := maxProcs
if numThreads > numCores {
numThreads = numCores
}
if numThreads > npoints {
numThreads = npoints
}

if numThreads < 2 {
for i := 0; i < npoints; i++ {
var point *P1Affine

switch val := pointsIf.(type) {
case []*P1Affine:
point = val[i]
case []P1Affine:
point = &val[i]
case P1Affines:
point = &val[i]
default:
panic(fmt.Sprintf("unsupported type %T", val))
}

if !C.go_p1_affine_validate(point, true) {
return false
}
}

return true
}

valid := int32(1)
curItem := uint32(0)

var wg sync.WaitGroup
wg.Add(numThreads)

for tid := 0; tid < numThreads; tid++ {
go func() {
for atomic.LoadInt32(&valid) != 0 {
work := atomic.AddUint32(&curItem, 1) - 1
if work >= uint32(npoints) {
break
}

var point *P1Affine

switch val := pointsIf.(type) {
case []*P1Affine:
point = val[work]
case []P1Affine:
point = &val[work]
case P1Affines:
point = &val[work]
default:
panic(fmt.Sprintf("unsupported type %T", val))
}

if !C.go_p1_affine_validate(point, true) {
atomic.StoreInt32(&valid, 0)
break
}
}

wg.Done()
}()
}

wg.Wait()

return atomic.LoadInt32(&valid) != 0
}

func (points P1Affines) Validate() bool {
return P1AffinesValidate(points)
}
func PairingAggregatePkInG2(ctx Pairing, PK *P2Affine, pkValidate bool,
sig *P1Affine, sigGroupcheck bool, msg []byte,
optional ...[]byte) int { // aug
Expand Down Expand Up @@ -2983,6 +3078,101 @@ func (points P2s) Mult(scalarsIf interface{}, nbits int) *P2 {
return points.ToAffine().Mult(scalarsIf, nbits)
}

//
// Group-check
//

func P2AffinesValidate(pointsIf interface{}) bool {
var npoints int
switch val := pointsIf.(type) {
case []*P2Affine:
npoints = len(val)
case []P2Affine:
npoints = len(val)
case P2Affines:
npoints = len(val)
default:
panic(fmt.Sprintf("unsupported type %T", val))
}

numCores := runtime.GOMAXPROCS(0)
numThreads := maxProcs
if numThreads > numCores {
numThreads = numCores
}
if numThreads > npoints {
numThreads = npoints
}

if numThreads < 2 {
for i := 0; i < npoints; i++ {
var point *P2Affine

switch val := pointsIf.(type) {
case []*P2Affine:
point = val[i]
case []P2Affine:
point = &val[i]
case P2Affines:
point = &val[i]
default:
panic(fmt.Sprintf("unsupported type %T", val))
}

if !C.go_p2_affine_validate(point, true) {
return false
}
}

return true
}

valid := int32(1)
curItem := uint32(0)

var wg sync.WaitGroup
wg.Add(numThreads)

for tid := 0; tid < numThreads; tid++ {
go func() {
for atomic.LoadInt32(&valid) != 0 {
work := atomic.AddUint32(&curItem, 1) - 1
if work >= uint32(npoints) {
break
}

var point *P2Affine

switch val := pointsIf.(type) {
case []*P2Affine:
point = val[work]
case []P2Affine:
point = &val[work]
case P2Affines:
point = &val[work]
default:
panic(fmt.Sprintf("unsupported type %T", val))
}

if !C.go_p2_affine_validate(point, true) {
atomic.StoreInt32(&valid, 0)
break
}
}

wg.Done()
}()
}

wg.Wait()

return atomic.LoadInt32(&valid) != 0
}

func (points P2Affines) Validate() bool {
return P2AffinesValidate(points)
}

func parseOpts(optional ...interface{}) ([]byte, [][]byte, bool, bool) {
var aug [][]byte // For aggregate verify
var augSingle []byte // For signing
Expand Down
95 changes: 95 additions & 0 deletions bindings/go/blst_px.tgo
Original file line number Diff line number Diff line change
Expand Up @@ -659,3 +659,98 @@ func (points P1Affines) Mult(scalarsIf interface{}, nbits int) *P1 {
func (points P1s) Mult(scalarsIf interface{}, nbits int) *P1 {
return points.ToAffine().Mult(scalarsIf, nbits)
}

//
// Group-check
//

func P1AffinesValidate(pointsIf interface{}) bool {
var npoints int
switch val := pointsIf.(type) {
case []*P1Affine:
npoints = len(val)
case []P1Affine:
npoints = len(val)
case P1Affines:
npoints = len(val)
default:
panic(fmt.Sprintf("unsupported type %T", val))
}

numCores := runtime.GOMAXPROCS(0)
numThreads := maxProcs
if numThreads > numCores {
numThreads = numCores
}
if numThreads > npoints {
numThreads = npoints
}

if numThreads < 2 {
for i := 0; i < npoints; i++ {
var point *P1Affine

switch val := pointsIf.(type) {
case []*P1Affine:
point = val[i]
case []P1Affine:
point = &val[i]
case P1Affines:
point = &val[i]
default:
panic(fmt.Sprintf("unsupported type %T", val))
}

if !C.go_p1_affine_validate(point, true) {
return false
}
}

return true
}

valid := int32(1)
curItem := uint32(0)

var wg sync.WaitGroup
wg.Add(numThreads)

for tid := 0; tid < numThreads; tid++ {
go func() {
for atomic.LoadInt32(&valid) != 0 {
work := atomic.AddUint32(&curItem, 1) - 1
if work >= uint32(npoints) {
break
}

var point *P1Affine

switch val := pointsIf.(type) {
case []*P1Affine:
point = val[work]
case []P1Affine:
point = &val[work]
case P1Affines:
point = &val[work]
default:
panic(fmt.Sprintf("unsupported type %T", val))
}

if !C.go_p1_affine_validate(point, true) {
atomic.StoreInt32(&valid, 0)
break
}
}

wg.Done()
}()
}

wg.Wait()

return atomic.LoadInt32(&valid) != 0
}

func (points P1Affines) Validate() bool {
return P1AffinesValidate(points)
}

0 comments on commit 07df658

Please sign in to comment.