|
| 1 | +package testkeygen |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/rand" |
| 5 | + |
| 6 | + "github.com/shutter-network/shutter/shlib/shcrypto" |
| 7 | + |
| 8 | + "github.com/shutter-network/rolling-shutter/rolling-shutter/medley/epochid" |
| 9 | +) |
| 10 | + |
| 11 | +// KeyGenerator is a helper tool to generate secret and public eon and epoch keys and key |
| 12 | +// shares. It will generate a new eon key every eonInterval epochs. |
| 13 | +type KeyGenerator struct { |
| 14 | + eonInterval uint64 |
| 15 | + eonKeyGen map[uint64]*EonKeys |
| 16 | + NumKeypers uint64 |
| 17 | + Threshold uint64 |
| 18 | +} |
| 19 | + |
| 20 | +func NewKeyGenerator(numKeypers uint64, threshold uint64) *KeyGenerator { |
| 21 | + return &KeyGenerator{ |
| 22 | + eonInterval: 100, // 0 stands for infinity |
| 23 | + eonKeyGen: make(map[uint64]*EonKeys), |
| 24 | + NumKeypers: numKeypers, |
| 25 | + Threshold: threshold, |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +// getEonIndex computes the index of the EON key to be used for the given epochID. We generate a new |
| 30 | +// eon key every eonInterval epochs. |
| 31 | +func (kg *KeyGenerator) getEonIndex(epochID epochid.EpochID) uint64 { |
| 32 | + if kg.eonInterval == 0 { |
| 33 | + return 0 |
| 34 | + } |
| 35 | + |
| 36 | + return epochID.Big().Uint64() / kg.eonInterval |
| 37 | +} |
| 38 | + |
| 39 | +func (kg *KeyGenerator) EonKeysForEpoch(epochID epochid.EpochID) *EonKeys { |
| 40 | + eonIndex := kg.getEonIndex(epochID) |
| 41 | + res, ok := kg.eonKeyGen[eonIndex] |
| 42 | + var err error |
| 43 | + if !ok { |
| 44 | + res, err = NewEonKeys( |
| 45 | + rand.Reader, |
| 46 | + kg.NumKeypers, |
| 47 | + kg.Threshold, |
| 48 | + ) |
| 49 | + if err != nil { |
| 50 | + return nil |
| 51 | + } |
| 52 | + kg.eonKeyGen[eonIndex] = res |
| 53 | + } |
| 54 | + return res |
| 55 | +} |
| 56 | + |
| 57 | +func (kg *KeyGenerator) EonPublicKeyShare(epochID epochid.EpochID, keyperIndex uint64) *shcrypto.EonPublicKeyShare { |
| 58 | + return kg.EonKeysForEpoch(epochID).keyperShares[keyperIndex].eonPublicKeyShare |
| 59 | +} |
| 60 | + |
| 61 | +func (kg *KeyGenerator) EonPublicKey(epochID epochid.EpochID) *shcrypto.EonPublicKey { |
| 62 | + return kg.EonKeysForEpoch(epochID).publicKey |
| 63 | +} |
| 64 | + |
| 65 | +func (kg *KeyGenerator) EonSecretKeyShare(epochID epochid.EpochID, keyperIndex uint64) *shcrypto.EonSecretKeyShare { |
| 66 | + return kg.EonKeysForEpoch(epochID).keyperShares[keyperIndex].eonSecretKeyShare |
| 67 | +} |
| 68 | + |
| 69 | +func (kg *KeyGenerator) EpochSecretKeyShare(epochID epochid.EpochID, keyperIndex uint64) *shcrypto.EpochSecretKeyShare { |
| 70 | + return kg.EonKeysForEpoch(epochID).keyperShares[keyperIndex].ComputeEpochSecretKeyShare(epochID) |
| 71 | +} |
| 72 | + |
| 73 | +func (kg *KeyGenerator) EpochSecretKey(epochID epochid.EpochID) *shcrypto.EpochSecretKey { |
| 74 | + epochSecretKey, err := kg.EonKeysForEpoch(epochID).EpochSecretKey(epochID) |
| 75 | + if err != nil { |
| 76 | + panic(err) |
| 77 | + } |
| 78 | + return epochSecretKey |
| 79 | +} |
| 80 | + |
| 81 | +func (kg *KeyGenerator) RandomEpochID(epochbytes []byte) epochid.EpochID { |
| 82 | + _, err := rand.Read(epochbytes) |
| 83 | + if err != nil { |
| 84 | + panic(err) |
| 85 | + } |
| 86 | + |
| 87 | + epochID, err := epochid.BytesToEpochID(epochbytes) |
| 88 | + if err != nil { |
| 89 | + panic(err) |
| 90 | + } |
| 91 | + return epochID |
| 92 | +} |
| 93 | + |
| 94 | +func (kg *KeyGenerator) RandomSigma() (shcrypto.Block, error) { |
| 95 | + return shcrypto.RandomSigma(rand.Reader) |
| 96 | +} |
0 commit comments