Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
crypto
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
elixxir
crypto
Commits
6d5f585d
Commit
6d5f585d
authored
Jan 6, 2020
by
Josh Brooks
Browse files
Options
Downloads
Patches
Plain Diff
Standardize mutex unlocks as defers
parent
aa6bc689
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
blockchain/block.go
+5
-5
5 additions, 5 deletions
blockchain/block.go
diffieHellman/dhkx.go
+1
-1
1 addition, 1 deletion
diffieHellman/dhkx.go
fastRNG/stream.go
+17
-17
17 additions, 17 deletions
fastRNG/stream.go
with
23 additions
and
23 deletions
blockchain/block.go
+
5
−
5
View file @
6d5f585d
...
...
@@ -78,9 +78,9 @@ func (b *Block) NextBlock() (*Block, error) {
// GetCreated returns a copy of the created coins list
func
(
b
*
Block
)
GetCreated
()
[]
coin
.
Coin
{
b
.
mutex
.
Lock
()
defer
b
.
mutex
.
Unlock
()
cCopy
:=
make
([]
coin
.
Coin
,
len
(
b
.
created
))
copy
(
cCopy
,
b
.
created
)
b
.
mutex
.
Unlock
()
return
cCopy
}
...
...
@@ -102,9 +102,9 @@ func (b *Block) AddCreated(c []coin.Coin) error {
// GetDestroyed returns a copy of the destroyed coins list
func
(
b
*
Block
)
GetDestroyed
()
[]
coin
.
Coin
{
b
.
mutex
.
Lock
()
defer
b
.
mutex
.
Unlock
()
cCopy
:=
make
([]
coin
.
Coin
,
len
(
b
.
destroyed
))
copy
(
cCopy
,
b
.
destroyed
)
b
.
mutex
.
Unlock
()
return
cCopy
}
...
...
@@ -139,8 +139,8 @@ func (b *Block) GetHash() (BlockHash, error) {
func
(
b
*
Block
)
GetPreviousHash
()
BlockHash
{
var
rtnBH
BlockHash
b
.
mutex
.
Lock
()
defer
b
.
mutex
.
Unlock
()
copy
(
rtnBH
[
:
],
b
.
previousHash
[
:
])
b
.
mutex
.
Unlock
()
return
rtnBH
}
...
...
@@ -148,7 +148,7 @@ func (b *Block) GetPreviousHash() BlockHash {
func
(
b
*
Block
)
GetLifecycle
()
BlockLifecycle
{
b
.
mutex
.
Lock
()
blc
:=
b
.
lifecycle
b
.
mutex
.
Unlock
()
defer
b
.
mutex
.
Unlock
()
return
blc
}
...
...
@@ -276,7 +276,7 @@ func Deserialize(sBlock []byte) (*Block, error) {
b
.
id
=
sb
.
ID
b
.
lifecycle
=
Baked
b
.
mutex
.
Unlock
()
defer
b
.
mutex
.
Unlock
()
return
&
b
,
nil
}
...
...
This diff is collapsed.
Click to expand it.
diffieHellman/dhkx.go
+
1
−
1
View file @
6d5f585d
This diff is collapsed.
Click to expand it.
fastRNG/stream.go
+
17
−
17
View file @
6d5f585d
...
...
@@ -38,7 +38,7 @@ type Stream struct {
rng
csprng
.
Source
source
[]
byte
numStream
uint
mut
sync
.
Mutex
mut
ex
sync
.
Mutex
fortunaHash
hash
.
Hash
}
...
...
@@ -81,8 +81,8 @@ func (sg *StreamGenerator) newStream() *Stream {
// If the # of open streams exceeds streamCount,
// this function blocks (and prints a log warning) until a stream is available
func
(
sg
*
StreamGenerator
)
GetStream
()
*
Stream
{
//Initialize a stream
var
retStream
*
Stream
// If there is a stream waiting to be used, take that from the channel and return in
select
{
case
retStream
=
<-
sg
.
waitingStreams
:
...
...
@@ -112,9 +112,10 @@ func (sg *StreamGenerator) Close(stream *Stream) {
// BlockSize into AES then run it until blockSize*scalingFactor bytes are read. Every time
// BlockSize*scalingFactor bytes are read this functions blocks until it rereads csprng.Source.
func
(
s
*
Stream
)
Read
(
b
[]
byte
)
(
int
,
error
)
{
s
.
mut
.
Lock
()
s
.
mutex
.
Lock
()
defer
s
.
mutex
.
Unlock
()
if
len
(
b
)
%
aes
.
BlockSize
!=
0
{
s
.
mut
.
Unlock
()
return
0
,
errors
.
New
(
"requested read length is not byte aligned"
)
}
...
...
@@ -136,7 +137,6 @@ func (s *Stream) Read(b []byte) (int, error) {
extension
=
make
([]
byte
,
aes
.
BlockSize
)
_
,
err
:=
s
.
rng
.
Read
(
extension
)
if
err
!=
nil
{
s
.
mut
.
Unlock
()
return
0
,
err
}
s
.
entropyCnt
=
s
.
streamGen
.
scalingFactor
...
...
@@ -149,7 +149,6 @@ func (s *Stream) Read(b []byte) (int, error) {
copy
(
s
.
source
,
dst
)
s
.
mut
.
Unlock
()
return
len
(
b
),
nil
}
...
...
@@ -161,11 +160,13 @@ func Fortuna(src, ext []byte, fortunaHash hash.Hash) cipher.Stream {
fortunaHash
.
Write
(
src
)
fortunaHash
.
Write
(
ext
)
key
:=
fortunaHash
.
Sum
(
nil
)
// Initialize a block cipher on that key
block
,
err
:=
aes
.
NewCipher
(
key
[
:
aes
.
BlockSize
])
if
err
!=
nil
{
jww
.
FATAL
.
Panicf
(
err
.
Error
())
}
// Encrypt the counter and place into destination
return
cipher
.
NewCTR
(
block
,
key
[
aes
.
BlockSize
:
2
*
aes
.
BlockSize
])
}
...
...
@@ -174,6 +175,5 @@ func Fortuna(src, ext []byte, fortunaHash hash.Hash) cipher.Stream {
// csprng.Source interface.
func
(
s
*
Stream
)
SetSeed
(
seed
[]
byte
)
error
{
jww
.
INFO
.
Printf
(
"Stream does not utilise SetSeed()."
)
return
nil
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment