Advertisement
Guest User

Testing elliptic curve addition formulas

a guest
Nov 14th, 2021
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function test_the_formula(A, B, x1, x2)
  2. {
  3.     const E = x => Math.sqrt(x**3 + A*x + B);
  4.    
  5.     const y1 = E(x1);
  6.     const y2 = E(x2);
  7.     const m = (y2 - y1)/(x2-x1);
  8.    
  9.     const x3_a = m**2 - x1 - x2;
  10.     const x3_b = (m**2*x1 + x1**2 + A - 2*m*y1)/x2;
  11.        
  12.     return Math.abs(x3_a - x3_b) < 0.00000001;
  13. }
  14.  
  15. function test()
  16. {
  17.     for (let A = 1; A < 100; A++)
  18.         for (let B = 1; B < 100; B++)
  19.         {
  20.             let x1 = Math.floor(Math.random()*10) + 1;
  21.             let x2 = x1;
  22.            
  23.             while (x1 == x2)
  24.                 x2 = Math.floor(Math.random()*10) + 1;
  25.                
  26.             if ( ! test_the_formula(A, B, x1, x2))
  27.             {
  28.                 console.log(`A = ${A}; B = ${B}; x1 = ${x1}; x2 = ${x2}`);
  29.                 return false;
  30.             }
  31.         }
  32.        
  33.     return true;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement