arnaucube
arnaucube/keccak256-circomPublic Self-hosted repository
keccak256-circom/test/permutations.js
test/permutations.jsRaw
  1const path = require("path");
  2
  3const chai = require("chai");
  4const assert = chai.assert;
  5
  6const wasm_tester = require("circom_tester").wasm;
  7const c_tester = require("circom_tester").c;
  8
  9const utils = require("./utils");
 10
 11describe("Theta test", function () {
 12    this.timeout(100000);
 13
 14    it ("Theta (testvector generated from go)", async () => {
 15	const cir = await wasm_tester(path.join(__dirname, "circuits", "theta_test.circom"));
 16
 17	const input =
 18	    utils.intsToBigInts([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]);
 19	const expectedOut =
 20	    utils.intsToBigInts([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]);
 21	const stateIn = utils.u64ArrayToBits(input);
 22	const expectedOutBits = utils.u64ArrayToBits(expectedOut);
 23
 24	const witness = await cir.calculateWitness({ "in": stateIn }, true);
 25
 26	const stateOut = witness.slice(1, 1+(25*64));
 27	const stateOutU64 = utils.bitsToU64Array(stateOut);
 28	// console.log(stateOutU64, expectedOut);
 29	assert.deepEqual(stateOutU64, expectedOut);
 30    });
 31    it ("Theta (same test as previous, but using c_tester to ensure that"+
 32	"circom_tester with c works as expected)", async () => {
 33	const cir = await c_tester(path.join(__dirname, "circuits", "theta_test.circom"));
 34
 35	const input =
 36		utils.intsToBigInts([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]);
 37	const expectedOut =
 38		utils.intsToBigInts([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]);
 39	const stateIn = utils.u64ArrayToBits(input);
 40	const expectedOutBits = utils.u64ArrayToBits(expectedOut);
 41
 42	const witness = await cir.calculateWitness({ "in": stateIn }, true);
 43
 44	const stateOut = witness.slice(1, 1+(25*64));
 45	const stateOutU64 = utils.bitsToU64Array(stateOut);
 46	// console.log(stateOutU64, expectedOut);
 47	assert.deepEqual(stateOutU64, expectedOut);
 48    });
 49
 50    it ("Theta (testvector generated from go)", async () => {
 51	const cir = await wasm_tester(path.join(__dirname, "circuits", "theta_test.circom"));
 52	// const cir = await c_tester(path.join(__dirname, "circuits", "theta_test.circom"));
 53
 54	const input = utils.strsToBigInts(["26388279066651", "246290629787648",
 55	    "26388279902208", "25165850", "246290605457408", "7784628352",
 56	    "844424965783552", "2305843009213694083", "844432714760192",
 57	    "2305843009249345539", "637534226", "14848", "641204224", "14354",
 58	    "3670528", "6308236288", "2130304761856", "648518346341354496",
 59	    "6309216256", "648520476645130240", "4611706359392501763",
 60	    "792677514882318336", "20340965113972", "4611732197915754499",
 61	    "792633534417207412"]);
 62	const expectedOut = utils.strsToBigInts(["3749081831850030700",
 63	    "1297317621190464868", "10017560217643747862",
 64	    "7854780639862409219", "13836147678645575967",
 65	    "3749090635727681271", "1297915755455157604",
 66	    "12323429615135705749", "7855062122598582297",
 67	    "16141814766035214620", "3749090628446369381",
 68	    "1297071330560683876", "10017586606556924438",
 69	    "7854780639837253643", "13835971756788491039",
 70	    "3749090634251287159", "1297070162329376100",
 71	    "9369068259580659222", "7854780645071013913",
 72	    "14484490034407743775", "8360757404916954740",
 73	    "1801500877105239396", "10017570663003408994",
 74	    "3243123208712177690", "14628605291203076459"]);
 75
 76	const stateIn = utils.u64ArrayToBits(input);
 77	const expectedOutBits = utils.u64ArrayToBits(expectedOut);
 78
 79	const witness = await cir.calculateWitness({ "in": stateIn }, true);
 80
 81	const stateOut = witness.slice(1, 1+(25*64));
 82	const stateOutU64 = utils.bitsToU64Array(stateOut);
 83	assert.deepEqual(stateOutU64, expectedOut);
 84    });
 85
 86
 87});
 88
 89describe("RhoPi test", function () {
 90    this.timeout(100000);
 91
 92    it ("RhoPi (testvector generated from go)", async () => {
 93	const cir = await wasm_tester(path.join(__dirname, "circuits", "rhopi_test.circom"));
 94
 95	const input =
 96	    utils.intsToBigInts([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	const expectedOut = utils.strsToBigInts(["0", "105553116266496",
 98	    "105553116266496", "37748736", "393216", "805306368", "9437184",
 99	    "80", "562949953421312", "13835058055282163714", "2", "448",
100	    "436207616", "4864", "5242880", "536870912", "343597383680",
101	    "11264", "557056", "1657324662872342528", "9223372036854775808",
102	    "288230376151711744", "7696581394432", "32985348833280", "84"]);
103	const stateIn = utils.u64ArrayToBits(input);
104	const expectedOutBits = utils.u64ArrayToBits(expectedOut);
105
106	const witness = await cir.calculateWitness({ "in": stateIn }, true);
107
108	const stateOut = witness.slice(1, 1+(25*64));
109	const stateOutU64 = utils.bitsToU64Array(stateOut);
110	// console.log(stateOutU64, expectedOut);
111	assert.deepEqual(stateOutU64, expectedOut);
112    });
113});
114
115describe("Chi test", function () {
116    this.timeout(100000);
117
118    it ("Chi (testvector generated from go)", async () => {
119	const cir = await wasm_tester(path.join(__dirname, "circuits", "chi_test.circom"));
120
121	const input =
122	    utils.intsToBigInts([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]);
123	const expectedOut = utils.intsToBigInts([2, 0, 6, 3, 5, 4, 14, 6, 12,
124	    11, 14, 10, 14, 13, 15, 14, 18, 16, 30, 3, 22, 20, 30, 19, 25]);
125	const stateIn = utils.u64ArrayToBits(input);
126	const expectedOutBits = utils.u64ArrayToBits(expectedOut);
127
128	const witness = await cir.calculateWitness({ "in": stateIn }, true);
129
130	const stateOut = witness.slice(1, 1+(25*64));
131	const stateOutU64 = utils.bitsToU64Array(stateOut);
132	// console.log(stateOutU64, expectedOut);
133	assert.deepEqual(stateOutU64, expectedOut);
134    });
135});
136
137describe("Iota test", function () {
138    this.timeout(100000);
139
140    it ("Iota 3 (testvector generated from go)", async () => {
141	const cir = await wasm_tester(path.join(__dirname, "circuits", "iota3_test.circom"));
142
143	const input =
144	    utils.intsToBigInts([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]);
145	const expectedOut =
146	    utils.strsToBigInts(["9223372039002292224",1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24]);
147	const stateIn = utils.u64ArrayToBits(input);
148	const expectedOutBits = utils.u64ArrayToBits(expectedOut);
149
150	const witness = await cir.calculateWitness({ "in": stateIn }, true);
151
152	const stateOut = witness.slice(1, 1+(25*64));
153	const stateOutU64 = utils.bitsToU64Array(stateOut);
154	// console.log(stateOutU64, expectedOut);
155	assert.deepEqual(stateOutU64, expectedOut);
156    });
157    it ("Iota 10 (testvector generated from go)", async () => {
158	const cir = await wasm_tester(path.join(__dirname, "circuits", "iota10_test.circom"));
159
160	const input =
161	    utils.strsToBigInts(["9223372039002292224",1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24]);
162	const expectedOut =
163	    utils.strsToBigInts(["9223372036854775817",1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24]);
164	const stateIn = utils.u64ArrayToBits(input);
165	const expectedOutBits = utils.u64ArrayToBits(expectedOut);
166
167	const witness = await cir.calculateWitness({ "in": stateIn }, true);
168
169	const stateOut = witness.slice(1, 1+(25*64));
170	const stateOutU64 = utils.bitsToU64Array(stateOut);
171	// console.log(stateOutU64, expectedOut);
172	assert.deepEqual(stateOutU64, expectedOut);
173    });
174});