plaintext-proof-data.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 = PlaintextProofTestData;
  13. /**
  14. * Provides the data needed by the plaintext zero-knowledge proof of knowledge
  15. * unit tests.
  16. */
  17. function PlaintextProofTestData() {
  18. var NUM_PLAINTEXT_ELEMENTS = 3;
  19. var PLAINTEXT_PROOF_STRING_DATA = 'Test Plaintext Proof Auxilary Data';
  20. var OTHER_PLAINTEXT_PROOF_STRING_DATA = PLAINTEXT_PROOF_STRING_DATA + '0';
  21. var NUM_PLAINTEXT_PROOF_PROGRESS_CHECKS = NUM_PLAINTEXT_ELEMENTS + 1;
  22. var elGamalEncrypter = elGamal.newService().newEncrypter();
  23. var commonTestData = new CommonTestData();
  24. var group_ = commonTestData.getGroup();
  25. var anotherGroup_ = commonTestData.getAnotherGroup();
  26. var publicKey_ =
  27. commonTestData.generateKeyPair(group_, NUM_PLAINTEXT_ELEMENTS).publicKey;
  28. var anotherPublicKey_;
  29. do {
  30. anotherPublicKey_ =
  31. commonTestData.generateKeyPair(group_, NUM_PLAINTEXT_ELEMENTS)
  32. .publicKey;
  33. } while (anotherPublicKey_.elements.equals(publicKey_.elements));
  34. var publicKeyWithLessElements_ =
  35. commonTestData.generateKeyPair(group_, (NUM_PLAINTEXT_ELEMENTS - 1))
  36. .publicKey;
  37. var publicKeyFromAnotherGroup_ =
  38. commonTestData.generateKeyPair(anotherGroup_, NUM_PLAINTEXT_ELEMENTS)
  39. .publicKey;
  40. var plaintext_ = commonTestData.generateRandomGroupElements(
  41. group_, NUM_PLAINTEXT_ELEMENTS);
  42. var anotherPlaintext_;
  43. do {
  44. anotherPlaintext_ = commonTestData.generateRandomGroupElements(
  45. group_, NUM_PLAINTEXT_ELEMENTS);
  46. } while (anotherPlaintext_.equals(plaintext_));
  47. var plaintextWithLessElements_ = commonTestData.generateRandomGroupElements(
  48. group_, (NUM_PLAINTEXT_ELEMENTS - 1));
  49. var plaintextFromAnotherGroup_ = commonTestData.generateRandomGroupElements(
  50. anotherGroup_, NUM_PLAINTEXT_ELEMENTS);
  51. var encryptedElements_ =
  52. elGamalEncrypter.init(publicKey_).encrypt(plaintext_, {saveSecret: true});
  53. var otherEncryptedElements_;
  54. do {
  55. otherEncryptedElements_ =
  56. elGamalEncrypter.init(publicKey_).encrypt(plaintext_, {
  57. saveSecret: true
  58. });
  59. } while (otherEncryptedElements_.secret.equals(encryptedElements_.secret) ||
  60. otherEncryptedElements_.phis.equals(encryptedElements_.phis));
  61. var encryptedElementsWithLessElements_ =
  62. elGamalEncrypter.init(publicKeyWithLessElements_)
  63. .encrypt(plaintextWithLessElements_, {saveSecret: true});
  64. var encryptedElementsFromAnotherGroup_ =
  65. elGamalEncrypter.init(publicKeyFromAnotherGroup_)
  66. .encrypt(plaintextFromAnotherGroup_, {saveSecret: true});
  67. this.getGroup = function() {
  68. return group_;
  69. };
  70. this.getAnotherGroup = function() {
  71. return anotherGroup_;
  72. };
  73. this.getPublicKey = function() {
  74. return publicKey_;
  75. };
  76. this.getAnotherPublicKey = function() {
  77. return anotherPublicKey_;
  78. };
  79. this.getPublicKeyWithLessElements = function() {
  80. return publicKeyWithLessElements_;
  81. };
  82. this.getPublicKeyFromAnotherGroup = function() {
  83. return publicKeyFromAnotherGroup_;
  84. };
  85. this.getPlaintext = function() {
  86. return plaintext_;
  87. };
  88. this.getAnotherPlaintext = function() {
  89. return anotherPlaintext_;
  90. };
  91. this.getPlaintextWithLessElements = function() {
  92. return plaintextWithLessElements_;
  93. };
  94. this.getPlaintextFromAnotherGroup = function() {
  95. return plaintextFromAnotherGroup_;
  96. };
  97. this.getEncryptedElements = function() {
  98. return encryptedElements_;
  99. };
  100. this.getOtherEncryptedElements = function() {
  101. return otherEncryptedElements_;
  102. };
  103. this.getEncryptedElementsWithLessElements = function() {
  104. return encryptedElementsWithLessElements_;
  105. };
  106. this.getEncryptedElementsFromAnotherGroup = function() {
  107. return encryptedElementsFromAnotherGroup_;
  108. };
  109. this.getStringData = function() {
  110. return PLAINTEXT_PROOF_STRING_DATA;
  111. };
  112. this.getOtherStringData = function() {
  113. return OTHER_PLAINTEXT_PROOF_STRING_DATA;
  114. };
  115. this.getNumProgressChecks = function() {
  116. return NUM_PLAINTEXT_PROOF_PROGRESS_CHECKS;
  117. };
  118. }