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.

562 lines
12 KiB

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