forked from dedis/kyber
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This new mask will pre-compute reusable values, speeding up repeated verification and aggregation of aggregate signatures (mostly the former).
- Loading branch information
Showing
2 changed files
with
116 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package bdn | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/drand/kyber" | ||
"github.com/drand/kyber/sign" | ||
) | ||
|
||
type Mask interface { | ||
GetBit(i int) (bool, error) | ||
SetBit(i int, enable bool) error | ||
|
||
IndexOfNthEnabled(nth int) int | ||
NthEnabledAtIndex(idx int) int | ||
|
||
Publics() []kyber.Point | ||
Participants() []kyber.Point | ||
|
||
CountEnabled() int | ||
CountTotal() int | ||
|
||
Len() int | ||
Mask() []byte | ||
SetMask(mask []byte) error | ||
Merge(mask []byte) error | ||
} | ||
|
||
var _ Mask = (*sign.Mask)(nil) | ||
|
||
// We need to rename this, otherwise we have a public field named Mask (when we embed it) which | ||
// conflicts with the function named Mask. It also makes it private, which is nice. | ||
type maskI = Mask | ||
|
||
type CachedMask struct { | ||
maskI | ||
coefs []kyber.Scalar | ||
pubKeyC []kyber.Point | ||
// We could call Mask.Publics() instead of keeping these here, but that function copies the | ||
// slice and this field lets us avoid that copy. | ||
publics []kyber.Point | ||
} | ||
|
||
// Convert the passed mask (likely a *sign.Mask) into a BDN-specific cached mask. This cached mask | ||
// will pre-compute coefficients and pre-aggregation public key points, slightly speeding up | ||
// signature aggregation and significantly speeding up public key aggregation. | ||
func NewCachedMask(mask Mask) (*CachedMask, error) { | ||
return newCachedMask(mask, true) | ||
} | ||
|
||
func newCachedMask(mask Mask, precomputePubC bool) (*CachedMask, error) { | ||
if m, ok := mask.(*CachedMask); ok { | ||
return m, nil | ||
} | ||
|
||
publics := mask.Publics() | ||
coefs, err := hashPointToR(publics) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to hash public keys: %w", err) | ||
} | ||
|
||
cm := &CachedMask{ | ||
maskI: mask, | ||
coefs: coefs, | ||
publics: publics, | ||
} | ||
|
||
if precomputePubC { | ||
pubKeyC := make([]kyber.Point, len(publics)) | ||
for i := range publics { | ||
pubKeyC[i] = cm.getOrComputePubC(i) | ||
} | ||
cm.pubKeyC = pubKeyC | ||
} | ||
|
||
return cm, err | ||
} | ||
|
||
// Clone copies the BDN mask while keeping the precomputed coefficients, etc. | ||
func (cm *CachedMask) Clone() *CachedMask { | ||
newMask, err := sign.NewMask(nil, cm.publics, nil) | ||
if err != nil { | ||
// Not possible given that we didn't pass our own key. | ||
panic(fmt.Sprintf("failed to create mask: %s", err)) | ||
} | ||
if err := newMask.SetMask(cm.maskI.Mask()); err != nil { | ||
// Not possible given that we're using the same sized mask. | ||
panic(fmt.Sprintf("failed to create mask: %s", err)) | ||
} | ||
return &CachedMask{ | ||
maskI: newMask, | ||
coefs: cm.coefs, | ||
pubKeyC: cm.pubKeyC, | ||
publics: cm.publics, | ||
} | ||
} | ||
|
||
func (cm *CachedMask) getOrComputePubC(i int) kyber.Point { | ||
if cm.pubKeyC == nil { | ||
// NOTE: don't cache here as we may be sharing this mask between threads. | ||
pub := cm.publics[i] | ||
pubC := pub.Clone().Mul(cm.coefs[i], pub) | ||
return pubC.Add(pubC, pub) | ||
} | ||
return cm.pubKeyC[i] | ||
} |