mpc4j icon indicating copy to clipboard operation
mpc4j copied to clipboard

想问下AHE的java库有没有建议

Open lzjluzijie opened this issue 11 months ago • 8 comments

首先想感谢一下作者,Z_p的okvs库写的质量很高很清晰。

我之前都是用libote系的库,没用过java库,想用这个库的同时用 additive homomorphic encryption 的库,想问一下有没有什么建议?我找到几个java库似乎都比较旧了:

  • https://github.com/n1analytics/javallier
  • https://labs.utdallas.edu/dspl/software/pailliertoolbox/

谢谢!

lzjluzijie avatar May 02 '25 03:05 lzjluzijie

在mpc4j的1.1.2版本包含了mpc4j-crypto-phe,里面就是我们修改过后的Javallier库。我认为这个库的质量很高。之前我们修改此库并合并到mpc4j的目的是支持基于半同态加密的乘法三元组生成。但现在由于有了silent OT,乘法三元组生成不再需要通信量大、计算缓慢的半同态加密方法,因此我们移除了此模块。

可以查看v1.1.2版本,里面也描述了我们在使用和修改Javallier时候遇到的问题。最大的问题是Javallier库会调用GMP来加速代数运算,但在M1和M3下编译出的GMP库版本不兼容。在v1.1.2里面,我们已经将所有GMP依赖改回了BigInteger(这个不会带来太大的性能损失,因为JDK17后的BigInteger已经优化得非常好了)。如果确实需要,可以告知我们你在实现哪个方案,我们可以根据情况看是否重新把Javallier引用回来。

liuweiran900217 avatar May 02 '25 06:05 liuweiran900217

非常感谢,我尝试在M3的mac上跑v1.1.2版本,PhePlaintextAdditionTest可以直接跑,但是PheAdditionTest不知道为什么似乎跑不了,intellij显示一直在instantiating tests,还有一次是A fatal error has been detected by the Java Runtime Environment。我又在windows上尝试,test都能正常跑,没有问题。想问下会是什么问题呢?我用的都是jdk21

lzjluzijie avatar May 03 '25 02:05 lzjluzijie

我参考unit test写了一段测试代码,发现循环运行几次到几百次就会 segmentation fault,我试了jdk21 23 24都这样,想问下这个是我本机的问题还是代码的问题呢?

    public static void phe() {
        PheEngine pheEngine = PheFactory.createInstance(PheFactory.PheType.PAI99, SECURE_RANDOM);
        PhePrivateKey sk = pheEngine.keyGen(new PheKeyGenParams(PheSecLevel.LAMBDA_40, false, 64));
        PhePublicKey pk = sk.getPublicKey();

        // 明文被加数、明文加数、明文加法结果、加法解码结果
        long a, b, plainResult, decodedResult;
        // 密文被加数、密文加数、密文加法结果
        PheCiphertext ciphertextA, ciphertextB, encryptedResult;
        // 编码被加数、编码加数、编码解密结果
        PhePlaintext encodedA, encodedB, decryptedResult;

        for (int i = 0; i < 1000; i++) {
            System.out.println(i);
            // 生成2个随机长整数,为避免溢出,右移1位
            a = SECURE_RANDOM.nextLong() >> 1;
            b = SECURE_RANDOM.nextLong() >> 1;
            // 如果是无符号编码,则生成结果都转换为正数
            if (!pk.isSigned() && (a < 0 || b < 0)) {
                a = Math.abs(a);
                b = Math.abs(b);
            }
            ciphertextA = pheEngine.encrypt(pk, a);
            ciphertextB = pheEngine.encrypt(pk, b);
            encodedA = pk.encode(a);
            encodedB = pk.encode(b);
            // 明文运算
            plainResult = a + b;
            // 密文与密文运算
            encryptedResult = pheEngine.add(pk, ciphertextA, ciphertextB);
            decryptedResult = pheEngine.decrypt(sk, encryptedResult);
            decodedResult = decryptedResult.decodeLong();
            Assert.assertEquals(plainResult, decodedResult);
            // 密文与明文运算
            encryptedResult = pheEngine.add(pk, ciphertextA, encodedB);
            decryptedResult = pheEngine.decrypt(sk, encryptedResult);
            decodedResult = decryptedResult.decodeLong();
            Assert.assertEquals(plainResult, decodedResult);
            // 明文与密文运算
            encryptedResult = pheEngine.add(pk, ciphertextB, encodedA);
            decryptedResult = pheEngine.decrypt(sk, encryptedResult);
            decodedResult = decryptedResult.decodeLong();
            Assert.assertEquals(plainResult, decodedResult);
        }
    }
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x0000000103c35244, pid=61621, tid=29443
#
# JRE version: OpenJDK Runtime Environment Corretto-21.0.7.6.1 (21.0.7+6) (build 21.0.7+6-LTS)
# Java VM: OpenJDK 64-Bit Server VM Corretto-21.0.7.6.1 (21.0.7+6-LTS, mixed mode, sharing, tiered, compressed oops, compressed class ptrs, g1 gc, bsd-aarch64)
# Problematic frame:
# V  [libjvm.dylib+0x2f1244]  frame::sender_for_compiled_frame(RegisterMap*) const+0x184
#
# No core dump will be written. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# If you would like to submit a bug report, please visit:
#   https://github.com/corretto/corretto-21/issues/

lzjluzijie avatar May 13 '25 02:05 lzjluzijie

这就是我们移除AHE的一个原因,这个AHE的实现使用了GMP-JNI,但是这个JNI似乎在MaxOS的M2/M3下并不稳定。想解决此问题的方法是把所有调用GMP-JNI的地方都改成调用BigInteger,但目前看AHE在mpc4j下没有使用的需要,所以我们就直接移除了。如果确实需要,我们可以在后面的版本里把代码再放进来。

liuweiran900217 avatar May 13 '25 03:05 liuweiran900217

哦哦是这个意思啊,那请问你们之前有完全改成BigInteger的代码吗?我想用你们的库跑一些实验

lzjluzijie avatar May 13 '25 03:05 lzjluzijie

我最近有点忙,我抽空弄一下。

liuweiran900217 avatar May 14 '25 07:05 liuweiran900217

我发布了v1.1.4-beta版本,把mpc4j-crypto-phe的代码恢复了,并且移除了jni-gmp以及其他模块的依赖。现在mpc4j-crypto-phe是独立的模块了,可以试试看。

liuweiran900217 avatar May 16 '25 04:05 liuweiran900217

非常感谢,我试了下能用了

lzjluzijie avatar May 16 '25 04:05 lzjluzijie