public void ThresholdKeyGen() { const int n = 7, f = 2; var keygen = new TrustedKeyGen(n, f); var shares = keygen.GetPrivateShares().ToArray(); var data = 0xdeadbeef.ToBytes().ToArray(); var pubKeys = new PublicKeySet(shares.Select(share => share.GetPublicKeyShare()), f); var signers = new IThresholdSigner[n]; for (var i = 0; i < n; ++i) { signers[i] = new ThresholdSigner(data, shares[i], pubKeys); } var signatureShares = signers.Select(signer => signer.Sign()).ToArray(); var sigs = new Signature[n]; var success = new bool[n]; for (var i = 0; i < n; ++i) success[i] = true; for (var i = 0; i < n; ++i) { for (var j = 0; j < n; ++j) { success[i] &= signers[i].AddShare(j, signatureShares[j], out var sig); if (sigs[i] == null && sig != null) sigs[i] = sig; } } for (int i = 0; i < n; ++i) { Assert.IsTrue(success[i], $"Player {i} did not terminate successfully"); Assert.IsTrue(pubKeys.SharedPublicKey.ValidateSignature(sigs[i], data)); } }
private void SetUpAllHonest(int n, int f) { _deliveryService = new DeliveryService(); _broadcasts = new IConsensusProtocol[n]; _broadcasters = new IConsensusBroadcaster[n]; _resultInterceptors = new ProtocolInvoker <BinaryAgreementId, bool> [n]; _privateKeys = new IPrivateConsensusKeySet[n]; var keygen = new TrustedKeyGen(n, f); var shares = keygen.GetPrivateShares().ToArray(); var pubKeys = new PublicKeySet(shares.Select(share => share.GetPublicKeyShare()), f); _publicKeys = new PublicConsensusKeySet(n, f, null !, pubKeys, Enumerable.Empty <ECDSAPublicKey>()); for (var i = 0; i < n; ++i) { _resultInterceptors[i] = new ProtocolInvoker <BinaryAgreementId, bool>(); _privateKeys[i] = new PrivateConsensusKeySet(null !, null !, shares[i]); _broadcasters[i] = new BroadcastSimulator(i, _publicKeys, _privateKeys[i], _deliveryService, true); } for (uint i = 0; i < n; ++i) { _broadcasts[i] = new BinaryAgreement(new BinaryAgreementId(0, 0), _publicKeys, _broadcasters[i]); _broadcasters[i].RegisterProtocols(new[] { _broadcasts[i], _resultInterceptors[i] }); } }
private void SetUpAllHonest(int n, int f) { _deliveryService = new DeliveryService(); _acs = new IConsensusProtocol[n]; _broadcasters = new BroadcastSimulator[n]; _resultInterceptors = new ProtocolInvoker <CommonSubsetId, ISet <EncryptedShare> > [n]; _privateKeys = new IPrivateConsensusKeySet[n]; var keygen = new TrustedKeyGen(n, f); var shares = keygen.GetPrivateShares().ToArray(); var pubKeys = new PublicKeySet(shares.Select(share => share.GetPublicKeyShare()), f); _publicKeys = new PublicConsensusKeySet( n, f, null !, pubKeys, Enumerable.Range(0, n) .Select(i => new ECDSAPublicKey { Buffer = ByteString.CopyFrom(i.ToBytes().ToArray()) }) ); for (var i = 0; i < n; ++i) { _resultInterceptors[i] = new ProtocolInvoker <CommonSubsetId, ISet <EncryptedShare> >(); _privateKeys[i] = new PrivateConsensusKeySet(null !, null !, shares[i]); _broadcasters[i] = new BroadcastSimulator(i, _publicKeys, _privateKeys[i], _deliveryService, false); } for (uint i = 0; i < n; ++i) { _acs[i] = new CommonSubset(new CommonSubsetId(0), _publicKeys, _broadcasters[i]); _broadcasters[i].RegisterProtocols(new[] { _acs[i], _resultInterceptors[i] }); } }
public void SetUp(int n, int f) { _deliveryService = new DeliveryService(); _broadcasts = new IConsensusProtocol[n]; _broadcasters = new IConsensusBroadcaster[n]; _resultInterceptors = new ProtocolInvoker <HoneyBadgerId, ISet <IRawShare> > [n]; var keygen = new TrustedKeyGen(n, f); var shares = keygen.GetPrivateShares().ToArray(); var pubKeys = new PublicKeySet(shares.Select(share => share.GetPublicKeyShare()), f); var tpkeKeygen = new Crypto.TPKE.TrustedKeyGen(n, f); var ecdsaKeys = Enumerable.Range(0, n) .Select(i => Crypto.GenerateRandomBytes(32)) .Select(x => x.ToPrivateKey()) .Select(k => new EcdsaKeyPair(k)) .ToArray(); _publicKeys = new PublicConsensusKeySet(n, f, tpkeKeygen.GetPubKey(), pubKeys, ecdsaKeys.Select(k => k.PublicKey)); _privateKeys = new IPrivateConsensusKeySet[n]; for (var i = 0; i < n; ++i) { _resultInterceptors[i] = new ProtocolInvoker <HoneyBadgerId, ISet <IRawShare> >(); _privateKeys[i] = new PrivateConsensusKeySet(ecdsaKeys[i], tpkeKeygen.GetPrivKey(i), shares[i]); _broadcasters[i] = new BroadcastSimulator(i, _publicKeys, _privateKeys[i], _deliveryService, true); } }
public bool HandleConfirm(PublicKey tpkeKey, PublicKeySet tsKeys) { var keyringHash = tpkeKey.ToBytes().Concat(tsKeys.ToBytes()).Keccak(); _confirmations.PutIfAbsent(keyringHash, 0); _confirmations[keyringHash] += 1; return(_confirmations[keyringHash] == Players - Faulty); }
public PublicConsensusKeySet(int n, int f, PublicKey tpkePublicKey, PublicKeySet thresholdSignaturePublicKeySet, IEnumerable <ECDSAPublicKey> ecdsaPublicKeys ) { N = n; F = f; TpkePublicKey = tpkePublicKey; ThresholdSignaturePublicKeySet = thresholdSignaturePublicKeySet; _ecdsaPublicKeys = ecdsaPublicKeys.ToList(); }
public ExecutionStatus KeyGenConfirm(UInt256 cycle, byte[] tpkePublicKey, byte[][] thresholdSignaturePublicKeys, SystemContractExecutionFrame frame) { Logger.LogDebug( $"KeyGenConfirm({tpkePublicKey.ToHex()}, [{string.Join(", ", thresholdSignaturePublicKeys.Select(s => s.ToHex()))}])"); if (cycle.ToBigInteger() != GetConsensusGeneration(frame)) { Logger.LogWarning($"Invalid cycle: {cycle}, now is {GetConsensusGeneration(frame)}"); return(ExecutionStatus.Ok); } frame.ReturnValue = new byte[] { }; frame.UseGas(GasMetering.KeygenConfirmCost); var players = thresholdSignaturePublicKeys.Length; var faulty = (players - 1) / 3; UInt256 keyringHash; PublicKeySet tsKeys; try { tsKeys = new PublicKeySet( thresholdSignaturePublicKeys.Select(x => Lachain.Crypto.ThresholdSignature.PublicKey.FromBytes(x)), faulty ); var tpkeKey = PublicKey.FromBytes(tpkePublicKey); keyringHash = tpkeKey.ToBytes().Concat(tsKeys.ToBytes()).Keccak(); } catch { Logger.LogError("GovernanceContract is halted in KeyGenConfirm"); return(ExecutionStatus.ExecutionHalted); } var gen = GetConsensusGeneration(frame); var votes = GetConfirmations(keyringHash.ToBytes(), gen); SetConfirmations(keyringHash.ToBytes(), gen, votes + 1); Logger.LogDebug($"KeygenConfirm: {votes + 1} collected for this keyset"); if (votes + 1 != players - faulty) { return(ExecutionStatus.Ok); } Logger.LogDebug($"KeygenConfirm: succeeded since collected {votes + 1} votes"); _lastSuccessfulKeygenBlock.Set(new BigInteger(frame.InvocationContext.Receipt.Block).ToUInt256().ToBytes()); SetPlayersCount(players); SetTSKeys(tsKeys); SetTpkeKey(tpkePublicKey); Emit(GovernanceInterface.EventKeygenConfirm, tpkePublicKey, thresholdSignaturePublicKeys); return(ExecutionStatus.Ok); }
public bool HasKeyForKeySet(PublicKeySet thresholdSignaturePublicKeySet, ulong beforeBlock) { try { return(thresholdSignaturePublicKeySet.Keys.Contains(_tsKeys.Predecessor(beforeBlock).Value .GetPublicKeyShare())); } catch (NoSuchItemException) { return(false); } }
public void UpdateValidators( IEnumerable <ECDSAPublicKey> ecdsaKeys, PublicKeySet tsKeys, PublicKey tpkePublicKey ) { var state = new ConsensusState( tpkePublicKey.ToBytes(), ecdsaKeys .Zip(tsKeys.Keys, (ecdsaKey, tsKey) => new ValidatorCredentials(ecdsaKey, tsKey.ToBytes())) .ToArray() ); SetConsensusState(state); }
public void SetUp(int n, int f) { var keygen = new TrustedKeyGen(n, f); var shares = keygen.GetPrivateShares().ToArray(); var pubKeys = new PublicKeySet(shares.Select(share => share.GetPublicKeyShare()), f); _deliveryService = new DeliveryService(); _coins = new IConsensusProtocol[n]; _broadcasters = new IConsensusBroadcaster[n]; _resultInterceptors = new ProtocolInvoker <CoinId, CoinResult> [n]; _wallets = new IPrivateConsensusKeySet[n]; _publicKeys = new PublicConsensusKeySet(n, f, null !, pubKeys, Enumerable.Empty <ECDSAPublicKey>()); for (var i = 0; i < n; ++i) { _resultInterceptors[i] = new ProtocolInvoker <CoinId, CoinResult>(); _wallets[i] = new PrivateConsensusKeySet(null !, null !, shares[i]); _broadcasters[i] = new BroadcastSimulator(i, _publicKeys, _wallets[i], _deliveryService, false); _coins[i] = new CommonCoin( new CoinId(0, 0, 0), _publicKeys, shares[i], _broadcasters[i] ); _broadcasters[i].RegisterProtocols(new[] { _coins[i], _resultInterceptors[i] }); } }
private PublicKeySet GetTSKeys() { var tsKeys = _tsKeys.Get(); return(PublicKeySet.FromBytes(tsKeys)); }
private void SetTSKeys(PublicKeySet tsKeys) { _tsKeys.Set(tsKeys.ToBytes().ToArray()); }