Skip to content

Instantly share code, notes, and snippets.

@alxiong
Last active April 22, 2019 11:50
Show Gist options
  • Save alxiong/217f42ee4d0dedc6182b182134caeed9 to your computer and use it in GitHub Desktop.
Save alxiong/217f42ee4d0dedc6182b182134caeed9 to your computer and use it in GitHub Desktop.
bn256 curve testing parameters generation
package main
import (
// "crypto/rand"
"fmt"
"github.com/cloudflare/bn256"
// "io"
"math/big"
)
func main() {
// a and b are two private keys
a, _ := new(big.Int).SetString("19863503168721767052470763080964584560829382977903706668947398297116896025507", 10)
b, _ := new(big.Int).SetString("42411803530570037984902453521978100812441758711834278806081178240791067183758", 10)
// qa and qb are cooresponding public keys
qa := new(bn256.G1).ScalarBaseMult(a)
qb := new(bn256.G1).ScalarBaseMult(b)
fmt.Println(qa)
// k1 and k2 are ECDH shared key thus should equal
k1 := new(bn256.G1).ScalarMult(qa, b)
k2 := new(bn256.G1).ScalarMult(qb, a)
fmt.Println(k1)
fmt.Println(k2)
one := big.NewInt(1)
// gen is the generator of G1
gen := new(bn256.G1).ScalarBaseMult(one)
fmt.Println(gen)
// weird behavior in precompile: inputting (1,2,1) gives the right answer, while (1,2,2) would give
// different answer than the following "result" variable.
m, err := hex.DecodeString("00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002")
if err != nil {
fmt.Println(err)
}
p := new(bn256.G1)
p.Unmarshal(m)
fmt.Println(p)
result := new(bn256.G1)
result.ScalarMult(p, big.NewInt(2))
fmt.Println(result)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment