Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Miniscript final version #764

Open
wants to merge 51 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
9adf2b0
WIP: prepare miniscript
joemphilips Oct 17, 2019
14829ff
Update
joemphilips Oct 18, 2019
d395699
Update
joemphilips Oct 29, 2019
c4a66cf
Update
joemphilips Oct 31, 2019
216ea37
Chane TypeCheck functionality to static
joemphilips Oct 31, 2019
6186ed4
Fix compile error
joemphilips Oct 31, 2019
a88edc6
redefine every Parser Combinator as function so that we can easily fi…
joemphilips Oct 31, 2019
168dd09
Update MiniscriptKey
joemphilips Nov 2, 2019
692a977
Fix StackOVerflow bug when parsing ConcretePolicy
joemphilips Nov 3, 2019
58ec205
Implement Types members and Compilers
joemphilips Nov 5, 2019
07518c7
Use single Exception for Internal Error
joemphilips Nov 7, 2019
0682061
Fix bug in Or expression parser
joemphilips Nov 7, 2019
7f3f8ef
Pass ConcretePolicy parser test
joemphilips Nov 7, 2019
0129c3a
Modify GetChild
joemphilips Nov 8, 2019
2c0d72e
Update Terminal Parser
joemphilips Nov 8, 2019
7f795c4
Add assertion to check contents after parsing
joemphilips Nov 8, 2019
464c870
Prepare Terminal.ToString and ToDebugSTring
joemphilips Nov 9, 2019
6ef1271
Change order of Wrapper parser in Terminal Parser
joemphilips Nov 9, 2019
b73d8e5
Implement Parser without Parser Combinator
joemphilips Nov 9, 2019
9f4cbcc
Prepare generator for ConcretePolicy
joemphilips Nov 10, 2019
183c529
* Prepare ConcretePolicyGenerator
joemphilips Nov 12, 2019
53d3f2c
finish ConcretePolicy Property tests
joemphilips Nov 12, 2019
eed733b
Delete unused DSLParser
joemphilips Nov 12, 2019
cf22eb3
Cleanup tests
joemphilips Nov 12, 2019
ecfa69e
Update tests
joemphilips Nov 12, 2019
d75e079
Update
joemphilips Nov 12, 2019
a06e13f
Update
joemphilips Nov 12, 2019
a5a162f
Update
joemphilips Nov 12, 2019
7ec6ae6
Pass every unit tests for miniscript
joemphilips Nov 12, 2019
54701d6
only generate positive number for times
joemphilips Nov 12, 2019
007715e
Fix bug in compiler
joemphilips Nov 14, 2019
7b70619
Speed up compiling by not throwing error
joemphilips Nov 14, 2019
4b066ed
Update compiler tests
joemphilips Nov 15, 2019
3f35aae
prepare ToString for AbstractPolicy
joemphilips Nov 15, 2019
4e1e59d
Fix more bugs in compiler
joemphilips Nov 15, 2019
20f4536
Avoid throwing FragmentPropertyException for internal flow-handling
joemphilips Nov 15, 2019
157888b
Fix some obvious bugs in ToString and Equals
joemphilips Nov 15, 2019
021dac0
Fix some compiler bugs
joemphilips Nov 17, 2019
e67563a
Fix every compiler bug caused by FragmentType Equality
joemphilips Nov 17, 2019
91f8843
update compiler tests
joemphilips Nov 17, 2019
546c13b
Fix bug in Malleability.CastTrue
joemphilips Nov 17, 2019
48cdabd
Implement AbstractPolicy.Sort
joemphilips Nov 17, 2019
b7534c1
Remove some unused files
joemphilips Nov 18, 2019
0a6b98d
Start writing deserializer
joemphilips Nov 19, 2019
03e5506
Prepare deserilizer
joemphilips Nov 19, 2019
d1d66c8
Prepare DummyKey for testing
joemphilips Nov 20, 2019
4ac52b1
Fix bug in OrI.ToString
joemphilips Nov 20, 2019
e5ac715
Fix bug in script num size
joemphilips Nov 20, 2019
c65bcaf
nitfix
joemphilips Nov 21, 2019
adb1390
Simplicy AstElem.Equals by serializing to script
joemphilips Nov 21, 2019
8a497ad
Merge branch 'fix_bug_in_compiler' into miniscript_final_version
joemphilips Nov 21, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 141 additions & 0 deletions NBitcoin.Tests/Generators/ConcretePolicyGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading;
using FsCheck;
using NBitcoin.Scripting.Miniscript;
using NBitcoin.Scripting.Miniscript.Policy;

