Skip to content

Instantly share code, notes, and snippets.

@benma
Created July 28, 2016 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benma/08b5fde37113ab6e7e1b24b9d7ef0c95 to your computer and use it in GitHub Desktop.
Save benma/08b5fde37113ab6e7e1b24b9d7ef0c95 to your computer and use it in GitHub Desktop.
ed255519 -> curve25519
package main
import (
"crypto/rand"
"fmt"
"github.com/agl/ed25519"
"github.com/agl/ed25519/extra25519"
"github.com/monetas/gotary/util/enc"
)
func newk() (*[32]byte, *[32]byte) {
edpk, edsk, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
panic(err)
}
cpk := new([32]byte)
if !extra25519.PublicKeyToCurve25519(cpk, edpk) {
panic("nope")
}
csk := new([32]byte)
extra25519.PrivateKeyToCurve25519(csk, edsk)
return cpk, csk
}
func main() {
ns, err := enc.NewNonceStream()
if err != nil {
panic(err)
}
for i := 0; i < 10000; i++ {
pk, sk := newk()
pk2, sk2 := newk()
cipher, err := enc.Encrypt([]byte("LOL"), pk2, sk, ns)
if err != nil {
panic(err)
}
decrypted, err := enc.Decrypt(cipher, pk, sk2)
if err != nil {
panic(err)
}
fmt.Println(string(decrypted))
}
}
@benma
Copy link
Author

benma commented Jul 28, 2016

package main

import (
    "crypto/rand"
    "fmt"

    "golang.org/x/crypto/curve25519"
    "golang.org/x/crypto/nacl/box"

    "github.com/agl/ed25519/extra25519"
)

func main() {
    n := 1000
    failed := 0
    repr := new([32]byte)
    for i := 0; i < n; i++ {
        pk, sk, err := box.GenerateKey(rand.Reader)
        if err != nil {
            panic(err)
        }
        pk2 := new([32]byte)
        curve25519.ScalarBaseMult(pk2, sk)
        if *pk != *pk2 {
            panic("nope")
        }

        if !extra25519.ScalarBaseMult(pk2, repr, sk) {
            failed++
        }
    }
    fmt.Println("failed ", 100*float32(failed)/float32(n), "%")
}

Output: failed 50.3 %

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment