NC - 网络编码的Java实现(1)-GF28基本运算

网络编码在GF28上的实现,用Java实现。

我自己搜了一下,Java相应的代码比较少,有一些cpp的代码,我就改成了java版本的了。
先改一个迦罗瓦域的加减乘除的基本运算。采用查表的方法。

public class GF28
{
    private static byte[] expTable = { 
        1, 3, 5, 15, 17, 51, 85, (byte) 255, 26, 46, 114, (byte) 150, (byte) 161, (byte) 248, 19, 53,
        95, (byte) 225, 56, 72, (byte) 216, 115, (byte) 149, (byte) 164, (byte) 247, 2, 6, 10, 30, 34, 102, (byte) 170,
        (byte) 229, 52, 92, (byte) 228, 55, 89, (byte) 235, 38, 106, (byte) 190, (byte) 217, 112, (byte) 144, (byte) 171, (byte) 230, 49,
        83, (byte) 245, 4, 12, 20, 60, 68, (byte) 204, 79, (byte) 209, 104, (byte) 184, (byte) 211, 110, (byte) 178, (byte) 205,
        76, (byte) 212, 103, (byte) 169, (byte) 224, 59, 77, (byte) 215, 98, (byte) 166, (byte) 241, 8, 24, 40, 120, (byte) 136,
        (byte) 131, (byte) 158, (byte) 185, (byte) 208, 107, (byte) 189, (byte) 220, 127, (byte) 129, (byte) 152, (byte) 179, (byte) 206, 73, (byte) 219, 118, (byte) 154,
        (byte) 181, (byte) 196, 87, (byte) 249, 16, 48, 80, (byte) 240, 11, 29, 39, 105, (byte) 187, (byte) 214, 97, (byte) 163,
        (byte) 254, 25, 43, 125, (byte) 135, (byte) 146, (byte) 173, (byte) 236, 47, 113, (byte) 147, (byte) 174, (byte) 233, 32, 96, (byte) 160,
        (byte) 251, 22, 58, 78, (byte) 210, 109, (byte) 183, (byte) 194, 93, (byte) 231, 50, 86, (byte) 250, 21, 63, 65,
        (byte) 195, 94, (byte) 226, 61, 71, (byte) 201, 64, (byte) 192, 91, (byte) 237, 44, 116, (byte) 156, (byte) 191, (byte) 218, 117,
        (byte) 159, (byte) 186, (byte) 213, 100, (byte) 172, (byte) 239, 42, 126, (byte) 130, (byte) 157, (byte) 188, (byte) 223, 122, (byte) 142, (byte) 137, (byte) 128,
        (byte) 155, (byte) 182, (byte) 193, 88, (byte) 232, 35, 101, (byte) 175, (byte) 234, 37, 111, (byte) 177, (byte) 200, 67, (byte) 197, 84,
        (byte) 252, 31, 33, 99, (byte) 165, (byte) 244, 7, 9, 27, 45, 119, (byte) 153, (byte) 176, (byte) 203, 70, (byte) 202,
        69, (byte) 207, 74, (byte) 222, 121, (byte) 139, (byte) 134, (byte) 145, (byte) 168, (byte) 227, 62, 66, (byte) 198, 81, (byte) 243, 14,
        18, 54, 90, (byte) 238, 41, 123, (byte) 141, (byte) 140, (byte) 143, (byte) 138, (byte) 133, (byte) 148, (byte) 167, (byte) 242, 13, 23,
        57, 75, (byte) 221, 124, (byte) 132, (byte) 151, (byte) 162, (byte) 253, 28, 36, 108, (byte) 180, (byte) 199, 82, (byte) 246, 1
    };

    private static int[] logTable = {
        -1, 0, 25, 1, 50, 2, 26, 198, 75, 199, 27, 104, 51, 238, 223, 3,
        100, 4, 224, 14, 52, 141, 129, 239, 76, 113, 8, 200, 248, 105, 28, 193,
        125, 194, 29, 181, 249, 185, 39, 106, 77, 228, 166, 114, 154, 201, 9, 120,
        101, 47, 138, 5, 33, 15, 225, 36, 18, 240, 130, 69, 53, 147, 218, 142,
        150, 143, 219, 189, 54, 208, 206, 148, 19, 92, 210, 241, 64, 70, 131, 56,
        102, 221, 253, 48, 191, 6, 139, 98, 179, 37, 226, 152, 34, 136, 145, 16,
        126, 110, 72, 195, 163, 182, 30, 66, 58, 107, 40, 84, 250, 133, 61, 186,
        43, 121, 10, 21, 155, 159, 94, 202, 78, 212, 172, 229, 243, 115, 167, 87,
        175, 88, 168, 80, 244, 234, 214, 116, 79, 174, 233, 213, 231, 230, 173, 232,
        44, 215, 117, 122, 235, 22, 11, 245, 89, 203, 95, 176, 156, 169, 81, 160,
        127, 12, 246, 111, 23, 196, 73, 236, 216, 67, 31, 45, 164, 118, 123, 183,
        204, 187, 62, 90, 251, 96, 177, 134, 59, 82, 161, 108, 170, 85, 41, 157,
        151, 178, 135, 144, 97, 190, 220, 252, 188, 149, 207, 205, 55, 63, 91, 209,
        83, 57, 132, 60, 65, 162, 109, 71, 20, 42, 158, 93, 86, 242, 211, 171,
        68, 17, 146, 217, 35, 32, 46, 137, 180, 124, 184, 38, 119, 153, 227, 165,
        103, 74, 237, 222, 197, 49, 254, 24, 13, 99, 140, 128, 192, 247, 112, 7,
    };


    public static byte add(byte a, byte b)
    {
        return (byte) (a ^ b);
    }

    public static byte sub(byte a, byte b)
    {
        return (byte) (a ^ b);
    }

    public static byte mul(byte a, byte b)
    {
        if (a == 0 || b == 0)
            return 0;
        else
        {
            int e = (logTable[a&0xff] + logTable[b&0xff]) % 255;
            return expTable[e];
        }
    }

    public static byte inv(byte a)
    {
        if (a == 0)
            return 0;
        else
        {
            int e = (255 - logTable[a&0xff]) % 255;
            return expTable[e];
        }
    }

}

由于Java中的byte是有符号型的,所以一些地方做了些转换。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值