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.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) {
 36        this.ctx = ctx;
 37        this.offset = offset;
 38        this.prefix = prefix;
 39        this.cur = 0;
 40    }
 41
 42    next() {
 43        this.cur = 1;
 44
 45        return this.current();
 46    }
 47
 48    current() {
 49        if (this.cur == 0) {
 50            return {offset: this.offset, name: this.prefix};
 51        }
 52    }
 53}
 54
 55class ArrayIt {
 56    constructor (ctx, type, sizes, offset, prefix) {
 57        if (sizes.length == 0) {
 58            if (type == "S") {
 59                return new SignalIt(ctx, offset, prefix);
 60            } else {
 61                return new ComponentIt(ctx, offset, prefix);
 62            }
 63        }
 64
 65        this.ctx = ctx;
 66        this.type = type;
 67        this.sizes = sizes;
 68        this.offset = offset;
 69        this.prefix = prefix;
 70
 71
 72
 73        this.subIt = null;
 74        this.cur = 0;
 75
 76        this.subArrSize = 1;
 77
 78        for (let i=1; i<sizes.length; i++) {
 79            this.subArrSize *= sizes[i];
 80        }
 81
 82        this._loadSubIt();
 83
 84
 85    }
 86
 87    _loadSubIt() {
 88        if (this.cur < this.sizes[0]) {
 89            this.subIt = new ArrayIt(this.ctx, this.type, this.sizes.slice(1), this.offset + this.cur*this.subArrSize, this.prefix + "[" + this.cur + "]");
 90        }
 91    }
 92
 93    next() {
 94        if (this.subIt) {
 95            const res = this.subIt.next();
 96            if (res == null) {
 97                this.subIt = null;
 98                this.cur++;
 99                this._loadSubIt();
100            }
101        }
102
103        return this.current();
104
105    }
106
107    current() {
108        if (this.subIt) {
109            return this.subIt.current();
110        } else {
111            return null;
112        }
113    }
114}
115
116class ComponentIt {
117    constructor (ctx, idxComponent, prefix) {
118        this.ctx = ctx;
119        this.idxComponent = idxComponent;
120        this.prefix = prefix;
121        this.names = Object.keys(ctx.components[idxComponent].names.o);
122
123        this.subIt = null;
124        this.cur = 0;
125        this._loadSubIt();
126
127    }
128
129    _loadSubIt() {
130        if (this.cur < this.names.length) {
131            const entrie = this.ctx.components[this.idxComponent].names.o[this.names[this.cur]];
132            this.subIt = new ArrayIt(this.ctx, entrie.type, entrie.sizes, entrie.offset, this.prefix + "." + this.names[this.cur]);
133        }
134    }
135
136    next() {
137        if (this.subIt) {
138            const res = this.subIt.next();
139            if (res == null) {
140                this.subIt = null;
141                this.cur++;
142                this._loadSubIt();
143            }
144        }
145
146        return this.current();
147    }
148
149    current() {
150        if (this.subIt) {
151            return this.subIt.current();
152        } else {
153            return null;
154        }
155    }
156}