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.Collections.Generic;
3using System.Numerics;
4using System.Security.Cryptography;
5using System.Text;
6using System.Xml;
7
9{
13 public abstract class PrimeFieldCurve : EllipticCurve
14 {
18 protected readonly ModulusP modP;
19
23 protected readonly ModulusP modN;
24
28 protected readonly BigInteger p;
29
37 public PrimeFieldCurve(BigInteger Prime, PointOnCurve BasePoint, BigInteger Order,
38 int Cofactor)
39 : this(Prime, BasePoint, Order, Cofactor, null)
40 {
41 }
42
51 public PrimeFieldCurve(BigInteger Prime, PointOnCurve BasePoint, BigInteger Order,
52 int Cofactor, byte[] Secret)
53 : base(BasePoint, Order, Cofactor, Secret)
54 {
55 if (Prime <= BigInteger.One)
56 throw new ArgumentException("Invalid prime base.", nameof(Prime));
57
58 this.p = Prime;
59 this.modP = new ModulusP(Prime);
60 this.modN = new ModulusP(Order);
61 }
62
66 public BigInteger Prime => this.p;
67
75 public override PointOnCurve ScalarMultiplication(byte[] N, PointOnCurve P, bool Normalize)
76 {
77 PointOnCurve Result = base.ScalarMultiplication(N, P, Normalize);
78
79 if (Normalize)
80 Result.Normalize(this);
81
82 return Result;
83 }
84
89 public override byte[] GenerateSecret()
90 {
91 byte[] B = new byte[this.orderBytes];
92 BigInteger D;
93
94 do
95 {
96 lock (rnd)
97 {
98 rnd.GetBytes(B);
99 }
100
101 B[this.orderBytes - 1] &= this.msbOrderMask;
102
103 D = ToInt(B);
104 }
105 while (D.IsZero || D >= this.n);
106
107 return B;
108 }
109
113 public ModulusP ModulusP => this.modP;
114
118 public ModulusP ModulusN => this.modN;
119
120 }
121}
Abstract base class for elliptic curves.
readonly int orderBytes
Number of bytes used for the order of the curve.
static BigInteger ToInt(byte[] Binary)
Converts a little-endian binary representation of a big integer to a BigInteger.
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
readonly ModulusP modN
Arithmetic modulus n
override PointOnCurve ScalarMultiplication(byte[] N, PointOnCurve P, bool Normalize)
Performs the scalar multiplication of N *P .
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)
ModulusP ModulusN
Arithmetic modulus n (the order)
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.
Represents a point on a curve.
Definition: PointOnCurve.cs:11
void Normalize(PrimeFieldCurve Curve)
Normalizes a point, if in homogeneous coorinates.