namespace NBitcoin.Tests.Generators
{
public class ConcretePolicyGenerator
{
public static Arbitrary<ConcretePolicy<PubKey, uint160>> PubKeyConcretePolicyArb()
=> new ArbitraryPubKeyConcretePolicy();

public class ArbitraryPubKeyConcretePolicy : Arbitrary<ConcretePolicy<PubKey, uint160>>
{
public override Gen<ConcretePolicy<PubKey, uint160>> Generator => Gen.Sized(ConcretePolicyGen);

/*
public override IEnumerable<ConcretePolicy<PubKey, uint160>> Shrinker(ConcretePolicy<PubKey, uint160> parent)
{
switch (parent)
{
case ConcretePolicy<PubKey, uint160>.And p:
foreach (var i in p.Item)
yield return i;
foreach (var i in Arb.Shrink(p.Item))
if (i.Count == 2)
yield return ConcretePolicy<PubKey, uint160>.NewAnd(i);
break;
case ConcretePolicy<PubKey, uint160>.Or p:
foreach (var i in p.Item)
yield return i.Item2;
foreach (var i in Arb.Shrink(p.Item))
if (i.Count == 2)
yield return ConcretePolicy<PubKey, uint160>.NewOr(i);
break;
case ConcretePolicy<PubKey, uint160>.Threshold p:
foreach (var subP in p.Item2)
{
yield return subP;
}
foreach (var i in Arb.Shrink(p.Item2).Select(s => s.Select(sub => Shrinker(sub))))
{
foreach (var i2 in i)
if (1 < i2.Count())
yield return ConcretePolicy<PubKey, uint160>.NewThreshold(1, i2);
}

foreach (var subP in Arb.Shrink(p.Item2))
{
if (1 < subP.Count())
yield return ConcretePolicy<PubKey, uint160>.NewThreshold(1, subP);
}
yield break;
}
}
*/

private static Gen<ConcretePolicy<PubKey, uint160>> ConcretePolicyGen(int size)
{
if (size == 0) return NonRecursivePolicyGen();
return Gen.Frequency(
Tuple.Create(3, NonRecursivePolicyGen()),
Tuple.Create(2, RecursivePolicyGen(size / 2))
);
}

private static Gen<ConcretePolicy<PubKey, uint160>> NonRecursivePolicyGen()
=> Gen.OneOf(new[]
{
KeyGen(),
AfterGen(),
OlderGen(),
Sha256Gen(),
Hash256Gen(),
Ripemd160Gen(),
Hash160Gen()
});

private static Gen<ConcretePolicy<PubKey, uint160>> KeyGen()
=>
from inner in CryptoGenerator.PublicKey()
select ConcretePolicy<PubKey, uint160>.NewKey(inner);

private static Gen<ConcretePolicy<PubKey, uint160>> AfterGen()
=>
from t in Arb.Generate<PositiveInt>()
select ConcretePolicy<PubKey, uint160>.NewAfter((uint)t.Get);
private static Gen<ConcretePolicy<PubKey, uint160>> OlderGen()
=>
from t in Arb.Generate<PositiveInt>()
select ConcretePolicy<PubKey, uint160>.NewOlder((uint)t.Get);

private static Gen<ConcretePolicy<PubKey, uint160>> Sha256Gen()
=>
from t in CryptoGenerator.Hash256()
select ConcretePolicy<PubKey, uint160>.NewSha256(t);

private static Gen<ConcretePolicy<PubKey, uint160>> Hash256Gen()
=>
from t in CryptoGenerator.Hash256()
select ConcretePolicy<PubKey, uint160>.NewHash256(t);

private static Gen<ConcretePolicy<PubKey, uint160>> Ripemd160Gen()
=>
from t in CryptoGenerator.Hash160()
select ConcretePolicy<PubKey, uint160>.NewRipemd160(t);

private static Gen<ConcretePolicy<PubKey, uint160>> Hash160Gen()
=>
from t in CryptoGenerator.Hash160()
select ConcretePolicy<PubKey, uint160>.NewHash160(t);

private static Gen<ConcretePolicy<PubKey, uint160>> RecursivePolicyGen(int size)
=>
Gen.OneOf(
(from sub in ConcretePolicyGen(size).Two()
select ConcretePolicy<PubKey, uint160>.NewAnd(new[] {sub.Item1, sub.Item2})),
(from prob in Arb.Generate<PositiveInt>().Two()
from sub in ConcretePolicyGen(size).Two()
select ConcretePolicy<PubKey, uint160>.NewOr(
new []{
Tuple.Create((uint)prob.Item1.Get, sub.Item1),
Tuple.Create((uint)prob.Item2.Get, sub.Item2)
})),
(from t in ThresholdContentsGen(size)
select ConcretePolicy<PubKey, uint160>.NewThreshold(t.Item1, t.Item2))
);

private static Gen<Tuple<uint, ConcretePolicy<PubKey, uint160>[]>> ThresholdContentsGen(int size)
=>
from n in Gen.Choose(1, 6)
from actualN in n == 1 ? Gen.Choose(2, 6) : Gen.Choose(n, 6)
from a in Gen.ArrayOf(actualN, ConcretePolicyGen(size))
select Tuple.Create((uint) n, a);
}
}

}
200 changes: 102 additions & 98 deletions NBitcoin.Tests/Generators/CryptoGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,98 +1,102 @@
using System;
using NBitcoin;
using FsCheck;
using System.Collections.Generic;
using Microsoft.FSharp.Collections;
using System.Linq;
using NBitcoin.Crypto;

