-
Notifications
You must be signed in to change notification settings - Fork 168
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
Optimize BDN Signature/Key Aggregation #546
Merged
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7738a1f
Add BDN test fixtures
Stebalien fa46ad8
Remove n^2 algorithm from signature/key aggregation
Stebalien ff60afc
Remove an unnecessary loop from hashPointToR
Stebalien 9acfb6f
Introduce a new CachedMask for BDN
Stebalien eb8f760
Ignore golangci lint
Stebalien 39ccfaa
Move Mask into BDN and remove the interface
Stebalien 2c83bb2
fix docs
Stebalien 3be7817
Document mutability of Mask fields
Stebalien ee8f48a
Merge branch 'master' into steb/optimize-aggregation
AnomalRoil 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" | ||
|
||
"go.dedis.ch/kyber/v4" | ||
"go.dedis.ch/kyber/v4/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 | ||
Stebalien marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
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) { | ||
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(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.
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.
Note: this is technically a breaking change if someone is abstracting over Scheme via an interface. I'm happy to explore alternatives (e.g., add a new function, etc.) if that's an issue.
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.
We are in the process of preparing a V4 release anyway 👍
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.
In that case.... I can drop the interface and switch to a BDN specific mask if that works better for you (not sure how much breaking you want).
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.
Yes, that would be totally fine by me. Actually Mask seems to only be used in
bdn
anyway, I'd be fine with it also being in internal or so, and have BDN expose the methods required to extract the list of participants from an aggregate signature.Summoning @pierluca and @K1li4nL in case they have other opinions