pedersen-fusion.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* Import core modules. */
  2. const crypto = require('crypto')
  3. const bigInt = require('big-integer')
  4. /* Set order. */
  5. const order = bigInt('fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141', 16)
  6. // const order = bigInt('115792089237316195423570985008687907852837564279074904382605163141518161494337')
  7. console.log('ORDER', order.toString())
  8. /* Set nonce. */
  9. const nonce = bigInt(0).add(42)
  10. console.log('NONCE', nonce.toString())
  11. try {
  12. /* Import core library. */
  13. const Pedersen = require('../libs/pedersen')
  14. /* Initialize p. */
  15. const p = crypto.randomBytes(32).toString('hex')
  16. /* Initialize g (Generator). */
  17. const g = crypto.randomBytes(32).toString('hex')
  18. const hP = bigInt(crypto.randomBytes(32).toString('hex'), 16)
  19. const hG = bigInt(crypto.randomBytes(32).toString('hex'), 16)
  20. const pow = hP.pow(5)
  21. const sq = pow.divide(hP).divide(hP).divide(hP).divide(hP)
  22. const MAX = bigInt('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 16).next()
  23. console.log('\nHUGE', hP, hG, pow, sq, hP.eq(sq), MAX)
  24. /* Initialize Pedersen. */
  25. const pederson = new Pedersen(p, g)
  26. /* Initialize secret. */
  27. const secret = crypto.randomBytes(32).toString('hex')
  28. /* Initialize test values. */
  29. // const aVal = bigInt(0x1337)
  30. // const bVal = bigInt(0x69)
  31. const aVal = bigInt(5)
  32. const bVal = bigInt(3)
  33. const dVal = bigInt(4)
  34. const fVal = bigInt(-7)
  35. console.log( // eslint-disable-line no-console
  36. '\n\t Test values',
  37. '\n\t----------------------------------------',
  38. '\n\t P-value :', p,
  39. '\n\t G-value :', g,
  40. '\n\t Secret :', secret,
  41. '\n',
  42. '\n\t A-value :', aVal,
  43. '\n\t B-value :', bVal,
  44. '\n\t D-value :', dVal,
  45. '\n\t F-value :', fVal,
  46. '\n',
  47. '\n\t Combined (A-value, B-value) :', aVal.add(bVal),
  48. '\n\t Combined (A-value, B-value, D-value) :', aVal.add(bVal).add(dVal)
  49. )
  50. /* Initialize test A. */
  51. const testA = pederson.commit(aVal, secret)
  52. console.log('\nTest A', testA) // eslint-disable-line no-console
  53. /* Perform verification for test A. */
  54. if (!pederson.verify(aVal, [testA], secret)) {
  55. throw new Error('Arbitrary verification test A failed.')
  56. }
  57. /* Initialize test B. */
  58. const testB = pederson.commit(bVal, secret)
  59. console.log('\nTest B', testB) // eslint-disable-line no-console
  60. /* Perform verification for test B. */
  61. if (!pederson.verify(bVal, [testB], secret)) {
  62. throw new Error('Arbitrary verification test B failed.')
  63. }
  64. /* Initialize test C. */
  65. const testC = pederson.combine([testA, testB])
  66. console.log('\nTest C', testC) // eslint-disable-line no-console
  67. /* Perform verification for combination of test A and test B. */
  68. if (!pederson.verify((aVal + bVal), [testC], secret)) {
  69. throw new Error('Combined verifcation test failed.')
  70. }
  71. /* Initialize test D. */
  72. const testD = pederson.commit(dVal, secret)
  73. console.log('\nTest D', testD) // eslint-disable-line no-console
  74. /* Perform verification for test D. */
  75. if (!pederson.verify(dVal, [testD], secret)) {
  76. throw new Error('Arbitrary verification test D failed.')
  77. }
  78. /* Initialize test E. */
  79. const testE = pederson.combine([testA, testB, testD])
  80. console.log('\nTest E', testE) // eslint-disable-line no-console
  81. /* Perform verification for combination of test A and test B. */
  82. if (!pederson.verify((aVal + bVal + dVal), [testE], secret)) {
  83. throw new Error('Combined verifcation test failed.')
  84. }
  85. /* Initialize test F. */
  86. // const testF = pederson.commit(fVal, secret)
  87. const testF = fVal.mod(order).minus(nonce)
  88. console.log('\nTest F', testF) // eslint-disable-line no-console
  89. /* Perform verification for test F. */
  90. // if (!pederson.verify(fVal, [testF], secret)) {
  91. // throw new Error('Arbitrary verification test F failed.')
  92. // }
  93. console.log('\n✅ All Pedersen (CashFusion) tests passed!\n') // eslint-disable-line no-console
  94. } catch (error) {
  95. /* eslint-disable-next-line no-console */
  96. console.error('\n⚠️ Failed to test pedersen commitments:', error)
  97. throw new Error(error)
  98. }