hare

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

core_test.ha (6553B)


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