GlvMultiplier.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using Org.BouncyCastle.Math.EC.Endo;
  4. namespace Org.BouncyCastle.Math.EC.Multiplier
  5. {
  6. public class GlvMultiplier
  7. : AbstractECMultiplier
  8. {
  9. protected readonly ECCurve curve;
  10. protected readonly GlvEndomorphism glvEndomorphism;
  11. public GlvMultiplier(ECCurve curve, GlvEndomorphism glvEndomorphism)
  12. {
  13. if (curve == null || curve.Order == null)
  14. throw new ArgumentException("Need curve with known group order", "curve");
  15. this.curve = curve;
  16. this.glvEndomorphism = glvEndomorphism;
  17. }
  18. protected override ECPoint MultiplyPositive(ECPoint p, BigInteger k)
  19. {
  20. if (!curve.Equals(p.Curve))
  21. throw new InvalidOperationException();
  22. BigInteger n = p.Curve.Order;
  23. BigInteger[] ab = glvEndomorphism.DecomposeScalar(k.Mod(n));
  24. BigInteger a = ab[0], b = ab[1];
  25. ECPointMap pointMap = glvEndomorphism.PointMap;
  26. if (glvEndomorphism.HasEfficientPointMap)
  27. {
  28. return ECAlgorithms.ImplShamirsTrickWNaf(p, a, pointMap, b);
  29. }
  30. return ECAlgorithms.ImplShamirsTrickWNaf(p, a, pointMap.Map(p), b);
  31. }
  32. }
  33. }
  34. #endif