namespace NBitcoin.Tests.Generators
{
public class CryptoGenerator
{
#region PrivateKey
public static Arbitrary<Key> KeysArb() =>
Arb.From(PrivateKey());

public static Arbitrary<ExtKey> ExtKeysArb() =>
Arb.From(ExtKey());

public static Arbitrary<List<Key>> KeysListArb() =>
Arb.From(PrivateKeys(15));

public static Arbitrary<KeyPath> ExtPathArb() =>
Arb.From(KeyPath());

public static Gen<Key> PrivateKey() => Gen.Fresh(() => new Key());

public static Gen<List<Key>> PrivateKeys(int n) =>
from pk in Gen.ListOf<Key>(n, PrivateKey())
select pk.ToList();

#endregion

public static Gen<PubKey> PublicKey() =>
PrivateKey().Select(p => p.PubKey);

public static Gen<List<PubKey>> PublicKeys() =>
from n in Gen.Choose(0, 15)
from pks in PublicKeys(n)
select pks;

public static Gen<List<PubKey>> PublicKeys(int n) =>
from pks in Gen.ListOf(n, PublicKey())
select pks.ToList();

#region hash
public static Gen<uint256> Hash256() =>
from bytes in PrimitiveGenerator.RandomBytes(32)
select new uint256(bytes);

public static Gen<uint160> Hash160() =>
from bytes in PrimitiveGenerator.RandomBytes(20)
select new uint160(bytes);
#endregion

#region ECDSASignature
public static Gen<ECDSASignature> ECDSA() =>
from hash in Hash256()
from priv in PrivateKey()
select priv.Sign(hash);

public static Gen<List<ECDSASignature>> ECDSAs() =>
from n in Gen.Choose(0, 20)
from sigs in ECDSAs(n)
select sigs.ToList();

public static Gen<List<ECDSASignature>> ECDSAs(int n) =>
from sigs in Gen.ListOf(n, ECDSA())
select sigs.ToList();
#endregion

#region TransactionSignature
public static Gen<TransactionSignature> TransactionSignature() =>
from rawsig in ECDSA()
from sighash in SigHashType()
select new TransactionSignature(rawsig, sighash);

public static Gen<SigHash> SigHashType() =>
Gen.OneOf<SigHash>(new List<Gen<SigHash>> {
Gen.Constant(SigHash.All),
Gen.Constant(SigHash.Single),
Gen.Constant(SigHash.None),
Gen.Constant(SigHash.AnyoneCanPay | SigHash.All),
Gen.Constant(SigHash.AnyoneCanPay | SigHash.Single),
Gen.Constant(SigHash.AnyoneCanPay | SigHash.None)
});
#endregion

public static Gen<ExtKey> ExtKey() => Gen.Fresh(() => new ExtKey());

public static Gen<KeyPath> KeyPath() =>
from raw in Gen.NonEmptyListOf(PrimitiveGenerator.RandomBytes(4))
let flattenBytes = raw.ToList().Aggregate((a, b) => a.Concat(b))
select NBitcoin.KeyPath.FromBytes(flattenBytes);

public static Gen<ExtPubKey> ExtPubKey() => ExtKey().Select(ek => ek.Neuter());
}
}
using System;
using NBitcoin;
using FsCheck;
using System.Collections.Generic;
using Microsoft.FSharp.Collections;
using System.Linq;
using NBitcoin.Crypto;

