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.

139 lines
3.4 KiB

  1. const chai = require("chai");
  2. const path = require("path");
  3. const crypto = require("crypto");
  4. const F1Field = require("ffjavascript").F1Field;
  5. const Scalar = require("ffjavascript").Scalar;
  6. exports.p = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
  7. const Fr = new F1Field(exports.p);
  8. const assert = chai.assert;
  9. const keccak256 = require("keccak256");
  10. const wasm_tester = require("circom_tester").wasm;
  11. // const printSignal = require("./helpers/printsignal");
  12. function bytesToU64(byteArray) {
  13. var value = 0;
  14. for ( var i = byteArray.length - 1; i >= 0; i--) {
  15. value = (value * 256) + byteArray[i];
  16. }
  17. return value;
  18. }
  19. function u64ToBytes(long) {
  20. var byteArray = [0, 0, 0, 0, 0, 0, 0, 0];
  21. for ( var index = 0; index < byteArray.length; index ++ ) {
  22. var byte = long & 0xff;
  23. byteArray [ index ] = byte;
  24. long = (long - byte) / 256 ;
  25. }
  26. return byteArray;
  27. }
  28. function u64ToBits(a) {
  29. const aBytes = u64ToBytes(a);
  30. return bytesToBits(aBytes);
  31. }
  32. function bytesToBits(b) {
  33. const bits = [];
  34. for (let i = 0; i < b.length; i++) {
  35. for (let j = 0; j < 8; j++) {
  36. if ((b[i]&(1<<j)) > 0) {
  37. bits.push(Fr.e(1));
  38. } else {
  39. bits.push(Fr.e(0));
  40. }
  41. }
  42. }
  43. return bits
  44. }
  45. function u64ArrayToBits(u) {
  46. let r = [];
  47. for (let i = 0; i < u.length; i++) {
  48. r = r.concat(u64ToBits(u[i]));
  49. }
  50. return r
  51. }
  52. function bitsToU64(b) {
  53. if (b.length != 64) {
  54. console.log("b.length = ", b.length, " max=64");
  55. return;
  56. }
  57. const by = bitsToBytes(b)
  58. return bytesToU64(by)
  59. }
  60. function bitsToBytes(a) {
  61. const len = Math.floor((a.length -1 )/8)+1;
  62. const b = [];
  63. for (let i=0; i<a.length; i++) {
  64. const p = Math.floor(i/8);
  65. if (a[i]==1) {
  66. b[p] |= 1<<(i%8);
  67. }
  68. }
  69. return b;
  70. }
  71. function bitsToU64Array(b) {
  72. const r = [];
  73. for (let i = 0; i < b.length/64; i++) {
  74. r.push(bitsToU64(b.slice(i*64, i*64+64)));
  75. }
  76. return r
  77. }
  78. function intsToBigInts(a) {
  79. let b = [];
  80. for (let i=0; i<a.length; i++) {
  81. b[i] = Fr.e(a[i]);
  82. }
  83. return b;
  84. }
  85. describe("Utils test", function () {
  86. this.timeout(100000);
  87. it ("utils", async () => {
  88. let a = 3;
  89. let aBits = u64ToBits(a);
  90. let a2 = bitsToU64(aBits);
  91. assert.equal(a2, a);
  92. a = 12345;
  93. aBits = u64ToBits(a);
  94. a2 = bitsToU64(aBits);
  95. assert.equal(a2, a);
  96. a = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24];
  97. aBits = u64ArrayToBits(a);
  98. a2 = bitsToU64Array(aBits);
  99. // console.log(a2, a);
  100. assert.deepEqual(a2, a);
  101. });
  102. });
  103. describe("Theta test", function () {
  104. this.timeout(100000);
  105. it ("Theta (testvector generated from go)", async () => {
  106. const cir = await wasm_tester(path.join(__dirname, "circuits", "theta_test.circom"));
  107. const input = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24];
  108. const expectedOut = [26,9,13,29,47,31,14,8,22,34,16,3,3,19,37,21,24,30,12,56,14,29,25,9,51];
  109. const stateIn = u64ArrayToBits(input);
  110. const expectedOutBits = u64ArrayToBits(expectedOut);
  111. const witness = await cir.calculateWitness({ "in": stateIn }, true);
  112. const stateOut = witness.slice(1, 1+(25*64));
  113. const stateOutU64 = bitsToU64Array(stateOut);
  114. // console.log(stateOutU64, expectedOut);
  115. assert.deepEqual(stateOutU64, expectedOut);
  116. });
  117. });