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.

146 lines
3.8KB

  1. package main
  2. import "fmt"
  3. import "math/big"
  4. //import "crypto/rand"
  5. import "encoding/hex"
  6. import "github.com/clearmatics/bn256"
  7. import "golang.org/x/crypto/sha3"
  8. var FIELD_MODULUS, w = new(big.Int).SetString("30644e72e131a029b85045b68181585d97816a916871ca8d3c208c16d87cfd47", 16)
  9. var GROUP_MODULUS, w1 = new(big.Int).SetString("30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001", 16)
  10. // this file basically implements curve based items
  11. type GeneratorParams struct {
  12. G *bn256.G1
  13. H *bn256.G1
  14. GSUM *bn256.G1
  15. Gs *GeneratorVector
  16. Hs *GeneratorVector
  17. }
  18. // the number if already reduced
  19. func HashtoNumber(input []byte) *big.Int {
  20. hasher := sha3.NewLegacyKeccak256()
  21. hasher.Write(input)
  22. hash := hasher.Sum(nil)
  23. return new(big.Int).SetBytes(hash[:])
  24. }
  25. // calculate hash and reduce it by curve's order
  26. func reducedhash(input []byte) *big.Int {
  27. return new(big.Int).Mod(HashtoNumber(input), bn256.Order)
  28. }
  29. func makestring64(input string) string {
  30. for len(input) != 64 {
  31. input = "0" + input
  32. }
  33. return input
  34. }
  35. func hextobytes(input string) []byte {
  36. ibytes, err := hex.DecodeString(input)
  37. if err != nil {
  38. panic(err)
  39. }
  40. return ibytes
  41. }
  42. // this should be merged , simplified just as simple as 25519
  43. func HashToPoint(seed *big.Int) *bn256.G1 {
  44. seed_reduced := new(big.Int)
  45. seed_reduced.Mod(seed, FIELD_MODULUS)
  46. p_1_4 := new(big.Int).Add(FIELD_MODULUS, new(big.Int).SetInt64(1))
  47. p_1_4 = p_1_4.Div(p_1_4, new(big.Int).SetInt64(4))
  48. for {
  49. tmp := new(big.Int)
  50. y, y_squared, y_resquare := new(big.Int), new(big.Int), new(big.Int) // basically y_sqaured = seed ^3 + 3 mod group order
  51. tmp.Exp(seed_reduced, new(big.Int).SetInt64(3), FIELD_MODULUS)
  52. y_squared.Add(tmp, new(big.Int).SetInt64(3))
  53. y_squared.Mod(y_squared, FIELD_MODULUS)
  54. y = y.Exp(y_squared, p_1_4, FIELD_MODULUS)
  55. y_resquare = y_resquare.Exp(y, new(big.Int).SetInt64(2), FIELD_MODULUS)
  56. if y_resquare.Cmp(y_squared) == 0 { // seed becomes x and y iis usy
  57. xstring := seed_reduced.Text(16)
  58. ystring := y.Text(16)
  59. var point bn256.G1
  60. xbytes, err := hex.DecodeString(makestring64(xstring))
  61. if err != nil {
  62. panic(err)
  63. }
  64. ybytes, err := hex.DecodeString(makestring64(ystring))
  65. if err != nil {
  66. panic(err)
  67. }
  68. point.Unmarshal(append(xbytes, ybytes...))
  69. return &point
  70. }
  71. seed_reduced.Add(seed_reduced, new(big.Int).SetInt64(1))
  72. seed_reduced.Mod(seed_reduced, FIELD_MODULUS)
  73. }
  74. return nil
  75. }
  76. func NewGeneratorParams(count int) *GeneratorParams {
  77. GP := &GeneratorParams{}
  78. var zeroes [64]byte
  79. GP.G = HashToPoint(HashtoNumber([]byte(PROTOCOL_CONSTANT + "G"))) // this is same as mybase or vice-versa
  80. GP.H = HashToPoint(HashtoNumber([]byte(PROTOCOL_CONSTANT + "H")))
  81. var gs, hs []*bn256.G1
  82. GP.GSUM = new(bn256.G1)
  83. GP.GSUM.Unmarshal(zeroes[:])
  84. for i := 0; i < count; i++ {
  85. gs = append(gs, HashToPoint(HashtoNumber(append([]byte(PROTOCOL_CONSTANT+"G"), hextobytes(makestring64(fmt.Sprintf("%x", i)))...))))
  86. hs = append(hs, HashToPoint(HashtoNumber(append([]byte(PROTOCOL_CONSTANT+"H"), hextobytes(makestring64(fmt.Sprintf("%x", i)))...))))
  87. GP.GSUM = new(bn256.G1).Add(GP.GSUM, gs[i])
  88. }
  89. GP.Gs = NewGeneratorVector(gs)
  90. GP.Hs = NewGeneratorVector(hs)
  91. return GP
  92. }
  93. func NewGeneratorParams3(h *bn256.G1, gs, hs *GeneratorVector) *GeneratorParams {
  94. GP := &GeneratorParams{}
  95. GP.G = HashToPoint(HashtoNumber([]byte(PROTOCOL_CONSTANT + "G"))) // this is same as mybase or vice-versa
  96. GP.H = h
  97. GP.Gs = gs
  98. GP.Hs = hs
  99. return GP
  100. }
  101. func (gp *GeneratorParams) Commit(blind *big.Int, gexps, hexps *FieldVector) *bn256.G1 {
  102. result := new(bn256.G1).ScalarMult(gp.H, blind)
  103. for i := range gexps.vector {
  104. result = new(bn256.G1).Add(result, new(bn256.G1).ScalarMult(gp.Gs.vector[i], gexps.vector[i]))
  105. }
  106. if hexps != nil {
  107. for i := range hexps.vector {
  108. result = new(bn256.G1).Add(result, new(bn256.G1).ScalarMult(gp.Hs.vector[i], hexps.vector[i]))
  109. }
  110. }
  111. return result
  112. }