|
| 1 | +import { assert } from "chai"; |
| 2 | +import { WitnessTester } from "circomkit"; |
| 3 | +import { circomkit } from "../common"; |
| 4 | + |
| 5 | +// input and output type of GFMULInt |
| 6 | +type Arr128 = number[][]; |
| 7 | + |
| 8 | +describe("gfmulint", () => { |
| 9 | + let circuit: WitnessTester<["a", "b"], ["res"]>; |
| 10 | + |
| 11 | + before(async () => { |
| 12 | + circuit = await circomkit.WitnessTester("gfmulint", { |
| 13 | + file: "aes-gcm/ghash_gfmul_int", |
| 14 | + template: "GFMULInt", |
| 15 | + }); |
| 16 | + console.log("#constraints:", await circuit.getConstraintCount()); |
| 17 | + }); |
| 18 | + |
| 19 | + it("should have correct number of constraints", async () => { |
| 20 | + await circuit.expectConstraintCount(74626, true); |
| 21 | + }); |
| 22 | + |
| 23 | + it("should output correct gfmul", async () => { |
| 24 | + const a = 128; |
| 25 | + const b = 128; |
| 26 | + const expected = a * b; |
| 27 | + const input = { a: pad_num_to_arr128(a), b: pad_num_to_arr128(b) }; |
| 28 | + |
| 29 | + let _res = await circuit.compute(input, ["res"]); |
| 30 | + console.log(`res: ${_res.res}`); |
| 31 | + let result = parse_arr128_to_number(_res.res as Arr128); |
| 32 | + console.log(`${a} x ${b} = ${result}`); |
| 33 | + assert.equal(result, expected); |
| 34 | + }); |
| 35 | +}); |
| 36 | + |
| 37 | +function pad_num_to_arr128(value: number): Arr128 { |
| 38 | + let tmp = value |
| 39 | + .toString(2) |
| 40 | + .padStart(128, "0") |
| 41 | + .split("") |
| 42 | + .map((bit) => parseInt(bit, 10)); |
| 43 | + return [tmp.slice(0, 64), tmp.slice(64, 128)]; |
| 44 | +} |
| 45 | + |
| 46 | +function parse_arr128_to_number(res: Arr128): number { |
| 47 | + let first_64: number[] = res[0]; |
| 48 | + let last_64: number[] = res[1]; |
| 49 | + let all_bits: number[] = first_64.concat(last_64); |
| 50 | + return parseInt(all_bits.join(""), 2); |
| 51 | +} |
0 commit comments