Ejemplo n.º 1
0
        public ValueMessage HandleCommit(int sender, CommitMessage message)
        {
            if (message.EncryptedRows.Length != Players)
            {
                throw new ArgumentException();
            }
            if (_keyGenStates[sender].Commitment != null)
            {
                throw new ArgumentException($"Double commit from sender {sender}");
            }
            _keyGenStates[sender].Commitment = message.Commitment;
            var myRowCommitted = message.Commitment.Evaluate(_myIdx + 1);
            var myRow          = DecryptRow(message.EncryptedRows[_myIdx], _keyPair.PrivateKey).ToArray();

            if (!myRow.Select(x => G1.Generator * x).SequenceEqual(myRowCommitted))
            {
                throw new ArgumentException("Commitment does not match");
            }

            return(new ValueMessage
            {
                Proposer = sender,
                EncryptedValues = Enumerable.Range(0, Players).Select(i => Crypto.Secp256K1Encrypt(
                                                                          _publicKeys[i].EncodeCompressed(),
                                                                          MclBls12381.EvaluatePolynomial(myRow, Fr.FromInt(i + 1)).ToBytes()
                                                                          )).ToArray()
            });
        }
Ejemplo n.º 2
0
        public void Test_Secp256K1EncryptDecryptRoundTrip()
        {
            var key      = Crypto.GeneratePrivateKey();
            var baseText =
                "0xf86d808504a817c800832dc6c0948e7b7262e0fa4616566591d51f998f16a79fb547880de0b6b3a76400008025a0115105d96a43f41a5ea562bb3e591cbfa431a8cdae9c3030457adca2cb854f78a012fb41922c53c73473563003667ed8e783359c91d95b42301e1955d530b1ca33"
                .HexToBytes();

            const int n       = 1000;
            var       startTs = TimeUtils.CurrentTimeMillis();

            for (var it = 0; it < n; ++it)
            {
                var plaintext = baseText.Concat(it.ToBytes()).ToArray();
                var cipher    = Crypto.Secp256K1Encrypt(key.ToPrivateKey().GetPublicKey().EncodeCompressed(), plaintext);
                var decrypted = Crypto.Secp256K1Decrypt(key, cipher);
                Assert.IsTrue(plaintext.SequenceEqual(decrypted));
            }

            var endTs = TimeUtils.CurrentTimeMillis();

            Console.WriteLine($"{n} encrypt/decrypt: {endTs - startTs}ms, avg = {(double) (endTs - startTs) / n}");
        }