Last active
June 26, 2024 05:13
-
-
Save HamdaanAliQuatil/e186aa1a9a1b40e550303a58c47cb709 to your computer and use it in GitHub Desktop.
Strong Diffie Hellman Inversion using Cloudflare's CIRCL library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"crypto/rand" | |
"crypto/sha256" | |
"fmt" | |
"github.com/cloudflare/circl/ecc/bls12381" | |
) | |
type KDF struct { | |
curve *bls12381.G1 | |
} | |
func hashToScalar(attributes [][]byte) *bls12381.Scalar { | |
h := sha256.New() | |
for _, attr := range attributes { | |
h.Write(attr) | |
} | |
digest := h.Sum(nil) | |
var s bls12381.Scalar | |
s.SetBytes(digest) | |
return &s | |
} | |
func generatePrimaryKey(kdf *KDF) (*bls12381.Scalar, *bls12381.G1, error) { | |
sk := new(bls12381.Scalar) | |
sk.Random(rand.Reader) | |
pk := new(bls12381.G1) | |
pk.ScalarMult(sk, bls12381.G1Generator()) | |
return sk, pk, nil | |
} | |
func deriveKeyPair( | |
kdf *KDF, | |
sk *bls12381.Scalar, | |
attributes [][]byte, | |
) (*bls12381.G1, error) { | |
attrHash := hashToScalar(attributes) | |
sk.Add(sk, attrHash) | |
sk.Inv(sk) | |
pk := new(bls12381.G1) | |
pk.ScalarMult(sk, bls12381.G1Generator()) | |
return pk, nil | |
} | |
func main() { | |
kdf := &KDF{ | |
curve: bls12381.G1Generator(), | |
} | |
// Generate primary key | |
sk, pk, err := generatePrimaryKey(kdf) | |
if err != nil { | |
fmt.Println("Error generating primary key:", err) | |
return | |
} | |
fmt.Println("Primary Key (Private):", sk) | |
fmt.Println("Primary Key (Public):", pk) | |
// Example attributes | |
attributes := [][]byte{ | |
[]byte("strong-diffie"), | |
[]byte("hellman-inversion"), | |
} | |
// Derive key pair | |
derivedPK, err := deriveKeyPair(kdf, sk, attributes) | |
if err != nil { | |
fmt.Println("Error deriving key pair:", err) | |
return | |
} | |
fmt.Println("Derived Public Key:", derivedPK) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> go run "c:\Users\Hamdaan\Desktop\go\circl.go" | |
Primary Key (Private): 0x61af1b490c1f1eeacb92d67814a8bfa9ec0aac7ab6d6b3040582e943598a0e07 | |
Primary Key (Public): x: 0x0880273608cd5c7a61aaee18a2b7931dc0106191dc503ee289ac69448f1577b4388b5f7add7b749c379467abed1d2d74 | |
y: 0x0881c6222aba2096ca39fb1f8d647fc4ee156b20b88529a04968789ef7d09946d463d48208240c6c0064a01847009797 | |
z: 0x1622bd1cde8077331f68506388710b7854cf77606b4d4a04a045afbb5bd0e4e06ce3bd754b9f4aea1eb825532acbc409 | |
Derived Public Key: x: 0x17e2c736aa2cd58887e186465a98eb7f4920a66ddfe9d2c8069e832e2d6befb371ba192aea9a2021a5a9ac6a8111dbcd | |
y: 0x0403d1d08757f65109b14417cda6983822dbfba4ea233574613fa84aefaba1345140012e92b733d4ddc0adcf07426b78 | |
z: 0x0c3571215212066ba7eca5defd9b699400a29b5677520f40c6fb9b1a5f68fe90bb430178b58efb8c2a7e032078f9ef6c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment