All files / kyber/lib/group/nist curve.js

100% Statements 22/22
100% Branches 2/2
100% Functions 7/7
100% Lines 22/22
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98    1x 1x 1x 1x 1x                                                 3x 3x 3x 3x 15x 3x   12x   3x 3x 3x                 1x       48x                 19x                 85x                   18x                 73x       1x  
"use strict";
 
const Scalar = require("./scalar");
const Point = require("./point");
const crypto = require("crypto");
const elliptic = require("elliptic");
const BN = require("bn.js");
 
/*
 * @module group/nist/curve
 */
 
/**
 * Class Weierstrass defines the weierstrass form of
 * elliptic curves
 */
class Weierstrass {
  /**
   * Create a new Weierstrass Curve
   *
   * @param {object} config - Curve configuration
   * @param {String} config.name - Curve name
   * @param {(String|Uint8Array|BN.jsObject)} config.p - Order of the underlying field. Little Endian if string or Uint8Array.
   * @param {(String|Uint8Array|BN.jsObject)} config.a - Curve Parameter a. Little Endian if string or Uint8Array.
   * @param {(String|Uint8Array|BN.jsObject)} config.b - Curve Parameter b. Little Endian if string or Uint8Array.
   * @param {(String|Uint8Array|BN.jsObject)} config.n - Order of the base point. Little Endian if string or Uint8Array
   * @param {(String|Uint8Array|BN.jsObject)} config.gx - x coordinate of the base point. Little Endian if string or Uint8Array
   * @param {(String|Uint8Array|BN.jsObject)} config.gy - y coordinate of the base point. Little Endian if string or Uint8Array
   * @param {number} config.bitSize - the size of the underlying field.
   */
  constructor(config) {
    let { name, bitSize, gx, gy, ...options } = config;
    this.name = name;
    options["g"] = [new BN(gx, 16, "le"), new BN(gy, 16, "le")];
    for (let k in options) {
      if (k === "g") {
        continue;
      }
      options[k] = new BN(options[k], 16, "le");
    }
    this.curve = new elliptic.curve.short(options);
    this.bitSize = bitSize;
    this.redN = BN.red(options.n);
  }
 
  /**
   * Returns the name of the curve
   *
   * @returns {string}
   */
  string() {
    return this.name;
  }
 
  _coordLen() {
    return (this.bitSize + 7) >> 3;
  }
 
  /**
   * Returns the size in bytes of a scalar
   *
   * @returns {number}
   */
  scalarLen() {
    return (this.curve.n.bitLength() + 7) >> 3;
  }
 
  /**
   * Returns the size in bytes of a point
   *
   * @returns {module:group/nist/scalar~Scalar}
   */
  scalar() {
    return new Scalar(this, this.redN);
  }
 
  /**
   * Returns the size in bytes of a point
   *
   * @returns {number}
   */
  pointLen() {
    // ANSI X9.62: 1 header byte plus 2 coords
    return this._coordLen() * 2 + 1;
  }
 
  /**
   * Returns a new Point
   *
   * @returns {module:group/nist/point~Point}
   */
  point() {
    return new Point(this);
  }
}
 
module.exports = Weierstrass;