pedersen.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. try {
  2. /* Import core library. */
  3. const Pedersen = require('../libs/pedersen')
  4. /* Initialize p. */
  5. const p = '925f15d93a513b441a78826069b4580e3ee37fc5'
  6. /* Initialize g (Generator). */
  7. const g = '959144013c88c9782d5edd2d12f54885aa4ba687'
  8. /* Initialize Pedersen. */
  9. const pederson = new Pedersen(p, g)
  10. /* Initialize secrets. */
  11. const secrets = {}
  12. /* Initialize i (counter). */
  13. let i = 0
  14. /* Loop 100 times. */
  15. while (i < 100) {
  16. /* Create new Pedersen secret. */
  17. const secret = pederson.newSecret()
  18. /* Validate Pedersen secret. */
  19. if (secret.length !== 40) {
  20. throw new Error('Generated invalid key.')
  21. }
  22. /* Set secret flag. */
  23. secrets[secret] = true
  24. /* Increment counter. */
  25. i++
  26. }
  27. /* Validate secrets. */
  28. if (Object.keys(secrets).length !== 100) {
  29. throw new Error('Basic secret collision test failed.')
  30. }
  31. /* Initialize secret. */
  32. const secret = '1184c47884aeead9816654a63d4209d6e8e906e29'
  33. /* Initialize test A. */
  34. const testA = pederson
  35. .commit('1', secret, 'e93c58e6f7f3f4b6f6f0e55f3a4191b87d58b7b1')
  36. console.log('\nTest A', testA) // eslint-disable-line no-console
  37. /* Set assertion A. */
  38. const assertionA = [
  39. '4b7680d6262cea707175d55e862a09ba71b55655',
  40. 'e93c58e6f7f3f4b6f6f0e55f3a4191b87d58b7b1'
  41. ]
  42. /* Validate test A. */
  43. if (testA.toString() !== assertionA.toString()) {
  44. throw new Error('Arbitrary signature test A failed.')
  45. }
  46. /* Initialize test B. */
  47. const testB = pederson
  48. .commit('2', secret, 'ba1303c4f29bd959f585dc0dcfb3dbd0cebecd48')
  49. console.log('\nTest B', testB) // eslint-disable-line no-console
  50. /* Set assertion B. */
  51. const assertionB = [
  52. 'ff5ad287a51bffddedaf342dfa685b0cf82286ce',
  53. 'ba1303c4f29bd959f585dc0dcfb3dbd0cebecd48'
  54. ]
  55. /* Validate test B. */
  56. if (testB.toString() !== assertionB.toString()) {
  57. throw new Error('Arbitrary signature test B failed.')
  58. }
  59. /* Initialize test C. */
  60. const testC = pederson.combine([testA, testB])
  61. console.log('\nTest C', testC) // eslint-disable-line no-console
  62. /* Set assertion C. */
  63. const assertionC = [
  64. '71e6ef28ea611f23d2240e4a786edc14611c96a3',
  65. '1a34f5cabea8fce10ec76c16d09f56d894c1784f9'
  66. ]
  67. /* Validate test C (combined test A and test B). */
  68. if (testC.toString() !== assertionC.toString()) {
  69. throw new Error('Arbitrary signature combination failed.')
  70. }
  71. /* Perform verification for test A. */
  72. if (!pederson.verify('1', [testA], secret)) {
  73. throw new Error('Arbitrary verification test A failed.')
  74. }
  75. /* Perform verification for test B. */
  76. if (!pederson.verify('2', [testB], secret)) {
  77. throw new Error('Arbitrary verification test B failed.')
  78. }
  79. /* Perform verification for test C (combination of test A and test B). */
  80. if (!pederson.verify('3', [pederson.combine([testA, testB])], secret)) {
  81. throw new Error('Combined verifcation test failed.')
  82. }
  83. /* Set message. */
  84. const message = 'aaaaaaaaaa'
  85. /* Initialize test D. */
  86. const testD = pederson
  87. .commit(message, secret, 'ba1303c4f29bd959f585dc0dcfb3dbd0cebecd48')
  88. console.log('\nTest D', testD) // eslint-disable-line no-console
  89. /* Perform verification for test D. */
  90. if (!pederson.verify(message, [testD], secret)) {
  91. throw new Error('Random verification test D failed.')
  92. }
  93. /* Set new Pedersen secret. */
  94. secret = pederson.newSecret()
  95. /* Initialize test E. */
  96. const testE = pederson.commit(message, secret)
  97. console.log('\nTest E', testE) // eslint-disable-line no-console
  98. /* Perform verification for test E. */
  99. if (!pederson.verify(message, [testE], secret)) {
  100. throw new Error('End-to-end test E failed.')
  101. }
  102. console.log('\n✅ All Pedersen tests passed!\n') // eslint-disable-line no-console
  103. } catch (error) {
  104. /* eslint-disable-next-line no-console */
  105. console.error('\n⚠️ Failed to test pedersen commitments:', error)
  106. throw new Error(error)
  107. }