sonr/crypto/sharing/v1/bls12381g2curve.go
Prad Nukala 807b2e86ec
feature/1220 origin handle exists method (#1241)
* feat: add docs and CI workflow for publishing to onsonr.dev

* (refactor): Move hway,motr executables to their own repos

* feat: simplify devnet and testnet configurations

* refactor: update import path for didcrypto package

* docs(networks): Add README with project overview, architecture, and community links

* refactor: Move network configurations to deploy directory

* build: update golang version to 1.23

* refactor: move logger interface to appropriate package

* refactor: Move devnet configuration to networks/devnet

* chore: improve release process with date variable

* (chore): Move Crypto Library

* refactor: improve code structure and readability in DID module

* feat: integrate Trunk CI checks

* ci: optimize CI workflow by removing redundant build jobs

---------

Co-authored-by: Darp Alakun <i@prad.nu>
2025-01-06 17:06:10 +00:00

109 lines
3.1 KiB
Go
Executable File

//
// Copyright Coinbase, Inc. All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//
package v1
import (
"crypto/elliptic"
"math/big"
"sync"
"github.com/onsonr/sonr/crypto/core/curves/native"
"github.com/onsonr/sonr/crypto/core/curves/native/bls12381"
)
var (
bls12381g2Initonce sync.Once
bls12381g2 Bls12381G2Curve
)
type Bls12381G2Curve struct {
*elliptic.CurveParams
}
func bls12381g2InitAll() {
bls12381g2.CurveParams = new(elliptic.CurveParams)
bls12381g2.P, _ = new(big.Int).SetString("1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab", 16)
bls12381g2.N = bls12381.Bls12381FqNew().Params.BiModulus
bls12381g2.B, _ = new(big.Int).SetString("0bbc3efc5008a26a0e1c8c3fad0059c051ac582950405194dd595f13570725ce8c22631a7918fd8ebaac93d50ce72271", 16)
bls12381g2.Gx, _ = new(big.Int).SetString("120177419e0bfb75edce6ecc21dbf440f0ae6acdf3d0e747154f95c7143ba1c17817fc679976fff55cb38790fd530c16", 16)
bls12381g2.Gy, _ = new(big.Int).SetString("0bbc3efc5008a26a0e1c8c3fad0059c051ac582950405194dd595f13570725ce8c22631a7918fd8ebaac93d50ce72271", 16)
bls12381g2.BitSize = 381
bls12381g2.Name = "Bls12381G1"
}
func Bls12381G2() *Bls12381G1Curve {
bls12381g2Initonce.Do(bls12381g2InitAll)
return &bls12381g1
}
func (curve *Bls12381G2Curve) Params() *elliptic.CurveParams {
return curve.CurveParams
}
func (curve *Bls12381G2Curve) IsOnCurve(x, y *big.Int) bool {
_, err := new(bls12381.G2).SetBigInt(x, y)
return err == nil
}
func (curve *Bls12381G2Curve) Add(x1, y1, x2, y2 *big.Int) (*big.Int, *big.Int) {
p1, err1 := new(bls12381.G2).SetBigInt(x1, y1)
p2, err2 := new(bls12381.G2).SetBigInt(x2, y2)
if err1 != nil || err2 != nil {
return nil, nil
}
return p1.Add(p1, p2).BigInt()
}
func (curve *Bls12381G2Curve) Double(x1, y1 *big.Int) (*big.Int, *big.Int) {
p, err := new(bls12381.G2).SetBigInt(x1, y1)
if err != nil {
return nil, nil
}
return p.Double(p).BigInt()
}
func (curve *Bls12381G2Curve) ScalarMult(Bx, By *big.Int, k []byte) (*big.Int, *big.Int) {
p, err := new(bls12381.G2).SetBigInt(Bx, By)
if err != nil {
return nil, nil
}
var bb [native.FieldBytes]byte
copy(bb[:], k)
s, err := bls12381.Bls12381FqNew().SetBytes(&bb)
if err != nil {
return nil, nil
}
return p.Mul(p, s).BigInt()
}
func (curve *Bls12381G2Curve) ScalarBaseMult(k []byte) (*big.Int, *big.Int) {
p := new(bls12381.G2).Generator()
var bb [native.FieldBytes]byte
copy(bb[:], k)
s, err := bls12381.Bls12381FqNew().SetBytes(&bb)
if err != nil {
return nil, nil
}
return p.Mul(p, s).BigInt()
}
// Hash an arbitrary byte sequence to a G1 point according to the hash-to-curve standard
func (curve *Bls12381G2Curve) Hash(msg []byte) (*big.Int, *big.Int) {
return new(bls12381.G1).Hash(native.EllipticPointHasherSha256(), msg, []byte("BLS12381G2_XMD:SHA-256_SSWU_RO_")).BigInt()
}
// CompressedBytesFromBigInts takes x and y coordinates and converts them to the BLS compressed point form
func (curve *Bls12381G2Curve) CompressedBytesFromBigInts(x, y *big.Int) ([]byte, error) {
p, err := new(bls12381.G2).SetBigInt(x, y)
if err != nil {
return nil, err
}
out := p.ToCompressed()
return out[:], nil
}