arnaucube
arnaucube/websnarkPublic Self-hosted repository
websnark/tools/buildwitness.js
tools/buildwitness.jsRaw
 1const assert = require("assert");
 2
 3function writeUint32(h, val) {
 4    h.dataView.setUint32(h.offset, val, true);
 5    h.offset += 4;
 6}
 7
 8
 9function writeBigInt(h, bi) {
10    for (let i=0; i<8; i++) {
11        const v = bi.shiftRight(i*32).and(0xFFFFFFFF).toJSNumber();
12        writeUint32(h, v);
13    }
14}
15
16
17function calculateBuffLen(witness) {
18    let size = 0;
19
20    // beta2, delta2
21    size += witness.length * 32;
22
23    return size;
24}
25
26
27function buildWitness(witness) {
28    const buffLen = calculateBuffLen(witness);
29
30    const buff = new ArrayBuffer(buffLen);
31
32    const h = {
33        dataView: new DataView(buff),
34        offset: 0
35    };
36
37    // writeUint32(h, witness.length);
38
39    for (let i=0; i<witness.length; i++) {
40        writeBigInt(h, witness[i]);
41    }
42
43    assert.equal(h.offset, buffLen);
44
45    return Buffer.from(buff);
46}
47
48module.exports = buildWitness;