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.

361 lines
7.5 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. )
  8. // []*big.Int
  9. func ArrayBigIntToString(b []*big.Int) []string {
  10. var o []string
  11. for i := 0; i < len(b); i++ {
  12. o = append(o, b[i].String())
  13. }
  14. return o
  15. }
  16. func ArrayStringToBigInt(s []string) ([]*big.Int, error) {
  17. var px []*big.Int
  18. for i := 0; i < len(s); i++ {
  19. param, ok := new(big.Int).SetString(s[i], 10)
  20. if !ok {
  21. return nil, errors.New("error parsing px from pxString")
  22. }
  23. px = append(px, param)
  24. }
  25. return px, nil
  26. }
  27. // [3]*big.Int
  28. func String3ToBigInt(s [3]string) ([3]*big.Int, error) {
  29. var o [3]*big.Int
  30. for i := 0; i < len(s); i++ {
  31. param, ok := new(big.Int).SetString(s[i], 10)
  32. if !ok {
  33. return o, errors.New("error parsing [3]*big.Int from [3]string")
  34. }
  35. o[i] = param
  36. }
  37. return o, nil
  38. }
  39. func BigInt3ToString(b [3]*big.Int) [3]string {
  40. var o [3]string
  41. o[0] = b[0].String()
  42. o[1] = b[1].String()
  43. o[2] = b[2].String()
  44. return o
  45. }
  46. // [][3]*big.Int
  47. func Array3StringToBigInt(s [][3]string) ([][3]*big.Int, error) {
  48. var o [][3]*big.Int
  49. for i := 0; i < len(s); i++ {
  50. parsed, err := String3ToBigInt(s[i])
  51. if err != nil {
  52. return o, err
  53. }
  54. o = append(o, parsed)
  55. }
  56. return o, nil
  57. }
  58. func Array3BigIntToString(b [][3]*big.Int) [][3]string {
  59. var o [][3]string
  60. for i := 0; i < len(b); i++ {
  61. o = append(o, BigInt3ToString(b[i]))
  62. }
  63. return o
  64. }
  65. func String2ToBigInt(s [2]string) ([2]*big.Int, error) {
  66. var o [2]*big.Int
  67. for i := 0; i < len(s); i++ {
  68. param, ok := new(big.Int).SetString(s[i], 10)
  69. if !ok {
  70. return o, errors.New("error parsing [2]*big.Int from [2]string")
  71. }
  72. o[i] = param
  73. }
  74. return o, nil
  75. }
  76. // [3][2]*big.Int
  77. func String32ToBigInt(s [3][2]string) ([3][2]*big.Int, error) {
  78. var o [3][2]*big.Int
  79. var err error
  80. o[0], err = String2ToBigInt(s[0])
  81. if err != nil {
  82. return o, err
  83. }
  84. o[1], err = String2ToBigInt(s[1])
  85. if err != nil {
  86. return o, err
  87. }
  88. o[2], err = String2ToBigInt(s[2])
  89. if err != nil {
  90. return o, err
  91. }
  92. return o, nil
  93. }
  94. func BigInt32ToString(b [3][2]*big.Int) [3][2]string {
  95. var o [3][2]string
  96. o[0][0] = b[0][0].String()
  97. o[0][1] = b[0][1].String()
  98. o[1][0] = b[1][0].String()
  99. o[1][1] = b[1][1].String()
  100. o[2][0] = b[2][0].String()
  101. o[2][1] = b[2][1].String()
  102. return o
  103. }
  104. // [][3][2]*big.Int
  105. func Array32StringToBigInt(s [][3][2]string) ([][3][2]*big.Int, error) {
  106. var o [][3][2]*big.Int
  107. for i := 0; i < len(s); i++ {
  108. parsed, err := String32ToBigInt(s[i])
  109. if err != nil {
  110. return o, err
  111. }
  112. o = append(o, parsed)
  113. }
  114. return o, nil
  115. }
  116. func Array32BigIntToString(b [][3][2]*big.Int) [][3][2]string {
  117. var o [][3][2]string
  118. for i := 0; i < len(b); i++ {
  119. o = append(o, BigInt32ToString(b[i]))
  120. }
  121. return o
  122. }
  123. // Setup
  124. type SetupString struct {
  125. // public
  126. G1T [][3]string
  127. G2T [][3][2]string
  128. Pk struct {
  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.G1T = Array3BigIntToString(setup.G1T)
  152. s.G2T = Array32BigIntToString(setup.G2T)
  153. s.Pk.A = Array3BigIntToString(setup.Pk.A)
  154. s.Pk.B = Array32BigIntToString(setup.Pk.B)
  155. s.Pk.C = Array3BigIntToString(setup.Pk.C)
  156. s.Pk.Kp = Array3BigIntToString(setup.Pk.Kp)
  157. s.Pk.Ap = Array3BigIntToString(setup.Pk.Ap)
  158. s.Pk.Bp = Array3BigIntToString(setup.Pk.Bp)
  159. s.Pk.Cp = Array3BigIntToString(setup.Pk.Cp)
  160. s.Pk.Z = ArrayBigIntToString(setup.Pk.Z)
  161. return s
  162. }
  163. func SetupFromString(s SetupString) (snark.Setup, error) {
  164. var o snark.Setup
  165. var err error
  166. o.G1T, err = Array3StringToBigInt(s.G1T)
  167. if err != nil {
  168. return o, err
  169. }
  170. o.G2T, err = Array32StringToBigInt(s.G2T)
  171. if err != nil {
  172. return o, err
  173. }
  174. o.Pk.A, err = Array3StringToBigInt(s.Pk.A)
  175. if err != nil {
  176. return o, err
  177. }
  178. o.Pk.B, err = Array32StringToBigInt(s.Pk.B)
  179. if err != nil {
  180. return o, err
  181. }
  182. o.Pk.C, err = Array3StringToBigInt(s.Pk.C)
  183. if err != nil {
  184. return o, err
  185. }
  186. o.Pk.Kp, err = Array3StringToBigInt(s.Pk.Kp)
  187. if err != nil {
  188. return o, err
  189. }
  190. o.Pk.Ap, err = Array3StringToBigInt(s.Pk.Ap)
  191. if err != nil {
  192. return o, err
  193. }
  194. o.Pk.Bp, err = Array3StringToBigInt(s.Pk.Bp)
  195. if err != nil {
  196. return o, err
  197. }
  198. o.Pk.Cp, err = Array3StringToBigInt(s.Pk.Cp)
  199. if err != nil {
  200. return o, err
  201. }
  202. o.Pk.Z, err = ArrayStringToBigInt(s.Pk.Z)
  203. if err != nil {
  204. return o, err
  205. }
  206. return o, nil
  207. }
  208. // circuit
  209. type CircuitString struct {
  210. NVars int
  211. NPublic int
  212. NSignals int
  213. PrivateInputs []string
  214. PublicInputs []string
  215. Signals []string
  216. Witness []string
  217. Constraints []circuitcompiler.Constraint
  218. R1CS struct {
  219. A [][]string
  220. B [][]string
  221. C [][]string
  222. }
  223. }
  224. func ArrayArrayBigIntToString(b [][]*big.Int) [][]string {
  225. var o [][]string
  226. for i := 0; i < len(b); i++ {
  227. o = append(o, ArrayBigIntToString(b[i]))
  228. }
  229. return o
  230. }
  231. func ArrayArrayStringToBigInt(s [][]string) ([][]*big.Int, error) {
  232. var o [][]*big.Int
  233. for i := 0; i < len(s); i++ {
  234. parsed, err := ArrayStringToBigInt(s[i])
  235. if err != nil {
  236. return o, err
  237. }
  238. o = append(o, parsed)
  239. }
  240. return o, nil
  241. }
  242. func CircuitToString(c circuitcompiler.Circuit) CircuitString {
  243. var cs CircuitString
  244. cs.NVars = c.NVars
  245. cs.NPublic = c.NPublic
  246. cs.NSignals = c.NSignals
  247. cs.PrivateInputs = c.PrivateInputs
  248. cs.PublicInputs = c.PublicInputs
  249. cs.Signals = c.Signals
  250. cs.Witness = ArrayBigIntToString(c.Witness)
  251. cs.Constraints = c.Constraints
  252. cs.R1CS.A = ArrayArrayBigIntToString(c.R1CS.A)
  253. cs.R1CS.B = ArrayArrayBigIntToString(c.R1CS.B)
  254. cs.R1CS.C = ArrayArrayBigIntToString(c.R1CS.C)
  255. return cs
  256. }
  257. func CircuitFromString(cs CircuitString) (circuitcompiler.Circuit, error) {
  258. var c circuitcompiler.Circuit
  259. var err error
  260. c.NVars = cs.NVars
  261. c.NPublic = cs.NPublic
  262. c.NSignals = cs.NSignals
  263. c.PrivateInputs = cs.PrivateInputs
  264. c.PublicInputs = cs.PublicInputs
  265. c.Signals = cs.Signals
  266. c.Witness, err = ArrayStringToBigInt(cs.Witness)
  267. if err != nil {
  268. return c, err
  269. }
  270. c.Constraints = cs.Constraints
  271. c.R1CS.A, err = ArrayArrayStringToBigInt(cs.R1CS.A)
  272. if err != nil {
  273. return c, err
  274. }
  275. c.R1CS.B, err = ArrayArrayStringToBigInt(cs.R1CS.B)
  276. if err != nil {
  277. return c, err
  278. }
  279. c.R1CS.C, err = ArrayArrayStringToBigInt(cs.R1CS.C)
  280. if err != nil {
  281. return c, err
  282. }
  283. return c, nil
  284. }
  285. // Proof
  286. type ProofString struct {
  287. PiA [3]string
  288. PiAp [3]string
  289. PiB [3][2]string
  290. PiBp [3]string
  291. PiC [3]string
  292. PiCp [3]string
  293. PiH [3]string
  294. PiKp [3]string
  295. }
  296. func ProofToString(p snark.Proof) ProofString {
  297. var s ProofString
  298. s.PiA = BigInt3ToString(p.PiA)
  299. s.PiAp = BigInt3ToString(p.PiAp)
  300. s.PiB = BigInt32ToString(p.PiB)
  301. s.PiBp = BigInt3ToString(p.PiBp)
  302. s.PiC = BigInt3ToString(p.PiC)
  303. s.PiCp = BigInt3ToString(p.PiCp)
  304. s.PiH = BigInt3ToString(p.PiH)
  305. s.PiKp = BigInt3ToString(p.PiKp)
  306. return s
  307. }
  308. func ProofFromString(s ProofString) (snark.Proof, error) {
  309. var p snark.Proof
  310. var err error
  311. p.PiA, err = String3ToBigInt(s.PiA)
  312. if err != nil {
  313. return p, err
  314. }
  315. p.PiAp, err = String3ToBigInt(s.PiAp)
  316. if err != nil {
  317. return p, err
  318. }
  319. p.PiB, err = String32ToBigInt(s.PiB)
  320. if err != nil {
  321. return p, err
  322. }
  323. p.PiBp, err = String3ToBigInt(s.PiBp)
  324. if err != nil {
  325. return p, err
  326. }
  327. p.PiC, err = String3ToBigInt(s.PiC)
  328. if err != nil {
  329. return p, err
  330. }
  331. p.PiCp, err = String3ToBigInt(s.PiCp)
  332. if err != nil {
  333. return p, err
  334. }
  335. p.PiH, err = String3ToBigInt(s.PiH)
  336. if err != nil {
  337. return p, err
  338. }
  339. p.PiKp, err = String3ToBigInt(s.PiKp)
  340. if err != nil {
  341. return p, err
  342. }
  343. return p, nil
  344. }