|
| 1 | +module NLitecoin.MimbleWimble.TransactionBuilder |
| 2 | + |
| 3 | +open System |
| 4 | + |
| 5 | +open NBitcoin |
| 6 | +open Org.BouncyCastle.Math |
| 7 | + |
| 8 | +type private Inputs = |
| 9 | + { |
| 10 | + TotalBlind: BlindingFactor |
| 11 | + TotalKey: uint256 |
| 12 | + Inputs: array<Input> |
| 13 | + } |
| 14 | + |
| 15 | +type private Outputs = |
| 16 | + { |
| 17 | + TotalBlind: BlindingFactor |
| 18 | + TotalKey: uint256 |
| 19 | + Outputs: array<Output> |
| 20 | + Coins: array<NLitecoin.MimbleWimble.Coin> |
| 21 | + } |
| 22 | + |
| 23 | +/// Creates a standard input with a stealth key (feature bit = 1)// Creates a standard input with a stealth key (feature bit = 1) |
| 24 | +let private CreateInput (outputId: Hash) (commitment: PedersenCommitment) (inputKey: uint256) (outputKey: uint256) = |
| 25 | + let features = InputFeatures.STEALTH_KEY_FEATURE_BIT |
| 26 | + |
| 27 | + let inputPubKey = PublicKey(inputKey.ToBytes() |> BigInt) |
| 28 | + let outputPubKey = PublicKey(outputKey.ToBytes() |> BigInt) |
| 29 | + |
| 30 | + // Hash keys (K_i||K_o) |
| 31 | + let keyHasher = Hasher() |
| 32 | + keyHasher.Append inputPubKey |
| 33 | + keyHasher.Append outputPubKey |
| 34 | + let keyHash = keyHasher.Hash().ToBytes() |
| 35 | + |
| 36 | + // Calculate aggregated key k_agg = k_i + HASH(K_i||K_o) * k_o |
| 37 | + let sigKey = |
| 38 | + BigInteger(outputKey.ToBytes()) |
| 39 | + .Multiply(BigInteger keyHash) |
| 40 | + .Add(BigInteger(inputKey.ToBytes())) |
| 41 | + |
| 42 | + let msgHasher = Hasher() |
| 43 | + //msgHasher.Append features |
| 44 | + msgHasher.Append outputId |
| 45 | + let msgHash = msgHasher.Hash().ToBytes() |
| 46 | + |
| 47 | + let schnorrSignature = |
| 48 | + // is this the right one? |
| 49 | + NBitcoin.Secp256k1.ECPrivKey.Create(sigKey.ToByteArrayUnsigned()).SignBIP340(msgHash) |
| 50 | + |
| 51 | + { |
| 52 | + Features = features |
| 53 | + OutputID = outputId |
| 54 | + Commitment = commitment |
| 55 | + InputPublicKey = Some inputPubKey |
| 56 | + OutputPublicKey = outputPubKey |
| 57 | + Signature = Signature(schnorrSignature.ToBytes() |> BigInt) |
| 58 | + ExtraData = Array.empty |
| 59 | + } |
| 60 | + |
| 61 | +let private CreateInputs (inputCoins: seq<NLitecoin.MimbleWimble.Coin>) : Inputs = |
| 62 | + let blinds, keys, inputs = |
| 63 | + [| for inputCoin in inputCoins do |
| 64 | + let blind = Pedersen.BlindSwitch inputCoin.Blind.Value inputCoin.Amount |
| 65 | + let ephemeralKey = NBitcoin.RandomUtils.GetUInt256() |
| 66 | + let input = |
| 67 | + CreateInput |
| 68 | + inputCoin.OutputId |
| 69 | + (Pedersen.Commit inputCoin.Amount blind) |
| 70 | + ephemeralKey |
| 71 | + inputCoin.SpendKey.Value |
| 72 | + yield blind, (BlindingFactor ephemeralKey, BlindingFactor inputCoin.SpendKey.Value), input |] |
| 73 | + |> Array.unzip3 |
| 74 | + |
| 75 | + let positiveKeys, negativeKeys = Array.unzip keys |
| 76 | + |
| 77 | + { |
| 78 | + TotalBlind = Pedersen.AddBlindingFactors blinds Array.empty |
| 79 | + TotalKey = (Pedersen.AddBlindingFactors positiveKeys negativeKeys).ToUInt256() |
| 80 | + Inputs = inputs |
| 81 | + } |
| 82 | + |
| 83 | +let private CreateOutput (senderPrivKey: uint256) (receiverAddr: StealthAddress) (value: uint64) : Output * BlindingFactor = |
| 84 | + let features = OutputFeatures.STANDARD_FIELDS_FEATURE_BIT |
| 85 | + |
| 86 | + // Generate 128-bit secret nonce 'n' = Hash128(T_nonce, sender_privkey) |
| 87 | + let n = |
| 88 | + let hasher = Hasher(HashTags.NONCE) |
| 89 | + hasher.Write(senderPrivKey.ToBytes()) |
| 90 | + hasher.Hash().ToBytes() |
| 91 | + |> Array.take 16 |
| 92 | + |> BigInt |
| 93 | + |
| 94 | + // Calculate unique sending key 's' = H(T_send, A, B, v, n) |
| 95 | + let s = |
| 96 | + let hasher = Hasher(HashTags.SEND_KEY) |
| 97 | + hasher.Append receiverAddr.ScanPubKey |
| 98 | + hasher.Append receiverAddr.SpendPubKey |
| 99 | + hasher.Write (BitConverter.GetBytes value) |
| 100 | + hasher.Append n |
| 101 | + hasher.Hash().ToBytes() |
| 102 | + |> BigInteger |
| 103 | + |
| 104 | + let A = |
| 105 | + match receiverAddr.ScanPubKey with |
| 106 | + | PublicKey pubKey -> BigInteger pubKey.Data |
| 107 | + |
| 108 | + let B = |
| 109 | + match receiverAddr.SpendPubKey with |
| 110 | + | PublicKey pubKey -> BigInteger pubKey.Data |
| 111 | + |
| 112 | + // Derive shared secret 't' = H(T_derive, s*A) |
| 113 | + let sA = A.Multiply s |
| 114 | + let t = |
| 115 | + let hasher = Hasher(HashTags.DERIVE) |
| 116 | + hasher.Write(sA.ToByteArrayUnsigned()) |
| 117 | + hasher.Hash() |
| 118 | + |
| 119 | + // Construct one-time public key for receiver 'Ko' = H(T_outkey, t)*B |
| 120 | + let Ko = |
| 121 | + let hasher = Hasher(HashTags.OUT_KEY) |
| 122 | + hasher.Append t |
| 123 | + B.Multiply(hasher.Hash().ToBytes() |> BigInteger); |
| 124 | + |
| 125 | + // Key exchange public key 'Ke' = s*B |
| 126 | + let Ke = B.Multiply s |
| 127 | + |
| 128 | + // Calc blinding factor and mask nonce and amount. |
| 129 | + let mask = OutputMask.FromShared(t.ToUInt256()) |
| 130 | + let blind = Pedersen.BlindSwitch mask.PreBlind (int64 value) |
| 131 | + let mv = mask.MaskValue value |
| 132 | + let mn = mask.MaskNonce n |
| 133 | + |
| 134 | + // Commitment 'C' = r*G + v*H |
| 135 | + let outputCommit = Pedersen.Commit (int64 value) blind |
| 136 | + |
| 137 | + // Calculate the ephemeral send pubkey 'Ks' = ks*G |
| 138 | + let Ks = Secp256k1.ECPubKey.Create(senderPrivKey.ToBytes()) |
| 139 | + |
| 140 | + // Derive view tag as first byte of H(T_tag, sA) |
| 141 | + let viewTag = |
| 142 | + let hasher = Hasher(HashTags.TAG) |
| 143 | + hasher.Write(sA.ToByteArrayUnsigned()) |
| 144 | + hasher.Hash().ToBytes().[0] |
| 145 | + |
| 146 | + let message = |
| 147 | + { |
| 148 | + Features = features |
| 149 | + StandardFields = |
| 150 | + Some { |
| 151 | + KeyExchangePubkey = PublicKey(Ke.ToByteArrayUnsigned() |> BigInt) |
| 152 | + ViewTag = viewTag |
| 153 | + MaskedValue = mv |
| 154 | + MaskedNonce = mn |
| 155 | + } |
| 156 | + ExtraData = Array.empty |
| 157 | + } |
| 158 | + |
| 159 | + failwith "not implemented" |
| 160 | + |
| 161 | +let private CreateOutputs (recipients: seq<Recipient>) : Outputs = |
| 162 | + let outputBlinds, outputs, coins = |
| 163 | + [| for recipient in recipients do |
| 164 | + let ephemeralKey = NBitcoin.RandomUtils.GetUInt256() |
| 165 | + let output, rawBlind = CreateOutput ephemeralKey recipient.Address (uint64 recipient.Amount) |
| 166 | + let outputBlind = Pedersen.BlindSwitch rawBlind recipient.Amount |
| 167 | + let coin = |
| 168 | + { Coin.Empty with |
| 169 | + Blind = Some rawBlind |
| 170 | + Amount = recipient.Amount |
| 171 | + OutputId = output.GetOutputID() |
| 172 | + SenderKey = Some ephemeralKey |
| 173 | + Address = Some recipient.Address |
| 174 | + } |
| 175 | + yield outputBlind, output, coin |
| 176 | + |] |
| 177 | + |> Array.unzip3 |
| 178 | + |
| 179 | + let outputKeys = |
| 180 | + coins |
| 181 | + |> Array.choose (fun coin -> coin.SenderKey) |
| 182 | + |> Array.map BlindingFactor |
| 183 | + |
| 184 | + { |
| 185 | + TotalBlind = Pedersen.AddBlindingFactors outputBlinds Array.empty |
| 186 | + TotalKey = (Pedersen.AddBlindingFactors outputKeys Array.empty).ToUInt256() |
| 187 | + Outputs = outputs |
| 188 | + Coins = coins |
| 189 | + } |
0 commit comments