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.

345 lines
6.9 KiB

  1. package gocircomprover
  2. import (
  3. "bytes"
  4. "encoding/hex"
  5. "encoding/json"
  6. "fmt"
  7. "math/big"
  8. "strconv"
  9. "strings"
  10. bn256 "github.com/ethereum/go-ethereum/crypto/bn256/cloudflare"
  11. )
  12. // ProvingKeyString is the equivalent to the ProvingKey struct in string representation
  13. type ProvingKeyString struct {
  14. A [][]string `json:"A"`
  15. B2 [][][]string `json:"B2"`
  16. B1 [][]string `json:"B1"`
  17. C [][]string `json:"C"`
  18. NVars int `json:"nVars"`
  19. NPublic int `json:"nPublic"`
  20. VkAlpha1 []string `json:"vk_alfa_1"`
  21. VkDelta1 []string `json:"vk_delta_1"`
  22. VkBeta1 []string `json:"vk_beta_1"`
  23. VkBeta2 [][]string `json:"vk_beta_2"`
  24. VkDelta2 [][]string `json:"vk_delta_2"`
  25. HExps [][]string `json:"hExps"`
  26. DomainSize int `json:"domainSize"`
  27. PolsA []map[string]string `json:"polsA"`
  28. PolsB []map[string]string `json:"polsB"`
  29. PolsC []map[string]string `json:"polsC"`
  30. }
  31. // WitnessString contains the Witness in string representation
  32. type WitnessString []string
  33. // ParseWitness parses the json []byte data into the Witness struct
  34. func ParseWitness(wJson []byte) (Witness, error) {
  35. var ws WitnessString
  36. err := json.Unmarshal(wJson, &ws)
  37. if err != nil {
  38. return nil, err
  39. }
  40. var w Witness
  41. for i := 0; i < len(ws); i++ {
  42. bi, err := stringToBigInt(ws[i])
  43. if err != nil {
  44. return nil, err
  45. }
  46. w = append(w, bi)
  47. }
  48. return w, nil
  49. }
  50. // ParseProvingKey parses the json []byte data into the ProvingKey struct
  51. func ParseProvingKey(pkJson []byte) (*ProvingKey, error) {
  52. var pkStr ProvingKeyString
  53. err := json.Unmarshal(pkJson, &pkStr)
  54. if err != nil {
  55. return nil, err
  56. }
  57. pk, err := provingKeyStringToProvingKey(pkStr)
  58. return pk, err
  59. }
  60. func provingKeyStringToProvingKey(ps ProvingKeyString) (*ProvingKey, error) {
  61. var p ProvingKey
  62. var err error
  63. p.A, err = arrayStringToG1(ps.A)
  64. if err != nil {
  65. return nil, err
  66. }
  67. p.B2, err = arrayStringToG2(ps.B2)
  68. if err != nil {
  69. return nil, err
  70. }
  71. p.B1, err = arrayStringToG1(ps.B1)
  72. if err != nil {
  73. return nil, err
  74. }
  75. p.C, err = arrayStringToG1(ps.C)
  76. if err != nil {
  77. return nil, err
  78. }
  79. p.NVars = ps.NVars
  80. p.NPublic = ps.NPublic
  81. p.VkAlpha1, err = stringToG1(ps.VkAlpha1)
  82. if err != nil {
  83. return nil, err
  84. }
  85. p.VkDelta1, err = stringToG1(ps.VkDelta1)
  86. if err != nil {
  87. return nil, err
  88. }
  89. p.VkBeta1, err = stringToG1(ps.VkBeta1)
  90. if err != nil {
  91. return nil, err
  92. }
  93. p.VkBeta2, err = stringToG2(ps.VkBeta2)
  94. if err != nil {
  95. return nil, err
  96. }
  97. p.VkDelta2, err = stringToG2(ps.VkDelta2)
  98. if err != nil {
  99. return nil, err
  100. }
  101. p.HExps, err = arrayStringToG1(ps.HExps)
  102. if err != nil {
  103. return nil, err
  104. }
  105. p.DomainSize = ps.DomainSize
  106. p.PolsA, err = polsStringToBigInt(ps.PolsA)
  107. if err != nil {
  108. return nil, err
  109. }
  110. p.PolsB, err = polsStringToBigInt(ps.PolsB)
  111. if err != nil {
  112. return nil, err
  113. }
  114. p.PolsC, err = polsStringToBigInt(ps.PolsC)
  115. if err != nil {
  116. return nil, err
  117. }
  118. return &p, nil
  119. }
  120. // polsStringToBigInt is for taking string polynomials and converting it to *big.Int polynomials
  121. func polsStringToBigInt(s []map[string]string) ([]map[int]*big.Int, error) {
  122. var o []map[int]*big.Int
  123. for i := 0; i < len(s); i++ {
  124. // var oi map[int]*big.Int
  125. oi := make(map[int]*big.Int)
  126. for j, v := range s[i] {
  127. si, err := stringToBigInt(v)
  128. if err != nil {
  129. return o, err
  130. }
  131. // oi = append(oi, si)
  132. jInt, err := strconv.Atoi(j)
  133. if err != nil {
  134. fmt.Println(j)
  135. return o, err
  136. }
  137. fmt.Println(jInt, si)
  138. oi[jInt] = si
  139. }
  140. o = append(o, oi)
  141. }
  142. return o, nil
  143. }
  144. func arrayStringToBigInt(s []string) ([]*big.Int, error) {
  145. var o []*big.Int
  146. for i := 0; i < len(s); i++ {
  147. si, err := stringToBigInt(s[i])
  148. if err != nil {
  149. return o, nil
  150. }
  151. o = append(o, si)
  152. }
  153. return o, nil
  154. }
  155. func stringToBigInt(s string) (*big.Int, error) {
  156. base := 10
  157. if bytes.HasPrefix([]byte(s), []byte("0x")) {
  158. base = 16
  159. s = strings.TrimPrefix(s, "0x")
  160. }
  161. n, ok := new(big.Int).SetString(s, base)
  162. if !ok {
  163. return nil, fmt.Errorf("Can not parse string to *big.Int: %s", s)
  164. }
  165. return n, nil
  166. }
  167. func addZPadding(b []byte) []byte {
  168. var z [32]byte
  169. var r []byte
  170. r = append(r, z[len(b):]...) // add padding on the left
  171. r = append(r, b...)
  172. return r[:32]
  173. }
  174. func stringToBytes(s string) ([]byte, error) {
  175. if s == "1" {
  176. s = "0"
  177. }
  178. bi, ok := new(big.Int).SetString(s, 10)
  179. if !ok {
  180. return nil, fmt.Errorf("error parsing bigint stringToBytes")
  181. }
  182. b := bi.Bytes()
  183. if len(b) != 32 {
  184. b = addZPadding(b)
  185. }
  186. return b, nil
  187. }
  188. func arrayStringToG1(h [][]string) ([]*bn256.G1, error) {
  189. var o []*bn256.G1
  190. for i := 0; i < len(h); i++ {
  191. hi, err := stringToG1(h[i])
  192. if err != nil {
  193. return o, err
  194. }
  195. o = append(o, hi)
  196. }
  197. return o, nil
  198. }
  199. func arrayStringToG2(h [][][]string) ([]*bn256.G2, error) {
  200. var o []*bn256.G2
  201. for i := 0; i < len(h); i++ {
  202. hi, err := stringToG2(h[i])
  203. if err != nil {
  204. return o, err
  205. }
  206. o = append(o, hi)
  207. }
  208. return o, nil
  209. }
  210. func stringToG1(h []string) (*bn256.G1, error) {
  211. if len(h) <= 2 {
  212. return nil, fmt.Errorf("not enought data for stringToG1")
  213. }
  214. h = h[:2]
  215. hexa := false
  216. if len(h[0]) > 1 {
  217. if "0x" == h[0][:2] {
  218. hexa = true
  219. }
  220. }
  221. in := ""
  222. var b []byte
  223. var err error
  224. if hexa {
  225. for i := range h {
  226. in += strings.TrimPrefix(h[i], "0x")
  227. }
  228. b, err = hex.DecodeString(in)
  229. if err != nil {
  230. return nil, err
  231. }
  232. } else {
  233. // TODO TMP
  234. // TODO use stringToBytes()
  235. if h[0] == "1" {
  236. h[0] = "0"
  237. }
  238. if h[1] == "1" {
  239. h[1] = "0"
  240. }
  241. bi0, ok := new(big.Int).SetString(h[0], 10)
  242. if !ok {
  243. return nil, fmt.Errorf("error parsing stringToG1")
  244. }
  245. bi1, ok := new(big.Int).SetString(h[1], 10)
  246. if !ok {
  247. return nil, fmt.Errorf("error parsing stringToG1")
  248. }
  249. b0 := bi0.Bytes()
  250. b1 := bi1.Bytes()
  251. if len(b0) != 32 {
  252. b0 = addZPadding(b0)
  253. }
  254. if len(b1) != 32 {
  255. b1 = addZPadding(b1)
  256. }
  257. b = append(b, b0...)
  258. b = append(b, b1...)
  259. }
  260. p := new(bn256.G1)
  261. _, err = p.Unmarshal(b)
  262. return p, err
  263. }
  264. func stringToG2(h [][]string) (*bn256.G2, error) {
  265. if len(h) <= 2 {
  266. return nil, fmt.Errorf("not enought data for stringToG2")
  267. }
  268. h = h[:2]
  269. hexa := false
  270. if len(h[0][0]) > 1 {
  271. if "0x" == h[0][0][:2] {
  272. hexa = true
  273. }
  274. }
  275. in := ""
  276. var b []byte
  277. var err error
  278. if hexa {
  279. for i := 0; i < len(h); i++ {
  280. for j := 0; j < len(h[i]); j++ {
  281. in += strings.TrimPrefix(h[i][j], "0x")
  282. }
  283. }
  284. b, err = hex.DecodeString(in)
  285. if err != nil {
  286. return nil, err
  287. }
  288. } else {
  289. // TODO TMP
  290. bH, err := stringToBytes(h[0][1])
  291. if err != nil {
  292. return nil, err
  293. }
  294. b = append(b, bH...)
  295. bH, err = stringToBytes(h[0][0])
  296. if err != nil {
  297. return nil, err
  298. }
  299. b = append(b, bH...)
  300. bH, err = stringToBytes(h[1][1])
  301. if err != nil {
  302. return nil, err
  303. }
  304. b = append(b, bH...)
  305. bH, err = stringToBytes(h[1][0])
  306. if err != nil {
  307. return nil, err
  308. }
  309. b = append(b, bH...)
  310. }
  311. p := new(bn256.G2)
  312. _, err = p.Unmarshal(b)
  313. return p, err
  314. }