arnaucube
arnaucube/circomPublic Self-hosted repository
circom/src/buildsyms.js
src/buildsyms.jsRaw
  1const Readable = require("stream").Readable;
  2
  3module.exports = function buildSyms(ctx) {
  4    const  rs = Readable();
  5
  6    let it = new ComponentIt(ctx, 0, "main");
  7
  8    let counter = 0;
  9
 10    rs._read = function() {
 11        const actual = it.current();
 12        if (actual == null ) {
 13            rs.push(null);
 14            return;
 15        }
 16
 17
 18        let s=actual.offset;
 19        while (ctx.signals[s].e >= 0) s = ctx.signals[s].e;
 20        let wId = ctx.signals[s].id;
 21        if (typeof(wId) == "undefined") wId=-1;
 22        rs.push(`${actual.offset},${wId},${actual.cIdx},${actual.name}\n`);
 23
 24        it.next();
 25        counter ++;
 26        if ((ctx.verbose)&&(counter%10000 == 0)) console.log("Symbols saved: "+counter);
 27    };
 28
 29    return rs;
 30};
 31
 32
 33
 34class SignalIt {
 35    constructor (ctx, offset, prefix, cIdx) {
 36        this.ctx = ctx;
 37        this.offset = offset;
 38        this.prefix = prefix;
 39        this.cur = 0;
 40        this.cIdx = cIdx;
 41    }
 42
 43    next() {
 44        this.cur = 1;
 45
 46        return this.current();
 47    }
 48
 49    current() {
 50        if (this.cur == 0) {
 51            return {offset: this.offset, name: this.prefix, cIdx: this.cIdx};
 52        }
 53    }
 54}
 55
 56class ArrayIt {
 57    constructor (ctx, type, sizes, offset, prefix, cIdx) {
 58        if (sizes.length == 0) {
 59            if (type == "S") {
 60                return new SignalIt(ctx, offset, prefix, cIdx);
 61            } else {
 62                return new ComponentIt(ctx, offset, prefix);
 63            }
 64        }
 65
 66        this.ctx = ctx;
 67        this.type = type;
 68        this.sizes = sizes;
 69        this.offset = offset;
 70        this.prefix = prefix;
 71        this.cIdx = cIdx;
 72
 73
 74
 75        this.subIt = null;
 76        this.cur = 0;
 77
 78        this.subArrSize = 1;
 79
 80        for (let i=1; i<sizes.length; i++) {
 81            this.subArrSize *= sizes[i];
 82        }
 83
 84        this._loadSubIt();
 85
 86
 87    }
 88
 89    _loadSubIt() {
 90        if (this.cur < this.sizes[0]) {
 91            this.subIt = new ArrayIt(this.ctx, this.type, this.sizes.slice(1), this.offset + this.cur*this.subArrSize, this.prefix + "[" + this.cur + "]", this.cIdx);
 92        }
 93    }
 94
 95    next() {
 96        if (this.subIt) {
 97            const res = this.subIt.next();
 98            if (res == null) {
 99                this.subIt = null;
100                this.cur++;
101                this._loadSubIt();
102            }
103        }
104
105        return this.current();
106
107    }
108
109    current() {
110        if (this.subIt) {
111            return this.subIt.current();
112        } else {
113            return null;
114        }
115    }
116}
117
118class ComponentIt {
119    constructor (ctx, idxComponent, prefix) {
120        this.ctx = ctx;
121        this.idxComponent = idxComponent;
122        this.prefix = prefix;
123        this.names = Object.keys(ctx.components[idxComponent].names.o);
124
125        this.subIt = null;
126        this.cur = 0;
127        this._loadSubIt();
128
129    }
130
131    _loadSubIt() {
132        if (this.cur < this.names.length) {
133            const entrie = this.ctx.components[this.idxComponent].names.o[this.names[this.cur]];
134            this.subIt = new ArrayIt(this.ctx, entrie.type, entrie.sizes, entrie.offset, this.prefix + "." + this.names[this.cur], this.idxComponent);
135        }
136    }
137
138    next() {
139        if (this.subIt) {
140            const res = this.subIt.next();
141            if (res == null) {
142                this.subIt = null;
143                this.cur++;
144                this._loadSubIt();
145            }
146        }
147
148        return this.current();
149    }
150
151    current() {
152        if (this.subIt) {
153            return this.subIt.current();
154        } else {
155            return null;
156        }
157    }
158}