Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: sr25519 #360

Merged
merged 117 commits into from
Jan 24, 2020
Merged

feat: sr25519 #360

merged 117 commits into from
Jan 24, 2020

Conversation

developerfred
Copy link
Contributor

@developerfred developerfred commented Nov 10, 2019

sr25519 - Module

  • accept address polkadot/substrate
  • sr22519 wait for [32]byte to input (seed,privateKey, Publickey) and respose with keys [32]bytes.

Struct files: Package sr25519 schnorrkel

.
+-- sr25519 
|   +-- sr25519test
|   |   +-- keys.go
|   +-- private.go
|   +-- public.go
|   +-- public_test.go
|   +-- end_end_test.go

Default Functions

  • PublicKeyFromBytes()
  • PrivateKeyFromBytes()

Nacl

  • decrypter
  • encrypter

fixes #327
fixes #505

@developerfred developerfred changed the title [WIP] sr25519 [Feature] sr25519 Nov 11, 2019
@developerfred developerfred changed the title [Feature] sr25519 #327 [Feature] sr25519 Nov 12, 2019
Copy link
Member

@robdefeo robdefeo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @developerfred this is a difficult issue and this looks like a promising start :) I have added some comments to the PR and also expanded the issue to clarify the scope a bit more.

I also added the prefered keys to use for sofia and charlotte this way its easier to reliably test the PR.

Thank you for your initial work on this.

crypto/secp256k1/private.go Outdated Show resolved Hide resolved
.gitignore Outdated Show resolved Hide resolved
.golangci.yml Show resolved Hide resolved
cmd/mailchain/commands/account.go Show resolved Hide resolved
crypto/cipher/nacl/end_end_test.go Outdated Show resolved Hide resolved
crypto/cipher/nacl/nacl_test.go Show resolved Hide resolved
crypto/sr25519/keys_test.go Outdated Show resolved Hide resolved
crypto/sr25519/keys_test.go Outdated Show resolved Hide resolved
crypto/sr25519/private.go Show resolved Hide resolved
crypto/sr25519/private.go Outdated Show resolved Hide resolved
func (pk PrivateKey) Bytes() []byte {
b := pk.key

return b[:]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unslice: could simplify b[:] to b (from gocritic)

@developerfred developerfred changed the title feat: sr25519 #327 feat: sr25519 Jan 6, 2020
)

// SofiaPrivateKey sr25519 key for testing purposes. Key is compromised do not use on mainnet's.
var SofiaPrivateKey crypto.PrivateKey //nolint: gochecknoglobals
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whyNoLint: include an explanation for nolint directive (from gocritic)

// SofiaPrivateKey sr25519 key for testing purposes. Key is compromised do not use on mainnet's.
var SofiaPrivateKey crypto.PrivateKey //nolint: gochecknoglobals
// SofiaPublicKey sr25519 key for testing purposes. Key is compromised do not use on mainnet's.
var SofiaPublicKey crypto.PublicKey //nolint: gochecknoglobals
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whyNoLint: include an explanation for nolint directive (from gocritic)

// SofiaPublicKey sr25519 key for testing purposes. Key is compromised do not use on mainnet's.
var SofiaPublicKey crypto.PublicKey //nolint: gochecknoglobals
// CharlottePrivateKey sr25519 key for testing purposes. Key is compromised do not use on mainnet's.
var CharlottePrivateKey crypto.PrivateKey //nolint: gochecknoglobals
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whyNoLint: include an explanation for nolint directive (from gocritic)

return k.FromUniformBytes(b)
}

func witness(nonce []byte) (*ristretto255.Scalar, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

witness - nonce is unused (from unparam)

h := sha512.Sum512(seed[:])
key := [32]byte{}
nonce := [32]byte{}
copy(key[:], h[:32])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only cuddled expressions if assigning variable or using from line above (from wsl)

if err := a.Decode(pk.key); err != nil {
return false
}
Rp := ristretto255.NewElement()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assignments should only be cuddled with other assignments (from wsl)


// https://github.com/w3f/schnorrkel/blob/718678e51006d84c7d8e4b6cde758906172e74f8/src/scalars.rs#L18
func divideScalarByCofactor(s []byte) []byte {
l := len(s) - 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mnd: Magic number: 1, in detected (from gomnd)

l := len(s) - 1
low := byte(0)
for i := range s {
r := s[l-i] & 0b00000111 // remainder
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mnd: Magic number: 0b00000111, in detected (from gomnd)

r := s[l-i] & 0b00000111 // remainder
s[l-i] >>= 3
s[l-i] += low
low = r << 5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mnd: Magic number: 5, in detected (from gomnd)

}

func (s *signature) Decode(sig []byte) error { // https://github.com/w3f/schnorrkel/blob/4112f6e8cb684a1cc6574f9097497e1e302ab9a8/src/sign.rs#L114
if len(sig) != 64 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mnd: Magic number: 64, in detected (from gomnd)

// CharlottePublicKey sr25519 key for testing purposes. Key is compromised do not use on mainnet's.
var CharlottePublicKey crypto.PublicKey //nolint: gochecknoglobals test key

//nolint: gochecknoinits
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whyNoLint: include an explanation for nolint directive (from gocritic)

//nolint: gochecknoinits
func init() {
var err error
SofiaPrivateKey, err = sr25519.PrivateKeyFromBytes(encodingtest.MustDecodeHex("5c6d7adf75bda1180c225d25f3aa8dc174bbfb3cddee11ae9a85982f6faf791a")) //nolint: lll
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whyNoLint: include an explanation for nolint directive (from gocritic)


SofiaPublicKey = SofiaPrivateKey.PublicKey()

CharlottePrivateKey, err = sr25519.PrivateKeyFromBytes(encodingtest.MustDecodeHex("23b063a581fd8e5e847c4e2b9c494247298791530f5293be369e8bf23a45d2bd")) //nolint: lll
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whyNoLint: include an explanation for nolint directive (from gocritic)

// form k
b := c.ExtractBytes(label, 64)
k := ristretto255.NewScalar()
return k.FromUniformBytes(b)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return statements should not be cuddled if block has more than two lines (from wsl)

copy(out[:32], s.R.Encode([]byte{}))
copy(out[32:], s.S.Encode([]byte{}))
out[63] |= 128
return out
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return statements should not be cuddled if block has more than two lines (from wsl)

sig[63] &= 127

s.S = ristretto255.NewScalar()
return s.S.Decode(sig[32:])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return statements should not be cuddled if block has more than two lines (from wsl)


// https://github.com/w3f/schnorrkel/blob/718678e51006d84c7d8e4b6cde758906172e74f8/src/scalars.rs#L18
func divideScalarByCofactor(s []byte) []byte {
l := len(s) - 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mnd: Magic number: 1, in detected (from gomnd)

low := byte(0)

for i := range s {
r := s[l-i] & 0b00000111 // remainder
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mnd: Magic number: 0b00000111, in detected (from gomnd)

r := s[l-i] & 0b00000111 // remainder
s[l-i] >>= 3
s[l-i] += low
low = r << 5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mnd: Magic number: 5, in detected (from gomnd)

}

func (s *signature) Decode(sig []byte) error { // https://github.com/w3f/schnorrkel/blob/4112f6e8cb684a1cc6574f9097497e1e302ab9a8/src/sign.rs#L114
if len(sig) != 64 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mnd: Magic number: 64, in detected (from gomnd)

if err != nil {
return nil, ErrSharedSecretGenerate
}
return sharedSecret[:], nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return statements should not be cuddled if block has more than two lines (from wsl)

}

func (s *signature) Decode(sig []byte) error { // https://github.com/w3f/schnorrkel/blob/4112f6e8cb684a1cc6574f9097497e1e302ab9a8/src/sign.rs#L114
if len(sig) != 64 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mnd: Magic number: 64, in detected (from gomnd)

@robdefeo robdefeo added this to In progress in protocol/substrate via automation Jan 24, 2020
protocol/substrate automation moved this from In progress to Reviewer approved Jan 24, 2020
Copy link
Member

@robdefeo robdefeo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @developerfred for all your contributions to this PR 😄 change look good, its ready to merge 👍

@robdefeo robdefeo merged commit b4be010 into mailchain:master Jan 24, 2020
protocol/substrate automation moved this from Reviewer approved to Done Jan 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

Successfully merging this pull request may close these issues.

create a key exchange for sr25519 package Support sr25519 curve
3 participants