File: curve.go

package info (click to toggle)
golang-github-cloudflare-circl 1.0.0%2B20200724-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 5,788 kB
  • sloc: asm: 19,418; ansic: 1,289; makefile: 54
file content (96 lines) | stat: -rw-r--r-- 2,146 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package fourq

import (
	"math/big"

	"github.com/cloudflare/circl/internal/conv"
)

// Size of scalars used for point multiplication.
const Size = 32

// Point represents an affine point of the curve. The identity is (0,1).
type Point struct{ X, Y Fq }

// CurveParams contains the parameters of the elliptic curve.
type CurveParams struct {
	Name string   // The canonical name of the curve.
	P    *big.Int // The order of the underlying field Fp.
	N    *big.Int // The order of the generator point.
	G    Point    // This is the generator point.
}

// Params returns the parameters for the curve.
func Params() *CurveParams {
	params := CurveParams{Name: "FourQ"}
	params.P = conv.Uint64Le2BigInt(prime[:])
	params.N = conv.Uint64Le2BigInt(orderGenerator[:])
	params.G.X = genX
	params.G.Y = genY
	return &params
}

// IsOnCurve reports whether the given P=(x,y) lies on the curve.
func (P *Point) IsOnCurve() bool {
	var _P pointR1
	P.toR1(&_P)
	return _P.IsOnCurve()
}

// SetGenerator assigns to P the generator point G.
func (P *Point) SetGenerator() { P.X = genX; P.Y = genY }

// SetIdentity assigns to P the identity element.
func (P *Point) SetIdentity() {
	var _P pointR1
	_P.SetIdentity()
	P.fromR1(&_P)
}

// IsIdentity returns true if P is the identity element.
func (P *Point) IsIdentity() bool {
	var _P pointR1
	P.toR1(&_P)
	return _P.IsIdentity()
}

// Add calculates a point addition P = Q + R.
func (P *Point) Add(Q, R *Point) {
	var _Q, _R pointR1
	var _R2 pointR2
	Q.toR1(&_Q)
	R.toR1(&_R)
	_R2.FromR1(&_R)
	_Q.add(&_R2)
	P.fromR1(&_Q)
}

// ScalarMult calculates P = k*Q, where Q is an N-torsion point.
func (P *Point) ScalarMult(k *[Size]byte, Q *Point) {
	var _P, _Q pointR1
	Q.toR1(&_Q)
	_Q.ClearCofactor()
	_P.ScalarMult(k, &_Q)
	P.fromR1(&_P)
}

// ScalarBaseMult calculates P = k*G, where G is the generator point.
func (P *Point) ScalarBaseMult(k *[Size]byte) {
	var _P pointR1
	_P.ScalarBaseMult(k)
	P.fromR1(&_P)
}

func (P *Point) fromR1(Q *pointR1) {
	Q.ToAffine()
	P.X = Q.X
	P.Y = Q.Y
}

func (P *Point) toR1(projP *pointR1) {
	projP.X = P.X
	projP.Y = P.Y
	projP.Ta = P.X
	projP.Tb = P.Y
	projP.Z.setOne()
}