arctic.rs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. use crate::lagrange::*;
  2. use crate::shine;
  3. use curve25519_dalek::ristretto::RistrettoPoint;
  4. use curve25519_dalek::scalar::Scalar;
  5. use sha2::Digest;
  6. use sha2::Sha256;
  7. pub use crate::lagrange::lagrange_polys;
  8. pub type PubKey = RistrettoPoint;
  9. pub struct SecKey {
  10. t: u32,
  11. k: u32,
  12. // This player's signature key share
  13. sk: Scalar,
  14. // This player's Shine key share
  15. shine_key: shine::PreprocKey,
  16. // The group public key
  17. pk: PubKey,
  18. }
  19. impl SecKey {
  20. pub fn delta(&self) -> usize {
  21. self.shine_key.delta()
  22. }
  23. }
  24. pub type R1Output = ([u8; 32], RistrettoPoint);
  25. pub type Signature = (RistrettoPoint, Scalar);
  26. // Generate Arctic keys using a trusted dealer. The output is the group
  27. // public key, a vector of each individual player's public key (unused
  28. // except in the robust Arctic case), and a vector of each individual
  29. // player's Arctic secret key.
  30. pub fn keygen(n: u32, t: u32) -> (PubKey, Vec<PubKey>, Vec<SecKey>) {
  31. assert!(t >= 1);
  32. assert!(n >= 2 * t - 1);
  33. let mut seckeys: Vec<SecKey> = Vec::new();
  34. // The Shine key shares
  35. let shinekeys = shine::Key::keygen(n, t);
  36. // The signature key shares
  37. let shamirpoly = ScalarPoly::rand((t as usize) - 1);
  38. let group_pubkey = shine::commit(&shamirpoly.coeffs[0]);
  39. let signkeys : Vec<Scalar> = (1..=n)
  40. .map(|k| shamirpoly.eval(&Scalar::from(k)))
  41. .collect();
  42. let player_pubkeys : Vec<PubKey> = signkeys
  43. .iter().map(shine::commit).collect();
  44. for k in 1..=n {
  45. seckeys.push(SecKey {
  46. t,
  47. k,
  48. sk: signkeys[(k-1) as usize],
  49. shine_key: shine::PreprocKey::preproc(&shinekeys[(k as usize) - 1]),
  50. pk: group_pubkey,
  51. });
  52. }
  53. (group_pubkey, player_pubkeys, seckeys)
  54. }
  55. // The hash function used to generate the value y that's the input to
  56. // shine::gen.
  57. fn hash2(pk: &PubKey, msg: &[u8]) -> [u8; 32] {
  58. let mut hash = Sha256::new();
  59. hash.update(pk.compress().as_bytes());
  60. hash.update(msg);
  61. hash.finalize().into()
  62. }
  63. // The hash function that's used to generate the challenge c for the
  64. // Schnorr signature. This function has to match the one for the
  65. // Schnorr verification implementation you're interoperating with, and
  66. // will depend on what group you're operating over.
  67. fn hash3(combcomm: &RistrettoPoint, pk: &PubKey, msg: &[u8]) -> Scalar {
  68. let mut hash = Sha256::new();
  69. hash.update(combcomm.compress().as_bytes());
  70. hash.update(pk.compress().as_bytes());
  71. hash.update(msg);
  72. let mut hashval = [0u8; 32];
  73. hashval[0..32].copy_from_slice(&hash.finalize());
  74. Scalar::from_bytes_mod_order(hashval)
  75. }
  76. // The first round of the signature protocol.
  77. pub fn sign1(sk: &SecKey, coalition: &[u32], msg: &[u8]) -> R1Output {
  78. assert!(coalition.len() >= 2 * (sk.t as usize) - 1);
  79. let y = hash2(&sk.pk, msg);
  80. (y, sk.shine_key.gen(&y).1)
  81. }
  82. // The second round of the signature protocol. Note: it is vital that
  83. // the ([u8;32], RistrettoPoint) received from all the parties' first
  84. // round were received over authenticated channels. If an adversary can
  85. // forge honest parties' round one messages, Arctic is _not_ secure.
  86. pub fn sign2_polys(
  87. pk: &PubKey,
  88. sk: &SecKey,
  89. coalition: &[u32],
  90. lag_polys: &[ScalarPoly],
  91. msg: &[u8],
  92. r1_outputs: &[R1Output],
  93. ) -> Option<Scalar> {
  94. // If the inputs are _malformed_, abort
  95. assert!(coalition.len() == lag_polys.len());
  96. assert!(coalition.len() == r1_outputs.len());
  97. assert!(coalition.len() >= 2 * (sk.t as usize) - 1);
  98. // Find my own entry in the coalition; abort if it's not there
  99. let kindex = coalition.iter().position(|&k| k == sk.k).unwrap();
  100. let y = hash2(pk, msg);
  101. // Make sure all the parties are submitting commitments for the same
  102. // y (the same pk and msg).
  103. if r1_outputs.iter().any(|(yk, _)| yk != &y) {
  104. return None;
  105. }
  106. let (my_eval, my_commit) = sk.shine_key.gen(&y);
  107. assert!(r1_outputs[kindex].1 == my_commit);
  108. // If the inputs are just corrupt values from malicious other
  109. // parties, return None but don't crash
  110. let commitments : Vec<RistrettoPoint> =
  111. r1_outputs.iter().map(|(_,commitment)| *commitment).collect();
  112. let combcomm = shine::combinecomm_polys(sk.t, lag_polys, &commitments)?;
  113. let c = hash3(&combcomm, pk, msg);
  114. Some(my_eval + c * sk.sk)
  115. }
  116. pub fn sign2(
  117. pk: &PubKey,
  118. sk: &SecKey,
  119. coalition: &[u32],
  120. msg: &[u8],
  121. r1_outputs: &[R1Output],
  122. ) -> Option<Scalar> {
  123. let polys = lagrange_polys(coalition);
  124. sign2_polys(pk, sk, coalition, &polys, msg, r1_outputs)
  125. }
  126. pub fn combine_polys(
  127. pk: &PubKey,
  128. t: u32,
  129. coalition: &[u32],
  130. lag_polys: &[ScalarPoly],
  131. msg: &[u8],
  132. r1_outputs: &[R1Output],
  133. sigshares: &[Scalar],
  134. ) -> Option<Signature> {
  135. assert!(coalition.len() == lag_polys.len());
  136. assert!(coalition.len() == r1_outputs.len());
  137. assert!(coalition.len() == sigshares.len());
  138. assert!(coalition.len() >= 2 * (t as usize) - 1);
  139. let z = interpolate_polys_0(lag_polys, sigshares);
  140. // Check the answer
  141. let commitments : Vec<RistrettoPoint> =
  142. r1_outputs.iter().map(|(_,commitment)| *commitment).collect();
  143. let combcomm = shine::agg_polys(t, lag_polys, &commitments);
  144. let c = hash3(&combcomm, pk, msg);
  145. if shine::commit(&z) == combcomm + c * pk {
  146. return Some((combcomm, z));
  147. }
  148. None
  149. }
  150. pub fn combine(
  151. pk: &PubKey,
  152. t: u32,
  153. coalition: &[u32],
  154. msg: &[u8],
  155. r1_outputs: &[R1Output],
  156. sigshares: &[Scalar],
  157. ) -> Option<Signature> {
  158. let polys = lagrange_polys(coalition);
  159. combine_polys(pk, t, coalition, &polys, msg, r1_outputs, sigshares)
  160. }
  161. pub fn verify(pk: &PubKey, msg: &[u8], sig: &Signature) -> bool {
  162. let c = hash3(&sig.0, pk, msg);
  163. shine::commit(&sig.1) == sig.0 + c * pk
  164. }
  165. #[test]
  166. pub fn test_arctic_good() {
  167. let n = 7u32;
  168. let t = 4u32;
  169. let (pubkey, _, seckeys) = keygen(n, t);
  170. let coalition = (1..=n).collect::<Vec<u32>>();
  171. let msg = b"A message to be signed";
  172. let r1_outputs: Vec<R1Output> = seckeys
  173. .iter()
  174. .map(|key| sign1(key, &coalition, msg))
  175. .collect();
  176. let sigshares: Vec<Scalar> = seckeys
  177. .iter()
  178. .map(|key| sign2(&pubkey, key, &coalition, msg, &r1_outputs).unwrap())
  179. .collect();
  180. let sig = combine(&pubkey, t, &coalition, msg, &r1_outputs, &sigshares).unwrap();
  181. assert!(verify(&pubkey, msg, &sig));
  182. }
  183. #[test]
  184. #[should_panic]
  185. pub fn test_arctic_bad1() {
  186. let n = 7u32;
  187. let t = 4u32;
  188. let (pubkey, _, seckeys) = keygen(n, t);
  189. let coalition = (1..=n).collect::<Vec<u32>>();
  190. let msg = b"A message to be signed";
  191. let mut r1_outputs: Vec<R1Output> = seckeys
  192. .iter()
  193. .map(|key| sign1(key, &coalition, msg))
  194. .collect();
  195. // Modify player 1's commitment
  196. let v = r1_outputs[1].1;
  197. r1_outputs[0].1 += v;
  198. // Player 1 should abort because its own commit is no longer in the
  199. // list
  200. sign2(&pubkey, &seckeys[0], &coalition, msg, &r1_outputs);
  201. }
  202. #[test]
  203. pub fn test_arctic_bad2() {
  204. let n = 7u32;
  205. let t = 4u32;
  206. let (pubkey, _, seckeys) = keygen(n, t);
  207. let coalition = (1..=n).collect::<Vec<u32>>();
  208. let msg = b"A message to be signed";
  209. let mut r1_outputs: Vec<R1Output> = seckeys
  210. .iter()
  211. .map(|key| sign1(key, &coalition, msg))
  212. .collect();
  213. // Modify player 1's commitment
  214. let v = r1_outputs[1].1;
  215. r1_outputs[0].1 += v;
  216. // Player 2 should return None because the commitments are
  217. // inconsistent
  218. assert_eq!(sign2(&pubkey, &seckeys[1], &coalition, msg, &r1_outputs), None);
  219. }
  220. #[test]
  221. pub fn test_arctic_bad3() {
  222. let n = 7u32;
  223. let t = 4u32;
  224. let (pubkey, _, seckeys) = keygen(n, t);
  225. let coalition = (1..=n).collect::<Vec<u32>>();
  226. let msg = b"A message to be signed";
  227. let mut r1_outputs: Vec<R1Output> = seckeys
  228. .iter()
  229. .map(|key| sign1(key, &coalition, msg))
  230. .collect();
  231. // Modify player 1's y value
  232. r1_outputs[0].0[0] += 1;
  233. // Player 2 should return None because the y values are
  234. // inconsistent
  235. assert_eq!(sign2(&pubkey, &seckeys[1], &coalition, msg, &r1_outputs), None);
  236. }
  237. #[test]
  238. pub fn test_arctic_bad4() {
  239. let n = 7u32;
  240. let t = 4u32;
  241. let (pubkey, _, seckeys) = keygen(n, t);
  242. let coalition = (1..=n).collect::<Vec<u32>>();
  243. let msg = b"A message to be signed";
  244. let r1_outputs: Vec<R1Output> = seckeys
  245. .iter()
  246. .map(|key| sign1(key, &coalition, msg))
  247. .collect();
  248. // Use a different message in round 2
  249. let msg2 = b"A message to be signef";
  250. // Player 2 should return None because the y values are
  251. // inconsistent
  252. assert_eq!(sign2(&pubkey, &seckeys[1], &coalition, msg2, &r1_outputs), None);
  253. }
  254. #[test]
  255. pub fn test_arctic_bad5() {
  256. let n = 7u32;
  257. let t = 4u32;
  258. let (pubkey, _, seckeys) = keygen(n, t);
  259. let coalition = (1..=n).collect::<Vec<u32>>();
  260. let msg = b"A message to be signed";
  261. let r1_outputs: Vec<R1Output> = seckeys
  262. .iter()
  263. .map(|key| sign1(key, &coalition, msg))
  264. .collect();
  265. let mut sigshares: Vec<Scalar> = seckeys
  266. .iter()
  267. .map(|key| sign2(&pubkey, key, &coalition, msg, &r1_outputs).unwrap())
  268. .collect();
  269. // Modify player 0's signature share
  270. sigshares[0] += Scalar::one();
  271. // Combine should return None because the shares don't combine to a
  272. // valid signature
  273. assert_eq!(
  274. combine(&pubkey, t, &coalition, msg, &r1_outputs, &sigshares),
  275. None
  276. );
  277. }
  278. #[test]
  279. pub fn test_arctic_bad6() {
  280. let n = 7u32;
  281. let t = 4u32;
  282. let (pubkey, _, seckeys) = keygen(n, t);
  283. let coalition = (1..=n).collect::<Vec<u32>>();
  284. let msg = b"A message to be signed";
  285. let r1_outputs: Vec<R1Output> = seckeys
  286. .iter()
  287. .map(|key| sign1(key, &coalition, msg))
  288. .collect();
  289. let sigshares: Vec<Scalar> = seckeys
  290. .iter()
  291. .map(|key| sign2(&pubkey, key, &coalition, msg, &r1_outputs).unwrap())
  292. .collect();
  293. // Modify the message
  294. let msg2 = b"A message to be signef";
  295. assert_eq!(
  296. combine(&pubkey, t, &coalition, msg2, &r1_outputs, &sigshares),
  297. None
  298. );
  299. }