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.

123 lines
3.2 KiB

5 years ago
  1. const assert = require("assert");
  2. const refBn128 = require("snarkjs").bn128;
  3. const refBigInt = require("snarkjs").bigInt;
  4. const buildBn128 = require("../index.js").buildBn128;
  5. describe("FFT tests", () => {
  6. it("create a basic FFT", async () => {
  7. const bn128 = await buildBn128();
  8. const N=4;
  9. const p = bn128.alloc(32*N);
  10. for (let i=0; i<N; i++) {
  11. bn128.putInt(p+i*32, i);
  12. }
  13. bn128.fft_toMontgomeryN(p, p, N);
  14. bn128.fft_fft(p, N);
  15. bn128.fft_ifft(p, N);
  16. bn128.fft_fromMontgomeryN(p, p, N);
  17. for (let i=0; i<N; i++) {
  18. const a = bn128.getInt(p+i*32);
  19. assert.equal(a,i);
  20. }
  21. });
  22. it("create a do it reverselly FFT", async () => {
  23. const bn128 = await buildBn128();
  24. const N=1024;
  25. const p = bn128.alloc(32*N);
  26. for (let i=0; i<N; i++) {
  27. bn128.putInt(p+i*32, i);
  28. }
  29. bn128.fft_toMontgomeryN(p, p, N);
  30. bn128.fft_ifft(p, N, 0);
  31. bn128.fft_fft(p, N, 0);
  32. bn128.fft_fromMontgomeryN(p, p, N);
  33. for (let i=0; i<N; i++) {
  34. const a = bn128.getInt(p+i*32);
  35. assert.equal(a,i);
  36. }
  37. });
  38. it("test with zeros", async () => {
  39. const bn128 = await buildBn128();
  40. const N=1024;
  41. const p = bn128.alloc(32*N);
  42. for (let i=0; i<N; i++) {
  43. bn128.putInt(p+i*32, (i%2 == 0)? 0 : 1);
  44. }
  45. bn128.fft_toMontgomeryN(p, p, N);
  46. bn128.fft_ifft(p, N, 0);
  47. bn128.fft_fft(p, N, 0);
  48. bn128.fft_fromMontgomeryN(p, p, N);
  49. for (let i=0; i<N; i++) {
  50. const a = bn128.getInt(p+i*32);
  51. assert.equal(a,(i%2 == 0)? 0 : 1);
  52. }
  53. });
  54. it("test interleaved", async () => {
  55. const bn128 = await buildBn128();
  56. const N=1024;
  57. const p = bn128.alloc(32*N);
  58. const pr1 = bn128.alloc(32*N*2);
  59. const pr2 = bn128.alloc(32*N*2);
  60. for (let i=0; i<N; i++) {
  61. bn128.putInt(p+i*32, i);
  62. }
  63. bn128.fft_toMontgomeryN(p, p, N);
  64. bn128.fft_fft(p, N, 0);
  65. bn128.fft_copyNInterleaved(p, pr1, N);
  66. for (let i=0; i<N; i++) {
  67. bn128.putInt(p+i*32, i);
  68. }
  69. bn128.fft_toMontgomeryN(p, p, N);
  70. bn128.fft_fft(p, N, 1);
  71. bn128.fft_copyNInterleaved(p, pr1+32, N);
  72. bn128.fft_fromMontgomeryN(pr1, pr1, N*2);
  73. for (let i=0; i<N; i++) {
  74. bn128.putInt(pr2+i*32, i);
  75. }
  76. for (let i=N; i<N*2; i++) {
  77. bn128.putInt(pr2+i*32, 0);
  78. }
  79. bn128.fft_toMontgomeryN(pr2, pr2, N*2);
  80. bn128.fft_fft(pr2, N*2, 0);
  81. bn128.fft_fromMontgomeryN(pr2, pr2, N*2);
  82. for (let i=0; i<N*2; i++) {
  83. const a = bn128.getInt(pr1+i*32, 1);
  84. const b = bn128.getInt(pr2+i*32, 1);
  85. assert(a.equals(b));
  86. }
  87. bn128.fft_toMontgomeryN(pr1, pr1, N*2);
  88. bn128.fft_ifft(pr1, N*2, 0);
  89. bn128.fft_fromMontgomeryN(pr1, pr1, N*2);
  90. for (let i=0; i<N; i++) {
  91. const a = bn128.getInt(pr1+i*32, 1);
  92. assert.equal(a,i);
  93. }
  94. for (let i=N; i<N*2; i++) {
  95. const a = bn128.getInt(pr1+i*32, 1);
  96. assert.equal(a,0);
  97. }
  98. });
  99. });