该库在 Swift 中实现了一个 PVSS 方案。该算法基于 Berry Schoenmakers 的 "一种简单的公开可验证的秘密共享方案及其在电子投票中的应用"。
秘密共享意味着经销商可以将一个秘密分成若干份,分发给一组参与者,只有通过协作结合他们的秘密部分才能重构秘密。该库还实现了阈值密码学,因此经销商可以决定是所有接收参与者都需要协作,还是只需较小的参与者子集就足以重构秘密。
除了普通的秘密共享方案外,PVSS 还以以下方式添加了可验证性:秘密被拆分成的所有部分都分别使用接收者的公钥进行加密。经销商发布所有加密的份额以及一个非交互式零知识证明,该证明允许所有人(不仅仅是接收参与者)验证解密的份额确实可以用于重构秘密。然后,参与者解密他们所有的份额并交换它们,同时附带另一个非交互式零知识证明,该证明允许接收参与者验证该份额实际上是解密的结果。
因此,PVSS 可以用于在一组参与者之间共享一个秘密,这样要么可以通过所有诚实参与者重构秘密,要么收到伪造份额的参与者可以识别出恶意方。
.Package(url: "https://github.com/FabioTacke/PubliclyVerifiableSecretSharing.git", majorVersion: 2)
pod 'PVSS', '~> 1.0'
自 2.0.0 版本以来,PVSS 使用 GMP 来加速计算。 如果您没有安装 GMP,则包含已编译的 GMP 库版本 6.1.2。 但是,您需要向编译器和链接器提供有关在哪里找到 GMP 头文件和库的信息。 示例
swift [build | test] -Xcc -Igmp/include -Xlinker -Lgmp/lib
如果您已经安装了 GMP,则可以替换这些路径。
本节将指导您完成 PVSS 方案中采取的基本步骤。
首先,如有必要,我们将秘密消息转换为数值。 在创建经销商时,也会创建一个 PVSS 实例,该实例包含每个参与者都需要知道的所有全局参数。
import PVSS
import BigInt
import Bignum
let secretMessage = "Correct horse battery staple."
let secret = Bignum(data: secretMessage.data(using: .utf8)!)
// Create PVSS Instance.
let dealer = Participant()
// Participants p1, p2 and p3.
let p1 = Participant(pvssInstance: dealer.pvssInstance)
let p2 = Participant(pvssInstance: dealer.pvssInstance)
let p3 = Participant(pvssInstance: dealer.pvssInstance)
经销商将秘密分成若干份,加密它们并创建一个证明,以便所有人都可以验证这些份额(一旦解密)可以用于重构秘密。 阈值确定重构秘密所需的份额数量。 加密的份额和证明然后捆绑在一起。
// Dealer that shares the secret among p1, p2 and p3.
let distributionBundle = dealer.distribute(secret: secret, publicKeys: [p1.publicKey, p2.publicKey, p3.publicKey], threshold: 3)
// p1 verifies distribution bundle containing encryted shares and proof. [p2 and p3 do this as well.]
assert(p1.pvssInstance.verify(distributionBundle: distributionBundle))
参与者从分发包中提取他们的份额并解密它们。 他们将它们与一个证明捆绑在一起,该证明允许接收者验证该份额确实是解密的结果。
// p1 extracts the share. [p2 and p3 do this as well.]
let s1 = p1.extractShare(distributionBundle: distributionBundle, privateKey: p1.privateKey)!
// p1, p2 and p3 exchange their shares.
// ...
// p1 verifies the share received from p2. [Actually everybody verifies every received share.]
assert(p1.pvssInstance.verify(shareBundle: s2, encryptedShare: distributionBundle.shares[p2.publicKey]!))
一旦参与者收集到至少 threshold
个份额,就可以重构秘密。
// p1 [as well as p2 and p3] can now reconstruct the secret.
let shareBundles = [s1, s2, s3]
let r1 = p1.pvssInstance.reconstruct(shareBundles: shareBundles, distributionBundle: distributionBundle)!
String(data: BigUInt(r1.description)!.serialize(), encoding: .utf8)!
// Correct horse battery staple.
PVSS 使用以下第三方库。
BigInt - Copyright (c) 2016-2017 Károly Lőrentey (MIT)
CryptoSwift - Copyright (c) 2014-2017 Marcin Krzyżanowski (zlib)
GMP - Copyright (c) 2007-2017 Free Software Foundation, Inc. (GNU LGPL)