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
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
51 additions
and
16 deletions
+51
-16
crypto_test.go
crypto/crypto_test.go
+8
-0
hash.go
crypto/hash.go
+23
-0
pre.go
pre.go
+20
-16
No files found.
crypto/crypto_test.go
View file @
68acbbed
...
...
@@ -76,3 +76,11 @@ func TestAddress(t *testing.T) {
fmt
.
Println
(
addr
)
}
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
import
(
"crypto/sha256"
"golang.org/x/crypto/ripemd160"
"math/big"
)
...
...
@@ -39,3 +40,24 @@ func Rimp160(b []byte) []byte {
rimpHash
(
b
,
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 (
"crypto/rand"
"errors"
"fmt"
secp256k1
"github.com/btcsuite/btcd/btcec"
"github.com/33cn/chain33-sdk-go/crypto"
"github.com/33cn/chain33-sdk-go/types"
secp256k1
"github.com/btcsuite/btcd/btcec"
"golang.org/x/crypto/blake2b"
"math/big"
)
...
...
@@ -67,15 +66,18 @@ func (p *EccPoit) ToPublicKey() *secp256k1.PublicKey {
}
func
hashToModInt
(
digest
[]
byte
)
*
big
.
Int
{
sum
:=
new
(
big
.
Int
)
.
SetBytes
(
digest
)
one
:=
big
.
NewInt
(
1
)
order_minus_1
:=
big
.
NewInt
(
0
)
order_minus_1
.
Sub
(
baseN
,
one
)
bigNum
:=
big
.
NewInt
(
0
)
bigNum
.
Mod
(
sum
,
order_minus_1
)
.
Add
(
bigNum
,
one
)
orderBits
:=
baseN
.
BitLen
()
orderBytes
:=
(
orderBits
+
7
)
/
8
if
len
(
digest
)
>
orderBytes
{
digest
=
digest
[
:
orderBytes
]
}
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
{
...
...
@@ -153,8 +155,8 @@ func GeneratePreEncryptKey(pubOwner []byte) ([]byte, string, string) {
pub_r
:=
types
.
ToHex
((
*
secp256k1
.
PublicKey
)(
&
priv_r
.
PublicKey
)
.
SerializeCompressed
())
pub_u
:=
types
.
ToHex
((
*
secp256k1
.
PublicKey
)(
&
priv_u
.
PublicKey
)
.
SerializeCompressed
())
return
result
.
SerializeCompressed
()[
1
:
]
,
pub_r
,
pub_u
share_key
:=
crypto
.
KDF
(
result
.
SerializeCompressed
(),
32
)
return
share_key
,
pub_r
,
pub_u
}
func
GenerateKeyFragments
(
privOwner
[]
byte
,
pubRecipient
[]
byte
,
numSplit
,
threshold
int
)
([]
*
KFrag
,
error
)
{
...
...
@@ -226,7 +228,7 @@ func AssembleReencryptFragment(privRecipient []byte, reKeyFrags []*ReKeyFrag) ([
dhBob
:=
dBobHash
.
Sum
(
nil
)
dhBobBN
:=
hashToModInt
(
dhBob
)
var
share_key
*
EccPoit
var
result
*
EccPoit
if
len
(
reKeyFrags
)
==
1
{
rPoint
,
err
:=
NewEccPoint
(
reKeyFrags
[
0
]
.
ReKeyR
)
if
err
!=
nil
{
...
...
@@ -239,7 +241,7 @@ func AssembleReencryptFragment(privRecipient []byte, reKeyFrags []*ReKeyFrag) ([
return
nil
,
err
}
share_key
=
rPoint
.
Add
(
uPoint
)
.
MulInt
(
dhBobBN
)
result
=
rPoint
.
Add
(
uPoint
)
.
MulInt
(
dhBobBN
)
}
else
{
var
eFinal
,
vFinal
*
EccPoit
...
...
@@ -281,8 +283,9 @@ func AssembleReencryptFragment(privRecipient []byte, reKeyFrags []*ReKeyFrag) ([
eFinal
=
e
.
Add
(
eFinal
)
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