Skip to content

Commit 442179f

Browse files
committed
MimbleWimble(Tests): addressed comments
Addressed code review comments. Added latest Fsdk package in order to use BetterAssert function.
1 parent e379c4e commit 442179f

File tree

7 files changed

+147
-145
lines changed

7 files changed

+147
-145
lines changed

Diff for: src/NLitecoin/MimbleWimble/EC.fs

+11-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ open Org.BouncyCastle.Math
88
open Org.BouncyCastle.Math.EC
99
open Org.BouncyCastle.Crypto.Digests
1010

11-
let curve = ECNamedCurveTable.GetByName("secp256k1")
11+
let curve = ECNamedCurveTable.GetByName "secp256k1"
1212
let domainParams = new ECDomainParameters(curve.Curve, curve.G, curve.N, curve.H, curve.GetSeed())
1313

1414
// see https://github.com/bitcoin-core/secp256k1/issues/1180#issuecomment-1356859346
@@ -42,7 +42,8 @@ type BigInteger with
4242

4343
type NBitcoin.Secp256k1.ECPrivKey with
4444
member self.ToBytes() =
45-
let bytes = Array.zeroCreate 32
45+
let numBytesInPrivateKey = 32
46+
let bytes = Array.zeroCreate numBytesInPrivateKey
4647
self.WriteToSpan(bytes.AsSpan())
4748
bytes
4849

@@ -88,28 +89,31 @@ let IsQuadVar (elem: ECFieldElement) =
8889
Jakobi elem >= 0
8990

9091
let SchnorrSign (key: array<byte>) (msgHash: array<byte>) : Signature =
92+
let numBytesInSha256 = 32
9193
let k0 =
9294
let hasher = Sha256Digest()
9395
hasher.BlockUpdate(key, 0, key.Length)
9496
hasher.BlockUpdate(msgHash, 0, msgHash.Length)
95-
let arr = Array.zeroCreate 32
97+
let arr = Array.zeroCreate numBytesInSha256
9698
hasher.DoFinal(arr, 0) |> ignore
9799
BigInteger.FromByteArrayUnsigned(arr).Mod(scalarOrder)
98100

99101
if k0 = BigInteger.Zero then
100102
failwith "Failure. This happens only with negligible probability."
101103

102104
let keyScalar = BigInteger.FromByteArrayUnsigned key
103-
assert(keyScalar < scalarOrder)
105+
Fsdk.Misc.BetterAssert (keyScalar < scalarOrder) "key is not in range [0; scalarOrder)"
104106

105107
let R = generatorG.Multiply(k0).Normalize()
106108
let k = if Jakobi R.AffineYCoord <> 1 then scalarOrder.Subtract k0 else k0
107109
let e =
108110
let hasher = Sha256Digest()
109-
hasher.BlockUpdate(R.AffineXCoord.GetEncoded(), 0, 32)
110-
hasher.BlockUpdate(generatorG.Multiply(keyScalar).GetEncoded(true), 0, 33)
111+
let xEncoded = R.AffineXCoord.GetEncoded()
112+
hasher.BlockUpdate(xEncoded, 0, xEncoded.Length)
113+
let keyScalarTimesGEncoded = generatorG.Multiply(keyScalar).GetEncoded(true)
114+
hasher.BlockUpdate(keyScalarTimesGEncoded, 0, keyScalarTimesGEncoded.Length)
111115
hasher.BlockUpdate(msgHash, 0, msgHash.Length)
112-
let arr = Array.zeroCreate 32
116+
let arr = Array.zeroCreate numBytesInSha256
113117
hasher.DoFinal(arr, 0) |> ignore
114118
BigInteger.FromByteArrayUnsigned(arr).Mod(scalarOrder)
115119

Diff for: src/NLitecoin/MimbleWimble/Pedersen.fs

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ let DeserializeCommitment (commitment: PedersenCommitment) : ECPoint =
2828
point
2929

3030
/// Generates a pedersen commitment: *commit = blind * G + value * H. The blinding factor is 32 bytes.
31-
let Commit (value: CAmount) (blind: BlindingFactor) : PedersenCommitment =
31+
let Commit (value: Amount) (blind: BlindingFactor) : PedersenCommitment =
3232
let result =
3333
let blind = blind.ToUInt256().ToBytes() |> BigInteger.FromByteArrayUnsigned
3434
let a = generatorG.Multiply(blind)
@@ -39,7 +39,7 @@ let Commit (value: CAmount) (blind: BlindingFactor) : PedersenCommitment =
3939
PedersenCommitment(BigInt bytes)
4040

