arnaucube
arnaucube/grapevine-sonobePublic Self-hosted repository
grapevine-sonobe/circom/node_modules/circomlib/test/eddsa.js
circom/node_modules/circomlib/test/eddsa.jsRaw
 1const chai = require("chai");
 2const path = require("path");
 3
 4const wasm_tester = require("circom_tester").wasm;
 5
 6const buildEddsa = require("circomlibjs").buildEddsa;
 7const buildBabyjub = require("circomlibjs").buildBabyjub;
 8
 9const Scalar = require("ffjavascript").Scalar;
10
11const assert = chai.assert;
12
13function print(circuit, w, s) {
14    console.log(s + ": " + w[circuit.getSignalIdx(s)]);
15}
16
17function buffer2bits(buff) {
18    const res = [];
19    for (let i=0; i<buff.length; i++) {
20        for (let j=0; j<8; j++) {
21            if ((buff[i]>>j)&1) {
22                res.push(1n);
23            } else {
24                res.push(0n);
25            }
26        }
27    }
28    return res;
29}
30
31
32describe("EdDSA test", function () {
33    let circuit;
34    let eddsa;
35    let babyJub;
36    let F;
37
38    this.timeout(100000);
39
40    before( async () => {
41        eddsa = await buildEddsa();
42        babyJub = await buildBabyjub();
43        F = babyJub.F;
44        circuit = await wasm_tester(path.join(__dirname, "circuits", "eddsa_test.circom"));
45    });
46
47
48    it("Sign a single 10 bytes from 0 to 9", async () => {
49        const msg = Buffer.from("00010203040506070809", "hex");
50
51//        const prvKey = crypto.randomBytes(32);
52
53        const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
54
55        const pubKey = eddsa.prv2pub(prvKey);
56
57        const pPubKey = babyJub.packPoint(pubKey);
58
59        const signature = eddsa.signPedersen(prvKey, msg);
60
61        const pSignature = eddsa.packSignature(signature);
62        const uSignature = eddsa.unpackSignature(pSignature);
63
64        assert(eddsa.verifyPedersen(msg, uSignature, pubKey));
65
66        const msgBits = buffer2bits( msg);
67        const r8Bits = buffer2bits( pSignature.slice(0, 32));
68        const sBits = buffer2bits( pSignature.slice(32, 64));
69        const aBits = buffer2bits( pPubKey);
70
71        const w = await circuit.calculateWitness({A: aBits, R8: r8Bits, S: sBits, msg: msgBits}, true);
72
73        await circuit.checkConstraints(w);
74    });
75});