Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
PrimeFieldCurve.cs
1using System;
2using System.Numerics;
3
5{
9 public abstract class PrimeFieldCurve : EllipticCurve
10 {
14 protected readonly ModulusP modP;
15
19 protected readonly ModulusP modN;
20
24 protected readonly BigInteger p;
25
33 public PrimeFieldCurve(BigInteger Prime, PointOnCurve BasePoint, BigInteger Order,
34 int Cofactor)
35 : this(Prime, BasePoint, Order, Cofactor, (byte[])null)
36 {
37 }
38
47 public PrimeFieldCurve(BigInteger Prime, PointOnCurve BasePoint, BigInteger Order,
48 int Cofactor, byte[] Secret)
49 : base(BasePoint, Order, Cofactor, Secret)
50 {
51 if (Prime <= BigInteger.One)
52 throw new ArgumentException("Invalid prime base.", nameof(Prime));
53
54 this.p = Prime;
55 this.modP = new ModulusP(Prime);
56 this.modN = new ModulusP(Order);
57 }
58
67 public PrimeFieldCurve(BigInteger Prime, PointOnCurve BasePoint, BigInteger Order,
68 int Cofactor, uint[] Secret)
69 : this(Prime, BasePoint, Order, Cofactor, ToByteSecret(Secret))
70 {
71 }
72
76 public BigInteger Prime => this.p;
77
82
87
93 public static BigInteger ToBigInteger(uint[] BigEndianDWords)
94 {
95 return ToInt(ToByteSecret(BigEndianDWords));
96 }
97
105 public static byte[] ToByteSecret(uint[] BigEndianDWords)
106 {
107 int i, c = BigEndianDWords.Length;
108 int j = c << 2;
109 byte[] B = new byte[((BigEndianDWords[0] & 0x80000000) != 0) ? j + 1 : j];
110 uint k;
111
112 for (i = 0; i < c; i++)
113 {
114 k = BigEndianDWords[i];
115
116 B[j - 4] = (byte)k;
117 k >>= 8;
118 B[j - 3] = (byte)k;
119 k >>= 8;
120 B[j - 2] = (byte)k;
121 k >>= 8;
122 B[j - 1] = (byte)k;
123
124 j -= 4;
125 }
126
127 return B;
128 }
129
137 public override PointOnCurve ScalarMultiplication(byte[] N, PointOnCurve P, bool Normalize)
138 {
139 PointOnCurve Result = base.ScalarMultiplication(N, P, Normalize);
140
141 if (Normalize)
142 Result.Normalize(this);
143
144 return Result;
145 }
146
151 public override byte[] GenerateSecret()
152 {
153 byte[] B = new byte[this.bigIntegerBytes];
154 BigInteger D;
155
156 do
157 {
158 lock (rnd)
159 {
160 rnd.GetBytes(B);
161 }
162
163 B[this.bigIntegerBytes - 1] &= this.msbOrderMask;
164
165 D = ToInt(B);
166 }
167 while (D.IsZero || D >= this.n);
168
169 return B;
170 }
171
175 public ModulusP ModulusP => this.modP;
176
180 public ModulusP ModulusN => this.modN;
181
182 }
183}
Abstract base class for elliptic curves.
static BigInteger ToInt(byte[] Binary, bool BigEndian)
Converts a little-endian binary representation of a big integer to a BigInteger.
readonly int bigIntegerBytes
Number of bytes used for big integers in the curve.
static readonly RandomNumberGenerator rnd
Random number generator
PointOnCurve BasePoint
Base-point of curve.
readonly byte msbOrderMask
Mask for most significant byte of scalars.
Integer arithmetic, modulus a prime.
Definition: ModulusP.cs:10
Base class of Elliptic curves over a prime field.
readonly ModulusP modP
Arithmetic modulus p
static BigInteger ToBigInteger(uint[] BigEndianDWords)
Converts a sequence of unsigned 32-bit integers to a BigInteger.
readonly ModulusP modN
Arithmetic modulus n
override PointOnCurve ScalarMultiplication(byte[] N, PointOnCurve P, bool Normalize)
Performs the scalar multiplication of N *P .
virtual HashFunctionStream HashFunctionStream
Hash function to use in signatures for data streams.
PrimeFieldCurve(BigInteger Prime, PointOnCurve BasePoint, BigInteger Order, int Cofactor, byte[] Secret)
Base class of Elliptic curves over a prime field.
ModulusP ModulusP
Arithmetic modulus p (the prime)
static byte[] ToByteSecret(uint[] BigEndianDWords)
Converts a sequence of unsigned 32-bit integers to a secret that can be used with BigInteger
ModulusP ModulusN
Arithmetic modulus n (the order)
PrimeFieldCurve(BigInteger Prime, PointOnCurve BasePoint, BigInteger Order, int Cofactor, uint[] Secret)
Base class of Elliptic curves over a prime field.
override byte[] GenerateSecret()
Generates a new secret.
PrimeFieldCurve(BigInteger Prime, PointOnCurve BasePoint, BigInteger Order, int Cofactor)
Base class of Elliptic curves over a prime field.
Contains methods for simple hash calculations.
Definition: Hashes.cs:57
static byte[] ComputeSHA256Hash(byte[] Data)
Computes the SHA-256 hash of a block of binary data.
Definition: Hashes.cs:469
delegate byte[] HashFunctionArray(byte[] Data)
Delegate to hash function.
HashFunction
Hash method enumeration.
Definition: Hashes.cs:26
Represents a point on a curve.
Definition: PointOnCurve.cs:10
void Normalize(PrimeFieldCurve Curve)
Normalizes a point, if in homogeneous coorinates.