-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
233 additions
and
10 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
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,69 @@ | ||
package share | ||
|
||
import ( | ||
"bytes" | ||
"crypto/rand" | ||
"fmt" | ||
"sort" | ||
) | ||
|
||
// RandShares generate 'total' amount of shares filled with random data. | ||
func RandShares(total int) []Share { | ||
if total&(total-1) != 0 { | ||
panic(fmt.Errorf("total must be power of 2: %d", total)) | ||
} | ||
|
||
shares := make([]Share, total) | ||
for i := range shares { | ||
shr := make([]byte, ShareSize) | ||
copy(shr[:NamespaceSize], RandomNamespace().Bytes()) | ||
if _, err := rand.Read(shr[NamespaceSize:]); err != nil { | ||
panic(err) | ||
} | ||
|
||
sh, err := NewShare(shr) | ||
if err != nil { | ||
panic(err) | ||
} | ||
if err = ValidateForData(sh.Namespace()); err != nil { | ||
panic(err) | ||
} | ||
|
||
shares[i] = *sh | ||
} | ||
sort.Slice(shares, func(i, j int) bool { return bytes.Compare(shares[i].ToBytes(), shares[j].ToBytes()) < 0 }) | ||
return shares | ||
} | ||
|
||
// RandSharesWithNamespace is same the as RandShares, but sets same namespace for all shares. | ||
func RandSharesWithNamespace(namespace Namespace, namespacedAmount, total int) []Share { | ||
if total&(total-1) != 0 { | ||
panic(fmt.Errorf("total must be power of 2: %d", total)) | ||
} | ||
|
||
if namespacedAmount > total { | ||
panic(fmt.Errorf("withNamespace must be less than total: %d", total)) | ||
} | ||
|
||
shares := make([]Share, total) | ||
for i := range shares { | ||
shr := make([]byte, ShareSize) | ||
if i < namespacedAmount { | ||
copy(shr[:NamespaceSize], namespace.Bytes()) | ||
} else { | ||
copy(shr[:NamespaceSize], RandomNamespace().Bytes()) | ||
} | ||
_, err := rand.Read(shr[NamespaceSize:]) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
sh, err := NewShare(shr) | ||
if err != nil { | ||
panic(err) | ||
} | ||
shares[i] = *sh | ||
} | ||
sort.Slice(shares, func(i, j int) bool { return bytes.Compare(shares[i].ToBytes(), shares[j].ToBytes()) < 0 }) | ||
return shares | ||
} |
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