|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Numerics; |
| 3 | +using System; |
| 4 | + |
| 5 | +namespace EllipticCurve |
| 6 | +{ |
| 7 | + public class CurveFp |
| 8 | + { |
| 9 | + public BigInteger A { get; private set; } |
| 10 | + public BigInteger B { get; private set; } |
| 11 | + public BigInteger P { get; private set; } |
| 12 | + public BigInteger N { get; private set; } |
| 13 | + public Point G { get; private set; } |
| 14 | + public string name { get; private set; } |
| 15 | + public int[] oid { get; private set; } |
| 16 | + public string nistName { get; private set; } |
| 17 | + |
| 18 | + |
| 19 | + public CurveFp(BigInteger A, BigInteger B, BigInteger P, BigInteger N, BigInteger Gx, BigInteger Gy, string name, int[] oid, string nistName = "") |
| 20 | + { |
| 21 | + this.A = A; |
| 22 | + this.B = B; |
| 23 | + this.P = P; |
| 24 | + this.N = N; |
| 25 | + G = new Point(Gx, Gy); |
| 26 | + this.name = name; |
| 27 | + this.nistName = nistName; |
| 28 | + this.oid = oid; |
| 29 | + } |
| 30 | + |
| 31 | + public bool contains(Point p) |
| 32 | + { |
| 33 | + return Utils.Integer.modulo( |
| 34 | + BigInteger.Pow(p.y, 2) - (BigInteger.Pow(p.x, 3) + A * p.x + B), |
| 35 | + P |
| 36 | + ).IsZero; |
| 37 | + } |
| 38 | + |
| 39 | + public int length() |
| 40 | + { |
| 41 | + return N.ToString("X").Length / 2; |
| 42 | + } |
| 43 | + |
| 44 | + } |
| 45 | + |
| 46 | + public static class Curves |
| 47 | + { |
| 48 | + |
| 49 | + public static CurveFp getCurveByName(string name) |
| 50 | + { |
| 51 | + name = name.ToLower(); |
| 52 | + |
| 53 | + if (name == "secp256k1") |
| 54 | + { |
| 55 | + return secp256k1; |
| 56 | + } |
| 57 | + if (name == "p256" | name == "prime256v1") |
| 58 | + { |
| 59 | + return prime256v1; |
| 60 | + } |
| 61 | + |
| 62 | + throw new ArgumentException("unknown curve " + name); |
| 63 | + } |
| 64 | + |
| 65 | + public static CurveFp secp256k1 = new CurveFp( |
| 66 | + Utils.BinaryAscii.numberFromHex("0000000000000000000000000000000000000000000000000000000000000000"), |
| 67 | + Utils.BinaryAscii.numberFromHex("0000000000000000000000000000000000000000000000000000000000000007"), |
| 68 | + Utils.BinaryAscii.numberFromHex("fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f"), |
| 69 | + Utils.BinaryAscii.numberFromHex("fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141"), |
| 70 | + Utils.BinaryAscii.numberFromHex("79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798"), |
| 71 | + Utils.BinaryAscii.numberFromHex("483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8"), |
| 72 | + "secp256k1", |
| 73 | + new int[] { 1, 3, 132, 0, 10 } |
| 74 | + ); |
| 75 | + |
| 76 | + public static CurveFp prime256v1 = new CurveFp( |
| 77 | + Utils.BinaryAscii.numberFromHex("ffffffff00000001000000000000000000000000fffffffffffffffffffffffc"), |
| 78 | + Utils.BinaryAscii.numberFromHex("5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b"), |
| 79 | + Utils.BinaryAscii.numberFromHex("ffffffff00000001000000000000000000000000ffffffffffffffffffffffff"), |
| 80 | + Utils.BinaryAscii.numberFromHex("ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551"), |
| 81 | + Utils.BinaryAscii.numberFromHex("6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296"), |
| 82 | + Utils.BinaryAscii.numberFromHex("4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5"), |
| 83 | + "prime256v1", |
| 84 | + new int[] { 1, 2, 840, 10045, 3, 1, 7 }, |
| 85 | + "P-256" |
| 86 | + ); |
| 87 | + |
| 88 | + public static CurveFp p256 = prime256v1; |
| 89 | + |
| 90 | + public static CurveFp[] supportedCurves = { secp256k1, prime256v1 }; |
| 91 | + |
| 92 | + public static Dictionary<string, CurveFp> curvesByOid = new Dictionary<string, CurveFp>() { |
| 93 | + {string.Join(",", secp256k1.oid), secp256k1}, |
| 94 | + {string.Join(",", prime256v1.oid), prime256v1} |
| 95 | + }; |
| 96 | + |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments