-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurve_test.go
100 lines (77 loc) · 3.12 KB
/
curve_test.go
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
97
98
99
100
/*
Basic Elliptic Curve primitives over Binary Field GF(2ⁿ)
Copyright (C) 2018 Xiphon
Greatly inspired by Kurt Rose's python implementation
https://gist.github.com/kurtbrose/4423605
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package ellipticbinary
import (
"encoding/hex"
"math/big"
"testing"
)
func TestPoint(t *testing.T) {
sect283k1 := &Curve{}
sect283k1.Name = "sect283k1"
sect283k1.P, _ = new(big.Int).SetString("0800000000000000000000000000000000000000000000000000000000000000000010a1", 16)
sect283k1.N, _ = new(big.Int).SetString("01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE9AE2ED07577265DFF7F94451E061E163C61", 16)
sect283k1.A, _ = new(big.Int).SetString("0", 10)
sect283k1.B, _ = new(big.Int).SetString("1", 10)
sect283k1.Gx, _ = new(big.Int).SetString("0503213f78ca44883f1a3b8162f188e553cd265f23c1567a16876913b0c2ac2458492836", 16)
sect283k1.Gy, _ = new(big.Int).SetString("01ccda380f1c9e318d90f95d07e5426fe87e45c0e8184698e45962364e34116177dd2259", 16)
sect283k1.BitSize = 283
var xVal, yVal *big.Int
{
bytes, _ := hex.DecodeString("aa160a283c315eaa6456d0156ec97d7332402abb709d4abf75031fdd0aa7861c84d35c")
xVal = big.NewInt(0).SetBytes(bytes)
bytes, _ = hex.DecodeString("04debb88a0aac5502feb6bb4ff6f0d16a0ad1c21d28e63202fd2c13bea637c04f1da862b")
yVal = big.NewInt(0).SetBytes(bytes)
if !sect283k1.IsOnCurve(xVal, yVal) {
t.FailNow()
}
if sect283k1.IsOnCurve(yVal, xVal) {
t.FailNow()
}
}
{
x, y := sect283k1.Add(xVal, yVal, sect283k1.Params().Gx, sect283k1.Params().Gy)
bytes, _ := hex.DecodeString("02b4b7a7117eb95c19a9d5365bd5e319039b2cd348a29dd5d62a059a13bda65da7826185")
xVaild := big.NewInt(0).SetBytes(bytes)
bytes, _ = hex.DecodeString("043ca8ebfc777ecc668736e51eac7ff7a463dddc91aa8a0f61334ce161298646c0a5a19e")
yVaild := big.NewInt(0).SetBytes(bytes)
if x.Cmp(xVaild) != 0 || y.Cmp(yVaild) != 0 {
t.FailNow()
}
}
{
x, y := sect283k1.Add(xVal, yVal, big.NewInt(0), big.NewInt(0))
if x.Cmp(xVal) != 0 || y.Cmp(yVal) != 0 {
t.FailNow()
}
x, y = sect283k1.Add(big.NewInt(0), big.NewInt(0), xVal, yVal)
if x.Cmp(xVal) != 0 || y.Cmp(yVal) != 0 {
t.FailNow()
}
}
{
num := big.NewInt(21910281)
x, y := sect283k1.ScalarBaseMult(num.Bytes())
bytes, _ := hex.DecodeString("04185e5d0268dca3823cdd9bd2bf71be3a5138c65a198983eb73c2258d9a8eb8a604fe6e")
xVaild := big.NewInt(0).SetBytes(bytes)
bytes, _ = hex.DecodeString("06653509ffa1715ea5767f74f4647803ca404cd32c5502929a4eccf4382c5be00dc14c47")
yVaild := big.NewInt(0).SetBytes(bytes)
if x.Cmp(xVaild) != 0 || y.Cmp(yVaild) != 0 {
t.FailNow()
}
}
}