namespace NBitcoin.Tests.Generators
{
public class CryptoGenerator
{
#region PrivateKey
public static Arbitrary<Key> KeysArb() =>
Arb.From(PrivateKey());

public static Arbitrary<ExtKey> ExtKeysArb() =>
Arb.From(ExtKey());

public static Arbitrary<List<Key>> KeysListArb() =>
Arb.From(PrivateKeys(15));

public static Arbitrary<KeyPath> ExtPathArb() =>
Arb.From(KeyPath());


public static Arbitrary<PubKey> PubKeyArb() =>
Arb.From(PublicKey());

public static Gen<Key> PrivateKey() => Gen.Fresh(() => new Key());

public static Gen<List<Key>> PrivateKeys(int n) =>
from pk in Gen.ListOf<Key>(n, PrivateKey())
select pk.ToList();

#endregion

public static Gen<PubKey> PublicKey() =>
PrivateKey().Select(p => p.PubKey);

public static Gen<List<PubKey>> PublicKeys() =>
from n in Gen.Choose(0, 15)
from pks in PublicKeys(n)
select pks;

public static Gen<List<PubKey>> PublicKeys(int n) =>
from pks in Gen.ListOf(n, PublicKey())
select pks.ToList();

#region hash
public static Gen<uint256> Hash256() =>
from bytes in PrimitiveGenerator.RandomBytes(32)
select new uint256(bytes);

public static Gen<uint160> Hash160() =>
from bytes in PrimitiveGenerator.RandomBytes(20)
select new uint160(bytes);
#endregion

#region ECDSASignature
public static Gen<ECDSASignature> ECDSA() =>
from hash in Hash256()
from priv in PrivateKey()
select priv.Sign(hash);

public static Gen<List<ECDSASignature>> ECDSAs() =>
from n in Gen.Choose(0, 20)
from sigs in ECDSAs(n)
select sigs.ToList();

public static Gen<List<ECDSASignature>> ECDSAs(int n) =>
from sigs in Gen.ListOf(n, ECDSA())
select sigs.ToList();
#endregion

#region TransactionSignature
public static Gen<TransactionSignature> TransactionSignature() =>
from rawsig in ECDSA()
from sighash in SigHashType()
select new TransactionSignature(rawsig, sighash);

public static Gen<SigHash> SigHashType() =>
Gen.OneOf<SigHash>(new List<Gen<SigHash>> {
Gen.Constant(SigHash.All),
Gen.Constant(SigHash.Single),
Gen.Constant(SigHash.None),
Gen.Constant(SigHash.AnyoneCanPay | SigHash.All),
Gen.Constant(SigHash.AnyoneCanPay | SigHash.Single),
Gen.Constant(SigHash.AnyoneCanPay | SigHash.None)
});
#endregion

public static Gen<ExtKey> ExtKey() => Gen.Fresh(() => new ExtKey());

public static Gen<KeyPath> KeyPath() =>
from raw in Gen.NonEmptyListOf(PrimitiveGenerator.RandomBytes(4))
let flattenBytes = raw.ToList().Aggregate((a, b) => a.Concat(b))
select NBitcoin.KeyPath.FromBytes(flattenBytes);

public static Gen<ExtPubKey> ExtPubKey() => ExtKey().Select(ek => ek.Neuter());
}
}
Loading