You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
2.1 KiB

  1. const bigInt=require("big-integer");
  2. const path = require("path");
  3. const util = require("util");
  4. const renderFile = util.promisify(require("ejs").renderFile);
  5. const runningAsScript = !module.parent;
  6. class ZqBuilder {
  7. constructor(q, name) {
  8. const self = this;
  9. this.q=bigInt(q);
  10. this.n64 = Math.floor((this.q.bitLength() - 1) / 64)+1;
  11. this.name = name;
  12. this.bigInt = bigInt;
  13. this.lastTmp=0;
  14. this.global = {};
  15. this.global.tmpLabel = function() {
  16. self.lastTmp++;
  17. return "tmp"+self.lastTmp;
  18. };
  19. }
  20. constantElement(v) {
  21. let S = "";
  22. const mask = bigInt("FFFFFFFFFFFFFFFF", 16);
  23. for (let i=0; i<this.n64; i++) {
  24. if (i>0) S = S+",";
  25. let shex = v.shiftRight(i*64).and(mask).toString(16);
  26. while (shex.length <16) shex = "0" + shex;
  27. S = S + "0x" + shex;
  28. }
  29. return S;
  30. }
  31. }
  32. async function buildField(q, name) {
  33. const builder = new ZqBuilder(q, name);
  34. const asm = await renderFile(path.join(__dirname, "fr.asm.ejs"), builder);
  35. const c = await renderFile(path.join(__dirname, "fr.c.ejs"), builder);
  36. const h = await renderFile(path.join(__dirname, "fr.h.ejs"), builder);
  37. return {asm: asm, h: h, c: c};
  38. }
  39. if (runningAsScript) {
  40. const fs = require("fs");
  41. var argv = require("yargs")
  42. .usage("Usage: $0 -q [primeNum] -n [name] -oc [out .c file] -oh [out .h file]")
  43. .demandOption(["q","n"])
  44. .alias("q", "prime")
  45. .alias("n", "name")
  46. .argv;
  47. const q = bigInt(argv.q);
  48. const asmFileName = (argv.oc) ? argv.oc : argv.name.toLowerCase() + ".asm";
  49. const hFileName = (argv.oc) ? argv.oc : argv.name.toLowerCase() + ".h";
  50. const cFileName = (argv.oc) ? argv.oc : argv.name.toLowerCase() + ".c";
  51. buildField(q, argv.name).then( (res) => {
  52. fs.writeFileSync(asmFileName, res.asm, "utf8");
  53. fs.writeFileSync(hFileName, res.h, "utf8");
  54. fs.writeFileSync(cFileName, res.c, "utf8");
  55. });
  56. } else {
  57. module.exports = buildField;
  58. }