github.com/deroproject/derosuite@v2.1.6-1.0.20200307070847-0f2e589c7a2b+incompatible/crypto/ringct/key.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 "io"
    20  //import "fmt"
    21  //import "crypto/rand"
    22  
    23  import "github.com/deroproject/derosuite/crypto"
    24  
    25  // bothe the function resturn identity of the ed25519 curve
    26  func identity() (result *crypto.Key) {
    27  	result = new(crypto.Key)
    28  	result[0] = 1
    29  	return
    30  }
    31  
    32  // convert a uint64 to a scalar
    33  func d2h(val uint64) (result *crypto.Key) {
    34  	result = new(crypto.Key)
    35  	for i := 0; val > 0; i++ {
    36  		result[i] = byte(val & 0xFF)
    37  		val /= 256
    38  	}
    39  	return
    40  }
    41  
    42  //32 byte key to uint long long
    43  // if the key holds a value > 2^64
    44  // then the value in the first 8 bytes is returned
    45  func h2d(input crypto.Key) (value uint64) {
    46  	for j := 7; j >= 0; j-- {
    47  		value = (value*256 + uint64(input[j]))
    48  	}
    49  	return value
    50  }
    51  
    52  // this gives you a commitment from an amount
    53  // this is used to convert tx fee or miner tx amount to commitment
    54  func Commitment_From_Amount(amount uint64) crypto.Key {
    55  	return *(crypto.ScalarMultH(d2h(amount)))
    56  }
    57  
    58  // this is used to convert miner tx commitment to  mask
    59  // equivalent to rctOps.cpp zeroCommit
    60  func ZeroCommitment_From_Amount(amount uint64) crypto.Key {
    61  	mask := *(identity())
    62  	mask = crypto.ScalarmultBase(mask)
    63  	am := d2h(amount)
    64  	bH := crypto.ScalarMultH(am)
    65  	crypto.AddKeys(&mask, &mask, bH)
    66  	return mask
    67  }