You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.6 KiB

  1. package multisig
  2. import (
  3. "github.com/tendermint/tendermint/crypto"
  4. )
  5. // PubKeyMultisigThreshold implements a K of N threshold multisig.
  6. type PubKeyMultisigThreshold struct {
  7. K uint `json:"threshold"`
  8. PubKeys []crypto.PubKey `json:"pubkeys"`
  9. }
  10. var _ crypto.PubKey = PubKeyMultisigThreshold{}
  11. // NewPubKeyMultisigThreshold returns a new PubKeyMultisigThreshold.
  12. // Panics if len(pubkeys) < k or 0 >= k.
  13. func NewPubKeyMultisigThreshold(k int, pubkeys []crypto.PubKey) crypto.PubKey {
  14. if k <= 0 {
  15. panic("threshold k of n multisignature: k <= 0")
  16. }
  17. if len(pubkeys) < k {
  18. panic("threshold k of n multisignature: len(pubkeys) < k")
  19. }
  20. return PubKeyMultisigThreshold{uint(k), pubkeys}
  21. }
  22. // VerifyBytes expects sig to be an amino encoded version of a MultiSignature.
  23. // Returns true iff the multisignature contains k or more signatures
  24. // for the correct corresponding keys,
  25. // and all signatures are valid. (Not just k of the signatures)
  26. // The multisig uses a bitarray, so multiple signatures for the same key is not
  27. // a concern.
  28. func (pk PubKeyMultisigThreshold) VerifyBytes(msg []byte, marshalledSig []byte) bool {
  29. var sig Multisignature
  30. err := cdc.UnmarshalBinaryBare(marshalledSig, &sig)
  31. if err != nil {
  32. return false
  33. }
  34. size := sig.BitArray.Size()
  35. // ensure bit array is the correct size
  36. if len(pk.PubKeys) != size {
  37. return false
  38. }
  39. // ensure size of signature list
  40. if len(sig.Sigs) < int(pk.K) || len(sig.Sigs) > size {
  41. return false
  42. }
  43. // ensure at least k signatures are set
  44. if sig.BitArray.NumTrueBitsBefore(size) < int(pk.K) {
  45. return false
  46. }
  47. // index in the list of signatures which we are concerned with.
  48. sigIndex := 0
  49. for i := 0; i < size; i++ {
  50. if sig.BitArray.GetIndex(i) {
  51. if !pk.PubKeys[i].VerifyBytes(msg, sig.Sigs[sigIndex]) {
  52. return false
  53. }
  54. sigIndex++
  55. }
  56. }
  57. return true
  58. }
  59. // Bytes returns the amino encoded version of the PubKeyMultisigThreshold
  60. func (pk PubKeyMultisigThreshold) Bytes() []byte {
  61. return cdc.MustMarshalBinaryBare(pk)
  62. }
  63. // Address returns tmhash(PubKeyMultisigThreshold.Bytes())
  64. func (pk PubKeyMultisigThreshold) Address() crypto.Address {
  65. return crypto.AddressHash(pk.Bytes())
  66. }
  67. // Equals returns true iff pk and other both have the same number of keys, and
  68. // all constituent keys are the same, and in the same order.
  69. func (pk PubKeyMultisigThreshold) Equals(other crypto.PubKey) bool {
  70. otherKey, sameType := other.(PubKeyMultisigThreshold)
  71. if !sameType {
  72. return false
  73. }
  74. if pk.K != otherKey.K || len(pk.PubKeys) != len(otherKey.PubKeys) {
  75. return false
  76. }
  77. for i := 0; i < len(pk.PubKeys); i++ {
  78. if !pk.PubKeys[i].Equals(otherKey.PubKeys[i]) {
  79. return false
  80. }
  81. }
  82. return true
  83. }