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.

116 lines
2.6 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. package bls
  2. import (
  3. "crypto/rand"
  4. "crypto/sha256"
  5. "fmt"
  6. "math/big"
  7. "github.com/arnaucube/go-snark/bn128"
  8. )
  9. // this BLS implementation uses the Go implementation of the BN128 pairing github.com/arnaucube/go-snark/bn128
  10. const bits = 2048
  11. // BLS is the data structure of the BLS signature scheme, including the BN128 pairing curve
  12. type BLS struct {
  13. Bn bn128.Bn128
  14. }
  15. type BLSKeys struct {
  16. PrivK *big.Int
  17. PubK [3]*big.Int
  18. }
  19. // NewBLS generates a new BLS scheme
  20. func NewBLS() (BLS, error) {
  21. bn, err := bn128.NewBn128()
  22. if err != nil {
  23. return BLS{}, err
  24. }
  25. bls := BLS{}
  26. bls.Bn = bn
  27. return bls, nil
  28. }
  29. // NewKeys generate new Private Key and Public Key
  30. func (bls BLS) NewKeys() (BLSKeys, error) {
  31. var err error
  32. k := BLSKeys{}
  33. k.PrivK, err = rand.Prime(rand.Reader, bits)
  34. if err != nil {
  35. return BLSKeys{}, err
  36. }
  37. // pubK = pk * G
  38. k.PubK = bls.Bn.G1.MulScalar(bls.Bn.G1.G, k.PrivK)
  39. return k, nil
  40. }
  41. // Hash hashes a message m
  42. func (bls BLS) Hash(m []byte) [3][2]*big.Int {
  43. h := sha256.New()
  44. h.Write(m)
  45. hash := h.Sum(nil)
  46. r := new(big.Int).SetBytes(hash)
  47. // get point over the curve
  48. point := bls.Bn.G2.MulScalar(bls.Bn.G2.G, r)
  49. return point
  50. }
  51. // Sign performs the BLS signature of a message m
  52. func (bls BLS) Sign(privK *big.Int, m []byte) [3][2]*big.Int {
  53. // s = pk * H(m)
  54. h := bls.Hash(m)
  55. sig := bls.Bn.G2.MulScalar(h, privK)
  56. return sig
  57. }
  58. // Verify checks the signature of a message m with the given Public Key
  59. func (bls BLS) Verify(m []byte, sig [3][2]*big.Int, pubK [3]*big.Int) bool {
  60. // checks e(P, G) == e(G, s)
  61. p1, err := bls.Bn.Pairing(pubK, bls.Hash(m))
  62. if err != nil {
  63. return false
  64. }
  65. p2, err := bls.Bn.Pairing(bls.Bn.G1.G, sig)
  66. if err != nil {
  67. return false
  68. }
  69. return bls.Bn.Fq12.Equal(p1, p2)
  70. }
  71. func (bls BLS) AggregateSignatures(signatures ...[3][2]*big.Int) [3][2]*big.Int {
  72. aggr := signatures[0]
  73. for _, sig := range signatures {
  74. aggr = bls.Bn.G2.Add(aggr, sig)
  75. }
  76. return aggr
  77. }
  78. func (bls BLS) VerifyAggregatedSignatures(aggrsig [3][2]*big.Int, pubKArray [][3]*big.Int, mArray [][]byte) bool {
  79. if len(pubKArray) != len(mArray) {
  80. fmt.Println("pubK array and msg array not with the same number of elements")
  81. return false
  82. }
  83. pairingGS, err := bls.Bn.Pairing(bls.Bn.G1.G, aggrsig)
  84. if err != nil {
  85. return false
  86. }
  87. pairingsMul, err := bls.Bn.Pairing(pubKArray[0], bls.Hash(mArray[0]))
  88. if err != nil {
  89. return false
  90. }
  91. for i := 1; i < len(pubKArray); i++ {
  92. e, err := bls.Bn.Pairing(pubKArray[i], bls.Hash(mArray[i]))
  93. if err != nil {
  94. return false
  95. }
  96. pairingsMul = bls.Bn.Fq12.Mul(pairingsMul, e)
  97. }
  98. if !bls.Bn.Fq12.Equal(pairingGS, pairingsMul) {
  99. return false
  100. }
  101. return true
  102. }