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.

585 lines
13 KiB

  1. package utils
  2. import (
  3. "errors"
  4. "math/big"
  5. snark "github.com/arnaucube/go-snark-study"
  6. "github.com/arnaucube/go-snark-study/circuitcompiler"
  7. "github.com/arnaucube/go-snark-study/groth16"
  8. )
  9. // []*big.Int
  10. func ArrayBigIntToString(b []*big.Int) []string {
  11. var o []string
  12. for i := 0; i < len(b); i++ {
  13. o = append(o, b[i].String())
  14. }
  15. return o
  16. }
  17. func ArrayStringToBigInt(s []string) ([]*big.Int, error) {
  18. var px []*big.Int
  19. for i := 0; i < len(s); i++ {
  20. param, ok := new(big.Int).SetString(s[i], 10)
  21. if !ok {
  22. return nil, errors.New("error parsing px from pxString")
  23. }
  24. px = append(px, param)
  25. }
  26. return px, nil
  27. }
  28. // [3]*big.Int
  29. func String3ToBigInt(s [3]string) ([3]*big.Int, error) {
  30. var o [3]*big.Int
  31. for i := 0; i < len(s); i++ {
  32. param, ok := new(big.Int).SetString(s[i], 10)
  33. if !ok {
  34. return o, errors.New("error parsing [3]*big.Int from [3]string")
  35. }
  36. o[i] = param
  37. }
  38. return o, nil
  39. }
  40. func BigInt3ToString(b [3]*big.Int) [3]string {
  41. var o [3]string
  42. o[0] = b[0].String()
  43. o[1] = b[1].String()
  44. o[2] = b[2].String()
  45. return o
  46. }
  47. // [][3]*big.Int
  48. func Array3StringToBigInt(s [][3]string) ([][3]*big.Int, error) {
  49. var o [][3]*big.Int
  50. for i := 0; i < len(s); i++ {
  51. parsed, err := String3ToBigInt(s[i])
  52. if err != nil {
  53. return o, err
  54. }
  55. o = append(o, parsed)
  56. }
  57. return o, nil
  58. }
  59. func Array3BigIntToString(b [][3]*big.Int) [][3]string {
  60. var o [][3]string
  61. for i := 0; i < len(b); i++ {
  62. o = append(o, BigInt3ToString(b[i]))
  63. }
  64. return o
  65. }
  66. func String2ToBigInt(s [2]string) ([2]*big.Int, error) {
  67. var o [2]*big.Int
  68. for i := 0; i < len(s); i++ {
  69. param, ok := new(big.Int).SetString(s[i], 10)
  70. if !ok {
  71. return o, errors.New("error parsing [2]*big.Int from [2]string")
  72. }
  73. o[i] = param
  74. }
  75. return o, nil
  76. }
  77. // [3][2]*big.Int
  78. func String32ToBigInt(s [3][2]string) ([3][2]*big.Int, error) {
  79. var o [3][2]*big.Int
  80. var err error
  81. o[0], err = String2ToBigInt(s[0])
  82. if err != nil {
  83. return o, err
  84. }
  85. o[1], err = String2ToBigInt(s[1])
  86. if err != nil {
  87. return o, err
  88. }
  89. o[2], err = String2ToBigInt(s[2])
  90. if err != nil {
  91. return o, err
  92. }
  93. return o, nil
  94. }
  95. func BigInt32ToString(b [3][2]*big.Int) [3][2]string {
  96. var o [3][2]string
  97. o[0][0] = b[0][0].String()
  98. o[0][1] = b[0][1].String()
  99. o[1][0] = b[1][0].String()
  100. o[1][1] = b[1][1].String()
  101. o[2][0] = b[2][0].String()
  102. o[2][1] = b[2][1].String()
  103. return o
  104. }
  105. // [][3][2]*big.Int
  106. func Array32StringToBigInt(s [][3][2]string) ([][3][2]*big.Int, error) {
  107. var o [][3][2]*big.Int
  108. for i := 0; i < len(s); i++ {
  109. parsed, err := String32ToBigInt(s[i])
  110. if err != nil {
  111. return o, err
  112. }
  113. o = append(o, parsed)
  114. }
  115. return o, nil
  116. }
  117. func Array32BigIntToString(b [][3][2]*big.Int) [][3][2]string {
  118. var o [][3][2]string
  119. for i := 0; i < len(b); i++ {
  120. o = append(o, BigInt32ToString(b[i]))
  121. }
  122. return o
  123. }
  124. // Setup
  125. type SetupString struct {
  126. // public
  127. Pk struct {
  128. G1T [][3]string
  129. A [][3]string
  130. B [][3][2]string
  131. C [][3]string
  132. Kp [][3]string
  133. Ap [][3]string
  134. Bp [][3]string
  135. Cp [][3]string
  136. Z []string
  137. }
  138. Vk struct {
  139. Vka [3][2]string
  140. Vkb [3]string
  141. Vkc [3][2]string
  142. IC [][3]string
  143. G1Kbg [3]string
  144. G2Kbg [3][2]string
  145. G2Kg [3][2]string
  146. Vkz [3][2]string
  147. }
  148. }
  149. func SetupToString(setup snark.Setup) SetupString {
  150. var s SetupString
  151. s.Pk.G1T = Array3BigIntToString(setup.Pk.G1T)
  152. s.Pk.A = Array3BigIntToString(setup.Pk.A)
  153. s.Pk.B = Array32BigIntToString(setup.Pk.B)
  154. s.Pk.C = Array3BigIntToString(setup.Pk.C)
  155. s.Pk.Kp = Array3BigIntToString(setup.Pk.Kp)
  156. s.Pk.Ap = Array3BigIntToString(setup.Pk.Ap)
  157. s.Pk.Bp = Array3BigIntToString(setup.Pk.Bp)
  158. s.Pk.Cp = Array3BigIntToString(setup.Pk.Cp)
  159. s.Pk.Z = ArrayBigIntToString(setup.Pk.Z)
  160. s.Vk.Vka = BigInt32ToString(setup.Vk.Vka)
  161. s.Vk.Vkb = BigInt3ToString(setup.Vk.Vkb)
  162. s.Vk.Vkc = BigInt32ToString(setup.Vk.Vkc)
  163. s.Vk.IC = Array3BigIntToString(setup.Vk.IC)
  164. s.Vk.G1Kbg = BigInt3ToString(setup.Vk.G1Kbg)
  165. s.Vk.G2Kbg = BigInt32ToString(setup.Vk.G2Kbg)
  166. s.Vk.G2Kg = BigInt32ToString(setup.Vk.G2Kg)
  167. s.Vk.Vkz = BigInt32ToString(setup.Vk.Vkz)
  168. return s
  169. }
  170. func SetupFromString(s SetupString) (snark.Setup, error) {
  171. var o snark.Setup
  172. var err error
  173. o.Pk.G1T, err = Array3StringToBigInt(s.Pk.G1T)
  174. if err != nil {
  175. return o, err
  176. }
  177. o.Pk.A, err = Array3StringToBigInt(s.Pk.A)
  178. if err != nil {
  179. return o, err
  180. }
  181. o.Pk.B, err = Array32StringToBigInt(s.Pk.B)
  182. if err != nil {
  183. return o, err
  184. }
  185. o.Pk.C, err = Array3StringToBigInt(s.Pk.C)
  186. if err != nil {
  187. return o, err
  188. }
  189. o.Pk.Kp, err = Array3StringToBigInt(s.Pk.Kp)
  190. if err != nil {
  191. return o, err
  192. }
  193. o.Pk.Ap, err = Array3StringToBigInt(s.Pk.Ap)
  194. if err != nil {
  195. return o, err
  196. }
  197. o.Pk.Bp, err = Array3StringToBigInt(s.Pk.Bp)
  198. if err != nil {
  199. return o, err
  200. }
  201. o.Pk.Cp, err = Array3StringToBigInt(s.Pk.Cp)
  202. if err != nil {
  203. return o, err
  204. }
  205. o.Pk.Z, err = ArrayStringToBigInt(s.Pk.Z)
  206. if err != nil {
  207. return o, err
  208. }
  209. o.Vk.Vka, err = String32ToBigInt(s.Vk.Vka)
  210. if err != nil {
  211. return o, err
  212. }
  213. o.Vk.Vkb, err = String3ToBigInt(s.Vk.Vkb)
  214. if err != nil {
  215. return o, err
  216. }
  217. o.Vk.Vkc, err = String32ToBigInt(s.Vk.Vkc)
  218. if err != nil {
  219. return o, err
  220. }
  221. o.Vk.IC, err = Array3StringToBigInt(s.Vk.IC)
  222. if err != nil {
  223. return o, err
  224. }
  225. o.Vk.G1Kbg, err = String3ToBigInt(s.Vk.G1Kbg)
  226. if err != nil {
  227. return o, err
  228. }
  229. o.Vk.G2Kbg, err = String32ToBigInt(s.Vk.G2Kbg)
  230. if err != nil {
  231. return o, err
  232. }
  233. o.Vk.G2Kg, err = String32ToBigInt(s.Vk.G2Kg)
  234. if err != nil {
  235. return o, err
  236. }
  237. o.Vk.Vkz, err = String32ToBigInt(s.Vk.Vkz)
  238. if err != nil {
  239. return o, err
  240. }
  241. return o, nil
  242. }
  243. // circuit
  244. type CircuitString struct {
  245. NVars int
  246. NPublic int
  247. NSignals int
  248. PrivateInputs []string
  249. PublicInputs []string
  250. Signals []string
  251. Witness []string
  252. Constraints []circuitcompiler.Constraint
  253. R1CS struct {
  254. A [][]string
  255. B [][]string
  256. C [][]string
  257. }
  258. }
  259. func ArrayArrayBigIntToString(b [][]*big.Int) [][]string {
  260. var o [][]string
  261. for i := 0; i < len(b); i++ {
  262. o = append(o, ArrayBigIntToString(b[i]))
  263. }
  264. return o
  265. }
  266. func ArrayArrayStringToBigInt(s [][]string) ([][]*big.Int, error) {
  267. var o [][]*big.Int
  268. for i := 0; i < len(s); i++ {
  269. parsed, err := ArrayStringToBigInt(s[i])
  270. if err != nil {
  271. return o, err
  272. }
  273. o = append(o, parsed)
  274. }
  275. return o, nil
  276. }
  277. func CircuitToString(c circuitcompiler.Circuit) CircuitString {
  278. var cs CircuitString
  279. cs.NVars = c.NVars
  280. cs.NPublic = c.NPublic
  281. cs.NSignals = c.NSignals
  282. cs.PrivateInputs = c.PrivateInputs
  283. cs.PublicInputs = c.PublicInputs
  284. cs.Signals = c.Signals
  285. cs.Witness = ArrayBigIntToString(c.Witness)
  286. cs.Constraints = c.Constraints
  287. cs.R1CS.A = ArrayArrayBigIntToString(c.R1CS.A)
  288. cs.R1CS.B = ArrayArrayBigIntToString(c.R1CS.B)
  289. cs.R1CS.C = ArrayArrayBigIntToString(c.R1CS.C)
  290. return cs
  291. }
  292. func CircuitFromString(cs CircuitString) (circuitcompiler.Circuit, error) {
  293. var c circuitcompiler.Circuit
  294. var err error
  295. c.NVars = cs.NVars
  296. c.NPublic = cs.NPublic
  297. c.NSignals = cs.NSignals
  298. c.PrivateInputs = cs.PrivateInputs
  299. c.PublicInputs = cs.PublicInputs
  300. c.Signals = cs.Signals
  301. c.Witness, err = ArrayStringToBigInt(cs.Witness)
  302. if err != nil {
  303. return c, err
  304. }
  305. c.Constraints = cs.Constraints
  306. c.R1CS.A, err = ArrayArrayStringToBigInt(cs.R1CS.A)
  307. if err != nil {
  308. return c, err
  309. }
  310. c.R1CS.B, err = ArrayArrayStringToBigInt(cs.R1CS.B)
  311. if err != nil {
  312. return c, err
  313. }
  314. c.R1CS.C, err = ArrayArrayStringToBigInt(cs.R1CS.C)
  315. if err != nil {
  316. return c, err
  317. }
  318. return c, nil
  319. }
  320. // Proof
  321. type ProofString struct {
  322. PiA [3]string
  323. PiAp [3]string
  324. PiB [3][2]string
  325. PiBp [3]string
  326. PiC [3]string
  327. PiCp [3]string
  328. PiH [3]string
  329. PiKp [3]string
  330. }
  331. func ProofToString(p snark.Proof) ProofString {
  332. var s ProofString
  333. s.PiA = BigInt3ToString(p.PiA)
  334. s.PiAp = BigInt3ToString(p.PiAp)
  335. s.PiB = BigInt32ToString(p.PiB)
  336. s.PiBp = BigInt3ToString(p.PiBp)
  337. s.PiC = BigInt3ToString(p.PiC)
  338. s.PiCp = BigInt3ToString(p.PiCp)
  339. s.PiH = BigInt3ToString(p.PiH)
  340. s.PiKp = BigInt3ToString(p.PiKp)
  341. return s
  342. }
  343. func ProofFromString(s ProofString) (snark.Proof, error) {
  344. var p snark.Proof
  345. var err error
  346. p.PiA, err = String3ToBigInt(s.PiA)
  347. if err != nil {
  348. return p, err
  349. }
  350. p.PiAp, err = String3ToBigInt(s.PiAp)
  351. if err != nil {
  352. return p, err
  353. }
  354. p.PiB, err = String32ToBigInt(s.PiB)
  355. if err != nil {
  356. return p, err
  357. }
  358. p.PiBp, err = String3ToBigInt(s.PiBp)
  359. if err != nil {
  360. return p, err
  361. }
  362. p.PiC, err = String3ToBigInt(s.PiC)
  363. if err != nil {
  364. return p, err
  365. }
  366. p.PiCp, err = String3ToBigInt(s.PiCp)
  367. if err != nil {
  368. return p, err
  369. }
  370. p.PiH, err = String3ToBigInt(s.PiH)
  371. if err != nil {
  372. return p, err
  373. }
  374. p.PiKp, err = String3ToBigInt(s.PiKp)
  375. if err != nil {
  376. return p, err
  377. }
  378. return p, nil
  379. }
  380. // groth
  381. type GrothPkString struct { // Proving Key
  382. BACDelta [][3]string
  383. Z []string
  384. G1 struct {
  385. Alpha [3]string
  386. Beta [3]string
  387. Delta [3]string
  388. At [][3]string
  389. BACGamma [][3]string
  390. }
  391. G2 struct {
  392. Beta [3][2]string
  393. Gamma [3][2]string
  394. Delta [3][2]string
  395. BACGamma [][3][2]string
  396. }
  397. PowersTauDelta [][3]string
  398. }
  399. type GrothVkString struct {
  400. IC [][3]string
  401. G1 struct {
  402. Alpha [3]string
  403. }
  404. G2 struct {
  405. Beta [3][2]string
  406. Gamma [3][2]string
  407. Delta [3][2]string
  408. }
  409. }
  410. type GrothSetupString struct {
  411. Pk GrothPkString
  412. Vk GrothVkString
  413. }
  414. func GrothSetupToString(setup groth16.Setup) GrothSetupString {
  415. var s GrothSetupString
  416. s.Pk.BACDelta = Array3BigIntToString(setup.Pk.BACDelta)
  417. s.Pk.Z = ArrayBigIntToString(setup.Pk.Z)
  418. s.Pk.G1.Alpha = BigInt3ToString(setup.Pk.G1.Alpha)
  419. s.Pk.G1.Beta = BigInt3ToString(setup.Pk.G1.Beta)
  420. s.Pk.G1.Delta = BigInt3ToString(setup.Pk.G1.Delta)
  421. s.Pk.G1.At = Array3BigIntToString(setup.Pk.G1.At)
  422. s.Pk.G1.BACGamma = Array3BigIntToString(setup.Pk.G1.BACGamma)
  423. s.Pk.G2.Beta = BigInt32ToString(setup.Pk.G2.Beta)
  424. s.Pk.G2.Gamma = BigInt32ToString(setup.Pk.G2.Gamma)
  425. s.Pk.G2.Delta = BigInt32ToString(setup.Pk.G2.Delta)
  426. s.Pk.G2.BACGamma = Array32BigIntToString(setup.Pk.G2.BACGamma)
  427. s.Pk.PowersTauDelta = Array3BigIntToString(setup.Pk.PowersTauDelta)
  428. s.Vk.IC = Array3BigIntToString(setup.Vk.IC)
  429. s.Vk.G1.Alpha = BigInt3ToString(setup.Vk.G1.Alpha)
  430. s.Vk.G2.Beta = BigInt32ToString(setup.Vk.G2.Beta)
  431. s.Vk.G2.Gamma = BigInt32ToString(setup.Vk.G2.Gamma)
  432. s.Vk.G2.Delta = BigInt32ToString(setup.Vk.G2.Delta)
  433. return s
  434. }
  435. func GrothVkFromString(s GrothVkString) (groth16.Vk, error) {
  436. var vk groth16.Vk
  437. var err error
  438. vk.IC, err = Array3StringToBigInt(s.IC)
  439. if err != nil {
  440. return vk, err
  441. }
  442. vk.G1.Alpha, err = String3ToBigInt(s.G1.Alpha)
  443. if err != nil {
  444. return vk, err
  445. }
  446. vk.G2.Beta, err = String32ToBigInt(s.G2.Beta)
  447. if err != nil {
  448. return vk, err
  449. }
  450. vk.G2.Gamma, err = String32ToBigInt(s.G2.Gamma)
  451. if err != nil {
  452. return vk, err
  453. }
  454. vk.G2.Delta, err = String32ToBigInt(s.G2.Delta)
  455. if err != nil {
  456. return vk, err
  457. }
  458. return vk, nil
  459. }
  460. func GrothSetupFromString(s GrothSetupString) (groth16.Setup, error) {
  461. var o groth16.Setup
  462. var err error
  463. o.Pk.BACDelta, err = Array3StringToBigInt(s.Pk.BACDelta)
  464. if err != nil {
  465. return o, err
  466. }
  467. o.Pk.Z, err = ArrayStringToBigInt(s.Pk.Z)
  468. if err != nil {
  469. return o, err
  470. }
  471. o.Pk.G1.Alpha, err = String3ToBigInt(s.Pk.G1.Alpha)
  472. if err != nil {
  473. return o, err
  474. }
  475. o.Pk.G1.Beta, err = String3ToBigInt(s.Pk.G1.Beta)
  476. if err != nil {
  477. return o, err
  478. }
  479. o.Pk.G1.Delta, err = String3ToBigInt(s.Pk.G1.Delta)
  480. if err != nil {
  481. return o, err
  482. }
  483. o.Pk.G1.At, err = Array3StringToBigInt(s.Pk.G1.At)
  484. if err != nil {
  485. return o, err
  486. }
  487. o.Pk.G1.BACGamma, err = Array3StringToBigInt(s.Pk.G1.BACGamma)
  488. if err != nil {
  489. return o, err
  490. }
  491. o.Pk.G2.Beta, err = String32ToBigInt(s.Pk.G2.Beta)
  492. if err != nil {
  493. return o, err
  494. }
  495. o.Pk.G2.Gamma, err = String32ToBigInt(s.Pk.G2.Gamma)
  496. if err != nil {
  497. return o, err
  498. }
  499. o.Pk.G2.Delta, err = String32ToBigInt(s.Pk.G2.Delta)
  500. if err != nil {
  501. return o, err
  502. }
  503. o.Pk.G2.BACGamma, err = Array32StringToBigInt(s.Pk.G2.BACGamma)
  504. if err != nil {
  505. return o, err
  506. }
  507. o.Pk.PowersTauDelta, err = Array3StringToBigInt(s.Pk.PowersTauDelta)
  508. if err != nil {
  509. return o, err
  510. }
  511. o.Vk.IC, err = Array3StringToBigInt(s.Vk.IC)
  512. if err != nil {
  513. return o, err
  514. }
  515. o.Vk.G1.Alpha, err = String3ToBigInt(s.Vk.G1.Alpha)
  516. if err != nil {
  517. return o, err
  518. }
  519. o.Vk.G2.Beta, err = String32ToBigInt(s.Vk.G2.Beta)
  520. if err != nil {
  521. return o, err
  522. }
  523. o.Vk.G2.Gamma, err = String32ToBigInt(s.Vk.G2.Gamma)
  524. if err != nil {
  525. return o, err
  526. }
  527. o.Vk.G2.Delta, err = String32ToBigInt(s.Vk.G2.Delta)
  528. if err != nil {
  529. return o, err
  530. }
  531. return o, nil
  532. }
  533. type GrothProofString struct {
  534. PiA [3]string
  535. PiB [3][2]string
  536. PiC [3]string
  537. }
  538. func GrothProofToString(p groth16.Proof) GrothProofString {
  539. var s GrothProofString
  540. s.PiA = BigInt3ToString(p.PiA)
  541. s.PiB = BigInt32ToString(p.PiB)
  542. s.PiC = BigInt3ToString(p.PiC)
  543. return s
  544. }
  545. func GrothProofFromString(s GrothProofString) (groth16.Proof, error) {
  546. var p groth16.Proof
  547. var err error
  548. p.PiA, err = String3ToBigInt(s.PiA)
  549. if err != nil {
  550. return p, err
  551. }
  552. p.PiB, err = String32ToBigInt(s.PiB)
  553. if err != nil {
  554. return p, err
  555. }
  556. p.PiC, err = String3ToBigInt(s.PiC)
  557. if err != nil {
  558. return p, err
  559. }
  560. return p, nil
  561. }