|
| 1 | +#include "../derive_basepoints.c" |
| 2 | +#include <ccan/err/err.h> |
| 3 | +#include <ccan/mem/mem.h> |
| 4 | +#include <ccan/str/hex/hex.h> |
| 5 | +#include <ccan/structeq/structeq.h> |
| 6 | +#include <common/utils.h> |
| 7 | +#include <stdio.h> |
| 8 | +#include <wally_core.h> |
| 9 | + |
| 10 | +/* AUTOGENERATED MOCKS START */ |
| 11 | +/* Generated stub for fromwire_pubkey */ |
| 12 | +void fromwire_pubkey(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct pubkey *pubkey UNNEEDED) |
| 13 | +{ fprintf(stderr, "fromwire_pubkey called!\n"); abort(); } |
| 14 | +/* Generated stub for towire_pubkey */ |
| 15 | +void towire_pubkey(u8 **pptr UNNEEDED, const struct pubkey *pubkey UNNEEDED) |
| 16 | +{ fprintf(stderr, "towire_pubkey called!\n"); abort(); } |
| 17 | +/* AUTOGENERATED MOCKS END */ |
| 18 | + |
| 19 | +STRUCTEQ_DEF(basepoints, 0, |
| 20 | + revocation.pubkey, |
| 21 | + payment.pubkey, |
| 22 | + htlc.pubkey, |
| 23 | + delayed_payment.pubkey); |
| 24 | +STRUCTEQ_DEF(secrets, 0, |
| 25 | + funding_privkey.secret.data, |
| 26 | + revocation_basepoint_secret.data, |
| 27 | + payment_basepoint_secret.data, |
| 28 | + htlc_basepoint_secret.data, |
| 29 | + delayed_payment_basepoint_secret.data); |
| 30 | +STRUCTEQ_DEF(privkey, 0, |
| 31 | + secret.data); |
| 32 | + |
| 33 | +struct info { |
| 34 | + struct secret seed; |
| 35 | + struct pubkey funding_pubkey; |
| 36 | + struct basepoints basepoints; |
| 37 | + struct secrets secrets; |
| 38 | + struct sha256 shaseed; |
| 39 | +}; |
| 40 | + |
| 41 | +/* We get a fresh one each time, to catch uninitialized fields */ |
| 42 | +static struct info *new_info(const tal_t *ctx) |
| 43 | +{ |
| 44 | + struct info *info = tal(ctx, struct info); |
| 45 | + memset(&info->seed, 7, sizeof(info->seed)); |
| 46 | + |
| 47 | + return info; |
| 48 | +} |
| 49 | + |
| 50 | +int main(void) |
| 51 | +{ |
| 52 | + setup_locale(); |
| 53 | + |
| 54 | + const tal_t *ctx = tal(NULL, char); |
| 55 | + struct info *baseline, *info; |
| 56 | + |
| 57 | + secp256k1_ctx = wally_get_secp_context(); |
| 58 | + baseline = new_info(ctx); |
| 59 | + assert(derive_basepoints(&baseline->seed, &baseline->funding_pubkey, |
| 60 | + &baseline->basepoints, |
| 61 | + &baseline->secrets, |
| 62 | + &baseline->shaseed)); |
| 63 | + |
| 64 | + /* Same seed, same result. */ |
| 65 | + info = new_info(ctx); |
| 66 | + assert(derive_basepoints(&info->seed, &info->funding_pubkey, |
| 67 | + &info->basepoints, |
| 68 | + &info->secrets, |
| 69 | + &info->shaseed)); |
| 70 | + assert(pubkey_eq(&baseline->funding_pubkey, &info->funding_pubkey)); |
| 71 | + assert(basepoints_eq(&baseline->basepoints, &info->basepoints)); |
| 72 | + assert(secrets_eq(&baseline->secrets, &info->secrets)); |
| 73 | + assert(sha256_eq(&baseline->shaseed, &info->shaseed)); |
| 74 | + |
| 75 | + /* Different seed, different result. */ |
| 76 | + for (size_t i = 0; i < sizeof(info->seed); i++) { |
| 77 | + for (size_t b = 0; b < CHAR_BIT; b++) { |
| 78 | + info = new_info(ctx); |
| 79 | + info->seed.data[i] ^= (1 << b); |
| 80 | + |
| 81 | + assert(derive_basepoints(&info->seed, |
| 82 | + &info->funding_pubkey, |
| 83 | + &info->basepoints, |
| 84 | + &info->secrets, |
| 85 | + &info->shaseed)); |
| 86 | + assert(!pubkey_eq(&baseline->funding_pubkey, |
| 87 | + &info->funding_pubkey)); |
| 88 | + assert(!basepoints_eq(&baseline->basepoints, |
| 89 | + &info->basepoints)); |
| 90 | + assert(!secrets_eq(&baseline->secrets, &info->secrets)); |
| 91 | + assert(!sha256_eq(&baseline->shaseed, &info->shaseed)); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /* Any field can be NULL (except seed). */ |
| 96 | + info = new_info(ctx); |
| 97 | + assert(derive_basepoints(&info->seed, NULL, |
| 98 | + &info->basepoints, |
| 99 | + &info->secrets, |
| 100 | + &info->shaseed)); |
| 101 | + assert(basepoints_eq(&baseline->basepoints, &info->basepoints)); |
| 102 | + assert(secrets_eq(&baseline->secrets, &info->secrets)); |
| 103 | + assert(sha256_eq(&baseline->shaseed, &info->shaseed)); |
| 104 | + |
| 105 | + info = new_info(ctx); |
| 106 | + assert(derive_basepoints(&info->seed, &info->funding_pubkey, |
| 107 | + NULL, |
| 108 | + &info->secrets, |
| 109 | + &info->shaseed)); |
| 110 | + assert(pubkey_eq(&baseline->funding_pubkey, &info->funding_pubkey)); |
| 111 | + assert(secrets_eq(&baseline->secrets, &info->secrets)); |
| 112 | + assert(sha256_eq(&baseline->shaseed, &info->shaseed)); |
| 113 | + |
| 114 | + info = new_info(ctx); |
| 115 | + assert(derive_basepoints(&info->seed, &info->funding_pubkey, |
| 116 | + &info->basepoints, |
| 117 | + NULL, |
| 118 | + &info->shaseed)); |
| 119 | + assert(pubkey_eq(&baseline->funding_pubkey, &info->funding_pubkey)); |
| 120 | + assert(basepoints_eq(&baseline->basepoints, &info->basepoints)); |
| 121 | + assert(sha256_eq(&baseline->shaseed, &info->shaseed)); |
| 122 | + |
| 123 | + info = new_info(ctx); |
| 124 | + assert(derive_basepoints(&info->seed, &info->funding_pubkey, |
| 125 | + &info->basepoints, |
| 126 | + &info->secrets, |
| 127 | + NULL)); |
| 128 | + assert(pubkey_eq(&baseline->funding_pubkey, &info->funding_pubkey)); |
| 129 | + assert(basepoints_eq(&baseline->basepoints, &info->basepoints)); |
| 130 | + assert(secrets_eq(&baseline->secrets, &info->secrets)); |
| 131 | + |
| 132 | + /* derive_payment_basepoint should give same results. */ |
| 133 | + info = new_info(ctx); |
| 134 | + assert(derive_payment_basepoint(&info->seed, &info->basepoints.payment, |
| 135 | + &info->secrets.payment_basepoint_secret)); |
| 136 | + assert(pubkey_eq(&baseline->basepoints.payment, |
| 137 | + &info->basepoints.payment)); |
| 138 | + assert(secret_eq(&baseline->secrets.payment_basepoint_secret, |
| 139 | + &info->secrets.payment_basepoint_secret)); |
| 140 | + |
| 141 | + /* derive_funding_key should give same results. */ |
| 142 | + info = new_info(ctx); |
| 143 | + assert(derive_funding_key(&info->seed, &info->funding_pubkey, |
| 144 | + &info->secrets.funding_privkey)); |
| 145 | + assert(pubkey_eq(&baseline->funding_pubkey, &info->funding_pubkey)); |
| 146 | + assert(privkey_eq(&baseline->secrets.funding_privkey, |
| 147 | + &info->secrets.funding_privkey)); |
| 148 | + |
| 149 | + /* derive_shaseed should give same results. */ |
| 150 | + info = new_info(ctx); |
| 151 | + assert(derive_shaseed(&info->seed, &info->shaseed)); |
| 152 | + assert(sha256_eq(&baseline->shaseed, &info->shaseed)); |
| 153 | + |
| 154 | + tal_free(ctx); |
| 155 | + wally_cleanup(0); |
| 156 | + return 0; |
| 157 | +} |
0 commit comments