forked from dedis/kyber
-
Notifications
You must be signed in to change notification settings - Fork 10
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
Introduce a new CachedMask for BDN #61
Closed
+294
−40
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1060518
Remove n^2 algorithm from signature/key aggregation
Stebalien 4bf5c5d
Remove an unnecessary loop from hashPointToR
Stebalien 6e99a9f
rename mask -> bitIndex
Stebalien ee6db8f
add test fixtures
Stebalien 307e6dc
flesh out mask test for getting/setting bits
Stebalien 07398f9
Introduce a new CachedMask for BDN
Stebalien File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,112 @@ | ||
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 mask with pre-computed terms. | ||
// | ||
// This cached mask will: | ||
// | ||
// 1. Pre-compute coefficients for signature aggregation. Once the CachedMask has been instantiated, | ||
// distinct sets of signatures can be aggregated without any BLAKE2S hashing. | ||
// 2. Pre-computes the terms for public key aggregation. Once the CachedMask has been instantiated, | ||
// distinct sets of public keys can be aggregated by simply summing the cached terms, ~2 orders | ||
// of magnitude faster than aggregating from scratch. | ||
func NewCachedMask(mask Mask) (*CachedMask, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Happy to bikeshed on the name. Maybe just callit |
||
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.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] | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a breaking change for anyone abstracting over
Scheme
using an interface (e.g., for testing). If that's going to be an issue, I can look into alternative APIs (e.g., adding additional methods, putting the methods on theCachedMask
, etc.