Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
chain33-sdk-go
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
link33
chain33-sdk-go
Commits
68acbbed
Commit
68acbbed
authored
Jun 10, 2020
by
pengjun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add kdf && update hashToModInt
parent
175b6ea3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
52 additions
and
18 deletions
+52
-18
crypto_test.go
crypto/crypto_test.go
+9
-2
hash.go
crypto/hash.go
+23
-0
pre.go
pre.go
+20
-16
No files found.
crypto/crypto_test.go
View file @
68acbbed
...
@@ -75,4 +75,12 @@ func TestAddress(t *testing.T) {
...
@@ -75,4 +75,12 @@ func TestAddress(t *testing.T) {
}
}
fmt
.
Println
(
addr
)
fmt
.
Println
(
addr
)
}
}
\ No newline at end of file
func
TestKDF
(
t
*
testing
.
T
)
{
key
:=
[]
byte
{
0x1
,
0x23
,
0x45
,
0x67
,
0x89
,
0xab
,
0xcd
,
0xef
,
0xfe
,
0xdc
,
0xba
,
0x98
,
0x76
,
0x54
,
0x32
,
0x10
,
0x11
}
keyf
:=
KDF
(
key
,
32
)
fmt
.
Println
(
keyf
)
fmt
.
Println
(
len
(
keyf
))
}
crypto/hash.go
View file @
68acbbed
...
@@ -3,6 +3,7 @@ package crypto
...
@@ -3,6 +3,7 @@ package crypto
import
(
import
(
"crypto/sha256"
"crypto/sha256"
"golang.org/x/crypto/ripemd160"
"golang.org/x/crypto/ripemd160"
"math/big"
)
)
...
@@ -39,3 +40,24 @@ func Rimp160(b []byte) []byte {
...
@@ -39,3 +40,24 @@ func Rimp160(b []byte) []byte {
rimpHash
(
b
,
out
[
:
])
rimpHash
(
b
,
out
[
:
])
return
out
[
:
]
return
out
[
:
]
}
}
func
KDF
(
x
[]
byte
,
length
int
)
[]
byte
{
var
c
[]
byte
var
ct
int64
=
1
h
:=
sha256
.
New
()
for
i
,
j
:=
0
,
(
length
+
31
)
/
32
;
i
<
j
;
i
++
{
h
.
Reset
()
h
.
Write
(
x
)
h
.
Write
(
big
.
NewInt
(
ct
)
.
Bytes
())
hash
:=
h
.
Sum
(
nil
)
if
i
+
1
==
j
&&
length
%
32
!=
0
{
c
=
append
(
c
,
hash
[
:
length
%
32
]
...
)
}
else
{
c
=
append
(
c
,
hash
...
)
}
ct
++
}
return
c
}
\ No newline at end of file
pre.go
View file @
68acbbed
...
@@ -4,10 +4,9 @@ import (
...
@@ -4,10 +4,9 @@ import (
"crypto/rand"
"crypto/rand"
"errors"
"errors"
"fmt"
"fmt"
secp256k1
"github.com/btcsuite/btcd/btcec"
"github.com/33cn/chain33-sdk-go/crypto"
"github.com/33cn/chain33-sdk-go/crypto"
"github.com/33cn/chain33-sdk-go/types"
"github.com/33cn/chain33-sdk-go/types"
secp256k1
"github.com/btcsuite/btcd/btcec"
"golang.org/x/crypto/blake2b"
"golang.org/x/crypto/blake2b"
"math/big"
"math/big"
)
)
...
@@ -67,15 +66,18 @@ func (p *EccPoit) ToPublicKey() *secp256k1.PublicKey {
...
@@ -67,15 +66,18 @@ func (p *EccPoit) ToPublicKey() *secp256k1.PublicKey {
}
}
func
hashToModInt
(
digest
[]
byte
)
*
big
.
Int
{
func
hashToModInt
(
digest
[]
byte
)
*
big
.
Int
{
sum
:=
new
(
big
.
Int
)
.
SetBytes
(
digest
)
orderBits
:=
baseN
.
BitLen
()
one
:=
big
.
NewInt
(
1
)
orderBytes
:=
(
orderBits
+
7
)
/
8
order_minus_1
:=
big
.
NewInt
(
0
)
if
len
(
digest
)
>
orderBytes
{
order_minus_1
.
Sub
(
baseN
,
one
)
digest
=
digest
[
:
orderBytes
]
}
bigNum
:=
big
.
NewInt
(
0
)
bigNum
.
Mod
(
sum
,
order_minus_1
)
.
Add
(
bigNum
,
one
)
return
bigNum
ret
:=
new
(
big
.
Int
)
.
SetBytes
(
digest
)
excess
:=
len
(
digest
)
*
8
-
orderBits
if
excess
>
0
{
ret
.
Rsh
(
ret
,
uint
(
excess
))
}
return
ret
}
}
func
makeShamirPolyCoeff
(
threshold
int
)
[]
*
big
.
Int
{
func
makeShamirPolyCoeff
(
threshold
int
)
[]
*
big
.
Int
{
...
@@ -153,8 +155,8 @@ func GeneratePreEncryptKey(pubOwner []byte) ([]byte, string, string) {
...
@@ -153,8 +155,8 @@ func GeneratePreEncryptKey(pubOwner []byte) ([]byte, string, string) {
pub_r
:=
types
.
ToHex
((
*
secp256k1
.
PublicKey
)(
&
priv_r
.
PublicKey
)
.
SerializeCompressed
())
pub_r
:=
types
.
ToHex
((
*
secp256k1
.
PublicKey
)(
&
priv_r
.
PublicKey
)
.
SerializeCompressed
())
pub_u
:=
types
.
ToHex
((
*
secp256k1
.
PublicKey
)(
&
priv_u
.
PublicKey
)
.
SerializeCompressed
())
pub_u
:=
types
.
ToHex
((
*
secp256k1
.
PublicKey
)(
&
priv_u
.
PublicKey
)
.
SerializeCompressed
())
share_key
:=
crypto
.
KDF
(
result
.
SerializeCompressed
(),
32
)
return
result
.
SerializeCompressed
()[
1
:
]
,
pub_r
,
pub_u
return
share_key
,
pub_r
,
pub_u
}
}
func
GenerateKeyFragments
(
privOwner
[]
byte
,
pubRecipient
[]
byte
,
numSplit
,
threshold
int
)
([]
*
KFrag
,
error
)
{
func
GenerateKeyFragments
(
privOwner
[]
byte
,
pubRecipient
[]
byte
,
numSplit
,
threshold
int
)
([]
*
KFrag
,
error
)
{
...
@@ -226,7 +228,7 @@ func AssembleReencryptFragment(privRecipient []byte, reKeyFrags []*ReKeyFrag) ([
...
@@ -226,7 +228,7 @@ func AssembleReencryptFragment(privRecipient []byte, reKeyFrags []*ReKeyFrag) ([
dhBob
:=
dBobHash
.
Sum
(
nil
)
dhBob
:=
dBobHash
.
Sum
(
nil
)
dhBobBN
:=
hashToModInt
(
dhBob
)
dhBobBN
:=
hashToModInt
(
dhBob
)
var
share_key
*
EccPoit
var
result
*
EccPoit
if
len
(
reKeyFrags
)
==
1
{
if
len
(
reKeyFrags
)
==
1
{
rPoint
,
err
:=
NewEccPoint
(
reKeyFrags
[
0
]
.
ReKeyR
)
rPoint
,
err
:=
NewEccPoint
(
reKeyFrags
[
0
]
.
ReKeyR
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -239,7 +241,7 @@ func AssembleReencryptFragment(privRecipient []byte, reKeyFrags []*ReKeyFrag) ([
...
@@ -239,7 +241,7 @@ func AssembleReencryptFragment(privRecipient []byte, reKeyFrags []*ReKeyFrag) ([
return
nil
,
err
return
nil
,
err
}
}
share_key
=
rPoint
.
Add
(
uPoint
)
.
MulInt
(
dhBobBN
)
result
=
rPoint
.
Add
(
uPoint
)
.
MulInt
(
dhBobBN
)
}
else
{
}
else
{
var
eFinal
,
vFinal
*
EccPoit
var
eFinal
,
vFinal
*
EccPoit
...
@@ -281,8 +283,9 @@ func AssembleReencryptFragment(privRecipient []byte, reKeyFrags []*ReKeyFrag) ([
...
@@ -281,8 +283,9 @@ func AssembleReencryptFragment(privRecipient []byte, reKeyFrags []*ReKeyFrag) ([
eFinal
=
e
.
Add
(
eFinal
)
eFinal
=
e
.
Add
(
eFinal
)
vFinal
=
v
.
Add
(
vFinal
)
vFinal
=
v
.
Add
(
vFinal
)
}
}
share_key
=
eFinal
.
Add
(
vFinal
)
.
MulInt
(
dhBobBN
)
result
=
eFinal
.
Add
(
vFinal
)
.
MulInt
(
dhBobBN
)
}
}
return
share_key
.
ToPublicKey
()
.
SerializeCompressed
(),
nil
share_key
:=
crypto
.
KDF
(
result
.
ToPublicKey
()
.
SerializeCompressed
(),
32
)
return
share_key
,
nil
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment