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.

498 lines
10 KiB

  1. package parsers
  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. "github.com/iden3/go-circom-prover-verifier/types"
  12. )
  13. // PkString is the equivalent to the Pk struct in string representation, containing the ProvingKey
  14. type PkString struct {
  15. A [][]string `json:"A"`
  16. B2 [][][]string `json:"B2"`
  17. B1 [][]string `json:"B1"`
  18. C [][]string `json:"C"`
  19. NVars int `json:"nVars"`
  20. NPublic int `json:"nPublic"`
  21. VkAlpha1 []string `json:"vk_alfa_1"`
  22. VkDelta1 []string `json:"vk_delta_1"`
  23. VkBeta1 []string `json:"vk_beta_1"`
  24. VkBeta2 [][]string `json:"vk_beta_2"`
  25. VkDelta2 [][]string `json:"vk_delta_2"`
  26. HExps [][]string `json:"hExps"`
  27. DomainSize int `json:"domainSize"`
  28. PolsA []map[string]string `json:"polsA"`
  29. PolsB []map[string]string `json:"polsB"`
  30. PolsC []map[string]string `json:"polsC"`
  31. }
  32. // WitnessString contains the Witness in string representation
  33. type WitnessString []string
  34. // ProofString is the equivalent to the Proof struct in string representation
  35. type ProofString struct {
  36. A []string `json:"pi_a"`
  37. B [][]string `json:"pi_b"`
  38. C []string `json:"pi_c"`
  39. Protocol string `json:"protocol"`
  40. }
  41. // VkString is the Verification Key data structure in string format (from json)
  42. type VkString struct {
  43. Alpha []string `json:"vk_alfa_1"`
  44. Beta [][]string `json:"vk_beta_2"`
  45. Gamma [][]string `json:"vk_gamma_2"`
  46. Delta [][]string `json:"vk_delta_2"`
  47. IC [][]string `json:"IC"`
  48. }
  49. // ParseWitness parses the json []byte data into the Witness struct
  50. func ParseWitness(wJson []byte) (types.Witness, error) {
  51. var ws WitnessString
  52. err := json.Unmarshal(wJson, &ws)
  53. if err != nil {
  54. return nil, err
  55. }
  56. var w types.Witness
  57. for i := 0; i < len(ws); i++ {
  58. bi, err := stringToBigInt(ws[i])
  59. if err != nil {
  60. return nil, err
  61. }
  62. w = append(w, bi)
  63. }
  64. return w, nil
  65. }
  66. // ParsePk parses the json []byte data into the Pk struct
  67. func ParsePk(pkJson []byte) (*types.Pk, error) {
  68. var pkStr PkString
  69. err := json.Unmarshal(pkJson, &pkStr)
  70. if err != nil {
  71. return nil, err
  72. }
  73. pk, err := pkStringToPk(pkStr)
  74. return pk, err
  75. }
  76. func pkStringToPk(ps PkString) (*types.Pk, error) {
  77. var p types.Pk
  78. var err error
  79. p.A, err = arrayStringToG1(ps.A)
  80. if err != nil {
  81. return nil, err
  82. }
  83. p.B2, err = arrayStringToG2(ps.B2)
  84. if err != nil {
  85. return nil, err
  86. }
  87. p.B1, err = arrayStringToG1(ps.B1)
  88. if err != nil {
  89. return nil, err
  90. }
  91. p.C, err = arrayStringToG1(ps.C)
  92. if err != nil {
  93. return nil, err
  94. }
  95. p.NVars = ps.NVars
  96. p.NPublic = ps.NPublic
  97. p.VkAlpha1, err = stringToG1(ps.VkAlpha1)
  98. if err != nil {
  99. return nil, err
  100. }
  101. p.VkDelta1, err = stringToG1(ps.VkDelta1)
  102. if err != nil {
  103. return nil, err
  104. }
  105. p.VkBeta1, err = stringToG1(ps.VkBeta1)
  106. if err != nil {
  107. return nil, err
  108. }
  109. p.VkBeta2, err = stringToG2(ps.VkBeta2)
  110. if err != nil {
  111. return nil, err
  112. }
  113. p.VkDelta2, err = stringToG2(ps.VkDelta2)
  114. if err != nil {
  115. return nil, err
  116. }
  117. p.HExps, err = arrayStringToG1(ps.HExps)
  118. if err != nil {
  119. return nil, err
  120. }
  121. p.DomainSize = ps.DomainSize
  122. p.PolsA, err = polsStringToBigInt(ps.PolsA)
  123. if err != nil {
  124. return nil, err
  125. }
  126. p.PolsB, err = polsStringToBigInt(ps.PolsB)
  127. if err != nil {
  128. return nil, err
  129. }
  130. p.PolsC, err = polsStringToBigInt(ps.PolsC)
  131. if err != nil {
  132. return nil, err
  133. }
  134. return &p, nil
  135. }
  136. func proofStringToProof(pr ProofString) (*types.Proof, error) {
  137. var p types.Proof
  138. var err error
  139. p.A, err = stringToG1(pr.A)
  140. if err != nil {
  141. return nil, err
  142. }
  143. p.B, err = stringToG2(pr.B)
  144. if err != nil {
  145. return nil, err
  146. }
  147. p.C, err = stringToG1(pr.C)
  148. if err != nil {
  149. return nil, err
  150. }
  151. return &p, nil
  152. }
  153. // ParseProof takes a json []byte and outputs the *Proof struct
  154. func ParseProof(pj []byte) (*types.Proof, error) {
  155. var pr ProofString
  156. err := json.Unmarshal(pj, &pr)
  157. if err != nil {
  158. return nil, err
  159. }
  160. p, err := proofStringToProof(pr)
  161. return p, err
  162. }
  163. // ParsePublicSignals takes a json []byte and outputs the []*big.Int struct
  164. func ParsePublicSignals(pj []byte) ([]*big.Int, error) {
  165. var pr []string
  166. err := json.Unmarshal(pj, &pr)
  167. if err != nil {
  168. return nil, err
  169. }
  170. var public []*big.Int
  171. for _, s := range pr {
  172. sb, err := stringToBigInt(s)
  173. if err != nil {
  174. return nil, err
  175. }
  176. public = append(public, sb)
  177. }
  178. return public, nil
  179. }
  180. // ParseVk takes a json []byte and outputs the *Vk struct
  181. func ParseVk(vj []byte) (*types.Vk, error) {
  182. var vr VkString
  183. err := json.Unmarshal(vj, &vr)
  184. if err != nil {
  185. return nil, err
  186. }
  187. v, err := vkStringToVk(vr)
  188. return v, err
  189. }
  190. func vkStringToVk(vr VkString) (*types.Vk, error) {
  191. var v types.Vk
  192. var err error
  193. v.Alpha, err = stringToG1(vr.Alpha)
  194. if err != nil {
  195. return nil, err
  196. }
  197. v.Beta, err = stringToG2(vr.Beta)
  198. if err != nil {
  199. return nil, err
  200. }
  201. v.Gamma, err = stringToG2(vr.Gamma)
  202. if err != nil {
  203. return nil, err
  204. }
  205. v.Delta, err = stringToG2(vr.Delta)
  206. if err != nil {
  207. return nil, err
  208. }
  209. for i := 0; i < len(vr.IC); i++ {
  210. p, err := stringToG1(vr.IC[i])
  211. if err != nil {
  212. return nil, err
  213. }
  214. v.IC = append(v.IC, p)
  215. }
  216. return &v, nil
  217. }
  218. // polsStringToBigInt is for taking string polynomials and converting it to *big.Int polynomials
  219. func polsStringToBigInt(s []map[string]string) ([]map[int]*big.Int, error) {
  220. var o []map[int]*big.Int
  221. for i := 0; i < len(s); i++ {
  222. // var oi map[int]*big.Int
  223. oi := make(map[int]*big.Int)
  224. for j, v := range s[i] {
  225. si, err := stringToBigInt(v)
  226. if err != nil {
  227. return o, err
  228. }
  229. // oi = append(oi, si)
  230. jInt, err := strconv.Atoi(j)
  231. if err != nil {
  232. return o, err
  233. }
  234. oi[jInt] = si
  235. }
  236. o = append(o, oi)
  237. }
  238. return o, nil
  239. }
  240. // ArrayBigIntToString converts an []*big.Int into []string, used to output the Public Signals
  241. func ArrayBigIntToString(bi []*big.Int) []string {
  242. var s []string
  243. for i := 0; i < len(bi); i++ {
  244. s = append(s, bi[i].String())
  245. }
  246. return s
  247. }
  248. func arrayStringToBigInt(s []string) ([]*big.Int, error) {
  249. var o []*big.Int
  250. for i := 0; i < len(s); i++ {
  251. si, err := stringToBigInt(s[i])
  252. if err != nil {
  253. return o, nil
  254. }
  255. o = append(o, si)
  256. }
  257. return o, nil
  258. }
  259. func stringToBigInt(s string) (*big.Int, error) {
  260. base := 10
  261. if bytes.HasPrefix([]byte(s), []byte("0x")) {
  262. base = 16
  263. s = strings.TrimPrefix(s, "0x")
  264. }
  265. n, ok := new(big.Int).SetString(s, base)
  266. if !ok {
  267. return nil, fmt.Errorf("Can not parse string to *big.Int: %s", s)
  268. }
  269. return n, nil
  270. }
  271. func addZPadding(b []byte) []byte {
  272. var z [32]byte
  273. var r []byte
  274. r = append(r, z[len(b):]...) // add padding on the left
  275. r = append(r, b...)
  276. return r[:32]
  277. }
  278. func stringToBytes(s string) ([]byte, error) {
  279. if s == "1" {
  280. s = "0"
  281. }
  282. bi, ok := new(big.Int).SetString(s, 10)
  283. if !ok {
  284. return nil, fmt.Errorf("error parsing bigint stringToBytes")
  285. }
  286. b := bi.Bytes()
  287. if len(b) != 32 {
  288. b = addZPadding(b)
  289. }
  290. return b, nil
  291. }
  292. func arrayStringToG1(h [][]string) ([]*bn256.G1, error) {
  293. var o []*bn256.G1
  294. for i := 0; i < len(h); i++ {
  295. hi, err := stringToG1(h[i])
  296. if err != nil {
  297. return o, err
  298. }
  299. o = append(o, hi)
  300. }
  301. return o, nil
  302. }
  303. func arrayStringToG2(h [][][]string) ([]*bn256.G2, error) {
  304. var o []*bn256.G2
  305. for i := 0; i < len(h); i++ {
  306. hi, err := stringToG2(h[i])
  307. if err != nil {
  308. return o, err
  309. }
  310. o = append(o, hi)
  311. }
  312. return o, nil
  313. }
  314. func stringToG1(h []string) (*bn256.G1, error) {
  315. if len(h) <= 2 {
  316. return nil, fmt.Errorf("not enought data for stringToG1")
  317. }
  318. h = h[:2]
  319. hexa := false
  320. if len(h[0]) > 1 {
  321. if "0x" == h[0][:2] {
  322. hexa = true
  323. }
  324. }
  325. in := ""
  326. var b []byte
  327. var err error
  328. if hexa {
  329. for i := range h {
  330. in += strings.TrimPrefix(h[i], "0x")
  331. }
  332. b, err = hex.DecodeString(in)
  333. if err != nil {
  334. return nil, err
  335. }
  336. } else {
  337. // TODO TMP
  338. // TODO use stringToBytes()
  339. if h[0] == "1" {
  340. h[0] = "0"
  341. }
  342. if h[1] == "1" {
  343. h[1] = "0"
  344. }
  345. bi0, ok := new(big.Int).SetString(h[0], 10)
  346. if !ok {
  347. return nil, fmt.Errorf("error parsing stringToG1")
  348. }
  349. bi1, ok := new(big.Int).SetString(h[1], 10)
  350. if !ok {
  351. return nil, fmt.Errorf("error parsing stringToG1")
  352. }
  353. b0 := bi0.Bytes()
  354. b1 := bi1.Bytes()
  355. if len(b0) != 32 {
  356. b0 = addZPadding(b0)
  357. }
  358. if len(b1) != 32 {
  359. b1 = addZPadding(b1)
  360. }
  361. b = append(b, b0...)
  362. b = append(b, b1...)
  363. }
  364. p := new(bn256.G1)
  365. _, err = p.Unmarshal(b)
  366. return p, err
  367. }
  368. func stringToG2(h [][]string) (*bn256.G2, error) {
  369. if len(h) <= 2 {
  370. return nil, fmt.Errorf("not enought data for stringToG2")
  371. }
  372. h = h[:2]
  373. hexa := false
  374. if len(h[0][0]) > 1 {
  375. if "0x" == h[0][0][:2] {
  376. hexa = true
  377. }
  378. }
  379. in := ""
  380. var b []byte
  381. var err error
  382. if hexa {
  383. for i := 0; i < len(h); i++ {
  384. for j := 0; j < len(h[i]); j++ {
  385. in += strings.TrimPrefix(h[i][j], "0x")
  386. }
  387. }
  388. b, err = hex.DecodeString(in)
  389. if err != nil {
  390. return nil, err
  391. }
  392. } else {
  393. // TODO TMP
  394. bH, err := stringToBytes(h[0][1])
  395. if err != nil {
  396. return nil, err
  397. }
  398. b = append(b, bH...)
  399. bH, err = stringToBytes(h[0][0])
  400. if err != nil {
  401. return nil, err
  402. }
  403. b = append(b, bH...)
  404. bH, err = stringToBytes(h[1][1])
  405. if err != nil {
  406. return nil, err
  407. }
  408. b = append(b, bH...)
  409. bH, err = stringToBytes(h[1][0])
  410. if err != nil {
  411. return nil, err
  412. }
  413. b = append(b, bH...)
  414. }
  415. p := new(bn256.G2)
  416. _, err = p.Unmarshal(b)
  417. return p, err
  418. }
  419. // ProofToJson outputs the Proof i Json format
  420. func ProofToJson(p *types.Proof) ([]byte, error) {
  421. var ps ProofString
  422. ps.A = make([]string, 3)
  423. ps.B = make([][]string, 3)
  424. ps.B[0] = make([]string, 2)
  425. ps.B[1] = make([]string, 2)
  426. ps.B[2] = make([]string, 2)
  427. ps.C = make([]string, 3)
  428. a := p.A.Marshal()
  429. ps.A[0] = new(big.Int).SetBytes(a[:32]).String()
  430. ps.A[1] = new(big.Int).SetBytes(a[32:64]).String()
  431. ps.A[2] = "1"
  432. b := p.B.Marshal()
  433. ps.B[0][1] = new(big.Int).SetBytes(b[:32]).String()
  434. ps.B[0][0] = new(big.Int).SetBytes(b[32:64]).String()
  435. ps.B[1][1] = new(big.Int).SetBytes(b[64:96]).String()
  436. ps.B[1][0] = new(big.Int).SetBytes(b[96:128]).String()
  437. ps.B[2][0] = "1"
  438. ps.B[2][1] = "0"
  439. c := p.C.Marshal()
  440. ps.C[0] = new(big.Int).SetBytes(c[:32]).String()
  441. ps.C[1] = new(big.Int).SetBytes(c[32:64]).String()
  442. ps.C[2] = "1"
  443. ps.Protocol = "groth"
  444. return json.Marshal(ps)
  445. }