Skip to content

Commit de289b8

Browse files
committed
fixed s256
1 parent 497dc71 commit de289b8

File tree

25 files changed

+2089
-222
lines changed

25 files changed

+2089
-222
lines changed

elliptic/e521/e521.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package e521
2+
3+
import (
4+
"sync"
5+
"encoding/asn1"
6+
)
7+
8+
// see doc
9+
// https://www.gov.br/iti/pt-br/assuntos/legislacao/documentos-principais/IN2022_22_DOC_ICP_01.01_assinado.pdf
10+
// https://eprint.iacr.org/2013/647
11+
12+
var (
13+
// E-521 EdDSA curve oid
14+
OIDE521 = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 44588, 2, 1}
15+
)
16+
17+
var once sync.Once
18+
19+
func E521() *E521Curve {
20+
once.Do(initAll)
21+
return e521
22+
}

elliptic/e521/e521_curves.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package e521
2+
3+
import (
4+
"math/big"
5+
)
6+
7+
var e521 *E521Curve
8+
9+
func initAll() {
10+
initE521()
11+
}
12+
13+
func initE521() {
14+
e521 = &E521Curve{
15+
Name: "E-521",
16+
P: bigFromHex("1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),
17+
N: bigFromHex("7ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd15b6c64746fc85f736b8af5e7ec53f04fbd8c4569a8f1f4540ea2435f5180d6b"),
18+
D: bigFromHex("1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa4331"),
19+
Gx: bigFromHex("752cb45c48648b189df90cb2296b2878a3bfd9f42fc6c818ec8bf3c9c0c6203913f6ecc5ccc72434b1ae949d568fc99c6059d0fb13364838aa302a940a2f19ba6c"),
20+
Gy: bigFromHex("0c"),
21+
BitSize: 521,
22+
}
23+
}
24+
25+
func bigFromHex(s string) (i *big.Int) {
26+
i = new(big.Int)
27+
i.SetString(s, 16)
28+
29+
return
30+
}

elliptic/e521/e521_test.go

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package e521
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
"math/big"
7+
"crypto/elliptic"
8+
9+
cryptobin_test "github.com/deatil/go-cryptobin/tool/test"
10+
)
11+
12+
func bigintFromHex(s string) *big.Int {
13+
result, _ := new(big.Int).SetString(s, 16)
14+
15+
return result
16+
}
17+
18+
func Test_Interface(t *testing.T) {
19+
var _ elliptic.Curve = (*E521Curve)(nil)
20+
}
21+
22+
func Test_Curve_Add(t *testing.T) {
23+
{
24+
a1 := bigintFromHex("135e8ba63870ade80365ee6b6832d971a83c8519310bed795809637bd61e4d54676d0823d7a95d26291be2742994d833b16d306dcea0574b57924aac6b62552ef81")
25+
b1 := bigintFromHex("d6e622c17fb2723b47ef82f0a704694689c96c5cc12f24b42a735b89283c6bd47fe0596dff8841603414b8b3a5c681d72750e03a807f6668a008738876e2f1fcde")
26+
a2 := bigintFromHex("10ffba2f442444980490d51fb67b6b29f30a96e00aeebb058fb396f1d56862925f84a403612cf7a32586abe1e8085f44e28426a2f0684c9e7adbfaf99bd2788aad0")
27+
b2 := bigintFromHex("5d33e51bfe1cbb3c263ad569b213be723a45920ac38070147d8d85c1779b4fe4eaa0912a17765f2d87bb2ac27106fb8d019152c373e9ea060f591c1d85141cc830")
28+
29+
xx, yy := E521().Add(a1, b1, a2, b2)
30+
31+
xx2 := fmt.Sprintf("%x", xx.Bytes())
32+
yy2 := fmt.Sprintf("%x", yy.Bytes())
33+
34+
xxcheck := "01c48fda4d86a4610f4211f0f7bf4f5fdfe0da463028bd86b4827fd26717404fa8eb0433cdd040611a776a3de97a1f4882dbb5688984ad48739d9d48eaeee413f644"
35+
yycheck := "01a7c2f2d27327cb4a53894392198c9a5b6563c43c92912f90b0efe86d618770a9d7911573919c0a37942e6a7fc484dd59b917849c6c2e9a400f9bb7c46486fe9c4a"
36+
37+
if xx2 != xxcheck {
38+
t.Errorf("xx fail, got %s, want %s", xx2, xxcheck)
39+
}
40+
if yy2 != yycheck {
41+
t.Errorf("yy fail, got %s, want %s", yy2, yycheck)
42+
}
43+
}
44+
45+
{
46+
a1 := bigintFromHex("135e8ba63870ade80365ee6b6832d971a83c8519310bed795809637bd61e4d54676d0823d7a95d26291be2742994d833b16d306dcea0574b57924aac6b62552ef81")
47+
b1 := bigintFromHex("d6e622c17fb2723b47ef82f0a704694689c96c5cc12f24b42a735b89283c6bd47fe0596dff8841603414b8b3a5c681d72750e03a807f6668a008738876e2f1fcde")
48+
49+
xx, yy := E521().Add(a1, b1, a1, b1)
50+
51+
xx2 := fmt.Sprintf("%x", xx.Bytes())
52+
yy2 := fmt.Sprintf("%x", yy.Bytes())
53+
54+
xxcheck := "019f5208229540e4292ac78b021184e00ee1cc5d0c15edf9b9d05d7466fcc93d38c5afa8ab13db1fab07163fe91ad2d0a6aca4377995230e4a685e6d19c1e0457594"
55+
yycheck := "01fbce936f5787a4a778aeaece860985404b226ad1de63ded30ec88acd2335686022c2622f1abf537a21d8685b7c3d590980fa358640279761e0e50d8eae6ac5e716"
56+
57+
if xx2 != xxcheck {
58+
t.Errorf("xx fail, got %s, want %s", xx2, xxcheck)
59+
}
60+
if yy2 != yycheck {
61+
t.Errorf("yy fail, got %s, want %s", yy2, yycheck)
62+
}
63+
}
64+
65+
{
66+
a1 := bigintFromHex("135e8ba63870ade80365ee6b6832d971a83c8519310bed795809637bd61e4d54676d0823d7a95d26291be2742994d833b16d306dcea0574b57924aac6b62552ef81")
67+
b1 := bigintFromHex("d6e622c17fb2723b47ef82f0a704694689c96c5cc12f24b42a735b89283c6bd47fe0596dff8841603414b8b3a5c681d72750e03a807f6668a008738876e2f1fcde")
68+
69+
xx, yy := E521().Double(a1, b1)
70+
71+
xx2 := fmt.Sprintf("%x", xx.Bytes())
72+
yy2 := fmt.Sprintf("%x", yy.Bytes())
73+
74+
xxcheck := "019f5208229540e4292ac78b021184e00ee1cc5d0c15edf9b9d05d7466fcc93d38c5afa8ab13db1fab07163fe91ad2d0a6aca4377995230e4a685e6d19c1e0457594"
75+
yycheck := "01fbce936f5787a4a778aeaece860985404b226ad1de63ded30ec88acd2335686022c2622f1abf537a21d8685b7c3d590980fa358640279761e0e50d8eae6ac5e716"
76+
77+
if xx2 != xxcheck {
78+
t.Errorf("xx fail, got %s, want %s", xx2, xxcheck)
79+
}
80+
if yy2 != yycheck {
81+
t.Errorf("yy fail, got %s, want %s", yy2, yycheck)
82+
}
83+
}
84+
85+
}
86+
87+
func Test_Curve_ScalarMult(t *testing.T) {
88+
{
89+
a1 := bigintFromHex("135e8ba63870ade80365ee6b6832d971a83c8519310bed795809637bd61e4d54676d0823d7a95d26291be2742994d833b16d306dcea0574b57924aac6b62552ef81")
90+
b1 := bigintFromHex("d6e622c17fb2723b47ef82f0a704694689c96c5cc12f24b42a735b89283c6bd47fe0596dff8841603414b8b3a5c681d72750e03a807f6668a008738876e2f1fcde")
91+
k := bigintFromHex("10ffba2f442444980490d51fb67b6b29f30a96e00aeebb058fb396f1d56862925f84a403612cf7a32586abe1e8085f44e28426a2f0684c9e7adbfaf99bd2788aad0")
92+
93+
xx, yy := E521().ScalarMult(a1, b1, k.Bytes())
94+
95+
xx2 := fmt.Sprintf("%x", xx.Bytes())
96+
yy2 := fmt.Sprintf("%x", yy.Bytes())
97+
98+
xxcheck := "462780410d3830461ea02f5f99df2ff32dd093667dadb782c7736d40f7d9863f7d7994c64718264d4aa5cec9ade662738e459c15ecf835776a0f31548a06ef4417"
99+
yycheck := "0119c56a9485cc6124b17571e09e84b6895d7cd9babf7cfee1b24339845b7d8f0807a433f3c11fe20788b412dac23ebb6b80183f45ea8ebdddd0e10990d43672bd2b"
100+
101+
if xx2 != xxcheck {
102+
t.Errorf("ScalarMult xx fail, got %s, want %s", xx2, xxcheck)
103+
}
104+
if yy2 != yycheck {
105+
t.Errorf("ScalarMult yy fail, got %s, want %s", yy2, yycheck)
106+
}
107+
}
108+
109+
{
110+
k := bigintFromHex("10ffba2f442444980490d51fb67b6b29f30a96e00aeebb058fb396f1d56862925f84a403612cf7a32586abe1e8085f44e28426a2f0684c9e7adbfaf99bd2788aad0")
111+
112+
xx, yy := E521().ScalarBaseMult(k.Bytes())
113+
114+
xx2 := fmt.Sprintf("%x", xx.Bytes())
115+
yy2 := fmt.Sprintf("%x", yy.Bytes())
116+
117+
xxcheck := "dc597d107588c5bcac37864a12ad8e601bd531747da45b102af45f1716f170ee48adc8aa25034f62d2245acc0c8802386663a1705c8df877e02c610ba7b8b68eb4"
118+
yycheck := "01a912a13ddd30d259e0676b791c1560f98dea1ab00761282d04a933d6d54e74dab79cce5cf226881b14b18188465607cb49860fd20bbbb7b0f40911b6b0bdc73fe9"
119+
120+
if xx2 != xxcheck {
121+
t.Errorf("ScalarBaseMult xx fail, got %s, want %s", xx2, xxcheck)
122+
}
123+
if yy2 != yycheck {
124+
t.Errorf("ScalarBaseMult yy fail, got %s, want %s", yy2, yycheck)
125+
}
126+
}
127+
128+
}
129+
130+
func Test_MarshalCompressed(t *testing.T) {
131+
a1 := bigintFromHex("135e8ba63870ade80365ee6b6832d971a83c8519310bed795809637bd61e4d54676d0823d7a95d26291be2742994d833b16d306dcea0574b57924aac6b62552ef81")
132+
b1 := bigintFromHex("d6e622c17fb2723b47ef82f0a704694689c96c5cc12f24b42a735b89283c6bd47fe0596dff8841603414b8b3a5c681d72750e03a807f6668a008738876e2f1fcde")
133+
134+
m := MarshalCompressed(E521(), a1, b1)
135+
136+
m2 := fmt.Sprintf("%x", m)
137+
mcheck := "0300d6e622c17fb2723b47ef82f0a704694689c96c5cc12f24b42a735b89283c6bd47fe0596dff8841603414b8b3a5c681d72750e03a807f6668a008738876e2f1fcde"
138+
139+
cryptobin_test.Equal(t, mcheck, m2)
140+
141+
mcheck2 := bigintFromHex(mcheck).Bytes()
142+
143+
x, y := UnmarshalCompressed(E521(), mcheck2)
144+
cryptobin_test.Equal(t, a1, x)
145+
cryptobin_test.Equal(t, b1, y)
146+
}

0 commit comments

Comments
 (0)