arnaucube
arnaucube/keccak256-circomPublic Self-hosted repository
keccak256-circom/test/keccakf.js
test/keccakf.jsRaw
  1const path = require("path");
  2
  3const chai = require("chai");
  4const assert = chai.assert;
  5
  6const c_tester = require("circom_tester").c;
  7
  8const utils = require("./utils");
  9
 10describe("keccakfRound test", function () {
 11    this.timeout(100000);
 12
 13    // apt install nlohmann-json3-dev
 14    // apt install nasm
 15    // apt install libgmp3-dev
 16
 17    it ("keccakfRound (testvector generated from go)", async () => {
 18	const cir = await c_tester(path.join(__dirname, "circuits", "keccakfRound0_test.circom"));
 19	await cir.loadConstraints();
 20	// console.log("n_constraints", cir.constraints.length);
 21
 22	const input =
 23	    [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];
 24	const expectedOut = utils.strsToBigInts(["26388279066651",
 25	    "246290629787648", "26388279902208", "25165850", "246290605457408",
 26	    "7784628352", "844424965783552", "2305843009213694083",
 27	    "844432714760192", "2305843009249345539", "637534226", "14848",
 28	    "641204224", "14354", "3670528", "6308236288", "2130304761856",
 29	    "648518346341354496", "6309216256", "648520476645130240",
 30	    "4611706359392501763", "792677514882318336", "20340965113972",
 31	    "4611732197915754499", "792633534417207412"]);
 32
 33	const stateIn = utils.u64ArrayToBits(input);
 34	const expectedOutBits = utils.u64ArrayToBits(expectedOut);
 35
 36	const witness = await cir.calculateWitness({ "in": stateIn }, true);
 37
 38	const stateOut = witness.slice(1, 1+(25*64));
 39	const stateOutU64 = utils.bitsToU64Array(stateOut);
 40	// console.log(stateOutU64, expectedOut);
 41	assert.deepEqual(stateOutU64, expectedOut);
 42    });
 43
 44    it ("keccakfRound 20 (testvector generated from go)", async () => {
 45	const cir = await c_tester(path.join(__dirname, "circuits", "keccakfRound20_test.circom"));
 46	await cir.loadConstraints();
 47	// console.log("n_constraints", cir.constraints.length);
 48
 49	const input = utils.strsToBigInts(["26388279066651", "246290629787648",
 50	    "26388279902208", "25165850", "246290605457408", "7784628352",
 51	    "844424965783552", "2305843009213694083", "844432714760192",
 52	    "2305843009249345539", "637534226", "14848", "641204224", "14354",
 53	    "3670528", "6308236288", "2130304761856", "648518346341354496",
 54	    "6309216256", "648520476645130240", "4611706359392501763",
 55	    "792677514882318336", "20340965113972", "4611732197915754499",
 56	    "792633534417207412"]);
 57	const expectedOut = utils.strsToBigInts(["17728382861289829725",
 58	    "13654073086381141005", "9912591532945168756",
 59	    "2030068283137172501", "5084683018496047808", "151244976540463006",
 60	    "11718217461613725815", "11636071286320763433",
 61	    "15039144509240642782", "11629028282864249197",
 62	    "2594633730779457624", "14005558505838459171",
 63	    "4612881094252610438", "2828009553220809993",
 64	    "4838578484623267135", "1006588603063111352",
 65	    "11109191860075454495", "1187545859779038208",
 66	    "14661669042642437042", "5345317080454741069",
 67	    "8196674451365552863", "635818354583088260",
 68	    "13515759754032305626", "1708499319988748543",
 69	    "7509292798507899312"]);
 70
 71	const stateIn = utils.u64ArrayToBits(input);
 72	const expectedOutBits = utils.u64ArrayToBits(expectedOut);
 73
 74	const witness = await cir.calculateWitness({ "in": stateIn }, true);
 75
 76	const stateOut = witness.slice(1, 1+(25*64));
 77	const stateOutU64 = utils.bitsToU64Array(stateOut);
 78	// console.log(stateOutU64, expectedOut);
 79	assert.deepEqual(stateOutU64, expectedOut);
 80    });
 81});
 82
 83describe("keccakf test", function () {
 84    this.timeout(100000);
 85
 86    let cir;
 87    before(async () => {
 88	cir = await c_tester(path.join(__dirname, "circuits", "keccakf_test.circom"));
 89	await cir.loadConstraints();
 90	console.log("n_constraints", cir.constraints.length);
 91    });
 92
 93    it ("keccakf 1 (testvector generated from go)", async () => {
 94	const input =
 95	    [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];
 96	const expectedOut = utils.strsToBigInts(["9472389783892099349",
 97	    "2159377575142921216", "17826682512249813373",
 98	    "2325963263767348549", "15086930817298358378",
 99	    "11661812091723830419", "3517755057770134847",
100	    "5223775837645169598", "933274647126506074", "3451250694486589320",
101	    "825065683101361807", "6192414258352188799",
102	    "14426505790672879210", "3326742392640380689",
103	    "16749975585634164134", "17847697619892908514",
104	    "11598434253200954839", "6049795840392747215",
105	    "8610635351954084385", "18234131770974529925",
106	    "15330347418010067760", "12047099911907354591",
107	    "4763389569697138851", "6779624089296570504",
108	    "15083668107635345971"]);
109
110	const stateIn = utils.u64ArrayToBits(input);
111	const expectedOutBits = utils.u64ArrayToBits(expectedOut);
112
113	const witness = await cir.calculateWitness({ "in": stateIn }, true);
114
115	const stateOut = witness.slice(1, 1+(25*64));
116	const stateOutU64 = utils.bitsToU64Array(stateOut);
117	// console.log(stateOutU64, expectedOut);
118	assert.deepEqual(stateOutU64, expectedOut);
119    });
120
121    it ("keccakf 2 (testvector generated from go)", async () => {
122	const input = utils.strsToBigInts(["9472389783892099349",
123	    "2159377575142921216", "17826682512249813373",
124	    "2325963263767348549", "15086930817298358378",
125	    "11661812091723830419", "3517755057770134847",
126	    "5223775837645169598", "933274647126506074", "3451250694486589320",
127	    "825065683101361807", "6192414258352188799",
128	    "14426505790672879210", "3326742392640380689",
129	    "16749975585634164134", "17847697619892908514",
130	    "11598434253200954839", "6049795840392747215",
131	    "8610635351954084385", "18234131770974529925",
132	    "15330347418010067760", "12047099911907354591",
133	    "4763389569697138851", "6779624089296570504",
134	    "15083668107635345971"]);
135	const expectedOut = utils.strsToBigInts(["269318771259381490",
136	    "15892848561416382510", "12485559500958802382",
137	    "4360182510883008729", "14284025675983944434",
138	    "8800366419087562177", "7881853509112258378",
139	    "9503857914080778528", "17110477940977988953",
140	    "13825318756568052601", "11460650932194163315",
141	    "13272167288297399439", "13599957064256729412",
142	    "12730838251751851758", "13736647180617564382",
143	    "5651695613583298166", "15496251216716036782",
144	    "9748494184433838858", "3637745438296580159",
145	    "3821184813198767406", "15603239432236101315",
146	    "3726326332491237029", "7819962668913661099",
147	    "2285898735263816116", "13518516210247555620"]);
148
149	const stateIn = utils.u64ArrayToBits(input);
150	const expectedOutBits = utils.u64ArrayToBits(expectedOut);
151
152	const witness = await cir.calculateWitness({ "in": stateIn }, true);
153
154	const stateOut = witness.slice(1, 1+(25*64));
155	const stateOutU64 = utils.bitsToU64Array(stateOut);
156	// console.log(stateOutU64, expectedOut);
157	assert.deepEqual(stateOutU64, expectedOut);
158    });
159});