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.

564 lines
12 KiB

  1. package utils
  2. import (
  3. "errors"
  4. "math/big"
  5. snark "github.com/arnaucube/go-snark"
  6. "github.com/arnaucube/go-snark/circuitcompiler"
  7. "github.com/arnaucube/go-snark/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. G1T [][3]string
  128. G2T [][3][2]string
  129. Pk struct {
  130. A [][3]string
  131. B [][3][2]string
  132. C [][3]string
  133. Kp [][3]string
  134. Ap [][3]string
  135. Bp [][3]string
  136. Cp [][3]string
  137. Z []string
  138. }
  139. Vk struct {
  140. Vka [3][2]string
  141. Vkb [3]string
  142. Vkc [3][2]string
  143. IC [][3]string
  144. G1Kbg [3]string
  145. G2Kbg [3][2]string
  146. G2Kg [3][2]string
  147. Vkz [3][2]string
  148. }
  149. }
  150. func SetupToString(setup snark.Setup) SetupString {
  151. var s SetupString
  152. s.G1T = Array3BigIntToString(setup.G1T)
  153. s.G2T = Array32BigIntToString(setup.G2T)
  154. s.Pk.A = Array3BigIntToString(setup.Pk.A)
  155. s.Pk.B = Array32BigIntToString(setup.Pk.B)
  156. s.Pk.C = Array3BigIntToString(setup.Pk.C)
  157. s.Pk.Kp = Array3BigIntToString(setup.Pk.Kp)
  158. s.Pk.Ap = Array3BigIntToString(setup.Pk.Ap)
  159. s.Pk.Bp = Array3BigIntToString(setup.Pk.Bp)
  160. s.Pk.Cp = Array3BigIntToString(setup.Pk.Cp)
  161. s.Pk.Z = ArrayBigIntToString(setup.Pk.Z)
  162. s.Vk.Vka = BigInt32ToString(setup.Vk.Vka)
  163. s.Vk.Vkb = BigInt3ToString(setup.Vk.Vkb)
  164. s.Vk.Vkc = BigInt32ToString(setup.Vk.Vkc)
  165. s.Vk.IC = Array3BigIntToString(setup.Vk.IC)
  166. s.Vk.G1Kbg = BigInt3ToString(setup.Vk.G1Kbg)
  167. s.Vk.G2Kbg = BigInt32ToString(setup.Vk.G2Kbg)
  168. s.Vk.G2Kg = BigInt32ToString(setup.Vk.G2Kg)
  169. s.Vk.Vkz = BigInt32ToString(setup.Vk.Vkz)
  170. return s
  171. }
  172. func SetupFromString(s SetupString) (snark.Setup, error) {
  173. var o snark.Setup
  174. var err error
  175. o.G1T, err = Array3StringToBigInt(s.G1T)
  176. if err != nil {
  177. return o, err
  178. }
  179. o.G2T, err = Array32StringToBigInt(s.G2T)
  180. if err != nil {
  181. return o, err
  182. }
  183. o.Pk.A, err = Array3StringToBigInt(s.Pk.A)
  184. if err != nil {
  185. return o, err
  186. }
  187. o.Pk.B, err = Array32StringToBigInt(s.Pk.B)
  188. if err != nil {
  189. return o, err
  190. }
  191. o.Pk.C, err = Array3StringToBigInt(s.Pk.C)
  192. if err != nil {
  193. return o, err
  194. }
  195. o.Pk.Kp, err = Array3StringToBigInt(s.Pk.Kp)
  196. if err != nil {
  197. return o, err
  198. }
  199. o.Pk.Ap, err = Array3StringToBigInt(s.Pk.Ap)
  200. if err != nil {
  201. return o, err
  202. }
  203. o.Pk.Bp, err = Array3StringToBigInt(s.Pk.Bp)
  204. if err != nil {
  205. return o, err
  206. }
  207. o.Pk.Cp, err = Array3StringToBigInt(s.Pk.Cp)
  208. if err != nil {
  209. return o, err
  210. }
  211. o.Pk.Z, err = ArrayStringToBigInt(s.Pk.Z)
  212. if err != nil {
  213. return o, err
  214. }
  215. o.Vk.Vka, err = String32ToBigInt(s.Vk.Vka)
  216. if err != nil {
  217. return o, err
  218. }
  219. o.Vk.Vkb, err = String3ToBigInt(s.Vk.Vkb)
  220. if err != nil {
  221. return o, err
  222. }
  223. o.Vk.Vkc, err = String32ToBigInt(s.Vk.Vkc)
  224. if err != nil {
  225. return o, err
  226. }
  227. o.Vk.IC, err = Array3StringToBigInt(s.Vk.IC)
  228. if err != nil {
  229. return o, err
  230. }
  231. o.Vk.G1Kbg, err = String3ToBigInt(s.Vk.G1Kbg)
  232. if err != nil {
  233. return o, err
  234. }
  235. o.Vk.G2Kbg, err = String32ToBigInt(s.Vk.G2Kbg)
  236. if err != nil {
  237. return o, err
  238. }
  239. o.Vk.G2Kg, err = String32ToBigInt(s.Vk.G2Kg)
  240. if err != nil {
  241. return o, err
  242. }
  243. o.Vk.Vkz, err = String32ToBigInt(s.Vk.Vkz)
  244. if err != nil {
  245. return o, err
  246. }
  247. return o, nil
  248. }
  249. // circuit
  250. type CircuitString struct {
  251. NVars int
  252. NPublic int
  253. NSignals int
  254. PrivateInputs []string
  255. PublicInputs []string
  256. Signals []string
  257. Witness []string
  258. Constraints []circuitcompiler.Constraint
  259. R1CS struct {
  260. A [][]string
  261. B [][]string
  262. C [][]string
  263. }
  264. }
  265. func ArrayArrayBigIntToString(b [][]*big.Int) [][]string {
  266. var o [][]string
  267. for i := 0; i < len(b); i++ {
  268. o = append(o, ArrayBigIntToString(b[i]))
  269. }
  270. return o
  271. }
  272. func ArrayArrayStringToBigInt(s [][]string) ([][]*big.Int, error) {
  273. var o [][]*big.Int
  274. for i := 0; i < len(s); i++ {
  275. parsed, err := ArrayStringToBigInt(s[i])
  276. if err != nil {
  277. return o, err
  278. }
  279. o = append(o, parsed)
  280. }
  281. return o, nil
  282. }
  283. func CircuitToString(c circuitcompiler.Circuit) CircuitString {
  284. var cs CircuitString
  285. cs.NVars = c.NVars
  286. cs.NPublic = c.NPublic
  287. cs.NSignals = c.NSignals
  288. cs.PrivateInputs = c.PrivateInputs
  289. cs.PublicInputs = c.PublicInputs
  290. cs.Signals = c.Signals
  291. cs.Witness = ArrayBigIntToString(c.Witness)
  292. cs.Constraints = c.Constraints
  293. cs.R1CS.A = ArrayArrayBigIntToString(c.R1CS.A)
  294. cs.R1CS.B = ArrayArrayBigIntToString(c.R1CS.B)
  295. cs.R1CS.C = ArrayArrayBigIntToString(c.R1CS.C)
  296. return cs
  297. }
  298. func CircuitFromString(cs CircuitString) (circuitcompiler.Circuit, error) {
  299. var c circuitcompiler.Circuit
  300. var err error
  301. c.NVars = cs.NVars
  302. c.NPublic = cs.NPublic
  303. c.NSignals = cs.NSignals
  304. c.PrivateInputs = cs.PrivateInputs
  305. c.PublicInputs = cs.PublicInputs
  306. c.Signals = cs.Signals
  307. c.Witness, err = ArrayStringToBigInt(cs.Witness)
  308. if err != nil {
  309. return c, err
  310. }
  311. c.Constraints = cs.Constraints
  312. c.R1CS.A, err = ArrayArrayStringToBigInt(cs.R1CS.A)
  313. if err != nil {
  314. return c, err
  315. }
  316. c.R1CS.B, err = ArrayArrayStringToBigInt(cs.R1CS.B)
  317. if err != nil {
  318. return c, err
  319. }
  320. c.R1CS.C, err = ArrayArrayStringToBigInt(cs.R1CS.C)
  321. if err != nil {
  322. return c, err
  323. }
  324. return c, nil
  325. }
  326. // Proof
  327. type ProofString struct {
  328. PiA [3]string
  329. PiAp [3]string
  330. PiB [3][2]string
  331. PiBp [3]string
  332. PiC [3]string
  333. PiCp [3]string
  334. PiH [3]string
  335. PiKp [3]string
  336. }
  337. func ProofToString(p snark.Proof) ProofString {
  338. var s ProofString
  339. s.PiA = BigInt3ToString(p.PiA)
  340. s.PiAp = BigInt3ToString(p.PiAp)
  341. s.PiB = BigInt32ToString(p.PiB)
  342. s.PiBp = BigInt3ToString(p.PiBp)
  343. s.PiC = BigInt3ToString(p.PiC)
  344. s.PiCp = BigInt3ToString(p.PiCp)
  345. s.PiH = BigInt3ToString(p.PiH)
  346. s.PiKp = BigInt3ToString(p.PiKp)
  347. return s
  348. }
  349. func ProofFromString(s ProofString) (snark.Proof, error) {
  350. var p snark.Proof
  351. var err error
  352. p.PiA, err = String3ToBigInt(s.PiA)
  353. if err != nil {
  354. return p, err
  355. }
  356. p.PiAp, err = String3ToBigInt(s.PiAp)
  357. if err != nil {
  358. return p, err
  359. }
  360. p.PiB, err = String32ToBigInt(s.PiB)
  361. if err != nil {
  362. return p, err
  363. }
  364. p.PiBp, err = String3ToBigInt(s.PiBp)
  365. if err != nil {
  366. return p, err
  367. }
  368. p.PiC, err = String3ToBigInt(s.PiC)
  369. if err != nil {
  370. return p, err
  371. }
  372. p.PiCp, err = String3ToBigInt(s.PiCp)
  373. if err != nil {
  374. return p, err
  375. }
  376. p.PiH, err = String3ToBigInt(s.PiH)
  377. if err != nil {
  378. return p, err
  379. }
  380. p.PiKp, err = String3ToBigInt(s.PiKp)
  381. if err != nil {
  382. return p, err
  383. }
  384. return p, nil
  385. }
  386. // groth
  387. type GrothSetupString struct {
  388. Pk struct { // Proving Key
  389. BACDelta [][3]string
  390. Z []string
  391. G1 struct {
  392. Alpha [3]string
  393. Beta [3]string
  394. Delta [3]string
  395. At [][3]string
  396. BACGamma [][3]string
  397. }
  398. G2 struct {
  399. Beta [3][2]string
  400. Gamma [3][2]string
  401. Delta [3][2]string
  402. BACGamma [][3][2]string
  403. }
  404. PowersTauDelta [][3]string
  405. }
  406. Vk struct {
  407. IC [][3]string
  408. G1 struct {
  409. Alpha [3]string
  410. }
  411. G2 struct {
  412. Beta [3][2]string
  413. Gamma [3][2]string
  414. Delta [3][2]string
  415. }
  416. }
  417. }
  418. func GrothSetupToString(setup groth16.Setup) GrothSetupString {
  419. var s GrothSetupString
  420. s.Pk.BACDelta = Array3BigIntToString(setup.Pk.BACDelta)
  421. s.Pk.Z = ArrayBigIntToString(setup.Pk.Z)
  422. s.Pk.G1.Alpha = BigInt3ToString(setup.Pk.G1.Alpha)
  423. s.Pk.G1.Beta = BigInt3ToString(setup.Pk.G1.Beta)
  424. s.Pk.G1.Delta = BigInt3ToString(setup.Pk.G1.Delta)
  425. s.Pk.G1.At = Array3BigIntToString(setup.Pk.G1.At)
  426. s.Pk.G1.BACGamma = Array3BigIntToString(setup.Pk.G1.BACGamma)
  427. s.Pk.G2.Beta = BigInt32ToString(setup.Pk.G2.Beta)
  428. s.Pk.G2.Gamma = BigInt32ToString(setup.Pk.G2.Gamma)
  429. s.Pk.G2.Delta = BigInt32ToString(setup.Pk.G2.Delta)
  430. s.Pk.G2.BACGamma = Array32BigIntToString(setup.Pk.G2.BACGamma)
  431. s.Pk.PowersTauDelta = Array3BigIntToString(setup.Pk.PowersTauDelta)
  432. s.Vk.IC = Array3BigIntToString(setup.Vk.IC)
  433. s.Vk.G1.Alpha = BigInt3ToString(setup.Vk.G1.Alpha)
  434. s.Vk.G2.Beta = BigInt32ToString(setup.Vk.G2.Beta)
  435. s.Vk.G2.Gamma = BigInt32ToString(setup.Vk.G2.Gamma)
  436. s.Vk.G2.Delta = BigInt32ToString(setup.Vk.G2.Delta)
  437. return s
  438. }
  439. func GrothSetupFromString(s GrothSetupString) (groth16.Setup, error) {
  440. var o groth16.Setup
  441. var err error
  442. o.Pk.BACDelta, err = Array3StringToBigInt(s.Pk.BACDelta)
  443. if err != nil {
  444. return o, err
  445. }
  446. o.Pk.Z, err = ArrayStringToBigInt(s.Pk.Z)
  447. if err != nil {
  448. return o, err
  449. }
  450. o.Pk.G1.Alpha, err = String3ToBigInt(s.Pk.G1.Alpha)
  451. if err != nil {
  452. return o, err
  453. }
  454. o.Pk.G1.Beta, err = String3ToBigInt(s.Pk.G1.Beta)
  455. if err != nil {
  456. return o, err
  457. }
  458. o.Pk.G1.Delta, err = String3ToBigInt(s.Pk.G1.Delta)
  459. if err != nil {
  460. return o, err
  461. }
  462. o.Pk.G1.At, err = Array3StringToBigInt(s.Pk.G1.At)
  463. if err != nil {
  464. return o, err
  465. }
  466. o.Pk.G1.BACGamma, err = Array3StringToBigInt(s.Pk.G1.BACGamma)
  467. if err != nil {
  468. return o, err
  469. }
  470. o.Pk.G2.Beta, err = String32ToBigInt(s.Pk.G2.Beta)
  471. if err != nil {
  472. return o, err
  473. }
  474. o.Pk.G2.Gamma, err = String32ToBigInt(s.Pk.G2.Gamma)
  475. if err != nil {
  476. return o, err
  477. }
  478. o.Pk.G2.Delta, err = String32ToBigInt(s.Pk.G2.Delta)
  479. if err != nil {
  480. return o, err
  481. }
  482. o.Pk.G2.BACGamma, err = Array32StringToBigInt(s.Pk.G2.BACGamma)
  483. if err != nil {
  484. return o, err
  485. }
  486. o.Pk.PowersTauDelta, err = Array3StringToBigInt(s.Pk.PowersTauDelta)
  487. if err != nil {
  488. return o, err
  489. }
  490. o.Vk.IC, err = Array3StringToBigInt(s.Vk.IC)
  491. if err != nil {
  492. return o, err
  493. }
  494. o.Vk.G1.Alpha, err = String3ToBigInt(s.Vk.G1.Alpha)
  495. if err != nil {
  496. return o, err
  497. }
  498. o.Vk.G2.Beta, err = String32ToBigInt(s.Vk.G2.Beta)
  499. if err != nil {
  500. return o, err
  501. }
  502. o.Vk.G2.Gamma, err = String32ToBigInt(s.Vk.G2.Gamma)
  503. if err != nil {
  504. return o, err
  505. }
  506. o.Vk.G2.Delta, err = String32ToBigInt(s.Vk.G2.Delta)
  507. if err != nil {
  508. return o, err
  509. }
  510. return o, nil
  511. }
  512. type GrothProofString struct {
  513. PiA [3]string
  514. PiB [3][2]string
  515. PiC [3]string
  516. }
  517. func GrothProofToString(p groth16.Proof) GrothProofString {
  518. var s GrothProofString
  519. s.PiA = BigInt3ToString(p.PiA)
  520. s.PiB = BigInt32ToString(p.PiB)
  521. s.PiC = BigInt3ToString(p.PiC)
  522. return s
  523. }
  524. func GrothProofFromString(s GrothProofString) (groth16.Proof, error) {
  525. var p groth16.Proof
  526. var err error
  527. p.PiA, err = String3ToBigInt(s.PiA)
  528. if err != nil {
  529. return p, err
  530. }
  531. p.PiB, err = String32ToBigInt(s.PiB)
  532. if err != nil {
  533. return p, err
  534. }
  535. p.PiC, err = String3ToBigInt(s.PiC)
  536. if err != nil {
  537. return p, err
  538. }
  539. return p, nil
  540. }