mirror of
https://github.com/onsonr/sonr.git
synced 2025-03-10 21:09:11 +00:00
* refactor: remove redundant branch trigger for scheduled releases * refactor: simplify process-compose commands and improve logging * refactor: remove redundant command * refactor: remove unused error variables and simplify database configuration * feat: introduce task runner for project automation * refactor: Remove hardcoded action and method from form components * refactor: move server setup to main.go and add prometheus metrics * refactor: move index handlers to render handlers * refactor: improve user identification logic in gateway and vault handlers * refactor: rename TitleDescription to TitleDesc for consistency * feat: integrate go-useragent library for enhanced user agent parsing * feat: enhance initial view rendering based on device type * feat: Add support for PostgreSQL database * fix: Use formatPsqlDSN() to properly set PostgreSQL DSN from command flags * feat: Add PostgreSQL support with fallback to SQLite in NewGormDB * feat: Add PostgreSQL connection validation with SQLite fallback * chore: update golang.org/x/crypto dependency to v0.31.0 * feat: add PKL-based configuration initialization * refactor: improve file naming consistency in cmd/sonrd * refactor: Improve init-pkl command with safer config file generation and error handling * fix: add logging for pkl evaluation results * refactor: Move credential handling to gateway context * refactor: Migrate session models to gateway package * refactor: rename models and update User model * chore: initial commit for address and pubkey functionality * refactor: move pubkey package to keys package * refactor: Rename models and add resolver service * feat: add gRPC clients for bank, DID, DWN, and SVC modules * refactor: Migrate title and description components from text package to hero package * refactor: improve file naming conventions * feat: add user credential validation * refactor: rename registration handlers and routes for clarity * <no value> * refactor: Decouple database and IPFS interactions from server setup * refactor: Migrate configuration from class-based to TOML-based structure * refactor: move network configuration files to sonr.net module * feature/1120-leverage-service-authorization * fix: correct DID identifier creation function name * feat: add compressed and uncompressed public keys to keyset * refactor: move address packages to crypto/address * feat: implement pubkey verification * refactor: remove ECDSA-related functions from keyshare and protocol modules * feat: Implement ECDSA signature serialization * <no value> * feat: add vault service for IPFS token storage * refactor: update ucan codec to use new DID generation method * refactor: refactor key management and move address parsers to keys package * refactor: rename key parsers and move to parsers package * fix: resolved import issues with the new spec * feat: improve user onboarding experience by updating button text and functionality * refactor: update point marshaling and unmarshaling methods to use JSON * refactor: remove unnecessary DID method from PubKey * refactor: Rename and refactor MPC key generation functions * test: Add comprehensive test suite for keyshare generation and validation * test: Fix keyshare role validation and encoding tests * feat: Update key share role tests with enclave initialization validation * test(mpc): refactor tests to focus on public API and remove internal role checks * refactor: Remove unnecessary role check in initKeyEnclave function * fix: Enforce strict order for validator and user keyshares in enclave initialization * fix: Update codec_test to match latest codec implementation * refactor: Update KeyEnclave to use string-based key shares and improve error handling * fix: Refactor MPC enclave to use string-based encoding and simplify key management * refactor: Remove redundant keyshare decoding tests in codec_test.go * fix: Resolve type conversion issues in MPC crypto enclave initialization * fix: Convert CID to byte slice in addEnclaveIPFS function * fix: Resolve type conversion and constant definition errors in MPC crypto utils * refactor: Simplify KeyShare encoding and role handling in MPC codec * fix: Resolve JSON unmarshaling type mismatch in KeyShare.Message() * fix: Refactor KeyEnclave to use struct and Enclave interface * fix: Resolve type and naming conflicts in MPC crypto package * refactor: Update codec_test.go to use new KeyEnclave struct fields * refactor: remove keyshare encoding and decoding logic * refactor: Remove unused JSON marshaling functions for curve points * fix: Improve signature serialization and deserialization in MPC crypto This commit addresses several issues with signature handling: - Fixed signature length to 65 bytes - Added proper padding for R and S values - Added nil and zero value checks - Improved error messages for signature parsing The changes ensure more robust signature encoding and decoding, preventing potential nil pointer and invalid signature issues. * fix: Update signature serialization to match protocol test approach * refactor: Simplify KeyEnclave struct and improve message handling * fix: Improve signature serialization and verification in MPC crypto module * refactor: Simplify enclave validation using IsValid method in test * refactor: Add marshaling and comprehensive tests for KeyEnclave * feat: Add JSON marshaling support for Point in KeyEnclave * refactor: Rename KeyEnclave to Enclave and update related functions * refactor: Update PubKey verification to use SHA3-256 hashing * test: Add comprehensive tests for DID and PubKey implementations * refactor: simplify DID key retrieval * test: refactor CI workflow and remove unused DIDAuth middleware * The changes look good! The updated workflows will now: 1. Run tests on push to master 2. Bump the version if the commit doesn't already start with 'bump:' 3. Trigger a release workflow automatically with the new version tag 4. Create and publish the release A few things to note: - Make sure you have the `peter-evans/repository-dispatch` action installed/available - The `commitizen-tools/commitizen-action` should output the new tag for this to work - Ensure your release workflow can handle the repository dispatch event Would you like me to review or suggest any additional modifications to the workflows? * ci(github actions): add build stage dependency for tests * fix(workflow): update workflow to trigger on PR edits * test: Update unit test dependencies * ci: Add GoReleaser dry-run check for merge group events * test: remove unnecessary dependencies between test jobs * ci: Make race and coverage tests depend on build tests
757 lines
16 KiB
Go
Executable File
757 lines
16 KiB
Go
Executable File
//
|
|
// Copyright Coinbase, Inc. All Rights Reserved.
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
|
|
package curves
|
|
|
|
import (
|
|
"crypto/elliptic"
|
|
crand "crypto/rand"
|
|
"crypto/sha256"
|
|
"crypto/subtle"
|
|
"fmt"
|
|
"io"
|
|
"math/big"
|
|
"testing"
|
|
|
|
"github.com/onsonr/sonr/crypto/core"
|
|
)
|
|
|
|
func BenchmarkP256(b *testing.B) {
|
|
// 1000 points
|
|
|
|
b.Run("1000 point hash - p256", func(b *testing.B) {
|
|
b.StopTimer()
|
|
points := make([][]byte, 1000)
|
|
for i := range points {
|
|
t := make([]byte, 32)
|
|
_, _ = crand.Read(t)
|
|
points[i] = t
|
|
}
|
|
acc := new(BenchPointP256).Identity()
|
|
b.StartTimer()
|
|
for _, pt := range points {
|
|
acc = acc.Hash(pt)
|
|
}
|
|
})
|
|
|
|
b.Run("1000 point hash - ct p256", func(b *testing.B) {
|
|
b.StopTimer()
|
|
points := make([][]byte, 1000)
|
|
for i := range points {
|
|
t := make([]byte, 32)
|
|
_, _ = crand.Read(t)
|
|
points[i] = t
|
|
}
|
|
acc := new(PointP256).Identity()
|
|
b.StartTimer()
|
|
for _, pt := range points {
|
|
acc = acc.Hash(pt)
|
|
}
|
|
})
|
|
|
|
b.Run("1000 point add - p256", func(b *testing.B) {
|
|
b.StopTimer()
|
|
points := make([]*BenchPointP256, 1000)
|
|
for i := range points {
|
|
points[i] = points[i].Random(crand.Reader).(*BenchPointP256)
|
|
}
|
|
acc := new(BenchPointP256).Identity()
|
|
b.StartTimer()
|
|
for _, pt := range points {
|
|
acc = acc.Add(pt)
|
|
}
|
|
})
|
|
b.Run("1000 point add - ct p256", func(b *testing.B) {
|
|
b.StopTimer()
|
|
curve := P256()
|
|
points := make([]*PointP256, 1000)
|
|
for i := range points {
|
|
points[i] = curve.NewIdentityPoint().Random(crand.Reader).(*PointP256)
|
|
}
|
|
acc := curve.NewIdentityPoint()
|
|
b.StartTimer()
|
|
for _, pt := range points {
|
|
acc = acc.Add(pt)
|
|
}
|
|
})
|
|
b.Run("1000 point double - p256", func(b *testing.B) {
|
|
b.StopTimer()
|
|
acc := new(BenchPointP256).Generator()
|
|
b.StartTimer()
|
|
for i := 0; i < 1000; i++ {
|
|
acc = acc.Double()
|
|
}
|
|
})
|
|
b.Run("1000 point double - ct p256", func(b *testing.B) {
|
|
b.StopTimer()
|
|
acc := new(PointP256).Generator()
|
|
b.StartTimer()
|
|
for i := 0; i < 1000; i++ {
|
|
acc = acc.Double()
|
|
}
|
|
})
|
|
b.Run("1000 point multiply - p256", func(b *testing.B) {
|
|
b.StopTimer()
|
|
scalars := make([]*BenchScalarP256, 1000)
|
|
for i := range scalars {
|
|
s := new(BenchScalarP256).Random(crand.Reader)
|
|
scalars[i] = s.(*BenchScalarP256)
|
|
}
|
|
acc := new(BenchPointP256).Generator().Mul(new(BenchScalarP256).New(2))
|
|
b.StartTimer()
|
|
for _, sc := range scalars {
|
|
acc = acc.Mul(sc)
|
|
}
|
|
})
|
|
b.Run("1000 point multiply - ct p256", func(b *testing.B) {
|
|
b.StopTimer()
|
|
scalars := make([]*ScalarP256, 1000)
|
|
for i := range scalars {
|
|
s := new(ScalarP256).Random(crand.Reader)
|
|
scalars[i] = s.(*ScalarP256)
|
|
}
|
|
acc := new(PointP256).Generator()
|
|
b.StartTimer()
|
|
for _, sc := range scalars {
|
|
acc = acc.Mul(sc)
|
|
}
|
|
})
|
|
b.Run("1000 scalar invert - p256", func(b *testing.B) {
|
|
b.StopTimer()
|
|
scalars := make([]*BenchScalarP256, 1000)
|
|
for i := range scalars {
|
|
s := new(BenchScalarP256).Random(crand.Reader)
|
|
scalars[i] = s.(*BenchScalarP256)
|
|
}
|
|
b.StartTimer()
|
|
for _, sc := range scalars {
|
|
_, _ = sc.Invert()
|
|
}
|
|
})
|
|
b.Run("1000 scalar invert - ct p256", func(b *testing.B) {
|
|
b.StopTimer()
|
|
scalars := make([]*ScalarP256, 1000)
|
|
for i := range scalars {
|
|
s := new(ScalarP256).Random(crand.Reader)
|
|
scalars[i] = s.(*ScalarP256)
|
|
}
|
|
b.StartTimer()
|
|
for _, sc := range scalars {
|
|
_, _ = sc.Invert()
|
|
}
|
|
})
|
|
b.Run("1000 scalar sqrt - p256", func(b *testing.B) {
|
|
b.StopTimer()
|
|
scalars := make([]*BenchScalarP256, 1000)
|
|
for i := range scalars {
|
|
s := new(BenchScalarP256).Random(crand.Reader)
|
|
scalars[i] = s.(*BenchScalarP256)
|
|
}
|
|
b.StartTimer()
|
|
for _, sc := range scalars {
|
|
_, _ = sc.Sqrt()
|
|
}
|
|
})
|
|
b.Run("1000 scalar sqrt - ct p256", func(b *testing.B) {
|
|
b.StopTimer()
|
|
scalars := make([]*ScalarP256, 1000)
|
|
for i := range scalars {
|
|
s := new(ScalarP256).Random(crand.Reader)
|
|
scalars[i] = s.(*ScalarP256)
|
|
}
|
|
b.StartTimer()
|
|
for _, sc := range scalars {
|
|
_, _ = sc.Sqrt()
|
|
}
|
|
})
|
|
}
|
|
|
|
type BenchScalarP256 struct {
|
|
value *big.Int
|
|
}
|
|
|
|
type BenchPointP256 struct {
|
|
x, y *big.Int
|
|
}
|
|
|
|
func (s *BenchScalarP256) Random(reader io.Reader) Scalar {
|
|
if reader == nil {
|
|
return nil
|
|
}
|
|
var seed [64]byte
|
|
_, _ = reader.Read(seed[:])
|
|
return s.Hash(seed[:])
|
|
}
|
|
|
|
func (s *BenchScalarP256) Hash(bytes []byte) Scalar {
|
|
xmd, err := expandMsgXmd(sha256.New(), bytes, []byte("P256_XMD:SHA-256_SSWU_RO_"), 48)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
v := new(big.Int).SetBytes(xmd)
|
|
return &BenchScalarP256{
|
|
value: v.Mod(v, elliptic.P256().Params().N),
|
|
}
|
|
}
|
|
|
|
func (s *BenchScalarP256) Zero() Scalar {
|
|
return &BenchScalarP256{
|
|
value: big.NewInt(0),
|
|
}
|
|
}
|
|
|
|
func (s *BenchScalarP256) One() Scalar {
|
|
return &BenchScalarP256{
|
|
value: big.NewInt(1),
|
|
}
|
|
}
|
|
|
|
func (s *BenchScalarP256) IsZero() bool {
|
|
return subtle.ConstantTimeCompare(s.value.Bytes(), []byte{}) == 1
|
|
}
|
|
|
|
func (s *BenchScalarP256) IsOne() bool {
|
|
return subtle.ConstantTimeCompare(s.value.Bytes(), []byte{1}) == 1
|
|
}
|
|
|
|
func (s *BenchScalarP256) IsOdd() bool {
|
|
return s.value.Bit(0) == 1
|
|
}
|
|
|
|
func (s *BenchScalarP256) IsEven() bool {
|
|
return s.value.Bit(0) == 0
|
|
}
|
|
|
|
func (s *BenchScalarP256) New(value int) Scalar {
|
|
v := big.NewInt(int64(value))
|
|
if value < 0 {
|
|
v.Mod(v, elliptic.P256().Params().N)
|
|
}
|
|
return &BenchScalarP256{
|
|
value: v,
|
|
}
|
|
}
|
|
|
|
func (s *BenchScalarP256) Cmp(rhs Scalar) int {
|
|
r, ok := rhs.(*BenchScalarP256)
|
|
if ok {
|
|
return s.value.Cmp(r.value)
|
|
} else {
|
|
return -2
|
|
}
|
|
}
|
|
|
|
func (s *BenchScalarP256) Square() Scalar {
|
|
return &BenchScalarP256{
|
|
value: new(big.Int).Exp(s.value, big.NewInt(2), elliptic.P256().Params().N),
|
|
}
|
|
}
|
|
|
|
func (s *BenchScalarP256) Double() Scalar {
|
|
v := new(big.Int).Add(s.value, s.value)
|
|
return &BenchScalarP256{
|
|
value: v.Mod(v, elliptic.P256().Params().N),
|
|
}
|
|
}
|
|
|
|
func (s *BenchScalarP256) Invert() (Scalar, error) {
|
|
return &BenchScalarP256{
|
|
value: new(big.Int).ModInverse(s.value, elliptic.P256().Params().N),
|
|
}, nil
|
|
}
|
|
|
|
func (s *BenchScalarP256) Sqrt() (Scalar, error) {
|
|
return &BenchScalarP256{
|
|
value: new(big.Int).ModSqrt(s.value, elliptic.P256().Params().N),
|
|
}, nil
|
|
}
|
|
|
|
func (s *BenchScalarP256) Cube() Scalar {
|
|
return &BenchScalarP256{
|
|
value: new(big.Int).Exp(s.value, big.NewInt(3), elliptic.P256().Params().N),
|
|
}
|
|
}
|
|
|
|
func (s *BenchScalarP256) Add(rhs Scalar) Scalar {
|
|
r, ok := rhs.(*BenchScalarP256)
|
|
if ok {
|
|
v := new(big.Int).Add(s.value, r.value)
|
|
return &BenchScalarP256{
|
|
value: v.Mod(v, elliptic.P256().Params().N),
|
|
}
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func (s *BenchScalarP256) Sub(rhs Scalar) Scalar {
|
|
r, ok := rhs.(*BenchScalarP256)
|
|
if ok {
|
|
v := new(big.Int).Sub(s.value, r.value)
|
|
return &BenchScalarP256{
|
|
value: v.Mod(v, elliptic.P256().Params().N),
|
|
}
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func (s *BenchScalarP256) Mul(rhs Scalar) Scalar {
|
|
r, ok := rhs.(*BenchScalarP256)
|
|
if ok {
|
|
v := new(big.Int).Mul(s.value, r.value)
|
|
return &BenchScalarP256{
|
|
value: v.Mod(v, elliptic.P256().Params().N),
|
|
}
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func (s *BenchScalarP256) MulAdd(y, z Scalar) Scalar {
|
|
return s.Mul(y).Add(z)
|
|
}
|
|
|
|
func (s *BenchScalarP256) Div(rhs Scalar) Scalar {
|
|
n := elliptic.P256().Params().N
|
|
r, ok := rhs.(*BenchScalarP256)
|
|
if ok {
|
|
v := new(big.Int).ModInverse(r.value, n)
|
|
v.Mul(v, s.value)
|
|
return &BenchScalarP256{
|
|
value: v.Mod(v, n),
|
|
}
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func (s *BenchScalarP256) Neg() Scalar {
|
|
z := new(big.Int).Neg(s.value)
|
|
return &BenchScalarP256{
|
|
value: z.Mod(z, elliptic.P256().Params().N),
|
|
}
|
|
}
|
|
|
|
func (s *BenchScalarP256) SetBigInt(v *big.Int) (Scalar, error) {
|
|
if v == nil {
|
|
return nil, fmt.Errorf("invalid value")
|
|
}
|
|
t := new(big.Int).Mod(v, elliptic.P256().Params().N)
|
|
if t.Cmp(v) != 0 {
|
|
return nil, fmt.Errorf("invalid value")
|
|
}
|
|
return &BenchScalarP256{
|
|
value: t,
|
|
}, nil
|
|
}
|
|
|
|
func (s *BenchScalarP256) BigInt() *big.Int {
|
|
return new(big.Int).Set(s.value)
|
|
}
|
|
|
|
func (s *BenchScalarP256) Bytes() []byte {
|
|
var out [32]byte
|
|
return s.value.FillBytes(out[:])
|
|
}
|
|
|
|
func (s *BenchScalarP256) SetBytes(bytes []byte) (Scalar, error) {
|
|
value := new(big.Int).SetBytes(bytes)
|
|
t := new(big.Int).Mod(value, elliptic.P256().Params().N)
|
|
if t.Cmp(value) != 0 {
|
|
return nil, fmt.Errorf("invalid byte sequence")
|
|
}
|
|
return &BenchScalarP256{
|
|
value: t,
|
|
}, nil
|
|
}
|
|
|
|
func (s *BenchScalarP256) SetBytesWide(bytes []byte) (Scalar, error) {
|
|
if len(bytes) < 32 || len(bytes) > 128 {
|
|
return nil, fmt.Errorf("invalid byte sequence")
|
|
}
|
|
value := new(big.Int).SetBytes(bytes)
|
|
value.Mod(value, elliptic.P256().Params().N)
|
|
return &BenchScalarP256{
|
|
value,
|
|
}, nil
|
|
}
|
|
|
|
func (s *BenchScalarP256) Point() Point {
|
|
return new(BenchPointP256).Identity()
|
|
}
|
|
|
|
func (s *BenchScalarP256) Clone() Scalar {
|
|
return &BenchScalarP256{
|
|
value: new(big.Int).Set(s.value),
|
|
}
|
|
}
|
|
|
|
func (s *BenchScalarP256) MarshalBinary() ([]byte, error) {
|
|
return scalarMarshalBinary(s)
|
|
}
|
|
|
|
func (s *BenchScalarP256) UnmarshalBinary(input []byte) error {
|
|
sc, err := scalarUnmarshalBinary(input)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ss, ok := sc.(*BenchScalarP256)
|
|
if !ok {
|
|
return fmt.Errorf("invalid scalar")
|
|
}
|
|
s.value = ss.value
|
|
return nil
|
|
}
|
|
|
|
func (s *BenchScalarP256) MarshalText() ([]byte, error) {
|
|
return scalarMarshalText(s)
|
|
}
|
|
|
|
func (s *BenchScalarP256) UnmarshalText(input []byte) error {
|
|
sc, err := scalarUnmarshalText(input)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ss, ok := sc.(*BenchScalarP256)
|
|
if !ok {
|
|
return fmt.Errorf("invalid scalar")
|
|
}
|
|
s.value = ss.value
|
|
return nil
|
|
}
|
|
|
|
func (s *BenchScalarP256) MarshalJSON() ([]byte, error) {
|
|
return scalarMarshalJson(s)
|
|
}
|
|
|
|
func (s *BenchScalarP256) UnmarshalJSON(input []byte) error {
|
|
sc, err := scalarUnmarshalJson(input)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
S, ok := sc.(*BenchScalarP256)
|
|
if !ok {
|
|
return fmt.Errorf("invalid type")
|
|
}
|
|
s.value = S.value
|
|
return nil
|
|
}
|
|
|
|
func (p *BenchPointP256) Random(reader io.Reader) Point {
|
|
var seed [64]byte
|
|
_, _ = reader.Read(seed[:])
|
|
return p.Hash(seed[:])
|
|
}
|
|
|
|
func (p *BenchPointP256) Hash(bytes []byte) Point {
|
|
curve := elliptic.P256()
|
|
|
|
domain := []byte("P256_XMD:SHA-256_SSWU_RO_")
|
|
uniformBytes, _ := expandMsgXmd(sha256.New(), bytes, domain, 96)
|
|
|
|
u0 := new(big.Int).SetBytes(uniformBytes[:48])
|
|
u1 := new(big.Int).SetBytes(uniformBytes[48:])
|
|
|
|
u0.Mod(u0, curve.Params().P)
|
|
u1.Mod(u1, curve.Params().P)
|
|
|
|
ssParams := p256SswuParams()
|
|
q0x, q0y := osswu3mod4(u0, ssParams)
|
|
q1x, q1y := osswu3mod4(u1, ssParams)
|
|
|
|
x, y := curve.Add(q0x, q0y, q1x, q1y)
|
|
|
|
return &BenchPointP256{
|
|
x, y,
|
|
}
|
|
}
|
|
|
|
func (p *BenchPointP256) Identity() Point {
|
|
return &BenchPointP256{
|
|
x: big.NewInt(0), y: big.NewInt(0),
|
|
}
|
|
}
|
|
|
|
func (p *BenchPointP256) Generator() Point {
|
|
curve := elliptic.P256().Params()
|
|
return &BenchPointP256{
|
|
x: new(big.Int).Set(curve.Gx),
|
|
y: new(big.Int).Set(curve.Gy),
|
|
}
|
|
}
|
|
|
|
func (p *BenchPointP256) IsIdentity() bool {
|
|
x := core.ConstantTimeEqByte(p.x, core.Zero)
|
|
y := core.ConstantTimeEqByte(p.y, core.Zero)
|
|
return (x & y) == 1
|
|
}
|
|
|
|
func (p *BenchPointP256) IsNegative() bool {
|
|
return p.y.Bit(0) == 1
|
|
}
|
|
|
|
func (p *BenchPointP256) IsOnCurve() bool {
|
|
return elliptic.P256().IsOnCurve(p.x, p.y)
|
|
}
|
|
|
|
func (p *BenchPointP256) Double() Point {
|
|
curve := elliptic.P256()
|
|
x, y := curve.Double(p.x, p.y)
|
|
return &BenchPointP256{x, y}
|
|
}
|
|
|
|
func (p *BenchPointP256) Scalar() Scalar {
|
|
return new(BenchScalarP256).Zero()
|
|
}
|
|
|
|
func (p *BenchPointP256) Neg() Point {
|
|
y := new(big.Int).Sub(elliptic.P256().Params().P, p.y)
|
|
y.Mod(y, elliptic.P256().Params().P)
|
|
return &BenchPointP256{x: p.x, y: y}
|
|
}
|
|
|
|
func (p *BenchPointP256) Add(rhs Point) Point {
|
|
if rhs == nil {
|
|
return nil
|
|
}
|
|
r, ok := rhs.(*BenchPointP256)
|
|
if ok {
|
|
x, y := elliptic.P256().Add(p.x, p.y, r.x, r.y)
|
|
return &BenchPointP256{x, y}
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func (p *BenchPointP256) Sub(rhs Point) Point {
|
|
if rhs == nil {
|
|
return nil
|
|
}
|
|
r, ok := rhs.Neg().(*BenchPointP256)
|
|
if ok {
|
|
x, y := elliptic.P256().Add(p.x, p.y, r.x, r.y)
|
|
return &BenchPointP256{x, y}
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func (p *BenchPointP256) Mul(rhs Scalar) Point {
|
|
if rhs == nil {
|
|
return nil
|
|
}
|
|
r, ok := rhs.(*BenchScalarP256)
|
|
if ok {
|
|
x, y := elliptic.P256().ScalarMult(p.x, p.y, r.value.Bytes())
|
|
return &BenchPointP256{x, y}
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func (p *BenchPointP256) Equal(rhs Point) bool {
|
|
r, ok := rhs.(*BenchPointP256)
|
|
if ok {
|
|
x := core.ConstantTimeEqByte(p.x, r.x)
|
|
y := core.ConstantTimeEqByte(p.y, r.y)
|
|
return (x & y) == 1
|
|
} else {
|
|
return false
|
|
}
|
|
}
|
|
|
|
func (p *BenchPointP256) Set(x, y *big.Int) (Point, error) {
|
|
// check is identity or on curve
|
|
xx := subtle.ConstantTimeCompare(x.Bytes(), []byte{})
|
|
yy := subtle.ConstantTimeCompare(y.Bytes(), []byte{})
|
|
// Checks are constant time
|
|
onCurve := elliptic.P256().IsOnCurve(x, y)
|
|
if !onCurve && (xx&yy) != 1 {
|
|
return nil, fmt.Errorf("invalid coordinates")
|
|
}
|
|
x = new(big.Int).Set(x)
|
|
y = new(big.Int).Set(y)
|
|
return &BenchPointP256{x, y}, nil
|
|
}
|
|
|
|
func (p *BenchPointP256) ToAffineCompressed() []byte {
|
|
var x [33]byte
|
|
x[0] = byte(2)
|
|
x[0] |= byte(p.y.Bit(0))
|
|
p.x.FillBytes(x[1:])
|
|
return x[:]
|
|
}
|
|
|
|
func (p *BenchPointP256) ToAffineUncompressed() []byte {
|
|
var out [65]byte
|
|
out[0] = byte(4)
|
|
p.x.FillBytes(out[1:33])
|
|
p.y.FillBytes(out[33:])
|
|
return out[:]
|
|
}
|
|
|
|
func (p *BenchPointP256) FromAffineCompressed(bytes []byte) (Point, error) {
|
|
if len(bytes) != 33 {
|
|
return nil, fmt.Errorf("invalid byte sequence")
|
|
}
|
|
sign := int(bytes[0])
|
|
if sign != 2 && sign != 3 {
|
|
return nil, fmt.Errorf("invalid sign byte")
|
|
}
|
|
sign &= 0x1
|
|
|
|
x := new(big.Int).SetBytes(bytes[1:])
|
|
rhs := rhsP256(x, elliptic.P256().Params())
|
|
// test that rhs is quadratic residue
|
|
// if not, then this Point is at infinity
|
|
y := new(big.Int).ModSqrt(rhs, elliptic.P256().Params().P)
|
|
if y != nil {
|
|
// fix the sign
|
|
if int(y.Bit(0)) != sign {
|
|
y.Neg(y)
|
|
y.Mod(y, elliptic.P256().Params().P)
|
|
}
|
|
} else {
|
|
x = new(big.Int)
|
|
y = new(big.Int)
|
|
}
|
|
return &BenchPointP256{
|
|
x, y,
|
|
}, nil
|
|
}
|
|
|
|
func (p *BenchPointP256) FromAffineUncompressed(bytes []byte) (Point, error) {
|
|
if len(bytes) != 65 {
|
|
return nil, fmt.Errorf("invalid byte sequence")
|
|
}
|
|
if bytes[0] != 4 {
|
|
return nil, fmt.Errorf("invalid sign byte")
|
|
}
|
|
x := new(big.Int).SetBytes(bytes[1:33])
|
|
y := new(big.Int).SetBytes(bytes[33:])
|
|
return &BenchPointP256{x, y}, nil
|
|
}
|
|
|
|
func (p *BenchPointP256) CurveName() string {
|
|
return elliptic.P256().Params().Name
|
|
}
|
|
|
|
func (p *BenchPointP256) SumOfProducts(points []Point, scalars []Scalar) Point {
|
|
nScalars := make([]*big.Int, len(scalars))
|
|
for i, sc := range scalars {
|
|
s, ok := sc.(*BenchScalarP256)
|
|
if !ok {
|
|
return nil
|
|
}
|
|
nScalars[i] = s.value
|
|
}
|
|
return sumOfProductsPippenger(points, nScalars)
|
|
}
|
|
|
|
func (p *BenchPointP256) X() *big.Int {
|
|
return new(big.Int).Set(p.x)
|
|
}
|
|
|
|
func (p *BenchPointP256) Y() *big.Int {
|
|
return new(big.Int).Set(p.y)
|
|
}
|
|
|
|
func (p *BenchPointP256) Params() *elliptic.CurveParams {
|
|
return elliptic.P256().Params()
|
|
}
|
|
|
|
func (p *BenchPointP256) MarshalBinary() ([]byte, error) {
|
|
return pointMarshalBinary(p)
|
|
}
|
|
|
|
func (p *BenchPointP256) UnmarshalBinary(input []byte) error {
|
|
pt, err := pointUnmarshalBinary(input)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ppt, ok := pt.(*BenchPointP256)
|
|
if !ok {
|
|
return fmt.Errorf("invalid point")
|
|
}
|
|
p.x = ppt.x
|
|
p.y = ppt.y
|
|
return nil
|
|
}
|
|
|
|
func (p *BenchPointP256) MarshalText() ([]byte, error) {
|
|
return pointMarshalText(p)
|
|
}
|
|
|
|
func (p *BenchPointP256) UnmarshalText(input []byte) error {
|
|
pt, err := pointUnmarshalText(input)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ppt, ok := pt.(*BenchPointP256)
|
|
if !ok {
|
|
return fmt.Errorf("invalid point")
|
|
}
|
|
p.x = ppt.x
|
|
p.y = ppt.y
|
|
return nil
|
|
}
|
|
|
|
func (p *BenchPointP256) MarshalJSON() ([]byte, error) {
|
|
return pointMarshalJSON(p)
|
|
}
|
|
|
|
func (p *BenchPointP256) UnmarshalJSON(input []byte) error {
|
|
pt, err := pointUnmarshalJSON(input)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
P, ok := pt.(*BenchPointP256)
|
|
if !ok {
|
|
return fmt.Errorf("invalid type")
|
|
}
|
|
p.x = P.x
|
|
p.y = P.y
|
|
return nil
|
|
}
|
|
|
|
// From https://datatracker.ietf.org/doc/html/draft-irtf-cfrg-hash-to-curve-11#section-8.2
|
|
func p256SswuParams() *sswuParams {
|
|
params := elliptic.P256().Params()
|
|
|
|
// c1 = (q - 3) / 4
|
|
c1 := new(big.Int).Set(params.P)
|
|
c1.Sub(c1, big.NewInt(3))
|
|
c1.Rsh(c1, 2)
|
|
|
|
a := big.NewInt(-3)
|
|
a.Mod(a, params.P)
|
|
b := new(big.Int).Set(params.B)
|
|
z := big.NewInt(-10)
|
|
z.Mod(z, params.P)
|
|
// sqrt(-Z^3)
|
|
zTmp := new(big.Int).Exp(z, big.NewInt(3), nil)
|
|
zTmp = zTmp.Neg(zTmp)
|
|
zTmp.Mod(zTmp, params.P)
|
|
c2 := new(big.Int).ModSqrt(zTmp, params.P)
|
|
|
|
return &sswuParams{
|
|
params, c1, c2, a, b, z,
|
|
}
|
|
}
|
|
|
|
// rhs of the curve equation
|
|
func rhsP256(x *big.Int, params *elliptic.CurveParams) *big.Int {
|
|
f := NewField(params.P)
|
|
r := f.NewElement(x)
|
|
r2 := r.Mul(r)
|
|
|
|
// x^3-3x+B
|
|
a := r.Mul(f.NewElement(big.NewInt(3)))
|
|
r = r2.Mul(r)
|
|
return r.Add(a.Neg()).Add(f.NewElement(params.B)).Value
|
|
}
|