title: How do I cast a number to BouncyCastle.BigInteger <--> System.Numerics.BigInteger
body: <p>The slowest part of my code is that I use Bouncy Castle's "BigInteger.Multiply()" method.  To try and speed things up I'm attempting to insert the   code below to see if there is any improvement.</p>
  
  <p>I know that I'm not doing something right, because I'm getting many errors and exceptions when Bouncy Castle see the BigInteger I'm sending to it:</p>
  
  <pre><code>         if (this.sign != 1 || val.sign != 1)
          {
              //todo: test encoding, twos compliment
          }
          var tempThis = this.ToByteArrayUnsigned();
          Array.Reverse(tempThis);
          System.Numerics.BigInteger aThis = new System.Numerics.BigInteger(tempThis);
  
          var tempThat = val.ToByteArrayUnsigned();
          Array.Reverse(tempThat);
          System.Numerics.BigInteger bThat = new System.Numerics.BigInteger(tempThat);
          var msResult = System.Numerics.BigInteger.Multiply(aThis, bThat);
  
          var tmpRet = msResult.ToByteArray();
          Array.Reverse(tmpRet);
          var ret = new BigInteger(tmpRet);
          return ret;
  </code></pre>
  
  <p>This is the original BC function</p>
  
  <pre><code>    public BigInteger Multiply(     BigInteger val)
      {
  
         // THEORY: If I use the native System.Numerics here, then I might get better performance
  
          if (val == this)
              return Square();
  
          if ((sign &amp; val.sign) == 0)
              return Zero;
  
          if (val.QuickPow2Check()) // val is power of two
          {
              BigInteger result = this.ShiftLeft(val.Abs().BitLength - 1);
              return val.sign &gt; 0 ? result : result.Negate();
          }
  
          if (this.QuickPow2Check()) // this is power of two
          {
              BigInteger result = val.ShiftLeft(this.Abs().BitLength - 1);
              return this.sign &gt; 0 ? result : result.Negate();
          }
  
          int resLength = magnitude.Length + val.magnitude.Length;
          int[] res = new int[resLength];
  
          Multiply(res, this.magnitude, val.magnitude);
  
          int resSign = sign ^ val.sign ^ 1;
          return new BigInteger(resSign, res, true);
      }
  </code></pre>