๐Ÿ“ฆ p0n1 / familiar_strangers

๐Ÿ“„ stranger_judge.js ยท 64 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64const express = require('express');
const bodyParser = require('body-parser');
const { wasm: wasm_tester } = require('circom_tester');
const path = require('path');

const app = express();
app.use(bodyParser.json());

const PASS = "pass";
const FAIL = "fail";

async function runCircuit(circuitPath, input) {
    const circuit = await wasm_tester(circuitPath, {
        output: path.join(__dirname, "./test/generated"),
        // recompile: true,
    });
    try {
        const witness = await circuit.calculateWitness({ in: input });
        await circuit.checkConstraints(witness);
        return PASS;
    } catch (error) {
        return error.toString();
    }
}

// Example inputs {"l1": "123", "l2": "123"}
// Test with curl
// curl -X POST http://localhost:3000/judge -H "Content-Type: application/json" -d '{"l1": "123", "l2": "123"}'

app.post('/judge', async (req, res) => {
    // random number for logging
    const identifier = Math.floor(Math.random() * 100000000000);
    const inputs = req.body;
    console.log("identifier", identifier, "inputs", inputs);

    let result = FAIL;

    // Level 1
    result = await runCircuit(path.join(__dirname, "test/level1_test.circom"), inputs.l1);
    if (result != PASS) {
        res.json({ success: false });
        return;
    }

    // Level 2
    if (inputs.l2.length <= 70) {
        res.json({ success: false });
        return;
    }
    result = await runCircuit(path.join(__dirname, "test/level2_test.circom"), inputs.l2);
    if (result != PASS) {
        res.json({ success: false });
        return;
    }

    res.json({ success: true });
    console.log("identifier", identifier, "success");
    return;
});

const port = 3000;
app.listen(port, () => {
    console.log(`Stranger Judge Service running on port ${port}`);
});