diff --git a/confidential/confidential.go b/confidential/confidential.go index 30ff878..d5aca55 100644 --- a/confidential/confidential.go +++ b/confidential/confidential.go @@ -1,3 +1,5 @@ +//go:build cgo + package confidential import ( diff --git a/confidential/confidential_nocgo.go b/confidential/confidential_nocgo.go new file mode 100644 index 0000000..8af7749 --- /dev/null +++ b/confidential/confidential_nocgo.go @@ -0,0 +1,199 @@ +//go:build !cgo + +package confidential + +import ( + "errors" + + "github.com/vulpemventures/go-elements/transaction" +) + +var errNoCGO = errors.New("confidential transactions require CGO") + +const ( + maxSurjectionTargets = 3 + maxScriptSize = 10000 +) + +var ( + Zero = make([]byte, 32) +) + +// UnblindOutputResult is the type returned by the functions that unblind tx +// outs. It contains the unblinded asset and value and also the respective +// blinding factors. +type UnblindOutputResult struct { + Value uint64 + Asset []byte + ValueBlindingFactor []byte + AssetBlindingFactor []byte +} + +// UnblindIssuanceResult is the type returned by the functions that unblind tx +// issuances. It contains the unblinded asset and token issuances. +type UnblindIssuanceResult struct { + Asset *UnblindOutputResult + Token *UnblindOutputResult +} + +// FinalValueBlindingFactorArgs is the type used to pass arguments to the +// FinalValueBlindingFactor function. +type FinalValueBlindingFactorArgs struct { + InValues []uint64 + OutValues []uint64 + InGenerators [][]byte + OutGenerators [][]byte + InFactors [][]byte + OutFactors [][]byte +} + +// RangeProofArgs is the type used to pass arguments to the RangeProof function. +type RangeProofArgs struct { + Value uint64 + Nonce [32]byte + Asset []byte + AssetBlindingFactor []byte + ValueBlindFactor [32]byte + ValueCommit []byte + ScriptPubkey []byte + Exp int + MinBits int +} + +// SurjectionProofArgs is the type used to pass arguments to the SurjectionProof function. +type SurjectionProofArgs struct { + OutputAsset []byte + OutputAssetBlindingFactor []byte + InputAssets [][]byte + InputAssetBlindingFactors [][]byte + Seed []byte + NumberOfTargets int +} + +// VerifySurjectionProofArgs is the type used to pass arguments to the VerifySurjectionProof function. +type VerifySurjectionProofArgs struct { + InputAssets [][]byte + InputAssetBlindingFactors [][]byte + OutputAsset []byte + OutputAssetBlindingFactor []byte + Proof []byte +} + +// NonceHash method generates hashed secret based on ecdh. +func NonceHash(pubKey, privKey []byte) ([32]byte, error) { + return [32]byte{}, errNoCGO +} + +// UnblindOutputWithKey method unblinds a confidential transaction output with +// the given blinding private key. +func UnblindOutputWithKey( + out *transaction.TxOutput, blindKey []byte, +) (*UnblindOutputResult, error) { + return nil, errNoCGO +} + +// UnblindOutputWithNonce method unblinds a confidential transaction output with +// the given nonce. +func UnblindOutputWithNonce( + out *transaction.TxOutput, nonce []byte, +) (*UnblindOutputResult, error) { + return nil, errNoCGO +} + +// UnblindIssuance method unblinds a confidential transaction issuance with +// the given blinding private keys. +func UnblindIssuance( + in *transaction.TxInput, blindKeys [][]byte, +) (*UnblindIssuanceResult, error) { + return nil, errNoCGO +} + +// FinalValueBlindingFactor method calculates the final value blinding factor. +func FinalValueBlindingFactor( + args FinalValueBlindingFactorArgs, +) ([32]byte, error) { + return [32]byte{}, errNoCGO +} + +// AssetCommitment method creates an asset commitment. +func AssetCommitment(asset, factor []byte) ([]byte, error) { + return nil, errNoCGO +} + +// ValueCommitment method creates a value commitment. +func ValueCommitment(value uint64, generator, factor []byte) ([]byte, error) { + return nil, errNoCGO +} + +// RangeProof method creates a range proof. +func RangeProof(args RangeProofArgs) ([]byte, error) { + return nil, errNoCGO +} + +// VerifyRangeProof method verifies a range proof. +func VerifyRangeProof(valueCommitment, assetCommitment, script, proof []byte) bool { + return false +} + +// SurjectionProof method creates a surjection proof. +func SurjectionProof(args SurjectionProofArgs) ([]byte, bool) { + return nil, false +} + +// VerifySurjectionProof method verifies a surjection proof. +func VerifySurjectionProof(args VerifySurjectionProofArgs) bool { + return false +} + +// CalculateScalarOffset calculates the scalar offset for a transaction. +// This is a no-op implementation when CGO is disabled. +func CalculateScalarOffset( + amount uint64, assetBlinder, valueBlinder []byte, +) ([]byte, error) { + return nil, errNoCGO +} + +// SubtractScalars subtracts two scalars. +// This is a no-op implementation when CGO is disabled. +func SubtractScalars(a []byte, b []byte) ([]byte, error) { + return nil, errNoCGO +} + +// ComputeAndAddToScalarOffset computes and adds to the scalar offset. +// This is a no-op implementation when CGO is disabled. +func ComputeAndAddToScalarOffset( + scalar []byte, value uint64, assetBlinder, valueBlinder []byte, +) ([]byte, error) { + return nil, errNoCGO +} + +// CreateBlindValueProof creates a blind value proof. +// This is a no-op implementation when CGO is disabled. +func CreateBlindValueProof( + rng func() ([]byte, error), + valueBlinder []byte, amount uint64, valueCommitment, assetCommitment []byte, +) ([]byte, error) { + return nil, errNoCGO +} + +// CreateBlindAssetProof creates a blind asset proof. +// This is a no-op implementation when CGO is disabled. +func CreateBlindAssetProof( + asset, assetCommitment, assetBlinder []byte, +) ([]byte, error) { + return nil, errNoCGO +} + +// VerifyBlindValueProof verifies a blind value proof. +// This is a no-op implementation when CGO is disabled. +func VerifyBlindValueProof( + value uint64, valueCommitment, assetCommitment, proof []byte, +) bool { + return false +} + +// VerifyBlindAssetProof verifies a blind asset proof. +// This is a no-op implementation when CGO is disabled. +func VerifyBlindAssetProof(asset, assetCommitment, proof []byte) bool { + return false +} diff --git a/confidential/zkp_generator.go b/confidential/zkp_generator.go index da713c0..6930ca5 100644 --- a/confidential/zkp_generator.go +++ b/confidential/zkp_generator.go @@ -1,3 +1,5 @@ +//go:build cgo + package confidential import ( diff --git a/confidential/zkp_generator_nocgo.go b/confidential/zkp_generator_nocgo.go new file mode 100644 index 0000000..da04976 --- /dev/null +++ b/confidential/zkp_generator_nocgo.go @@ -0,0 +1,101 @@ +//go:build !cgo + +package confidential + +import ( + "github.com/vulpemventures/go-elements/psetv2" + "github.com/vulpemventures/go-elements/transaction" +) + +// zkpGenerator is the type that provides methods to generate zero-knowledge proofs. +type zkpGenerator struct { + masterBlindingKey interface{} + inBlindingKeys [][]byte + rng func() ([]byte, error) + ownedInputs map[uint32]psetv2.OwnedInput +} + +// NewZKPGeneratorFromMasterBlindingKey creates a new zkpGenerator from a master blinding key. +func NewZKPGeneratorFromMasterBlindingKey( + masterBlindingKey []byte, opts interface{}, +) (*zkpGenerator, error) { + return nil, errNoCGO +} + +// NewZKPGeneratorFromBlindingKeys creates a new zkpGenerator from blinding keys. +func NewZKPGeneratorFromBlindingKeys( + inBlindingKeys [][]byte, opts interface{}, +) *zkpGenerator { + return &zkpGenerator{} +} + +// ComputeAndAddToScalarOffset computes and adds to the scalar offset. +// This is a no-op implementation when CGO is disabled. +func (g *zkpGenerator) ComputeAndAddToScalarOffset( + scalar []byte, value uint64, assetBlinder, valueBlinder []byte, +) ([]byte, error) { + return nil, errNoCGO +} + +// SubtractScalars subtracts two scalars. +// This is a no-op implementation when CGO is disabled. +func (g *zkpGenerator) SubtractScalars(a, b []byte) ([]byte, error) { + return nil, errNoCGO +} + +// LastValueCommitment creates a value commitment. +// This is a no-op implementation when CGO is disabled. +func (g *zkpGenerator) LastValueCommitment( + value uint64, asset, blinder []byte, +) ([]byte, error) { + return nil, errNoCGO +} + +// LastBlindValueProof creates a blind value proof. +// This is a no-op implementation when CGO is disabled. +func (g *zkpGenerator) LastBlindValueProof( + value uint64, valueCommitment, assetCommitment, blinder []byte, +) ([]byte, error) { + return nil, errNoCGO +} + +// LastValueRangeProof creates a range proof. +// This is a no-op implementation when CGO is disabled. +func (g *zkpGenerator) LastValueRangeProof( + value uint64, asset, assetBlinder, valueCommitment, valueBlinder, + scriptPubkey, nonce []byte, +) ([]byte, error) { + return nil, errNoCGO +} + +// UnblindInputs unblinds inputs. +// This is a no-op implementation when CGO is disabled. +func (g *zkpGenerator) UnblindInputs( + p *psetv2.Pset, inputIndexes []uint32, +) ([]psetv2.OwnedInput, error) { + return nil, errNoCGO +} + +// BlindIssuances blinds issuances. +// This is a no-op implementation when CGO is disabled. +func (g *zkpGenerator) BlindIssuances( + p *psetv2.Pset, blindingKeysByIndex map[uint32][]byte, +) ([]psetv2.InputIssuanceBlindingArgs, error) { + return nil, errNoCGO +} + +// BlindOutputs blinds outputs. +// This is a no-op implementation when CGO is disabled. +func (g *zkpGenerator) BlindOutputs( + p *psetv2.Pset, outputIndexes []uint32, +) ([]psetv2.OutputBlindingArgs, error) { + return nil, errNoCGO +} + +// unblindOutput unblinds an output. +// This is a no-op implementation when CGO is disabled. +func (g *zkpGenerator) unblindOutput( + out *transaction.TxOutput, +) (*psetv2.OwnedInput, error) { + return nil, errNoCGO +} diff --git a/confidential/zkp_validator.go b/confidential/zkp_validator.go index 58ca31e..93cfee1 100644 --- a/confidential/zkp_validator.go +++ b/confidential/zkp_validator.go @@ -1,3 +1,5 @@ +//go:build cgo + package confidential type zkpValidator struct{} diff --git a/confidential/zkp_validator_nocgo.go b/confidential/zkp_validator_nocgo.go new file mode 100644 index 0000000..4782143 --- /dev/null +++ b/confidential/zkp_validator_nocgo.go @@ -0,0 +1,44 @@ +//go:build !cgo + +package confidential + +// zkpValidator is the type that provides methods to validate zero-knowledge proofs. +type zkpValidator struct{} + +// NewZKPValidator creates a new zkpValidator. +func NewZKPValidator() *zkpValidator { + return &zkpValidator{} +} + +// VerifyValueRangeProof verifies a range proof. +// This is a no-op implementation when CGO is disabled. +func (v *zkpValidator) VerifyValueRangeProof( + valueCommitment, assetCommitment, script, proof []byte, +) bool { + return false +} + +// VerifyAssetSurjectionProof verifies a surjection proof. +// This is a no-op implementation when CGO is disabled. +func (v *zkpValidator) VerifyAssetSurjectionProof( + inAssets, inAssetBlinders [][]byte, + outAsset, outAssetBlinder, proof []byte, +) bool { + return false +} + +// VerifyBlindValueProof verifies a blind value proof. +// This is a no-op implementation when CGO is disabled. +func (v *zkpValidator) VerifyBlindValueProof( + value uint64, valueCommitment, assetCommitment, proof []byte, +) bool { + return false +} + +// VerifyBlindAssetProof verifies a blind asset proof. +// This is a no-op implementation when CGO is disabled. +func (v *zkpValidator) VerifyBlindAssetProof( + asset, assetCommitment, proof []byte, +) bool { + return false +} diff --git a/pegincontract/contract.go b/pegincontract/contract.go index 1147020..edb8012 100644 --- a/pegincontract/contract.go +++ b/pegincontract/contract.go @@ -1,3 +1,5 @@ +//go:build cgo + package pegincontract import ( diff --git a/pegincontract/contract_nocgo.go b/pegincontract/contract_nocgo.go new file mode 100644 index 0000000..3ba4f96 --- /dev/null +++ b/pegincontract/contract_nocgo.go @@ -0,0 +1,24 @@ +//go:build !cgo + +package pegincontract + +import ( + "errors" +) + +var errNoCGO = errors.New("pegin contract requires CGO") + +// Calculate calculates the pegin contract. +// This is a no-op implementation when CGO is disabled. +func Calculate( + federationScript []byte, + scriptPubKey []byte, +) ([]byte, error) { + return nil, errNoCGO +} + +// IsLiquidV1 checks if the script is a Liquid V1 script. +// This is a no-op implementation when CGO is disabled. +func IsLiquidV1(script []byte) (bool, error) { + return false, errNoCGO +}