Optimized ScalMul to be not recursive
This commit is contained in:
		
							parent
							
								
									77709309ad
								
							
						
					
					
						commit
						5424f5b75c
					
				| @ -58,19 +58,36 @@ namespace Ed25519 | |||||||
| 
 | 
 | ||||||
|         public readonly EdPoint ScalarMul(BigInteger e) |         public readonly EdPoint ScalarMul(BigInteger e) | ||||||
|         { |         { | ||||||
|             if (e.Equals(BigInteger.Zero)) |             var numberOperations = (int) Math.Ceiling(BigInteger.Log(e, 2)) + 1; | ||||||
|  |             var series = new bool[numberOperations]; | ||||||
|  |             var previousNumber = e; | ||||||
|  |             for (var n = 0; n < numberOperations; n++) | ||||||
|             { |             { | ||||||
|                 return new EdPoint |                 if (n == 0) | ||||||
|  |                 { | ||||||
|  |                     series[n] = !e.IsEven; | ||||||
|  |                     continue; | ||||||
|  |                 } | ||||||
|  | 
 | ||||||
|  |                 var number = BigInteger.Divide(previousNumber, Constants.TWO); | ||||||
|  |                 series[n] = !number.IsEven; | ||||||
|  |                 previousNumber = number; | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|  |             var result = new EdPoint | ||||||
|             { |             { | ||||||
|                 X = BigInteger.Zero, |                 X = BigInteger.Zero, | ||||||
|                 Y = BigInteger.One, |                 Y = BigInteger.One, | ||||||
|             }; |             }; | ||||||
|  | 
 | ||||||
|  |             for (var n = numberOperations - 2; n >= 0; n--) | ||||||
|  |             { | ||||||
|  |                 result = result.EdwardsSquare(); | ||||||
|  |                 if (series[n]) | ||||||
|  |                     result = result.Edwards(this); | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|             var q = this.ScalarMul(e / Constants.TWO); |             return result; | ||||||
|             q = q.EdwardsSquare(); |  | ||||||
| 
 |  | ||||||
|             return e.IsEven ? q : q.Edwards(this); |  | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         public EdPoint EdwardsSquare() |         public EdPoint EdwardsSquare() | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user