github.com/jiajun1992/watercarver@v0.0.0-20191031150618-dfc2b17c0c4a/go-ethereum/ctcrypto/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/ethereum/go-ethereum/ctcrypto/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 func D2h(val uint64) (result *crypto.Key) { 33 return d2h(val) 34 } 35 36 // convert a uint64 to a scalar 37 func d2h(val uint64) (result *crypto.Key) { 38 result = new(crypto.Key) 39 for i := 0; val > 0; i++ { 40 result[i] = byte(val & 0xFF) 41 val /= 256 42 } 43 return 44 } 45 46 func H2d(input crypto.Key) (value uint64) { 47 return h2d(input) 48 } 49 50 //32 byte key to uint long long 51 // if the key holds a value > 2^64 52 // then the value in the first 8 bytes is returned 53 func h2d(input crypto.Key) (value uint64) { 54 for j := 7; j >= 0; j-- { 55 value = (value*256 + uint64(input[j])) 56 } 57 return value 58 } 59 60 // this gives you a commitment from an amount 61 // this is used to convert tx fee or miner tx amount to commitment 62 func Commitment_From_Amount(amount uint64) crypto.Key { 63 return *(crypto.ScalarMultH(d2h(amount))) 64 } 65 66 // this is used to convert miner tx commitment to mask 67 // equivalent to rctOps.cpp zeroCommit 68 func ZeroCommitment_From_Amount(amount uint64) crypto.Key { 69 mask := *(identity()) 70 mask = crypto.ScalarmultBase(mask) 71 am := d2h(amount) 72 bH := crypto.ScalarMultH(am) 73 crypto.AddKeys(&mask, &mask, bH) 74 return mask 75 }