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.

558 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. 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 GrothSetupString struct {
  382. Pk struct { // Proving Key
  383. BACDelta [][3]string
  384. Z []string
  385. G1 struct {
  386. Alpha [3]string
  387. Beta [3]string
  388. Delta [3]string
  389. At [][3]string
  390. BACGamma [][3]string
  391. }
  392. G2 struct {
  393. Beta [3][2]string
  394. Gamma [3][2]string
  395. Delta [3][2]string
  396. BACGamma [][3][2]string
  397. }
  398. PowersTauDelta [][3]string
  399. }
  400. Vk struct {
  401. IC [][3]string
  402. G1 struct {
  403. Alpha [3]string
  404. }
  405. G2 struct {
  406. Beta [3][2]string
  407. Gamma [3][2]string
  408. Delta [3][2]string
  409. }
  410. }
  411. }
  412. func GrothSetupToString(setup groth16.Setup) GrothSetupString {
  413. var s GrothSetupString
  414. s.Pk.BACDelta = Array3BigIntToString(setup.Pk.BACDelta)
  415. s.Pk.Z = ArrayBigIntToString(setup.Pk.Z)
  416. s.Pk.G1.Alpha = BigInt3ToString(setup.Pk.G1.Alpha)
  417. s.Pk.G1.Beta = BigInt3ToString(setup.Pk.G1.Beta)
  418. s.Pk.G1.Delta = BigInt3ToString(setup.Pk.G1.Delta)
  419. s.Pk.G1.At = Array3BigIntToString(setup.Pk.G1.At)
  420. s.Pk.G1.BACGamma = Array3BigIntToString(setup.Pk.G1.BACGamma)
  421. s.Pk.G2.Beta = BigInt32ToString(setup.Pk.G2.Beta)
  422. s.Pk.G2.Gamma = BigInt32ToString(setup.Pk.G2.Gamma)
  423. s.Pk.G2.Delta = BigInt32ToString(setup.Pk.G2.Delta)
  424. s.Pk.G2.BACGamma = Array32BigIntToString(setup.Pk.G2.BACGamma)
  425. s.Pk.PowersTauDelta = Array3BigIntToString(setup.Pk.PowersTauDelta)
  426. s.Vk.IC = Array3BigIntToString(setup.Vk.IC)
  427. s.Vk.G1.Alpha = BigInt3ToString(setup.Vk.G1.Alpha)
  428. s.Vk.G2.Beta = BigInt32ToString(setup.Vk.G2.Beta)
  429. s.Vk.G2.Gamma = BigInt32ToString(setup.Vk.G2.Gamma)
  430. s.Vk.G2.Delta = BigInt32ToString(setup.Vk.G2.Delta)
  431. return s
  432. }
  433. func GrothSetupFromString(s GrothSetupString) (groth16.Setup, error) {
  434. var o groth16.Setup
  435. var err error
  436. o.Pk.BACDelta, err = Array3StringToBigInt(s.Pk.BACDelta)
  437. if err != nil {
  438. return o, err
  439. }
  440. o.Pk.Z, err = ArrayStringToBigInt(s.Pk.Z)
  441. if err != nil {
  442. return o, err
  443. }
  444. o.Pk.G1.Alpha, err = String3ToBigInt(s.Pk.G1.Alpha)
  445. if err != nil {
  446. return o, err
  447. }
  448. o.Pk.G1.Beta, err = String3ToBigInt(s.Pk.G1.Beta)
  449. if err != nil {
  450. return o, err
  451. }
  452. o.Pk.G1.Delta, err = String3ToBigInt(s.Pk.G1.Delta)
  453. if err != nil {
  454. return o, err
  455. }
  456. o.Pk.G1.At, err = Array3StringToBigInt(s.Pk.G1.At)
  457. if err != nil {
  458. return o, err
  459. }
  460. o.Pk.G1.BACGamma, err = Array3StringToBigInt(s.Pk.G1.BACGamma)
  461. if err != nil {
  462. return o, err
  463. }
  464. o.Pk.G2.Beta, err = String32ToBigInt(s.Pk.G2.Beta)
  465. if err != nil {
  466. return o, err
  467. }
  468. o.Pk.G2.Gamma, err = String32ToBigInt(s.Pk.G2.Gamma)
  469. if err != nil {
  470. return o, err
  471. }
  472. o.Pk.G2.Delta, err = String32ToBigInt(s.Pk.G2.Delta)
  473. if err != nil {
  474. return o, err
  475. }
  476. o.Pk.G2.BACGamma, err = Array32StringToBigInt(s.Pk.G2.BACGamma)
  477. if err != nil {
  478. return o, err
  479. }
  480. o.Pk.PowersTauDelta, err = Array3StringToBigInt(s.Pk.PowersTauDelta)
  481. if err != nil {
  482. return o, err
  483. }
  484. o.Vk.IC, err = Array3StringToBigInt(s.Vk.IC)
  485. if err != nil {
  486. return o, err
  487. }
  488. o.Vk.G1.Alpha, err = String3ToBigInt(s.Vk.G1.Alpha)
  489. if err != nil {
  490. return o, err
  491. }
  492. o.Vk.G2.Beta, err = String32ToBigInt(s.Vk.G2.Beta)
  493. if err != nil {
  494. return o, err
  495. }
  496. o.Vk.G2.Gamma, err = String32ToBigInt(s.Vk.G2.Gamma)
  497. if err != nil {
  498. return o, err
  499. }
  500. o.Vk.G2.Delta, err = String32ToBigInt(s.Vk.G2.Delta)
  501. if err != nil {
  502. return o, err
  503. }
  504. return o, nil
  505. }
  506. type GrothProofString struct {
  507. PiA [3]string
  508. PiB [3][2]string
  509. PiC [3]string
  510. }
  511. func GrothProofToString(p groth16.Proof) GrothProofString {
  512. var s GrothProofString
  513. s.PiA = BigInt3ToString(p.PiA)
  514. s.PiB = BigInt32ToString(p.PiB)
  515. s.PiC = BigInt3ToString(p.PiC)
  516. return s
  517. }
  518. func GrothProofFromString(s GrothProofString) (groth16.Proof, error) {
  519. var p groth16.Proof
  520. var err error
  521. p.PiA, err = String3ToBigInt(s.PiA)
  522. if err != nil {
  523. return p, err
  524. }
  525. p.PiB, err = String32ToBigInt(s.PiB)
  526. if err != nil {
  527. return p, err
  528. }
  529. p.PiC, err = String3ToBigInt(s.PiC)
  530. if err != nil {
  531. return p, err
  532. }
  533. return p, nil
  534. }