Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
dup-crypto-rs
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
nodes
rust
modules
dup-crypto-rs
Commits
c3d8af94
Commit
c3d8af94
authored
5 years ago
by
Éloïs
Browse files
Options
Downloads
Patches
Plain Diff
[fix] ed25519:pubkey don't have min size. empty pubkey must be supported
parent
8068663f
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/bases/b58.rs
+6
-1
6 additions, 1 deletion
src/bases/b58.rs
src/keys/ed25519.rs
+14
-17
14 additions, 17 deletions
src/keys/ed25519.rs
src/keys/mod.rs
+8
-18
8 additions, 18 deletions
src/keys/mod.rs
with
28 additions
and
36 deletions
src/bases/b58.rs
+
6
−
1
View file @
c3d8af94
...
@@ -25,7 +25,12 @@ pub trait ToBase58 {
...
@@ -25,7 +25,12 @@ pub trait ToBase58 {
/// Create an array of 32 bytes from a Base58 string.
/// Create an array of 32 bytes from a Base58 string.
pub
fn
str_base58_to_32bytes
(
base58_data
:
&
str
)
->
Result
<
([
u8
;
32
],
usize
),
BaseConvertionError
>
{
pub
fn
str_base58_to_32bytes
(
base58_data
:
&
str
)
->
Result
<
([
u8
;
32
],
usize
),
BaseConvertionError
>
{
match
bs58
::
decode
(
base58_data
)
.into_vec
()
{
let
mut
source
=
base58_data
;
while
!
source
.is_empty
()
&&
&
source
[
0
..
1
]
==
"1"
{
source
=
&
source
[
1
..
];
}
match
bs58
::
decode
(
source
)
.into_vec
()
{
Ok
(
result
)
=>
{
Ok
(
result
)
=>
{
let
len
=
result
.len
();
let
len
=
result
.len
();
if
len
<=
32
{
if
len
<=
32
{
...
...
This diff is collapsed.
Click to expand it.
src/keys/ed25519.rs
+
14
−
17
View file @
c3d8af94
...
@@ -43,11 +43,9 @@ use unwrap::unwrap;
...
@@ -43,11 +43,9 @@ use unwrap::unwrap;
use
zeroize
::
Zeroize
;
use
zeroize
::
Zeroize
;
/// Maximal size of a public key in bytes
/// Maximal size of a public key in bytes
pub
static
PUBKEY_SIZE_IN_BYTES
:
&
usize
=
&
32
;
pub
const
PUBKEY_SIZE_IN_BYTES
:
usize
=
32
;
/// Minimal size of a public key in bytes
/// constf a signature in bytes
pub
static
PUBKEY_MIN_SIZE_IN_BYTES
:
&
usize
=
&
31
;
pub
const
SIG_SIZE_IN_BYTES
:
usize
=
64
;
/// Size of a signature in bytes
pub
static
SIG_SIZE_IN_BYTES
:
&
usize
=
&
64
;
/// Store a ed25519 signature.
/// Store a ed25519 signature.
#[derive(Clone,
Copy)]
#[derive(Clone,
Copy)]
...
@@ -167,7 +165,7 @@ impl Default for PublicKey {
...
@@ -167,7 +165,7 @@ impl Default for PublicKey {
fn
default
()
->
Self
{
fn
default
()
->
Self
{
PublicKey
{
PublicKey
{
datas
:
[
0u8
;
32
],
datas
:
[
0u8
;
32
],
len
:
32
,
len
:
0
,
}
}
}
}
}
}
...
@@ -182,9 +180,9 @@ impl TryFrom<&[u8]> for PublicKey {
...
@@ -182,9 +180,9 @@ impl TryFrom<&[u8]> for PublicKey {
type
Error
=
PubkeyFromBytesError
;
type
Error
=
PubkeyFromBytesError
;
fn
try_from
(
bytes
:
&
[
u8
])
->
Result
<
Self
,
Self
::
Error
>
{
fn
try_from
(
bytes
:
&
[
u8
])
->
Result
<
Self
,
Self
::
Error
>
{
if
bytes
.len
()
>
*
PUBKEY_SIZE_IN_BYTES
||
bytes
.len
()
<
*
PUBKEY_MIN_SIZE_IN_BYTES
{
if
bytes
.len
()
>
PUBKEY_SIZE_IN_BYTES
{
Err
(
PubkeyFromBytesError
::
InvalidBytesLen
{
Err
(
PubkeyFromBytesError
::
InvalidBytesLen
{
expected
:
*
PUBKEY_SIZE_IN_BYTES
,
expected
:
PUBKEY_SIZE_IN_BYTES
,
found
:
bytes
.len
(),
found
:
bytes
.len
(),
})
})
}
else
{
}
else
{
...
@@ -223,15 +221,8 @@ impl super::PublicKey for PublicKey {
...
@@ -223,15 +221,8 @@ impl super::PublicKey for PublicKey {
#[inline]
#[inline]
fn
from_base58
(
base58_data
:
&
str
)
->
Result
<
Self
,
BaseConvertionError
>
{
fn
from_base58
(
base58_data
:
&
str
)
->
Result
<
Self
,
BaseConvertionError
>
{
let
(
datas
,
len
)
=
b58
::
str_base58_to_32bytes
(
base58_data
)
?
;
let
(
datas
,
len
)
=
b58
::
str_base58_to_32bytes
(
base58_data
)
?
;
if
len
<
*
PUBKEY_MIN_SIZE_IN_BYTES
{
Err
(
BaseConvertionError
::
InvalidLength
{
expected
:
*
PUBKEY_SIZE_IN_BYTES
,
found
:
len
,
})
}
else
{
Ok
(
PublicKey
{
datas
,
len
})
Ok
(
PublicKey
{
datas
,
len
})
}
}
}
fn
to_bytes_vector
(
&
self
)
->
Vec
<
u8
>
{
fn
to_bytes_vector
(
&
self
)
->
Vec
<
u8
>
{
self
.as_ref
()
.to_vec
()
self
.as_ref
()
.to_vec
()
...
@@ -489,6 +480,12 @@ mod tests {
...
@@ -489,6 +480,12 @@ mod tests {
);
);
}
}
#[test]
fn
test_pubkey_111_from_base58
()
{
let
public58
=
"11111111111111111111111111111111111111111111"
;
let
_
=
unwrap!
(
super
::
PublicKey
::
from_base58
(
public58
));
}
#[test]
#[test]
fn
base58_public_key
()
{
fn
base58_public_key
()
{
let
public58
=
"DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV"
;
let
public58
=
"DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV"
;
...
@@ -578,7 +575,7 @@ mod tests {
...
@@ -578,7 +575,7 @@ mod tests {
// Test signature serialization/deserialization
// Test signature serialization/deserialization
let
mut
bin_sig
=
bincode
::
serialize
(
&
signature
)
.expect
(
"Fail to serialize signature !"
);
let
mut
bin_sig
=
bincode
::
serialize
(
&
signature
)
.expect
(
"Fail to serialize signature !"
);
assert_eq!
(
*
SIG_SIZE_IN_BYTES
,
bin_sig
.len
());
assert_eq!
(
SIG_SIZE_IN_BYTES
,
bin_sig
.len
());
assert_eq!
(
signature
.to_bytes_vector
(),
bin_sig
);
assert_eq!
(
signature
.to_bytes_vector
(),
bin_sig
);
assert_eq!
(
assert_eq!
(
signature
,
signature
,
...
...
This diff is collapsed.
Click to expand it.
src/keys/mod.rs
+
8
−
18
View file @
c3d8af94
...
@@ -162,7 +162,7 @@ impl Sig {
...
@@ -162,7 +162,7 @@ impl Sig {
/// Get Sig size in bytes
/// Get Sig size in bytes
pub
fn
size_in_bytes
(
&
self
)
->
usize
{
pub
fn
size_in_bytes
(
&
self
)
->
usize
{
match
*
self
{
match
*
self
{
Sig
::
Ed25519
(
_
)
=>
*
ed25519
::
SIG_SIZE_IN_BYTES
+
2
,
Sig
::
Ed25519
(
_
)
=>
ed25519
::
SIG_SIZE_IN_BYTES
+
2
,
Sig
::
Schnorr
()
=>
panic!
(
"Schnorr algo not yet supported !"
),
Sig
::
Schnorr
()
=>
panic!
(
"Schnorr algo not yet supported !"
),
}
}
}
}
...
@@ -494,7 +494,7 @@ mod tests {
...
@@ -494,7 +494,7 @@ mod tests {
let
sig_str_b64
=
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=="
.to_owned
();
let
sig_str_b64
=
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=="
.to_owned
();
let
sig
=
Sig
::
Ed25519
(
ed25519
::
Signature
(
sig_bytes
));
let
sig
=
Sig
::
Ed25519
(
ed25519
::
Signature
(
sig_bytes
));
assert_eq!
(
sig
.size_in_bytes
(),
*
ed25519
::
SIG_SIZE_IN_BYTES
+
2
);
assert_eq!
(
sig
.size_in_bytes
(),
ed25519
::
SIG_SIZE_IN_BYTES
+
2
);
assert_eq!
(
sig_str_b64
,
format!
(
"{}"
,
sig
));
assert_eq!
(
sig_str_b64
,
format!
(
"{}"
,
sig
));
assert_eq!
(
KeysAlgo
::
Ed25519
,
sig
.algo
());
assert_eq!
(
KeysAlgo
::
Ed25519
,
sig
.algo
());
...
@@ -508,10 +508,7 @@ mod tests {
...
@@ -508,10 +508,7 @@ mod tests {
#[test]
#[test]
fn
pubkey
()
{
fn
pubkey
()
{
let
pubkey_default
=
PubKey
::
default
();
let
pubkey_default
=
PubKey
::
default
();
let
pubkey_bytes
=
[
let
pubkey_bytes
=
[];
0u8
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
];
let
pubkey
=
PubKey
::
Ed25519
(
unwrap!
(
ed25519
::
PublicKey
::
try_from
(
&
pubkey_bytes
[
..
])));
let
pubkey
=
PubKey
::
Ed25519
(
unwrap!
(
ed25519
::
PublicKey
::
try_from
(
&
pubkey_bytes
[
..
])));
assert_eq!
(
pubkey_default
,
pubkey
);
assert_eq!
(
pubkey_default
,
pubkey
);
...
@@ -521,15 +518,15 @@ mod tests {
...
@@ -521,15 +518,15 @@ mod tests {
PubKey
::
from_str
(
&
pubkey_str_b58
)
.expect
(
"Fail to parse pubkey !"
)
PubKey
::
from_str
(
&
pubkey_str_b58
)
.expect
(
"Fail to parse pubkey !"
)
);
);
assert_eq!
(
pubkey
.size_in_bytes
(),
*
ed25519
::
PUBKEY_SIZE_IN_BYTES
+
3
);
assert_eq!
(
pubkey
.size_in_bytes
(),
ed25519
::
PUBKEY_SIZE_IN_BYTES
+
3
);
assert_eq!
(
pubkey_str_b58
,
format!
(
"{}"
,
pubkey
));
assert_eq!
(
""
,
&
format!
(
"{}"
,
pubkey
));
assert_eq!
(
KeysAlgo
::
Ed25519
,
pubkey
.algo
());
assert_eq!
(
KeysAlgo
::
Ed25519
,
pubkey
.algo
());
assert_eq!
(
KeysAlgo
::
Schnorr
,
PubKey
::
Schnorr
()
.algo
());
assert_eq!
(
KeysAlgo
::
Schnorr
,
PubKey
::
Schnorr
()
.algo
());
assert_eq!
(
pubkey_bytes
.to_vec
(),
pubkey
.to_bytes_vector
());
assert_eq!
(
pubkey_bytes
.to_vec
(),
pubkey
.to_bytes_vector
());
assert_eq!
(
pubkey_str_b58
,
pubkey
.to_base58
());
assert_eq!
(
""
,
&
pubkey
.to_base58
());
assert_eq!
(
assert_eq!
(
Err
(
SigError
::
InvalidSig
),
Err
(
SigError
::
InvalidSig
),
...
@@ -721,14 +718,7 @@ mod tests {
...
@@ -721,14 +718,7 @@ mod tests {
fn
pubkey_from_bytes
()
{
fn
pubkey_from_bytes
()
{
assert_eq!
(
assert_eq!
(
Err
(
PubkeyFromBytesError
::
InvalidBytesLen
{
Err
(
PubkeyFromBytesError
::
InvalidBytesLen
{
expected
:
*
ed25519
::
PUBKEY_SIZE_IN_BYTES
,
expected
:
ed25519
::
PUBKEY_SIZE_IN_BYTES
,
found
:
2
,
}),
PubKey
::
from_bytes
(
&
[
0u8
,
1
]),
);
assert_eq!
(
Err
(
PubkeyFromBytesError
::
InvalidBytesLen
{
expected
:
*
ed25519
::
PUBKEY_SIZE_IN_BYTES
,
found
:
33
,
found
:
33
,
}),
}),
PubKey
::
from_bytes
(
&
[
PubKey
::
from_bytes
(
&
[
...
@@ -738,7 +728,7 @@ mod tests {
...
@@ -738,7 +728,7 @@ mod tests {
);
);
assert_eq!
(
assert_eq!
(
Ok
(
PubKey
::
Ed25519
(
ed25519
::
PublicKey
::
default
())),
Ok
(
PubKey
::
Ed25519
(
ed25519
::
PublicKey
::
default
())),
PubKey
::
from_bytes
(
&
[
0u8
;
32
]),
PubKey
::
from_bytes
(
&
[]),
);
);
}
}
}
}
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