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.

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