github.com/deroproject/derosuite@v2.1.6-1.0.20200307070847-0f2e589c7a2b+incompatible/crypto/ringct/ringct_full.go (about)

     1  // Copyright 2017-2018 DERO Project. All rights reserved.
     2  // Use of this source code in any form is governed by RESEARCH license.
     3  // license can be found in the LICENSE file.
     4  // GPG: 0F39 E425 8C65 3947 702A  8234 08B2 0360 A03A 9DE8
     5  //
     6  //
     7  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
     8  // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     9  // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
    10  // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    11  // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    12  // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    13  // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    14  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
    15  // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    16  
    17  package ringct
    18  
    19  import "fmt"
    20  import "github.com/deroproject/derosuite/crypto"
    21  
    22  /* this files handles the generation and verification in ringct full */
    23  
    24  // NOTE the transaction must have been expanded earlier and must have a key image, mixring etc
    25  // this is implementation of verRctMG from rctSigs.cpp file
    26  func (r *RctSig) VerifyRCTFull_Core() (result bool) {
    27  	result = false
    28  	if r.sigType != RCTTypeFull {
    29  		if DEBUGGING_MODE {
    30  			fmt.Printf("Signature NOT RingCT MG type, verification failed\n")
    31  		}
    32  		result = false
    33  		return
    34  	}
    35  
    36  	// some sanity checking
    37  	/* if len(r.MixRing) != 1 { // this is hard code 1 for rct mg
    38  	       if DEBUGGING_MODE {
    39  	           fmt.Printf("RingCT MG  must have mixring rows 1\n")
    40  	       }
    41  	       result= false
    42  	       return
    43  	   }
    44  	   if len(r.MixRing[0]) <= 1 { // mixing should be more than 1
    45  	       if DEBUGGING_MODE {
    46  	           fmt.Printf("RingCT MG  mixring  cannot be 1 or less\n")
    47  	       }
    48  	       result= false
    49  	       return
    50  	   }*/
    51  
    52  	pre_mlsag_hash := crypto.Key(Get_pre_mlsag_hash(r))
    53  	txfeekey := Commitment_From_Amount(r.txFee)
    54  
    55  	cols := len(r.MixRing)
    56  	rows := len(r.MixRing[0])
    57  
    58  	//  fmt.Printf("cols %d rows %d \n", cols, rows)
    59  
    60  	// if cols = 1 ,  if mixin = 5 , rows = 5
    61  	// create a matrix of the form
    62  	// 0  0
    63  	// 1  1
    64  	// 2  2
    65  	// 3  3
    66  	// 4  4
    67  	// 5  5   // yes there is an extra row
    68  
    69  	M := make([][]crypto.Key, cols)
    70  	for i := 0; i < (cols); i++ {
    71  		M[i] = make([]crypto.Key, rows+1, rows+1)
    72  		for j := 0; j < (rows + 1); j++ { // yes there is an extra column
    73  			M[i][j] = Identity // fill it with identity
    74  			// fmt.Printf("M[%d][%d] %s\n",i,j, M[i][j])
    75  		}
    76  	}
    77  
    78  	for j := 0; j < rows; j++ {
    79  		for i := 0; i < cols; i++ {
    80  			//fmt.Printf("j %d i %d \n", j,i)
    81  			//   fmt.Printf("f j %d i %d  %s\n", j,i, M[i][j])
    82  			//fmt.Printf("i %d rows %d \n", i, rows)
    83  			M[i][j] = r.MixRing[i][j].Destination
    84  
    85  			//    fmt.Printf("f M[i][rows] == %s\n",M[i][rows]);
    86  			crypto.AddKeys(&M[i][rows], &M[i][rows], &r.MixRing[i][j].Mask) //add Ci in last row
    87  			//    fmt.Printf("f M[i][rows] =  %s\n",M[i][rows]);
    88  		}
    89  	}
    90  
    91  	for i := 0; i < cols; i++ {
    92  		for j := 0; j < len(r.OutPk); j++ {
    93  			crypto.SubKeys(&M[i][rows], &M[i][rows], &r.OutPk[j].Mask) //subtract output Ci's in last row
    94  			//    fmt.Printf("s i %d j %d  %s \n",i,j,M[i][rows]);
    95  		}
    96  		//subtract txn fee output in last row
    97  		crypto.SubKeys(&M[i][rows], &M[i][rows], &txfeekey)
    98  
    99  		//  fmt.Printf("s M[i][rows] = %s\n",M[i][rows])
   100  	}
   101  
   102  	// do the mlsag verification
   103  
   104  	result = MLSAG_Ver(pre_mlsag_hash, M, &r.MlsagSigs[0], rows, r)
   105  
   106  	if DEBUGGING_MODE {
   107  		if result {
   108  			fmt.Printf("Signature Full successfully verified\n")
   109  		} else {
   110  			fmt.Printf("RCT MG  signarure verification failed\n")
   111  		}
   112  
   113  	}
   114  
   115  	return
   116  }