arithm.ha (1960B)
1 use bytes; 2 3 @test fn add() void = { 4 let result: [4]u8 = [0...]; 5 6 let bx = fromhex("002132a0"); 7 let by = fromhex("00ff3201"); 8 9 let carry = add(bx, by, 0); 10 11 assert(carry == 0); 12 13 decode(result, bx); 14 assert(bytes::equal([0x00, 0x21, 0x32, 0xa0], result)); 15 16 let carry = add(bx, by, 1); 17 18 assert(carry == 0); 19 decode(result, bx); 20 assert(bytes::equal([0x01, 0x20, 0x64, 0xa1], result)); 21 }; 22 23 @test fn muladd_small() void = { 24 const mod = fromhex("0100"); 25 let x = fromhex("0100"); 26 27 muladd_small(x, 3, mod); 28 assert(equalshex(x, "03")); 29 30 free(mod); 31 free(x); 32 33 const mod = fromhex("1000000000"); 34 let x = fromhexmod("0000000001", mod); 35 muladd_small(x, 27, mod); 36 assert(equalshex(x, "8000001b")); 37 38 free(mod); 39 free(x); 40 }; 41 42 @test fn mulacc() void = { 43 let d = fromhex("0000000000000000"); 44 let a = fromhex("0010"); 45 let b = fromhex("0014"); 46 47 defer free(d); 48 defer free(a); 49 defer free(b); 50 51 mulacc(d, a, b); 52 assert(equalshex(d, "0140")); 53 54 mulacc(d, a, b); 55 assert(equalshex(d, "0280")); 56 }; 57 58 @test fn rshift() void = { 59 let x = fromhex("1020304050"); 60 rshift(x, 1); 61 assert(equalshex(x, "0810182028")); 62 free(x); 63 64 let x = fromhex("00"); 65 rshift(x, 1); 66 assert(equalshex(x, "")); 67 free(x); 68 }; 69 70 @test fn reduce() void = { 71 let m = fromhex("87654321"); 72 73 let a = fromhex("1234"); 74 let x = fromhex("00000000"); 75 reduce(x, a, m); 76 assert(equalshex(x, "1234")); 77 free(a); 78 free(x); 79 80 let a = fromhex("0123456789abcdef"); 81 let x = fromhex("00000000"); 82 reduce(x, a, m); 83 assert(equalshex(x, "14786ead")); 84 free(a); 85 free(x); 86 87 free(m); 88 }; 89 90 @test fn modpow() void = { 91 let m = fromhex("87654321"); 92 let x = fromhexmod("00f03202", m); 93 94 let e: [_]u8 = [0x00, 0x00, 0xc1, 0xf4]; 95 const m0i = ninv31(m[1]); 96 97 let tmp: [10]word = [0...]; 98 modpow(x, e, m, m0i, tmp); 99 100 assert(equalshex(x, "3de073fc")); 101 free(x); 102 103 let x = fromhexmod("00f03202", m); 104 let tmp: [20]word = [0...]; 105 106 modpow(x, e, m, m0i, tmp); 107 assert(equalshex(x, "3de073fc")); 108 109 free(m); 110 free(x); 111 };