github.com/dusk-network/dusk-crypto@v0.1.3/mlsag/proof.go (about)

     1  package mlsag
     2  
     3  import (
     4  	"errors"
     5  	"math/rand"
     6  	"time"
     7  
     8  	ristretto "github.com/bwesterb/go-ristretto"
     9  )
    10  
    11  type Proof struct {
    12  	// index indicating the column of the secret keys
    13  	index int
    14  
    15  	// private keys corresponding to a column
    16  	// in the matrix
    17  	privKeys PrivKeys
    18  
    19  	//Signer pubkeys
    20  	signerPubKeys PubKeys
    21  
    22  	// All pubKeys including the decoys
    23  	// There should exist an index j such that
    24  	// pubKeys[j][i] = privKeys[i] * G
    25  	pubKeysMatrix []PubKeys
    26  
    27  	// message to be signed
    28  	msg []byte
    29  }
    30  
    31  func (p *Proof) addPubKeys(keys PubKeys) {
    32  	//	// xxx: return an error if there is already a key vector in marix and their sizes do not match
    33  	p.pubKeysMatrix = append(p.pubKeysMatrix, keys)
    34  }
    35  
    36  func (p *Proof) AddDecoy(keys PubKeys) {
    37  	keys.decoy = true
    38  	p.addPubKeys(keys)
    39  }
    40  
    41  func (p *Proof) AddDecoys(keys []PubKeys) {
    42  	for _, key := range keys {
    43  		p.AddDecoy(key)
    44  	}
    45  }
    46  
    47  func (proof *Proof) addSignerPubKey() {
    48  	// Add signers pubkey to matrix
    49  	proof.signerPubKeys.decoy = false
    50  	proof.addPubKeys(proof.signerPubKeys)
    51  }
    52  
    53  func (p *Proof) AddSecret(privKey ristretto.Scalar) {
    54  
    55  	// Generate pubkey for given privkey
    56  	rawPubKey := privKeyToPubKey(privKey)
    57  
    58  	// Add pubkey to signers set of pubkeys
    59  	p.signerPubKeys.AddPubKey(rawPubKey)
    60  	// Add privkey to signers set of priv keys
    61  	p.privKeys.AddPrivateKey(privKey)
    62  }
    63  
    64  func privKeyToPubKey(privkey ristretto.Scalar) ristretto.Point {
    65  	var pubkey ristretto.Point
    66  	pubkey.ScalarMultBase(&privkey)
    67  	return pubkey
    68  }
    69  
    70  // shuffle all pubkeys and sets the index
    71  func (p *Proof) shuffleSet() error {
    72  	r := rand.New(rand.NewSource(time.Now().Unix()))
    73  	for i := len(p.pubKeysMatrix) - 1; i > 0; i-- {
    74  		j := r.Intn(i + 1)
    75  		p.pubKeysMatrix[i], p.pubKeysMatrix[j] = p.pubKeysMatrix[j], p.pubKeysMatrix[i]
    76  	}
    77  	// XXX: Optimise away the below for loop by storing the index when appended
    78  	// and following it in the first loop. We can also get rid of the decoy flag too
    79  
    80  	// Find our index
    81  	for i := range p.pubKeysMatrix {
    82  		pubKey := p.pubKeysMatrix[i]
    83  		if !pubKey.decoy {
    84  			p.index = i
    85  			return nil
    86  		}
    87  	}
    88  
    89  	// If we get here, then we could not find the index of the signers pubkey
    90  	return errors.New("could not find the index of the non-decoy vector of pubkeys")
    91  }
    92  
    93  func (p *Proof) LenMembers() int {
    94  	return len(p.pubKeysMatrix)
    95  }