plaintext-equality-proof-data.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 = PlaintextEqualityProofTestData;
  13. /**
  14. * Provides the data needed by the plaintext equality zero-knowledge proof of
  15. * knowledge unit tests.
  16. */
  17. function PlaintextEqualityProofTestData() {
  18. var NUM_PLAINTEXT_ELEMENTS = 3;
  19. var PLAINTEXT_EQUALITY_PROOF_STRING_DATA =
  20. 'Test Plaintext Equality Proof Auxilary Data';
  21. var OTHER_PLAINTEXT_EQUALITY_PROOF_STRING_DATA =
  22. PLAINTEXT_EQUALITY_PROOF_STRING_DATA + '0';
  23. var NUM_PLAINTEXT_EQUALITY_PROOF_PROGRESS_CHECKS =
  24. (NUM_PLAINTEXT_ELEMENTS * 2) + 2;
  25. var elGamalEncrypter = elGamal.newService().newEncrypter();
  26. var commonTestData = new CommonTestData();
  27. var group_ = commonTestData.getGroup();
  28. var anotherGroup_ = commonTestData.getAnotherGroup();
  29. var primaryPublicKey_ =
  30. commonTestData.generateKeyPair(group_, NUM_PLAINTEXT_ELEMENTS).publicKey;
  31. var secondaryPublicKey_ =
  32. commonTestData.generateKeyPair(group_, NUM_PLAINTEXT_ELEMENTS).publicKey;
  33. var anotherPrimaryPublicKey_;
  34. do {
  35. anotherPrimaryPublicKey_ =
  36. commonTestData.generateKeyPair(group_, NUM_PLAINTEXT_ELEMENTS)
  37. .publicKey;
  38. } while (
  39. anotherPrimaryPublicKey_.elements.equals(primaryPublicKey_.elements));
  40. var anotherSecondaryPublicKey_;
  41. do {
  42. anotherSecondaryPublicKey_ =
  43. commonTestData.generateKeyPair(group_, NUM_PLAINTEXT_ELEMENTS)
  44. .publicKey;
  45. } while (
  46. anotherSecondaryPublicKey_.elements.equals(secondaryPublicKey_.elements));
  47. var publicKeyWithLessElements_ =
  48. commonTestData.generateKeyPair(group_, (NUM_PLAINTEXT_ELEMENTS - 1))
  49. .publicKey;
  50. var publicKeyFromAnotherGroup_ =
  51. commonTestData.generateKeyPair(anotherGroup_, NUM_PLAINTEXT_ELEMENTS)
  52. .publicKey;
  53. var plaintext = commonTestData.generateRandomGroupElements(
  54. group_, NUM_PLAINTEXT_ELEMENTS);
  55. var anotherPlaintext;
  56. do {
  57. anotherPlaintext = commonTestData.generateRandomGroupElements(
  58. group_, NUM_PLAINTEXT_ELEMENTS);
  59. } while (anotherPlaintext.equals(plaintext));
  60. var plaintextWithLessElements = commonTestData.generateRandomGroupElements(
  61. group_, (NUM_PLAINTEXT_ELEMENTS - 1));
  62. var plaintextFromAnotherGroup = commonTestData.generateRandomGroupElements(
  63. anotherGroup_, NUM_PLAINTEXT_ELEMENTS);
  64. var primaryEncryptedElements_ =
  65. elGamalEncrypter.init(primaryPublicKey_).encrypt(plaintext, {
  66. saveSecret: true
  67. });
  68. var secondaryEncryptedElements_ =
  69. elGamalEncrypter.init(secondaryPublicKey_).encrypt(plaintext, {
  70. saveSecret: true
  71. });
  72. var otherPrimaryEncryptedElements_;
  73. do {
  74. otherPrimaryEncryptedElements_ =
  75. elGamalEncrypter.init(primaryPublicKey_).encrypt(plaintext, {
  76. saveSecret: true
  77. });
  78. } while (otherPrimaryEncryptedElements_.secret.equals(
  79. primaryEncryptedElements_.secret) ||
  80. otherPrimaryEncryptedElements_.phis.equals(
  81. primaryEncryptedElements_.phis));
  82. var otherSecondaryEncryptedElements_;
  83. do {
  84. otherSecondaryEncryptedElements_ =
  85. elGamalEncrypter.init(secondaryPublicKey_).encrypt(plaintext, {
  86. saveSecret: true
  87. });
  88. } while (otherSecondaryEncryptedElements_.secret.equals(
  89. secondaryEncryptedElements_.secret) ||
  90. otherSecondaryEncryptedElements_.phis.equals(
  91. secondaryEncryptedElements_.phis));
  92. var encryptedElementsWithLessElements_ =
  93. elGamalEncrypter.init(publicKeyWithLessElements_)
  94. .encrypt(plaintextWithLessElements, {saveSecret: true});
  95. var encryptedElementsFromAnotherGroup_ =
  96. elGamalEncrypter.init(publicKeyFromAnotherGroup_)
  97. .encrypt(plaintextFromAnotherGroup, {saveSecret: true});
  98. this.getGroup = function() {
  99. return group_;
  100. };
  101. this.getAnotherGroup = function() {
  102. return anotherGroup_;
  103. };
  104. this.getPrimaryPublicKey = function() {
  105. return primaryPublicKey_;
  106. };
  107. this.getSecondaryPublicKey = function() {
  108. return secondaryPublicKey_;
  109. };
  110. this.getAnotherPrimaryPublicKey = function() {
  111. return anotherPrimaryPublicKey_;
  112. };
  113. this.getAnotherSecondaryPublicKey = function() {
  114. return anotherSecondaryPublicKey_;
  115. };
  116. this.getPublicKeyWithLessElements = function() {
  117. return publicKeyWithLessElements_;
  118. };
  119. this.getPublicKeyFromAnotherGroup = function() {
  120. return publicKeyFromAnotherGroup_;
  121. };
  122. this.getPrimaryEncryptedElements = function() {
  123. return primaryEncryptedElements_;
  124. };
  125. this.getSecondaryEncryptedElements = function() {
  126. return secondaryEncryptedElements_;
  127. };
  128. this.getOtherPrimaryEncryptedElements = function() {
  129. return otherPrimaryEncryptedElements_;
  130. };
  131. this.getOtherSecondaryEncryptedElements = function() {
  132. return otherSecondaryEncryptedElements_;
  133. };
  134. this.getEncryptedElementsWithLessElements = function() {
  135. return encryptedElementsWithLessElements_;
  136. };
  137. this.getEncryptedElementsFromAnotherGroup = function() {
  138. return encryptedElementsFromAnotherGroup_;
  139. };
  140. this.getStringData = function() {
  141. return PLAINTEXT_EQUALITY_PROOF_STRING_DATA;
  142. };
  143. this.getOtherStringData = function() {
  144. return OTHER_PLAINTEXT_EQUALITY_PROOF_STRING_DATA;
  145. };
  146. this.getNumProgressChecks = function() {
  147. return NUM_PLAINTEXT_EQUALITY_PROOF_PROGRESS_CHECKS;
  148. };
  149. }