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.

72 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(label) {
  16. self.lastTmp++;
  17. label = label || "tmp";
  18. return label+"_"+self.lastTmp;
  19. };
  20. }
  21. constantElement(v) {
  22. let S = "";
  23. const mask = bigInt("FFFFFFFFFFFFFFFF", 16);
  24. for (let i=0; i<this.n64; i++) {
  25. if (i>0) S = S+",";
  26. let shex = v.shiftRight(i*64).and(mask).toString(16);
  27. while (shex.length <16) shex = "0" + shex;
  28. S = S + "0x" + shex;
  29. }
  30. return S;
  31. }
  32. }
  33. async function buildField(q, name) {
  34. const builder = new ZqBuilder(q, name);
  35. const asm = await renderFile(path.join(__dirname, "fr.asm.ejs"), builder);
  36. const c = await renderFile(path.join(__dirname, "fr.c.ejs"), builder);
  37. const h = await renderFile(path.join(__dirname, "fr.h.ejs"), builder);
  38. return {asm: asm, h: h, c: c};
  39. }
  40. if (runningAsScript) {
  41. const fs = require("fs");
  42. var argv = require("yargs")
  43. .usage("Usage: $0 -q [primeNum] -n [name] -oc [out .c file] -oh [out .h file]")
  44. .demandOption(["q","n"])
  45. .alias("q", "prime")
  46. .alias("n", "name")
  47. .argv;
  48. const q = bigInt(argv.q);
  49. const asmFileName = (argv.oc) ? argv.oc : argv.name.toLowerCase() + ".asm";
  50. const hFileName = (argv.oc) ? argv.oc : argv.name.toLowerCase() + ".h";
  51. const cFileName = (argv.oc) ? argv.oc : argv.name.toLowerCase() + ".c";
  52. buildField(q, argv.name).then( (res) => {
  53. fs.writeFileSync(asmFileName, res.asm, "utf8");
  54. fs.writeFileSync(hFileName, res.h, "utf8");
  55. fs.writeFileSync(cFileName, res.c, "utf8");
  56. });
  57. } else {
  58. module.exports = buildField;
  59. }