Skip to content

Commit 07df658

Browse files
committed
bindings/go/blst_px.tgo: add multi-point validate.
1 parent df27e60 commit 07df658

File tree

2 files changed

+285
-0
lines changed

2 files changed

+285
-0
lines changed

bindings/go/blst.go

+190
Original file line numberDiff line numberDiff line change
@@ -2320,6 +2320,101 @@ func (points P1Affines) Mult(scalarsIf interface{}, nbits int) *P1 {
23202320
func (points P1s) Mult(scalarsIf interface{}, nbits int) *P1 {
23212321
return points.ToAffine().Mult(scalarsIf, nbits)
23222322
}
2323+
2324+
//
2325+
// Group-check
2326+
//
2327+
2328+
func P1AffinesValidate(pointsIf interface{}) bool {
2329+
var npoints int
2330+
switch val := pointsIf.(type) {
2331+
case []*P1Affine:
2332+
npoints = len(val)
2333+
case []P1Affine:
2334+
npoints = len(val)
2335+
case P1Affines:
2336+
npoints = len(val)
2337+
default:
2338+
panic(fmt.Sprintf("unsupported type %T", val))
2339+
}
2340+
2341+
numCores := runtime.GOMAXPROCS(0)
2342+
numThreads := maxProcs
2343+
if numThreads > numCores {
2344+
numThreads = numCores
2345+
}
2346+
if numThreads > npoints {
2347+
numThreads = npoints
2348+
}
2349+
2350+
if numThreads < 2 {
2351+
for i := 0; i < npoints; i++ {
2352+
var point *P1Affine
2353+
2354+
switch val := pointsIf.(type) {
2355+
case []*P1Affine:
2356+
point = val[i]
2357+
case []P1Affine:
2358+
point = &val[i]
2359+
case P1Affines:
2360+
point = &val[i]
2361+
default:
2362+
panic(fmt.Sprintf("unsupported type %T", val))
2363+
}
2364+
2365+
if !C.go_p1_affine_validate(point, true) {
2366+
return false
2367+
}
2368+
}
2369+
2370+
return true
2371+
}
2372+
2373+
valid := int32(1)
2374+
curItem := uint32(0)
2375+
2376+
var wg sync.WaitGroup
2377+
wg.Add(numThreads)
2378+
2379+
for tid := 0; tid < numThreads; tid++ {
2380+
go func() {
2381+
for atomic.LoadInt32(&valid) != 0 {
2382+
work := atomic.AddUint32(&curItem, 1) - 1
2383+
if work >= uint32(npoints) {
2384+
break
2385+
}
2386+
2387+
var point *P1Affine
2388+
2389+
switch val := pointsIf.(type) {
2390+
case []*P1Affine:
2391+
point = val[work]
2392+
case []P1Affine:
2393+
point = &val[work]
2394+
case P1Affines:
2395+
point = &val[work]
2396+
default:
2397+
panic(fmt.Sprintf("unsupported type %T", val))
2398+
}
2399+
2400+
if !C.go_p1_affine_validate(point, true) {
2401+
atomic.StoreInt32(&valid, 0)
2402+
break
2403+
}
2404+
}
2405+
2406+
wg.Done()
2407+
}()
2408+
}
2409+
2410+
wg.Wait()
2411+
2412+
return atomic.LoadInt32(&valid) != 0
2413+
}
2414+
2415+
func (points P1Affines) Validate() bool {
2416+
return P1AffinesValidate(points)
2417+
}
23232418
func PairingAggregatePkInG2(ctx Pairing, PK *P2Affine, pkValidate bool,
23242419
sig *P1Affine, sigGroupcheck bool, msg []byte,
23252420
optional ...[]byte) int { // aug
@@ -2983,6 +3078,101 @@ func (points P2s) Mult(scalarsIf interface{}, nbits int) *P2 {
29833078
return points.ToAffine().Mult(scalarsIf, nbits)
29843079
}
29853080

3081+
//
3082+
// Group-check
3083+
//
3084+
3085+
func P2AffinesValidate(pointsIf interface{}) bool {
3086+
var npoints int
3087+
switch val := pointsIf.(type) {
3088+
case []*P2Affine:
3089+
npoints = len(val)
3090+
case []P2Affine:
3091+
npoints = len(val)
3092+
case P2Affines:
3093+
npoints = len(val)
3094+
default:
3095+
panic(fmt.Sprintf("unsupported type %T", val))
3096+
}
3097+
3098+
numCores := runtime.GOMAXPROCS(0)
3099+
numThreads := maxProcs
3100+
if numThreads > numCores {
3101+
numThreads = numCores
3102+
}
3103+
if numThreads > npoints {
3104+
numThreads = npoints
3105+
}
3106+
3107+
if numThreads < 2 {
3108+
for i := 0; i < npoints; i++ {
3109+
var point *P2Affine
3110+
3111+
switch val := pointsIf.(type) {
3112+
case []*P2Affine:
3113+
point = val[i]
3114+
case []P2Affine:
3115+
point = &val[i]
3116+
case P2Affines:
3117+
point = &val[i]
3118+
default:
3119+
panic(fmt.Sprintf("unsupported type %T", val))
3120+
}
3121+
3122+
if !C.go_p2_affine_validate(point, true) {
3123+
return false
3124+
}
3125+
}
3126+
3127+
return true
3128+
}
3129+
3130+
valid := int32(1)
3131+
curItem := uint32(0)
3132+
3133+
var wg sync.WaitGroup
3134+
wg.Add(numThreads)
3135+
3136+
for tid := 0; tid < numThreads; tid++ {
3137+
go func() {
3138+
for atomic.LoadInt32(&valid) != 0 {
3139+
work := atomic.AddUint32(&curItem, 1) - 1
3140+
if work >= uint32(npoints) {
3141+
break
3142+
}
3143+
3144+
var point *P2Affine
3145+
3146+
switch val := pointsIf.(type) {
3147+
case []*P2Affine:
3148+
point = val[work]
3149+
case []P2Affine:
3150+
point = &val[work]
3151+
case P2Affines:
3152+
point = &val[work]
3153+
default:
3154+
panic(fmt.Sprintf("unsupported type %T", val))
3155+
}
3156+
3157+
if !C.go_p2_affine_validate(point, true) {
3158+
atomic.StoreInt32(&valid, 0)
3159+
break
3160+
}
3161+
}
3162+
3163+
wg.Done()
3164+
}()
3165+
}
3166+
3167+
wg.Wait()
3168+
3169+
return atomic.LoadInt32(&valid) != 0
3170+
}
3171+
3172+
func (points P2Affines) Validate() bool {
3173+
return P2AffinesValidate(points)
3174+
}
3175+
29863176
func parseOpts(optional ...interface{}) ([]byte, [][]byte, bool, bool) {
29873177
var aug [][]byte // For aggregate verify
29883178
var augSingle []byte // For signing

bindings/go/blst_px.tgo

+95
Original file line numberDiff line numberDiff line change
@@ -659,3 +659,98 @@ func (points P1Affines) Mult(scalarsIf interface{}, nbits int) *P1 {
659659
func (points P1s) Mult(scalarsIf interface{}, nbits int) *P1 {
660660
return points.ToAffine().Mult(scalarsIf, nbits)
661661
}
662+
663+
//
664+
// Group-check
665+
//
666+
667+
func P1AffinesValidate(pointsIf interface{}) bool {
668+
var npoints int
669+
switch val := pointsIf.(type) {
670+
case []*P1Affine:
671+
npoints = len(val)
672+
case []P1Affine:
673+
npoints = len(val)
674+
case P1Affines:
675+
npoints = len(val)
676+
default:
677+
panic(fmt.Sprintf("unsupported type %T", val))
678+
}
679+
680+
numCores := runtime.GOMAXPROCS(0)
681+
numThreads := maxProcs
682+
if numThreads > numCores {
683+
numThreads = numCores
684+
}
685+
if numThreads > npoints {
686+
numThreads = npoints
687+
}
688+
689+
if numThreads < 2 {
690+
for i := 0; i < npoints; i++ {
691+
var point *P1Affine
692+
693+
switch val := pointsIf.(type) {
694+
case []*P1Affine:
695+
point = val[i]
696+
case []P1Affine:
697+
point = &val[i]
698+
case P1Affines:
699+
point = &val[i]
700+
default:
701+
panic(fmt.Sprintf("unsupported type %T", val))
702+
}
703+
704+
if !C.go_p1_affine_validate(point, true) {
705+
return false
706+
}
707+
}
708+
709+
return true
710+
}
711+
712+
valid := int32(1)
713+
curItem := uint32(0)
714+
715+
var wg sync.WaitGroup
716+
wg.Add(numThreads)
717+
718+
for tid := 0; tid < numThreads; tid++ {
719+
go func() {
720+
for atomic.LoadInt32(&valid) != 0 {
721+
work := atomic.AddUint32(&curItem, 1) - 1
722+
if work >= uint32(npoints) {
723+
break
724+
}
725+
726+
var point *P1Affine
727+
728+
switch val := pointsIf.(type) {
729+
case []*P1Affine:
730+
point = val[work]
731+
case []P1Affine:
732+
point = &val[work]
733+
case P1Affines:
734+
point = &val[work]
735+
default:
736+
panic(fmt.Sprintf("unsupported type %T", val))
737+
}
738+
739+
if !C.go_p1_affine_validate(point, true) {
740+
atomic.StoreInt32(&valid, 0)
741+
break
742+
}
743+
}
744+
745+
wg.Done()
746+
}()
747+
}
748+
749+
wg.Wait()
750+
751+
return atomic.LoadInt32(&valid) != 0
752+
}
753+
754+
func (points P1Affines) Validate() bool {
755+
return P1AffinesValidate(points)
756+
}

0 commit comments

Comments
 (0)