exponent.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright 2018 Scytl Secure Electronic Voting SA
  3. *
  4. * All rights reserved
  5. *
  6. * See our extended copyright notice in *file 'Copyright.txt' which is part of this source code package
  7. */
  8. /* jshint node:true */
  9. 'use strict';
  10. var validator = require('./input-validator');
  11. var codec = require('scytl-codec');
  12. module.exports = Exponent;
  13. /**
  14. * @class Exponent
  15. * @classdesc Encapsulates an exponent. To instantiate this object, use the
  16. * method {@link MathematicalService.newExponent}.
  17. * @property {forge.jsbn.BigInteger} q The order of the exponent's associated
  18. * @property {forge.jsbn.BigInteger} value The value of the exponent.
  19. */
  20. function Exponent(q, value) {
  21. this.q = q;
  22. this.value = value.mod(q);
  23. Object.freeze(this);
  24. }
  25. Exponent.prototype = {
  26. /**
  27. * Adds this exponent to the exponent provided as input, using the formula:
  28. * <p>
  29. * <code>(this + exponent) mod q</code>
  30. *
  31. * @function add
  32. * @memberof Exponent
  33. * @param {Exponent}
  34. * exponent The exponent to be added to this exponent.
  35. * @returns {Exponent} The result of the addition operation.
  36. * @throws {Error}
  37. * If the input data validation fails.
  38. */
  39. add: function(exponent) {
  40. checkExponent(exponent, 'exponent to add to this exponent', this.q);
  41. var newValue = this.value.add(exponent.value).mod(this.q);
  42. return new Exponent(this.q, newValue);
  43. },
  44. /**
  45. * Subtracts the exponent provided as input from this exponent, using the
  46. * formula:
  47. * <p>
  48. * <code>(this - exponent) mod q</code>
  49. *
  50. * @function subtract
  51. * @memberof Exponent
  52. * @param {Exponent}
  53. * exponent The exponent to be subtracted from this exponent.
  54. * @returns {Exponent} The result of the subtraction operation.
  55. * @throws {Error}
  56. * If the input data validation fails.
  57. */
  58. subtract: function(exponent) {
  59. checkExponent(exponent, 'exponent to subtract from this exponent', this.q);
  60. var newValue = this.value.subtract(exponent.value).mod(this.q);
  61. return new Exponent(this.q, newValue);
  62. },
  63. /**
  64. * Multiplies this exponent with the exponent provided as input, using the
  65. * formula:
  66. * <p>
  67. * <code>(this * exponent) mod q</code>
  68. *
  69. * @function multiply
  70. * @memberof Exponent
  71. * @param {Exponent}
  72. * exponent The exponent to be multiplied with this exponent.
  73. * @returns {Exponent} The result of the multiplication operation.
  74. * @throws {Error}
  75. * If the input data validation fails.
  76. */
  77. multiply: function(exponent) {
  78. checkExponent(exponent, 'exponent to multiply with this exponent', this.q);
  79. var newValue = this.value.multiply(exponent.value).mod(this.q);
  80. return new Exponent(this.q, newValue);
  81. },
  82. /**
  83. * Negates this exponent, using the formula:
  84. * <p>
  85. * <code>(-this) mod q</code>
  86. *
  87. * @function negate
  88. * @memberof Exponent
  89. * @returns {Exponent} The result of the negation operation.
  90. */
  91. negate: function() {
  92. return new Exponent(this.q, this.value.negate().mod(this.q));
  93. },
  94. /**
  95. * Checks if this exponent is equal to the exponent provided as input.
  96. * <p>
  97. * The exponents are considered equal if:
  98. * <ul>
  99. * <li>They are associated with the same Zp subgroup.</li>
  100. * <li>They have the same value.</li>
  101. * </ul>
  102. *
  103. * @function equals
  104. * @memberof Exponent
  105. * @param {Exponent}
  106. * exponent The exponent to compare with this exponent.
  107. * @returns {boolean} True if the equality holds, false otherwise.
  108. * @throws {Error}
  109. * If the input data validation fails.
  110. */
  111. equals: function(exponent) {
  112. checkExponent(exponent, 'exponent to compare with this exponent', this.q);
  113. if (exponent.q.equals(this.q) && exponent.value.equals(this.value)) {
  114. return true;
  115. } else {
  116. return false;
  117. }
  118. },
  119. /**
  120. * Serializes this object into a JSON string representation.
  121. * <p>
  122. * <b>IMPORTANT:</b> This serialization must be exactly the same as the
  123. * corresponding serialization in the library <code>cryptoLib</code>,
  124. * implemented in Java, since the two libraries are expected to communicate
  125. * with each other via these serializations.
  126. *
  127. * @function toJson
  128. * @memberof Exponent
  129. * @returns {string} The JSON string representation of this object.
  130. */
  131. toJson: function() {
  132. return JSON.stringify({
  133. exponent: {
  134. q: codec.base64Encode(this.q),
  135. value: codec.base64Encode(this.value)
  136. }
  137. });
  138. }
  139. };
  140. function checkExponent(exponent, label, q) {
  141. validator.checkExponent(exponent, label, q);
  142. validator.checkIsInstanceOf(exponent, Exponent, 'Exponent', label);
  143. }