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.

341 lines
10 KiB

  1. package parsers
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "os"
  6. "testing"
  7. "github.com/iden3/go-circom-prover-verifier/types"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. )
  11. func TestParseArrayG1(t *testing.T) {
  12. aS := [][]string{
  13. {
  14. "16145916318196730299582072104388453231952213805668281741813587224450782397538",
  15. "4434505318477484327659527264104806919103674231447634885054368605283938696207",
  16. "1",
  17. },
  18. {
  19. "10618406967550056457559358662746625591602641004174976323307214433994084907915",
  20. "1843236360452735081347085412539192450068665510574800388201121698908391533923",
  21. "1",
  22. },
  23. {
  24. "1208972877970123411566574123860641832032384890981476033353526096830198333194",
  25. "777503551507025252294438107100944741641946695980350712141258191590862204805",
  26. "1",
  27. },
  28. {
  29. "0",
  30. "1",
  31. "0",
  32. },
  33. }
  34. a, err := arrayStringToG1(aS)
  35. assert.Nil(t, err)
  36. assert.Equal(t, "bn256.G1(23b243c928ce40c4cc2dad366e9f61723aef65866e1c66f42a08697f2f030462, 09cdd7500688fb487ec9f27b5a732d68fa0f3ddca5c2c790e330fdfb03b77c0f)", a[0].String())
  37. assert.Equal(t, "bn256.G1(1779ce2c586b5fc523e72e755d969a63473052aaad7c11eb5bf0ecdcfdfefb8b, 04133c1c74206dace57cd3d76ce59be381bbf08f51a5b3edc2b0c183d43eed63)", a[1].String())
  38. assert.Equal(t, "bn256.G1(02ac4120598d2f2bb81bc09b8df596403360577b0c5ff52485d1ef2200f23f0a, 01b80d298de75f867d6484c55c3da02c042bb4eb9a1734d3386786aaa669af85)", a[2].String())
  39. assert.Equal(t, "bn256.G1(0000000000000000000000000000000000000000000000000000000000000000, 0000000000000000000000000000000000000000000000000000000000000001)", a[3].String())
  40. }
  41. func TestParseG2(t *testing.T) {
  42. aS := [][]string{
  43. {
  44. "9283666785342556550467669770956850930982548182701254051508520248901282197973",
  45. "11369378229277445316894458966429873744779877313900506577160370623273013178252",
  46. },
  47. {
  48. "10625777544326349817513295021482494426101347915428005055375725845993157551870",
  49. "21401790227434807639472120486932615400751346915707967674912972446672152512583",
  50. },
  51. {
  52. "1",
  53. "0",
  54. },
  55. }
  56. a, err := stringToG2(aS)
  57. assert.Nil(t, err)
  58. assert.Equal(t, "bn256.G2((1922d70c934543aa655ec3277f7fa10a25ec973a4f001a7c54ce4954b4916f8c, 14865e836947c42cf35b47d30e06535fff9dab319c4296e28afde368960671d5), (2f50fbe77925b0a9d718c9ab38638bafa7c65f43f0d09035e518df97ad294847, 177dfa1a3b8627faf0425d9511bcb4c6ca986ea05e3803b5c643c35b94a7e6fe))", a.String())
  59. aS = [][]string{
  60. {
  61. "13973091636763944887728510851169742544309374663995476311690518173988838518856",
  62. "12903946180439304546475897520537621821375470264150438270817301786763517825250",
  63. },
  64. {
  65. "370374369234123593044872519351942112043402224488849374153134091815693350697",
  66. "17423079115073430837335625309232513526393852743032331213038909731579295753224",
  67. },
  68. {
  69. "1",
  70. "0",
  71. },
  72. }
  73. a, err = stringToG2(aS)
  74. assert.Nil(t, err)
  75. assert.Equal(t, "bn256.G2((1c875fed67fff3b35f115b03706ec45f281b5f6cc71a99107240e09fce4910e2, 1ee47d566e9a099626b9860bcd96f6d4a1ed65f115d3efa8e05e5f42cc793048), (26851d022ce9961df65a430811824aaf3118710ac03b0614a50c05ee27d8e408, 00d19fdce25b0d78fb317a5f1789823b7ed76274b0d1be9c685792c73b347729))", a.String())
  76. }
  77. func TestParseArrayG2(t *testing.T) {
  78. aS := [][][]string{
  79. {
  80. {
  81. "0",
  82. "0",
  83. },
  84. {
  85. "1",
  86. "0",
  87. },
  88. {
  89. "0",
  90. "0",
  91. },
  92. },
  93. {
  94. {
  95. "0",
  96. "0",
  97. },
  98. {
  99. "1",
  100. "0",
  101. },
  102. {
  103. "0",
  104. "0",
  105. },
  106. },
  107. {
  108. {
  109. "0",
  110. "0",
  111. },
  112. {
  113. "1",
  114. "0",
  115. },
  116. {
  117. "0",
  118. "0",
  119. },
  120. },
  121. {
  122. {
  123. "9283666785342556550467669770956850930982548182701254051508520248901282197973",
  124. "11369378229277445316894458966429873744779877313900506577160370623273013178252",
  125. },
  126. {
  127. "10625777544326349817513295021482494426101347915428005055375725845993157551870",
  128. "21401790227434807639472120486932615400751346915707967674912972446672152512583",
  129. },
  130. {
  131. "1",
  132. "0",
  133. },
  134. },
  135. }
  136. a, err := arrayStringToG2(aS)
  137. assert.Nil(t, err)
  138. assert.Equal(t, "bn256.G2((0000000000000000000000000000000000000000000000000000000000000000, 0000000000000000000000000000000000000000000000000000000000000000), (0000000000000000000000000000000000000000000000000000000000000000, 0000000000000000000000000000000000000000000000000000000000000001))", a[0].String())
  139. assert.Equal(t, "bn256.G2((1922d70c934543aa655ec3277f7fa10a25ec973a4f001a7c54ce4954b4916f8c, 14865e836947c42cf35b47d30e06535fff9dab319c4296e28afde368960671d5), (2f50fbe77925b0a9d718c9ab38638bafa7c65f43f0d09035e518df97ad294847, 177dfa1a3b8627faf0425d9511bcb4c6ca986ea05e3803b5c643c35b94a7e6fe))", a[3].String())
  140. }
  141. func testCircuitParseWitnessBin(t *testing.T, circuit string) {
  142. witnessBinFile, err := os.Open("../testdata/" + circuit + "/witness.bin")
  143. require.Nil(t, err)
  144. defer witnessBinFile.Close()
  145. witness, err := ParseWitnessBin(witnessBinFile)
  146. require.Nil(t, err)
  147. witnessJson, err := ioutil.ReadFile("../testdata/" + circuit + "/witness.json")
  148. require.Nil(t, err)
  149. w, err := ParseWitness(witnessJson)
  150. require.Nil(t, err)
  151. assert.Equal(t, len(w), len(witness))
  152. assert.Equal(t, w[0], witness[0])
  153. assert.Equal(t, w[1], witness[1])
  154. assert.Equal(t, w[10], witness[10])
  155. assert.Equal(t, w[len(w)-3], witness[len(w)-3])
  156. assert.Equal(t, w[len(w)-2], witness[len(w)-2])
  157. assert.Equal(t, w[len(w)-1], witness[len(w)-1])
  158. }
  159. func TestParseWitnessBin(t *testing.T) {
  160. testCircuitParseWitnessBin(t, "circuit1k")
  161. testCircuitParseWitnessBin(t, "circuit5k")
  162. }
  163. func TestProofSmartContractFormat(t *testing.T) {
  164. proofJson, err := ioutil.ReadFile("../testdata/circuit1k/proof.json")
  165. require.Nil(t, err)
  166. proof, err := ParseProof(proofJson)
  167. require.Nil(t, err)
  168. pS := ProofToString(proof)
  169. pSC := ProofToSmartContractFormat(proof)
  170. assert.Nil(t, err)
  171. assert.Equal(t, pS.A[0], pSC.A[0])
  172. assert.Equal(t, pS.A[1], pSC.A[1])
  173. assert.Equal(t, pS.B[0][0], pSC.B[0][1])
  174. assert.Equal(t, pS.B[0][1], pSC.B[0][0])
  175. assert.Equal(t, pS.B[1][0], pSC.B[1][1])
  176. assert.Equal(t, pS.B[1][1], pSC.B[1][0])
  177. assert.Equal(t, pS.C[0], pSC.C[0])
  178. assert.Equal(t, pS.C[1], pSC.C[1])
  179. assert.Equal(t, pS.Protocol, pSC.Protocol)
  180. pSC2 := ProofStringToSmartContractFormat(pS)
  181. assert.Equal(t, pSC, pSC2)
  182. }
  183. func TestProofJSON(t *testing.T) {
  184. proofJson, err := ioutil.ReadFile("../testdata/circuit1k/proof.json")
  185. require.Nil(t, err)
  186. proof, err := ParseProof(proofJson)
  187. require.Nil(t, err)
  188. proof1JSON, err := json.Marshal(proof)
  189. require.Nil(t, err)
  190. var proof1 types.Proof
  191. err = json.Unmarshal(proof1JSON, &proof1)
  192. require.Nil(t, err)
  193. require.Equal(t, *proof, proof1)
  194. }
  195. func testCircuitParsePkBin(t *testing.T, circuit string) {
  196. pkBinFile, err := os.Open("../testdata/" + circuit + "/proving_key.bin")
  197. require.Nil(t, err)
  198. defer pkBinFile.Close()
  199. pk, err := ParsePkBin(pkBinFile)
  200. require.Nil(t, err)
  201. pkJson, err := ioutil.ReadFile("../testdata/" + circuit + "/proving_key.json")
  202. require.Nil(t, err)
  203. pkJ, err := ParsePk(pkJson)
  204. require.Nil(t, err)
  205. assert.Equal(t, pkJ.NVars, pk.NVars)
  206. assert.Equal(t, pkJ.NPublic, pk.NPublic)
  207. assert.Equal(t, pkJ.DomainSize, pk.DomainSize)
  208. assert.Equal(t, pkJ.VkAlpha1, pk.VkAlpha1)
  209. assert.Equal(t, pkJ.VkBeta1, pk.VkBeta1)
  210. assert.Equal(t, pkJ.VkDelta1, pk.VkDelta1)
  211. assert.Equal(t, pkJ.VkDelta2, pk.VkDelta2)
  212. assert.Equal(t, pkJ.PolsA, pk.PolsA)
  213. assert.Equal(t, pkJ.PolsB, pk.PolsB)
  214. assert.Equal(t, pkJ.A, pk.A)
  215. assert.Equal(t, pkJ.B1, pk.B1)
  216. assert.Equal(t, pkJ.B2, pk.B2)
  217. assert.Equal(t, pkJ.C, pk.C)
  218. assert.Equal(t, pkJ.HExps[:pkJ.DomainSize], pk.HExps[:pk.DomainSize]) // circom behaviour
  219. assert.Equal(t, pkJ.NVars, pk.NVars)
  220. assert.Equal(t, pkJ.NPublic, pk.NPublic)
  221. assert.Equal(t, pkJ.DomainSize, pk.DomainSize)
  222. assert.Equal(t, pkJ.PolsC, pk.PolsC)
  223. }
  224. func TestParsePkBin(t *testing.T) {
  225. testCircuitParsePkBin(t, "circuit1k")
  226. testCircuitParsePkBin(t, "circuit5k")
  227. }
  228. func testGoCircomPkFormat(t *testing.T, circuit string) {
  229. pkJson, err := ioutil.ReadFile("../testdata/" + circuit + "/proving_key.json")
  230. require.Nil(t, err)
  231. pk, err := ParsePk(pkJson)
  232. require.Nil(t, err)
  233. pkGBin, err := PkToGoBin(pk)
  234. require.Nil(t, err)
  235. err = ioutil.WriteFile("../testdata/"+circuit+"/proving_key.go.bin", pkGBin, 0644)
  236. assert.Nil(t, err)
  237. // parse ProvingKeyGo
  238. pkGoBinFile, err := os.Open("../testdata/" + circuit + "/proving_key.go.bin")
  239. require.Nil(t, err)
  240. defer pkGoBinFile.Close()
  241. pkG, err := ParsePkGoBin(pkGoBinFile)
  242. require.Nil(t, err)
  243. assert.Equal(t, pk.VkAlpha1, pkG.VkAlpha1)
  244. assert.Equal(t, pk.VkBeta1, pkG.VkBeta1)
  245. assert.Equal(t, pk.VkDelta1, pkG.VkDelta1)
  246. assert.Equal(t, pk.VkBeta2, pkG.VkBeta2)
  247. assert.Equal(t, pk.VkDelta2, pkG.VkDelta2)
  248. assert.Equal(t, pk.A, pkG.A)
  249. assert.Equal(t, pk.B1, pkG.B1)
  250. assert.Equal(t, pk.B2, pkG.B2)
  251. assert.Equal(t, pk.C, pkG.C)
  252. assert.Equal(t, pk.HExps, pkG.HExps)
  253. assert.Equal(t, pk.PolsA, pkG.PolsA)
  254. assert.Equal(t, pk.PolsB, pkG.PolsB)
  255. assert.Equal(t, pk.NVars, pkG.NVars)
  256. assert.Equal(t, pk.NPublic, pkG.NPublic)
  257. assert.Equal(t, pk.DomainSize, pkG.DomainSize)
  258. assert.Equal(t, pk.PolsC, pkG.PolsC)
  259. // pkPrettyJSON, err := json.MarshalIndent(pk, "", " ")
  260. // require.Nil(t, err)
  261. // pkGoPrettyJSON, err := json.MarshalIndent(pkG, "", " ")
  262. // require.Nil(t, err)
  263. // assert.Equal(t, pkPrettyJSON, pkGoPrettyJSON)
  264. }
  265. func TestGoCircomPkFormat(t *testing.T) {
  266. testGoCircomPkFormat(t, "circuit1k")
  267. testGoCircomPkFormat(t, "circuit5k")
  268. // testGoCircomPkFormat(t, "circuit10k")
  269. // testGoCircomPkFormat(t, "circuit20k")
  270. }
  271. func benchmarkParsePk(b *testing.B, circuit string) {
  272. pkJson, err := ioutil.ReadFile("../testdata/" + circuit + "/proving_key.json")
  273. require.Nil(b, err)
  274. pkBinFile, err := os.Open("../testdata/" + circuit + "/proving_key.bin")
  275. require.Nil(b, err)
  276. defer pkBinFile.Close()
  277. pkGoBinFile, err := os.Open("../testdata/" + circuit + "/proving_key.go.bin")
  278. require.Nil(b, err)
  279. defer pkGoBinFile.Close()
  280. b.Run("ParsePkJson "+circuit, func(b *testing.B) {
  281. for i := 0; i < b.N; i++ {
  282. _, err = ParsePk(pkJson)
  283. require.Nil(b, err)
  284. }
  285. })
  286. b.Run("ParsePkBin "+circuit, func(b *testing.B) {
  287. for i := 0; i < b.N; i++ {
  288. pkBinFile.Seek(0, 0)
  289. _, err = ParsePkBin(pkBinFile)
  290. require.Nil(b, err)
  291. }
  292. })
  293. b.Run("ParsePkGoBin "+circuit, func(b *testing.B) {
  294. for i := 0; i < b.N; i++ {
  295. pkGoBinFile.Seek(0, 0)
  296. _, err = ParsePkGoBin(pkGoBinFile)
  297. require.Nil(b, err)
  298. }
  299. })
  300. }
  301. func BenchmarkParsePk(b *testing.B) {
  302. benchmarkParsePk(b, "circuit1k")
  303. benchmarkParsePk(b, "circuit5k")
  304. // benchmarkParsePk(b, "circuit10k")
  305. // benchmarkParsePk(b, "circuit20k")
  306. }