summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2023-01-16 18:32:47 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2023-01-16 18:32:47 +0700
commit08118820fead0949657925456e9ef56efd3500f6 (patch)
treeff91208db0e74ee6f277c8fc485a1338f8552fa3
parentRefactor RC2WrapEngine (diff)
downloadBouncyCastle.NET-ed25519-08118820fead0949657925456e9ef56efd3500f6.tar.xz
Avoid allocations
-rw-r--r--crypto/src/crypto/engines/ElGamalEngine.cs13
1 files changed, 6 insertions, 7 deletions
diff --git a/crypto/src/crypto/engines/ElGamalEngine.cs b/crypto/src/crypto/engines/ElGamalEngine.cs
index 2e80302a6..8903f495e 100644
--- a/crypto/src/crypto/engines/ElGamalEngine.cs
+++ b/crypto/src/crypto/engines/ElGamalEngine.cs
@@ -3,6 +3,7 @@ using System;
 using Org.BouncyCastle.Crypto.Parameters;
 using Org.BouncyCastle.Math;
 using Org.BouncyCastle.Security;
+using Org.BouncyCastle.Utilities;
 
 namespace Org.BouncyCastle.Crypto.Engines
 {
@@ -157,14 +158,12 @@ namespace Org.BouncyCastle.Crypto.Engines
 
 				output = new byte[this.GetOutputBlockSize()];
 
-				// TODO Add methods to allow writing BigInteger to existing byte array?
-				byte[] out1 = gamma.ToByteArrayUnsigned();
-				byte[] out2 = phi.ToByteArrayUnsigned();
-				out1.CopyTo(output, output.Length / 2 - out1.Length);
-				out2.CopyTo(output, output.Length - out2.Length);
-			}
+				int mid = output.Length / 2;
+                BigIntegers.AsUnsignedByteArray(gamma, output, 0, mid);
+                BigIntegers.AsUnsignedByteArray(phi, output, mid, output.Length - mid);
+            }
 
-			return output;
+            return output;
 		}
 	}
 }