simple-plaintext-equality-proof-data.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 CommonTestData = require('./common-data');
  11. var elGamal = require('scytl-elgamal');
  12. module.exports = SimplePlaintextEqualityProofTestData;
  13. /**
  14. * Provides the data needed by the simple plaintext equality zero-knowledge
  15. * proof of knowledge unit tests.
  16. */
  17. function SimplePlaintextEqualityProofTestData() {
  18. var NUM_SIMPLE_PLAINTEXT_ELEMENTS = 4;
  19. var SIMPLE_PLAINTEXT_EQUALITY_PROOF_STRING_DATA =
  20. 'Test Simple Plaintext Equality Proof Auxilary Data';
  21. var OTHER_SIMPLE_PLAINTEXT_EQUALITY_PROOF_STRING_DATA =
  22. SIMPLE_PLAINTEXT_EQUALITY_PROOF_STRING_DATA + '0';
  23. var NUM_SIMPLE_PLAINTEXT_EQUALITY_PROOF_PROGRESS_CHECKS =
  24. NUM_SIMPLE_PLAINTEXT_ELEMENTS + 1;
  25. var elGamalEncrypter = elGamal.newService().newEncrypter();
  26. var commonTestData = new CommonTestData();
  27. var group_ = commonTestData.getGroup();
  28. var anotherGroup_ = commonTestData.getAnotherGroup();
  29. var publicKey =
  30. commonTestData.generateKeyPair(group_, NUM_SIMPLE_PLAINTEXT_ELEMENTS)
  31. .publicKey;
  32. var primaryPublicKey_ = commonTestData.getSubPublicKey(
  33. group_, publicKey, 0, (NUM_SIMPLE_PLAINTEXT_ELEMENTS / 2));
  34. var secondaryPublicKey_ = commonTestData.getSubPublicKey(
  35. group_, publicKey, (NUM_SIMPLE_PLAINTEXT_ELEMENTS / 2),
  36. NUM_SIMPLE_PLAINTEXT_ELEMENTS);
  37. var anotherPrimaryPublicKey_;
  38. do {
  39. anotherPrimaryPublicKey_ =
  40. commonTestData
  41. .generateKeyPair(group_, NUM_SIMPLE_PLAINTEXT_ELEMENTS / 2)
  42. .publicKey;
  43. } while (
  44. anotherPrimaryPublicKey_.elements.equals(primaryPublicKey_.elements));
  45. var anotherSecondaryPublicKey_;
  46. do {
  47. anotherSecondaryPublicKey_ =
  48. commonTestData
  49. .generateKeyPair(group_, NUM_SIMPLE_PLAINTEXT_ELEMENTS / 2)
  50. .publicKey;
  51. } while (
  52. anotherSecondaryPublicKey_.elements.equals(secondaryPublicKey_.elements));
  53. var primaryPublicKeyWithLessElements_ = commonTestData.getSubPublicKey(
  54. group_, publicKey, 1, (NUM_SIMPLE_PLAINTEXT_ELEMENTS / 2));
  55. var secondaryPublicKeyWithLessElements_ = commonTestData.getSubPublicKey(
  56. group_, publicKey, ((NUM_SIMPLE_PLAINTEXT_ELEMENTS / 2) + 1),
  57. NUM_SIMPLE_PLAINTEXT_ELEMENTS);
  58. var publicKeyFromAnotherGroup =
  59. commonTestData
  60. .generateKeyPair(anotherGroup_, NUM_SIMPLE_PLAINTEXT_ELEMENTS)
  61. .publicKey;
  62. var primaryPublicKeyFromAnotherGroup_ = commonTestData.getSubPublicKey(
  63. anotherGroup_, publicKeyFromAnotherGroup, 0,
  64. (NUM_SIMPLE_PLAINTEXT_ELEMENTS / 2));
  65. var secondaryPublicKeyFromAnotherGroup_ = commonTestData.getSubPublicKey(
  66. anotherGroup_, publicKeyFromAnotherGroup,
  67. (NUM_SIMPLE_PLAINTEXT_ELEMENTS / 2), NUM_SIMPLE_PLAINTEXT_ELEMENTS);
  68. var simplePlaintext_ = commonTestData.generateSimplePlaintext(
  69. group_, NUM_SIMPLE_PLAINTEXT_ELEMENTS);
  70. var simplePlaintextFromAnotherGroup = commonTestData.generateSimplePlaintext(
  71. anotherGroup_, NUM_SIMPLE_PLAINTEXT_ELEMENTS);
  72. var encryptedElements = elGamalEncrypter.init(publicKey).encrypt(
  73. simplePlaintext_, {saveSecret: true});
  74. var primaryEncryptedElements_ = commonTestData.getSubEncryptedElements(
  75. encryptedElements, 0, (NUM_SIMPLE_PLAINTEXT_ELEMENTS / 2));
  76. var secondaryEncryptedElements_ = commonTestData.getSubEncryptedElements(
  77. encryptedElements, (NUM_SIMPLE_PLAINTEXT_ELEMENTS / 2),
  78. NUM_SIMPLE_PLAINTEXT_ELEMENTS);
  79. var subPlaintext;
  80. var otherPrimaryEncryptedElements_;
  81. do {
  82. subPlaintext = commonTestData.generateSimplePlaintext(
  83. group_, (NUM_SIMPLE_PLAINTEXT_ELEMENTS / 2));
  84. otherPrimaryEncryptedElements_ =
  85. elGamalEncrypter.init(primaryPublicKey_).encrypt(subPlaintext, {
  86. saveSecret: true
  87. });
  88. } while (subPlaintext.equals(
  89. simplePlaintext_.slice(0, (NUM_SIMPLE_PLAINTEXT_ELEMENTS / 2))));
  90. var otherSecondaryEncryptedElements_;
  91. do {
  92. subPlaintext = commonTestData.generateSimplePlaintext(
  93. group_, (NUM_SIMPLE_PLAINTEXT_ELEMENTS / 2));
  94. otherSecondaryEncryptedElements_ =
  95. elGamalEncrypter.init(secondaryPublicKey_).encrypt(subPlaintext, {
  96. saveSecret: true
  97. });
  98. } while (subPlaintext.equals(simplePlaintext_.slice(
  99. (NUM_SIMPLE_PLAINTEXT_ELEMENTS / 2), NUM_SIMPLE_PLAINTEXT_ELEMENTS)));
  100. var primaryEncryptedElementsWithLessElements_ =
  101. commonTestData.getSubEncryptedElements(
  102. encryptedElements, 1, (NUM_SIMPLE_PLAINTEXT_ELEMENTS / 2));
  103. var secondaryEncryptedElementsWithLessElements_ =
  104. commonTestData.getSubEncryptedElements(
  105. encryptedElements, ((NUM_SIMPLE_PLAINTEXT_ELEMENTS / 2) + 1),
  106. NUM_SIMPLE_PLAINTEXT_ELEMENTS);
  107. encryptedElements =
  108. elGamalEncrypter.init(publicKeyFromAnotherGroup)
  109. .encrypt(simplePlaintextFromAnotherGroup, {saveSecret: true});
  110. var primaryEncryptedElementsFromAnotherGroup_ =
  111. commonTestData.getSubEncryptedElements(
  112. encryptedElements, 0, (NUM_SIMPLE_PLAINTEXT_ELEMENTS / 2));
  113. var secondaryEncryptedElementsFromAnotherGroup_ =
  114. commonTestData.getSubEncryptedElements(
  115. encryptedElements, (NUM_SIMPLE_PLAINTEXT_ELEMENTS / 2),
  116. NUM_SIMPLE_PLAINTEXT_ELEMENTS);
  117. var nonSimplePlaintext_ = commonTestData.generateNonSimplePlaintext(
  118. group_, NUM_SIMPLE_PLAINTEXT_ELEMENTS);
  119. encryptedElements = elGamalEncrypter.init(publicKey).encrypt(
  120. nonSimplePlaintext_, {saveSecret: true});
  121. var primaryEncryptedElementsForNonSimplePlaintext_ =
  122. commonTestData.getSubEncryptedElements(
  123. encryptedElements, 0, (NUM_SIMPLE_PLAINTEXT_ELEMENTS / 2));
  124. var secondaryEncryptedElementsForNonSimplePlaintext_ =
  125. commonTestData.getSubEncryptedElements(
  126. encryptedElements, (NUM_SIMPLE_PLAINTEXT_ELEMENTS / 2),
  127. NUM_SIMPLE_PLAINTEXT_ELEMENTS);
  128. this.getGroup = function() {
  129. return group_;
  130. };
  131. this.getAnotherGroup = function() {
  132. return anotherGroup_;
  133. };
  134. this.getPrimaryPublicKey = function() {
  135. return primaryPublicKey_;
  136. };
  137. this.getSecondaryPublicKey = function() {
  138. return secondaryPublicKey_;
  139. };
  140. this.getAnotherPrimaryPublicKey = function() {
  141. return anotherPrimaryPublicKey_;
  142. };
  143. this.getAnotherSecondaryPublicKey = function() {
  144. return anotherSecondaryPublicKey_;
  145. };
  146. this.getPrimaryPublicKeyWithLessElements = function() {
  147. return primaryPublicKeyWithLessElements_;
  148. };
  149. this.getSecondaryPublicKeyWithLessElements = function() {
  150. return secondaryPublicKeyWithLessElements_;
  151. };
  152. this.getPrimaryPublicKeyFromAnotherGroup = function() {
  153. return primaryPublicKeyFromAnotherGroup_;
  154. };
  155. this.getSecondaryPublicKeyFromAnotherGroup = function() {
  156. return secondaryPublicKeyFromAnotherGroup_;
  157. };
  158. this.getPrimaryEncryptedElements = function() {
  159. return primaryEncryptedElements_;
  160. };
  161. this.getSecondaryEncryptedElements = function() {
  162. return secondaryEncryptedElements_;
  163. };
  164. this.getOtherPrimaryEncryptedElements = function() {
  165. return otherPrimaryEncryptedElements_;
  166. };
  167. this.getOtherSecondaryEncryptedElements = function() {
  168. return otherSecondaryEncryptedElements_;
  169. };
  170. this.getPrimaryEncryptedElementsWithLessElements = function() {
  171. return primaryEncryptedElementsWithLessElements_;
  172. };
  173. this.getSecondaryEncryptedElementsWithLessElements = function() {
  174. return secondaryEncryptedElementsWithLessElements_;
  175. };
  176. this.getPrimaryEncryptedElementsFromAnotherGroup = function() {
  177. return primaryEncryptedElementsFromAnotherGroup_;
  178. };
  179. this.getSecondaryEncryptedElementsFromAnotherGroup = function() {
  180. return secondaryEncryptedElementsFromAnotherGroup_;
  181. };
  182. this.getStringData = function() {
  183. return SIMPLE_PLAINTEXT_EQUALITY_PROOF_STRING_DATA;
  184. };
  185. this.getOtherStringData = function() {
  186. return OTHER_SIMPLE_PLAINTEXT_EQUALITY_PROOF_STRING_DATA;
  187. };
  188. this.getNonSimplePlaintext = function() {
  189. return nonSimplePlaintext_;
  190. };
  191. this.getPrimaryEncryptedElementsForNonSimplePlaintext = function() {
  192. return primaryEncryptedElementsForNonSimplePlaintext_;
  193. };
  194. this.getSecondaryEncryptedElementsForNonSimplePlaintext = function() {
  195. return secondaryEncryptedElementsForNonSimplePlaintext_;
  196. };
  197. this.getNumProgressChecks = function() {
  198. return NUM_SIMPLE_PLAINTEXT_EQUALITY_PROOF_PROGRESS_CHECKS;
  199. };
  200. }