Skip to content

Commit

Permalink
Merge pull request #11 from torusresearch/feat/ed25519
Browse files Browse the repository at this point in the history
feat/ed25519
  • Loading branch information
himanshuchawla009 authored Nov 23, 2023
2 parents 0269c38 + 65782e3 commit 1972f8a
Show file tree
Hide file tree
Showing 457 changed files with 64,022 additions and 108,482 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.DS_Store
TODOs.org
TODOs.org
.idea
/.idea/
36 changes: 36 additions & 0 deletions ed25519/curve.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package ed25519

import (
"fmt"
"github.com/torusresearch/torus-common/common"
"github.com/torusresearch/torus-common/ed25519/lib"
"golang.org/x/crypto/sha3"
)

type EdwardsCurve struct {
*lib.Ed25519Curve
}

// GeneratorOrder = 2^252 + 27742317777372353535851937790883648493 = 1000000000000000000000000000000014DEF9DEA2F79CD65812631A5CF5D3ED = Curve.N
// FieldOrder = 2^255 - 19 = 7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFED = Curve.P

var (
Curve = &EdwardsCurve{lib.Ed25519()}
G = common.Point{X: *Curve.Gx, Y: *Curve.Gy}
H = HashToPoint(G.X.Bytes())
)

func HashToPoint(data []byte) common.Point {
return common.BigIntToPoint(Curve.Hash(data))
}

func Keccak256(data ...[]byte) []byte {
d := sha3.NewLegacyKeccak256()
for _, b := range data {
n, err := d.Write(b)
if err != nil {
fmt.Printf("Could not write n %v err %v", n, err)
}
}
return d.Sum(nil)
}
173 changes: 173 additions & 0 deletions ed25519/lib/ed25519.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package lib

import (
"crypto/elliptic"
"github.com/bwesterb/go-ristretto/edwards25519"
"math/big"
"sync"
)

var ed25519Initonce sync.Once
var ed25519 Ed25519Curve

type Ed25519Curve struct {
*elliptic.CurveParams
}

func ed25519InitAll() {
generatorX, generatorY := getGenerator()
// taken from https://datatracker.ietf.org/doc/html/rfc8032
ed25519.CurveParams = new(elliptic.CurveParams)
ed25519.P, _ = new(big.Int).SetString("7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFED", 16)
ed25519.N, _ = new(big.Int).SetString("1000000000000000000000000000000014DEF9DEA2F79CD65812631A5CF5D3ED", 16)
ed25519.Gx = generatorX
ed25519.Gy = generatorY
ed25519.BitSize = 255
ed25519.Name = "ed25519"
}

func getGenerator() (*big.Int, *big.Int) {
generator := ED25519().NewGeneratorPoint()
generatorBytes := generator.ToAffineUncompressed()
var genX, genY [32]byte
copy(genX[:], generatorBytes[:32])
copy(genY[:], generatorBytes[32:])

var x, y edwards25519.FieldElement
x.SetBytes(&genX)
y.SetBytes(&genY)

return x.BigInt(), y.BigInt()
}

func Ed25519() *Ed25519Curve {
ed25519Initonce.Do(ed25519InitAll)
return &ed25519
}

func (curve *Ed25519Curve) Params() *elliptic.CurveParams {
return curve.CurveParams
}

func (curve *Ed25519Curve) IsOnCurve(x, y *big.Int) bool {
ed25519Curve := ED25519()
p, err := ed25519Curve.Point.Set(x, y)
if err != nil {
return false
}
return p.IsOnCurve()
}

func (curve *Ed25519Curve) Add(x1, y1, x2, y2 *big.Int) (*big.Int, *big.Int, error) {
ed25519Curve := ED25519()
p1, err := ed25519Curve.Point.Set(x1, y1)
if err != nil {
return nil, nil, err
}
p2, err := ed25519Curve.Point.Set(x2, y2)
if err != nil {
return nil, nil, err
}

resBytes := p1.Add(p2).ToAffineUncompressed()
var xBytes, yBytes [32]byte
copy(xBytes[:], resBytes[:32])
copy(yBytes[:], resBytes[32:])

var x, y edwards25519.FieldElement
x.SetBytes(&xBytes)
y.SetBytes(&yBytes)

return x.BigInt(), y.BigInt(), nil
}

func (curve *Ed25519Curve) Double(x1, y1 *big.Int) (*big.Int, *big.Int, error) {
ed25519Curve := ED25519()
p, err := ed25519Curve.Point.Set(x1, y1)
if err != nil {
return nil, nil, err
}

resBytes := p.Double().ToAffineUncompressed()
var xBytes, yBytes [32]byte
copy(xBytes[:], resBytes[:32])
copy(yBytes[:], resBytes[32:])

var x, y edwards25519.FieldElement
x.SetBytes(&xBytes)
y.SetBytes(&yBytes)

return x.BigInt(), y.BigInt(), nil
}

func (curve *Ed25519Curve) ScalarMult(Bx, By, k *big.Int) (*big.Int, *big.Int, error) {
ed25519Curve := ED25519()
p, err := ed25519Curve.Point.Set(Bx, By)
if err != nil {
return nil, nil, err
}

s, err := ED25519().Scalar.SetBigInt(k)
if err != nil {
return nil, nil, err
}

resBytes := p.Mul(s).ToAffineUncompressed()
var xBytes, yBytes [32]byte
copy(xBytes[:], resBytes[:32])
copy(yBytes[:], resBytes[32:])

var x, y edwards25519.FieldElement
x.SetBytes(&xBytes)
y.SetBytes(&yBytes)

return x.BigInt(), y.BigInt(), nil
}

func (curve *Ed25519Curve) ScalarBaseMult(k *big.Int) (*big.Int, *big.Int, error) {
s, err := ED25519().Scalar.SetBigInt(k)
if err != nil {
return nil, nil, err
}
resBytes := ED25519().ScalarBaseMult(s).ToAffineUncompressed()
var xBytes, yBytes [32]byte
copy(xBytes[:], resBytes[:32])
copy(yBytes[:], resBytes[32:])

var x, y edwards25519.FieldElement
x.SetBytes(&xBytes)
y.SetBytes(&yBytes)

return x.BigInt(), y.BigInt(), nil
}

func (curve *Ed25519Curve) Neg(Bx, By *big.Int) (*big.Int, *big.Int, error) {
ed25519Curve := ED25519()
p, err := ed25519Curve.Point.Set(Bx, By)
if err != nil {
return nil, nil, err
}

resBytes := p.Neg().ToAffineUncompressed()
var xBytes, yBytes [32]byte
copy(xBytes[:], resBytes[:32])
copy(yBytes[:], resBytes[32:])

var x, y edwards25519.FieldElement
x.SetBytes(&xBytes)
y.SetBytes(&yBytes)

return x.BigInt(), y.BigInt(), nil
}

func (curve *Ed25519Curve) Hash(msg []byte) (*big.Int, *big.Int) {
ed25519Curve := ED25519()
data := ed25519Curve.Point.Hash(msg).ToAffineUncompressed()
var xBytes, yBytes [32]byte
copy(xBytes[:], data[:32])
copy(yBytes[:], data[32:])
var x, y edwards25519.FieldElement
x.SetBytes(&xBytes)
y.SetBytes(&yBytes)
return x.BigInt(), y.BigInt()
}
Loading

0 comments on commit 1972f8a

Please sign in to comment.