hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

basic+test.ha (6162B)


      1 // SPDX-License-Identifier: MPL-2.0
      2 // (c) Hare authors <https://harelang.org>
      3 
      4 use crypto::ec;
      5 use bytes;
      6 
      7 
      8 @test fn p256_invalidpoint() void = {
      9 	let priv = p256();
     10 	priv.x[..] = [
     11 		0x7d, 0x7d, 0xc5, 0xf7, 0x1e, 0xb2, 0x9d, 0xda, 0xf8, 0x0d,
     12 		0x62, 0x14, 0x63, 0x2e, 0xea, 0xe0, 0x3d, 0x90, 0x58, 0xaf,
     13 		0x1f, 0xb6, 0xd2, 0x2e, 0xd8, 0x0b, 0xad, 0xb6, 0x2b, 0xc1,
     14 		0xa5, 0x34,
     15 	];
     16 
     17 	const pub: [_]u8 = [
     18 		0x04, 0xea, 0xd2, 0x18, 0x59, 0x01, 0x19, 0xe8, 0x87, 0x6b,
     19 		0x29, 0x14, 0x6f, 0xf8, 0x9c, 0xa6, 0x17, 0x70, 0xc4, 0xed,
     20 		0xbb, 0xf9, 0x7d, 0x38, 0xce, 0x38, 0x5e, 0xd2, 0x81, 0xd8,
     21 		0xa6, 0xb2, 0x30, 0x28, 0xaf, 0x61, 0x28, 0x1f, 0xd3, 0x5e,
     22 		0x2f, 0xa7, 0x00, 0x25, 0x23, 0xac, 0xc8, 0x5a, 0x42, 0x9c,
     23 		0xb0, 0x6e, 0xe6, 0x64, 0x83, 0x25, 0x38, 0x9f, 0x59, 0xed,
     24 		0xfc, 0xe1, 0x40, 0x51, 0x41,
     25 	];
     26 
     27 	const otherpub: [_]u8 = [
     28 		0x04, 0x70, 0x0c, 0x48, 0xf7, 0x7f, 0x56, 0x58, 0x4c, 0x5c,
     29 		0xc6, 0x32, 0x00, 0x00, 0x00, 0x00, 0xb9, 0x1b, 0x6b, 0xac,
     30 		0xce, 0x3a, 0x4d, 0xf6, 0xb4, 0x2c, 0xe7, 0xcc, 0x83, 0x88,
     31 		0x33, 0xd2, 0x00, 0xdb, 0x71, 0xe5, 0x09, 0xe3, 0xfd, 0x9b,
     32 		0x06, 0x0d, 0xdb, 0x20, 0xba, 0x5c, 0x51, 0xdc, 0xc5, 0x94,
     33 		0x8d, 0x46, 0xfb, 0xf6, 0x40, 0xdf, 0xe0, 0x44, 0x17, 0x82,
     34 		0xca, 0xb8, 0x5f, 0xa4, 0xac,
     35 	];
     36 
     37 	const shared: [_]u8 = [
     38 		0x46, 0xfc, 0x62, 0x10, 0x64, 0x20, 0xff, 0x01, 0x2e, 0x54,
     39 		0xa4, 0x34, 0xfb, 0xdd, 0x2d, 0x25, 0xcc, 0xc5, 0x85, 0x20,
     40 		0x60, 0x56, 0x1e, 0x68, 0x04, 0x0d, 0xd7, 0x77, 0x89, 0x97,
     41 		0xbd, 0x7b,
     42 	];
     43 
     44 	let dpub: [ec::P256_POINTSZ]u8 = [0...];
     45 	pubkey(dpub, &priv);
     46 	assert(bytes::equal(dpub, pub));
     47 
     48 	let dshared: [P256_SHAREDSZ]u8 = [0...];
     49 	let zero: [P256_SHAREDSZ]u8 = [0...];
     50 
     51 	assert(derive(dshared, &priv, otherpub) is invalidkey);
     52 	assert(bytes::equal(dshared, zero));
     53 };
     54 
     55 @test fn p384_invalidpoint() void = {
     56 	let priv = p384();
     57 	priv.x[..] = [
     58 		0x3c, 0xc3, 0x12, 0x2a, 0x68, 0xf0, 0xd9, 0x50, 0x27, 0xad,
     59 		0x38, 0xc0, 0x67, 0x91, 0x6b, 0xa0, 0xeb, 0x8c, 0x38, 0x89,
     60 		0x4d, 0x22, 0xe1, 0xb1, 0x56, 0x18, 0xb6, 0x81, 0x8a, 0x66,
     61 		0x17, 0x74, 0xad, 0x46, 0x3b, 0x20, 0x5d, 0xa8, 0x8c, 0xf6,
     62 		0x99, 0xab, 0x4d, 0x43, 0xc9, 0xcf, 0x98, 0xa1,
     63 	];
     64 	const pub: [_]u8 = [
     65 		0x04, 0x98, 0x03, 0x80, 0x7f, 0x2f, 0x6d, 0x2f, 0xd9, 0x66,
     66 		0xcd, 0xd0, 0x29, 0x0b, 0xd4, 0x10, 0xc0, 0x19, 0x03, 0x52,
     67 		0xfb, 0xec, 0x7f, 0xf6, 0x24, 0x7d, 0xe1, 0x30, 0x2d, 0xf8,
     68 		0x6f, 0x25, 0xd3, 0x4f, 0xe4, 0xa9, 0x7b, 0xef, 0x60, 0xcf,
     69 		0xf5, 0x48, 0x35, 0x5c, 0x01, 0x5d, 0xbb, 0x3e, 0x5f, 0xba,
     70 		0x26, 0xca, 0x69, 0xec, 0x2f, 0x5b, 0x5d, 0x9d, 0xad, 0x20,
     71 		0xcc, 0x9d, 0xa7, 0x11, 0x38, 0x3a, 0x9d, 0xbe, 0x34, 0xea,
     72 		0x3f, 0xa5, 0xa2, 0xaf, 0x75, 0xb4, 0x65, 0x02, 0x62, 0x9a,
     73 		0xd5, 0x4d, 0xd8, 0xb7, 0xd7, 0x3a, 0x8a, 0xbb, 0x06, 0xa3,
     74 		0xa3, 0xbe, 0x47, 0xd6, 0x50, 0xcc, 0x99,
     75 	];
     76 
     77 	const otherpub: [_]u8 = [
     78 		0x04, 0xa7, 0xc7, 0x6b, 0x97, 0x0c, 0x3b, 0x5f, 0xe8, 0xb0,
     79 		0x5d, 0x28, 0x38, 0xae, 0x04, 0xab, 0x47, 0x69, 0x7b, 0x9e,
     80 		0xaf, 0x52, 0xe7, 0x64, 0x59, 0x00, 0x00, 0x00, 0x00, 0xe7,
     81 		0x51, 0x32, 0x72, 0x73, 0x44, 0x66, 0xb4, 0x00, 0x09, 0x1a,
     82 		0xdb, 0xf2, 0xd6, 0x8c, 0x58, 0xe0, 0xc5, 0x00, 0x66, 0xac,
     83 		0x68, 0xf1, 0x9f, 0x2e, 0x1c, 0xb8, 0x79, 0xae, 0xd4, 0x3a,
     84 		0x99, 0x69, 0xb9, 0x1a, 0x08, 0x39, 0xc4, 0xc3, 0x8a, 0x49,
     85 		0x74, 0x9b, 0x00, 0x00, 0x00, 0x00, 0x24, 0x34, 0x51, 0x91,
     86 		0x5e, 0xd0, 0x90, 0x5a, 0x32, 0xb0, 0x60, 0x99, 0x2b, 0x46,
     87 		0x8c, 0x64, 0x76, 0x6f, 0xc8, 0x43, 0x7a,
     88 	];
     89 
     90 	let dpub: [ec::P384_POINTSZ]u8 = [0...];
     91 	pubkey(dpub, &priv);
     92 	assert(bytes::equal(dpub, pub));
     93 
     94 	let dshared: [P384_SHAREDSZ]u8 = [0...];
     95 	let zero: [P384_SHAREDSZ]u8 = [0...];
     96 
     97 	assert(derive(dshared, &priv, otherpub) is invalidkey);
     98 	assert(bytes::equal(dshared, zero));
     99 };
    100 
    101 @test fn p521_invalidpoint() void = {
    102 	let priv = p521();
    103 	priv.x[..] = [
    104 		0x01, 0x7e, 0xec, 0xc0, 0x7a, 0xb4, 0xb3, 0x29, 0x06, 0x8f,
    105 		0xba, 0x65, 0xe5, 0x6a, 0x1f, 0x88, 0x90, 0xaa, 0x93, 0x5e,
    106 		0x57, 0x13, 0x4a, 0xe0, 0xff, 0xcc, 0xe8, 0x02, 0x73, 0x51,
    107 		0x51, 0xf4, 0xea, 0xc6, 0x56, 0x4f, 0x6e, 0xe9, 0x97, 0x4c,
    108 		0x5e, 0x68, 0x87, 0xa1, 0xfe, 0xfe, 0xe5, 0x74, 0x3a, 0xe2,
    109 		0x24, 0x1b, 0xfe, 0xb9, 0x5d, 0x5c, 0xe3, 0x1d, 0xdc, 0xb6,
    110 		0xf9, 0xed, 0xb4, 0xd6, 0xfc, 0x47,
    111 	];
    112 	const pub: [_]u8 = [
    113 		0x04, 0x00, 0x60, 0x2f, 0x9d, 0x0c, 0xf9, 0xe5, 0x26, 0xb2,
    114 		0x9e, 0x22, 0x38, 0x1c, 0x20, 0x3c, 0x48, 0xa8, 0x86, 0xc2,
    115 		0xb0, 0x67, 0x30, 0x33, 0x36, 0x63, 0x14, 0xf1, 0xff, 0xbc,
    116 		0xba, 0x24, 0x0b, 0xa4, 0x2f, 0x4e, 0xf3, 0x8a, 0x76, 0x17,
    117 		0x46, 0x35, 0xf9, 0x1e, 0x6b, 0x4e, 0xd3, 0x42, 0x75, 0xeb,
    118 		0x01, 0xc8, 0x46, 0x7d, 0x05, 0xca, 0x80, 0x31, 0x5b, 0xf1,
    119 		0xa7, 0xbb, 0xd9, 0x45, 0xf5, 0x50, 0xa5, 0x01, 0xb7, 0xc8,
    120 		0x5f, 0x26, 0xf5, 0xd4, 0xb2, 0xd7, 0x35, 0x5c, 0xf6, 0xb0,
    121 		0x21, 0x17, 0x65, 0x99, 0x43, 0x76, 0x2b, 0x6d, 0x1d, 0xb5,
    122 		0xab, 0x4f, 0x1d, 0xbc, 0x44, 0xce, 0x7b, 0x29, 0x46, 0xeb,
    123 		0x6c, 0x7d, 0xe3, 0x42, 0x96, 0x28, 0x93, 0xfd, 0x38, 0x7d,
    124 		0x1b, 0x73, 0xd7, 0xa8, 0x67, 0x2d, 0x1f, 0x23, 0x69, 0x61,
    125 		0x17, 0x0b, 0x7e, 0xb3, 0x57, 0x99, 0x53, 0xee, 0x5c, 0xdc,
    126 		0x88, 0xcd, 0x2d,
    127 	];
    128 
    129 	const otherpub: [_]u8 = [
    130 		0x04, 0x00, 0x68, 0x5a, 0x48, 0xe8, 0x6c, 0x79, 0xf0, 0xf0,
    131 		0x87, 0x5f, 0x7b, 0xc1, 0x8d, 0x25, 0xeb, 0x5f, 0xc8, 0xc0,
    132 		0xb0, 0x7e, 0x5d, 0xa4, 0xf4, 0x37, 0x0f, 0x3a, 0x94, 0x90,
    133 		0x34, 0x08, 0x54, 0x33, 0x4b, 0x1e, 0x1b, 0x87, 0xfa, 0x39,
    134 		0x54, 0x64, 0xc6, 0x06, 0x00, 0x00, 0x00, 0x00, 0x70, 0xd0,
    135 		0xf7, 0x85, 0x60, 0x1d, 0x37, 0xc0, 0x98, 0x70, 0xeb, 0xf1,
    136 		0x76, 0x66, 0x68, 0x77, 0xa2, 0x04, 0x6d, 0x01, 0xba, 0x52,
    137 		0xc5, 0x6f, 0xc8, 0x77, 0x6d, 0x9e, 0x8f, 0x5d, 0xb4, 0xf0,
    138 		0xcc, 0x27, 0x63, 0x6d, 0x0b, 0x74, 0x1b, 0xbe, 0x05, 0x40,
    139 		0x06, 0x97, 0x94, 0x2e, 0x80, 0xb7, 0x39, 0x88, 0x4a, 0x83,
    140 		0xbd, 0xe9, 0x9e, 0x0f, 0x67, 0x16, 0x93, 0x9e, 0x63, 0x2b,
    141 		0xc8, 0x00, 0x00, 0x00, 0x00, 0xcc, 0xd4, 0x43, 0xa3, 0x48,
    142 		0xb6, 0xc3, 0xe5, 0x22, 0x49, 0x79, 0x55, 0xa4, 0xf3, 0xc3,
    143 		0x02, 0xf6, 0x76,
    144 	];
    145 
    146 	let dpub: [ec::P521_POINTSZ]u8 = [0...];
    147 	pubkey(dpub, &priv);
    148 	assert(bytes::equal(dpub, pub));
    149 
    150 	let dshared: [P521_SHAREDSZ]u8 = [0...];
    151 	let zero: [P521_SHAREDSZ]u8 = [0...];
    152 
    153 	assert(derive(dshared, &priv, otherpub) is invalidkey);
    154 	assert(bytes::equal(dshared, zero));
    155 };