arnaucube
arnaucube/circomPublic Self-hosted repository
circom/c/buildasm/tester.cpp
c/buildasm/tester.cppRaw
  1#include <string>
  2#include <iostream>
  3
  4#include <regex>
  5#include <string>
  6#include <iostream>
  7#include <stdexcept>
  8#include <sstream>
  9
 10#include <stdio.h>      /* printf, NULL */
 11#include <stdlib.h>
 12#include <cassert>
 13
 14
 15#include "fr.h"
 16
 17
 18typedef void (*Func1)(PFrElement, PFrElement);
 19typedef void (*Func2)(PFrElement, PFrElement, PFrElement);
 20typedef void *FuncAny;
 21
 22typedef struct {
 23    FuncAny fn;
 24    int nOps;
 25} FunctionSpec;
 26
 27std::map<std::string, FunctionSpec> functions;
 28std::vector<FrElement> stack;
 29
 30void addFunction(std::string name, FuncAny f, int nOps) {
 31    FunctionSpec fs;
 32    fs.fn = f;
 33    fs.nOps = nOps;
 34    functions[name] = fs;
 35}
 36
 37void fillMap() {
 38    addFunction("add", (FuncAny)Fr_add, 2);
 39    addFunction("sub", (FuncAny)Fr_sub, 2);
 40    addFunction("neg", (FuncAny)Fr_neg, 1);
 41    addFunction("mul", (FuncAny)Fr_mul, 2);
 42    addFunction("band", (FuncAny)Fr_band, 2);
 43    addFunction("bor", (FuncAny)Fr_bor, 2);
 44    addFunction("bxor", (FuncAny)Fr_bxor, 2);
 45    addFunction("eq", (FuncAny)Fr_eq, 2);
 46    addFunction("neq", (FuncAny)Fr_neq, 2);
 47    addFunction("lt", (FuncAny)Fr_lt, 2);
 48    addFunction("gt", (FuncAny)Fr_gt, 2);
 49    addFunction("leq", (FuncAny)Fr_leq, 2);
 50    addFunction("geq", (FuncAny)Fr_geq, 2);
 51}
 52
 53u_int64_t readInt(std::string &s) {
 54    if (s.rfind("0x", 0) == 0) {
 55        return std::stoull(s.substr(2), 0, 16);
 56    } else {
 57        return std::stoull(s, 0, 10);
 58    }
 59}
 60
 61void pushNumber(std::vector<std::string> &v) {
 62    u_int64_t a;
 63    if ((v.size()<1) || (v.size() > (Fr_N64+1))) {
 64        printf("Invalid Size: %d  -  %d \n", v.size(), Fr_N64);
 65        throw std::runtime_error("Invalid number of parameters for number");
 66    }
 67    FrElement e;
 68    a = readInt(v[0]);
 69    *(u_int64_t *)(&e) = a;
 70    for (int i=0; i<Fr_N64; i++) {
 71        if (i+1 < v.size()) {
 72            a = readInt(v[i+1]);
 73        } else {
 74            a = 0;
 75        }
 76        e.longVal[i] = a;
 77    }
 78    stack.push_back(e);
 79}
 80
 81void callFunction(FunctionSpec fs) {
 82    if (stack.size() < fs.nOps) {
 83        throw new std::runtime_error("Not enough elements in stack");
 84    }
 85    if (fs.nOps == 1) {
 86        FrElement a = stack.back();
 87        stack.pop_back();
 88        FrElement c;
 89        (*(Func1)fs.fn)(&c, &a);
 90        stack.push_back(c);
 91    } else if (fs.nOps == 2) {
 92        FrElement b = stack.back();
 93        stack.pop_back();
 94        FrElement a = stack.back();
 95        stack.pop_back();
 96        FrElement c;
 97        (*(Func2)fs.fn)(&c, &a, &b);
 98        stack.push_back(c);
 99    } else {
100        assert(false);
101    }
102}
103
104void processLine(std::string &line) {
105    std::regex re("(\\s*[,;]\\s*)|\\s+"); // whitespace
106
107    std::sregex_token_iterator begin( line.begin(), line.end(), re ,-1);
108    std::sregex_token_iterator end;
109    std::vector<std::string> tokens;
110
111    std::copy(begin, end, std::back_inserter(tokens));
112
113    // Remove initial empty tokens
114    while ((tokens.size() > 0)&&(tokens[0] == "")) {
115        tokens.erase(tokens.begin());
116    }
117
118    // Empty lines are valid but are not processed
119    if (tokens.size() == 0) return;
120
121    auto search = functions.find(tokens[0]);
122    if (search == functions.end()) {
123        pushNumber(tokens);
124    } else {
125        if (tokens.size() != 1) {
126            throw std::runtime_error("Functions does not accept parameters");
127        }
128        callFunction(search->second);
129    }
130}
131
132int main(void)
133{
134    fillMap();
135    std::string line;
136    int i=0;
137    while (std::getline(std::cin, line)) {
138        processLine(line);
139        // if (i%1000 == 0) printf("%d\n", i);
140        // printf("%d\n", i);
141        i++;
142    }
143    // Print the elements in the stack
144    //
145    for (int i=0; i<stack.size(); i++) {
146        char *s;
147        s = Fr_element2str(&stack[i]);
148        printf("%s\n", s);
149        free(s);
150    }
151    return EXIT_SUCCESS;
152}
153
154
155
156/*
157
158
159#include <stdlib.h>
160#include <string.h>
161#include "fr.h"
162
163typedef void (*Func2)(PFrElement, PFrElement, PFrElement);
164
165typedef struct {
166    const char *fnName;
167    Func2 fn;
168} FN;
169
170
171#define NFN 2
172FN fns[NFN] = {
173    {"add", Fr_add},
174    {"mul", Fr_mul},
175};
176
177int main(int argc, char **argv) {
178
179    if (argc <= 1) {
180        fprintf( stderr, "invalid number of parameters");
181        return 1;
182    }
183
184    for (int i=0; i< NFN;i++) {
185        if (strcmp(argv[1], fns[i].fnName) == 0) {
186            if (argc != 4) {
187                fprintf( stderr, "invalid number of parameters");
188                return 1;
189            }
190            FrElement a;
191            FrElement b;
192
193            Fr_str2element(&a, argv[2]);
194            Fr_str2element(&b, argv[3]);
195            FrElement c;
196            fns[i].fn(&c, &a, &b);
197
198            char *s;
199            s = Fr_element2str(&c);
200            printf("%s", s);
201            free(s);
202            return 0;
203        }
204    }
205    fprintf( stderr, "invalid operation %s", argv[1]);
206    return 1;
207}
208
209*/