4141
/// Calculates the blinding factor x' = x + SHA256(xG+vH | xJ), used in the switch commitment x'G+vH.
42-
let BlindSwitch (blindingFactor: BlindingFactor) (amount: CAmount) : BlindingFactor =
42+
let BlindSwitch (blindingFactor: BlindingFactor) (amount: Amount) : BlindingFactor =
4343
let hasher = Sha256Digest()
4444

4545
let x = blindingFactor.ToUInt256().ToBytes() |> BigInteger.FromByteArrayUnsigned

Diff for: src/NLitecoin/MimbleWimble/TransactionBuilder.fs

+20-14
Original file line numberDiff line numberDiff line change
@@ -249,17 +249,23 @@ let private CreateOutputs (recipients: seq<Recipient>) : Outputs =
249249
let private CreateKernel
250250
(blind: BlindingFactor)
251251
(stealthBlind: BlindingFactor)
252-
(fee: CAmount)
253-
(peginAmount: Option<CAmount>)
252+
(fee: Amount)
253+
(peginAmount: Option<Amount>)
254254
(pegouts: array<PegOutCoin>)
255255
: Kernel =
256256
let featuresByte =
257-
(if fee > 0L then KernelFeatures.FEE_FEATURE_BIT else enum 0) |||
258-
(match peginAmount with
259-
| Some value when value > 0L -> KernelFeatures.PEGIN_FEATURE_BIT
260-
| _ -> enum 0) |||
261-
(if pegouts.Length > 0 then KernelFeatures.PEGOUT_FEATURE_BIT else enum 0) |||
262-
KernelFeatures.STEALTH_EXCESS_FEATURE_BIT
257+
(if fee > 0L then
258+
KernelFeatures.FEE_FEATURE_BIT
259+
else
260+
enum 0)
261+
||| (match peginAmount with
262+
| Some value when value > 0L -> KernelFeatures.PEGIN_FEATURE_BIT
263+
| _ -> enum 0)
264+
||| (if pegouts.Length > 0 then
265+
KernelFeatures.PEGOUT_FEATURE_BIT
266+
else
267+
enum 0)
268+
||| KernelFeatures.STEALTH_EXCESS_FEATURE_BIT
263269

264270
let excessCommit = Pedersen.Commit 0L blind
265271

@@ -278,14 +284,14 @@ let private CreateKernel
278284
let stream = BitcoinStream(byteStream, true)
279285

280286
stream.ReadWrite (featuresByte |> uint8) |> ignore
281-
Helpers.write stream excessCommit
287+
Helpers.Write stream excessCommit
282288
stream.ReadWriteAsVarInt (fee |> uint64 |> ref)
283289
match peginAmount with
284290
| Some amount -> stream.ReadWriteAsVarInt (amount |> uint64 |> ref)
285291
| None -> ()
286292
if pegouts.Length > 0 then
287-
Helpers.writeArray stream pegouts
288-
Helpers.write stream (BigInt stealthExcess)
293+
Helpers.WriteArray stream pegouts
294+
Helpers.Write stream (BigInt stealthExcess)
289295

290296
let hasher = Hasher()
291297
hasher.Write(byteStream.ToArray())
@@ -312,8 +318,8 @@ let BuildTransaction
312318
(inputCoins: array<Coin>)
313319
(recipients: array<Recipient>)
314320
(pegouts: array<PegOutCoin>)
315-
(peginAmount: Option<CAmount>)
316-
(fee: CAmount)
321+
(peginAmount: Option<Amount>)
322+
(fee: Amount)
317323
: TransactionBuildResult =
318324
let pegoutTotal = pegouts |> Array.sumBy (fun pegout -> pegout.Amount)
319325
let recipientTotal = recipients |> Array.sumBy (fun recipient -> recipient.Amount)
@@ -328,7 +334,7 @@ let BuildTransaction
328334
pegoutTotal
329335
recipientTotal
330336
fee)
331-
raise (IncorrectBalanceException msg)
337+
raise <| IncorrectBalanceException msg
332338

333339
let inputs = CreateInputs inputCoins
334340
let outputs = CreateOutputs recipients

0 commit comments

Comments
 (0)