arctic.rs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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 R1Output values received from all the parties' first round were
  84. // received over authenticated channels. If an adversary can forge
  85. // 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. // If the inputs are just corrupt values from malicious other
  101. // parties, return None but don't crash
  102. let y = hash2(pk, msg);
  103. // Make sure all the parties are submitting commitments for the same
  104. // y (the same pk and msg).
  105. if r1_outputs.iter().any(|(yj, _)| yj != &y) {
  106. return None;
  107. }
  108. let (my_eval, my_commit) = sk.shine_key.gen(&y);
  109. assert!(r1_outputs[kindex].1 == my_commit);
  110. let commitments : Vec<RistrettoPoint> =
  111. r1_outputs.iter().map(|(_,commitment)| *commitment).collect();
  112. if ! shine::verify_polys(sk.t, lag_polys, &commitments) {
  113. return None;
  114. }
  115. let combcomm = shine::agg_polys(sk.t, lag_polys, &commitments);
  116. let c = hash3(&combcomm, pk, msg);
  117. Some(my_eval + c * sk.sk)
  118. }
  119. pub fn sign2(
  120. pk: &PubKey,
  121. sk: &SecKey,
  122. coalition: &[u32],
  123. msg: &[u8],
  124. r1_outputs: &[R1Output],
  125. ) -> Option<Scalar> {
  126. let polys = lagrange_polys(coalition);
  127. sign2_polys(pk, sk, coalition, &polys, msg, r1_outputs)
  128. }
  129. pub fn combine_polys(
  130. pk: &PubKey,
  131. t: u32,
  132. coalition: &[u32],
  133. lag_polys: &[ScalarPoly],
  134. msg: &[u8],
  135. r1_outputs: &[R1Output],
  136. sigshares: &[Scalar],
  137. ) -> Option<Signature> {
  138. assert!(coalition.len() == lag_polys.len());
  139. assert!(coalition.len() == r1_outputs.len());
  140. assert!(coalition.len() == sigshares.len());
  141. assert!(coalition.len() >= 2 * (t as usize) - 1);
  142. let z = interpolate_polys_0(lag_polys, sigshares);
  143. // Check the answer
  144. let commitments : Vec<RistrettoPoint> =
  145. r1_outputs.iter().map(|(_,commitment)| *commitment).collect();
  146. let combcomm = shine::agg_polys(t, lag_polys, &commitments);
  147. let c = hash3(&combcomm, pk, msg);
  148. if shine::commit(&z) == combcomm + c * pk {
  149. return Some((combcomm, z));
  150. }
  151. None
  152. }
  153. pub fn combine(
  154. pk: &PubKey,
  155. t: u32,
  156. coalition: &[u32],
  157. msg: &[u8],
  158. r1_outputs: &[R1Output],
  159. sigshares: &[Scalar],
  160. ) -> Option<Signature> {
  161. let polys = lagrange_polys(coalition);
  162. combine_polys(pk, t, coalition, &polys, msg, r1_outputs, sigshares)
  163. }
  164. pub fn verify(pk: &PubKey, msg: &[u8], sig: &Signature) -> bool {
  165. let c = hash3(&sig.0, pk, msg);
  166. shine::commit(&sig.1) == sig.0 + c * pk
  167. }
  168. #[test]
  169. pub fn test_arctic_good() {
  170. let n = 7u32;
  171. let t = 4u32;
  172. let (pubkey, _, seckeys) = keygen(n, t);
  173. let coalition = (1..=n).collect::<Vec<u32>>();
  174. let msg = b"A message to be signed";
  175. let r1_outputs: Vec<R1Output> = seckeys
  176. .iter()
  177. .map(|key| sign1(key, &coalition, msg))
  178. .collect();
  179. let sigshares: Vec<Scalar> = seckeys
  180. .iter()
  181. .map(|key| sign2(&pubkey, key, &coalition, msg, &r1_outputs).unwrap())
  182. .collect();
  183. let sig = combine(&pubkey, t, &coalition, msg, &r1_outputs, &sigshares).unwrap();
  184. assert!(verify(&pubkey, msg, &sig));
  185. }
  186. #[test]
  187. #[should_panic]
  188. pub fn test_arctic_bad1() {
  189. let n = 7u32;
  190. let t = 4u32;
  191. let (pubkey, _, seckeys) = keygen(n, t);
  192. let coalition = (1..=n).collect::<Vec<u32>>();
  193. let msg = b"A message to be signed";
  194. let mut r1_outputs: Vec<R1Output> = seckeys
  195. .iter()
  196. .map(|key| sign1(key, &coalition, msg))
  197. .collect();
  198. // Modify player 1's commitment
  199. let v = r1_outputs[1].1;
  200. r1_outputs[0].1 += v;
  201. // Player 1 should abort because its own commit is no longer in the
  202. // list
  203. sign2(&pubkey, &seckeys[0], &coalition, msg, &r1_outputs);
  204. }
  205. #[test]
  206. pub fn test_arctic_bad2() {
  207. let n = 7u32;
  208. let t = 4u32;
  209. let (pubkey, _, seckeys) = keygen(n, t);
  210. let coalition = (1..=n).collect::<Vec<u32>>();
  211. let msg = b"A message to be signed";
  212. let mut r1_outputs: Vec<R1Output> = seckeys
  213. .iter()
  214. .map(|key| sign1(key, &coalition, msg))
  215. .collect();
  216. // Modify player 1's commitment
  217. let v = r1_outputs[1].1;
  218. r1_outputs[0].1 += v;
  219. // Player 2 should return None because the commitments are
  220. // inconsistent
  221. assert_eq!(sign2(&pubkey, &seckeys[1], &coalition, msg, &r1_outputs), None);
  222. }
  223. #[test]
  224. pub fn test_arctic_bad3() {
  225. let n = 7u32;
  226. let t = 4u32;
  227. let (pubkey, _, seckeys) = keygen(n, t);
  228. let coalition = (1..=n).collect::<Vec<u32>>();
  229. let msg = b"A message to be signed";
  230. let mut r1_outputs: Vec<R1Output> = seckeys
  231. .iter()
  232. .map(|key| sign1(key, &coalition, msg))
  233. .collect();
  234. // Modify player 1's y value
  235. r1_outputs[0].0[0] += 1;
  236. // Player 2 should return None because the y values are
  237. // inconsistent
  238. assert_eq!(sign2(&pubkey, &seckeys[1], &coalition, msg, &r1_outputs), None);
  239. }
  240. #[test]
  241. pub fn test_arctic_bad4() {
  242. let n = 7u32;
  243. let t = 4u32;
  244. let (pubkey, _, seckeys) = keygen(n, t);
  245. let coalition = (1..=n).collect::<Vec<u32>>();
  246. let msg = b"A message to be signed";
  247. let r1_outputs: Vec<R1Output> = seckeys
  248. .iter()
  249. .map(|key| sign1(key, &coalition, msg))
  250. .collect();
  251. // Use a different message in round 2
  252. let msg2 = b"A message to be signef";
  253. // Player 2 should return None because the y values are
  254. // inconsistent
  255. assert_eq!(sign2(&pubkey, &seckeys[1], &coalition, msg2, &r1_outputs), None);
  256. }
  257. #[test]
  258. pub fn test_arctic_bad5() {
  259. let n = 7u32;
  260. let t = 4u32;
  261. let (pubkey, _, seckeys) = keygen(n, t);
  262. let coalition = (1..=n).collect::<Vec<u32>>();
  263. let msg = b"A message to be signed";
  264. let r1_outputs: Vec<R1Output> = seckeys
  265. .iter()
  266. .map(|key| sign1(key, &coalition, msg))
  267. .collect();
  268. let mut sigshares: Vec<Scalar> = seckeys
  269. .iter()
  270. .map(|key| sign2(&pubkey, key, &coalition, msg, &r1_outputs).unwrap())
  271. .collect();
  272. // Modify player 0's signature share
  273. sigshares[0] += Scalar::one();
  274. // Combine should return None because the shares don't combine to a
  275. // valid signature
  276. assert_eq!(
  277. combine(&pubkey, t, &coalition, msg, &r1_outputs, &sigshares),
  278. None
  279. );
  280. }
  281. #[test]
  282. pub fn test_arctic_bad6() {
  283. let n = 7u32;
  284. let t = 4u32;
  285. let (pubkey, _, seckeys) = keygen(n, t);
  286. let coalition = (1..=n).collect::<Vec<u32>>();
  287. let msg = b"A message to be signed";
  288. let r1_outputs: Vec<R1Output> = seckeys
  289. .iter()
  290. .map(|key| sign1(key, &coalition, msg))
  291. .collect();
  292. let sigshares: Vec<Scalar> = seckeys
  293. .iter()
  294. .map(|key| sign2(&pubkey, key, &coalition, msg, &r1_outputs).unwrap())
  295. .collect();
  296. // Modify the message
  297. let msg2 = b"A message to be signef";
  298. assert_eq!(
  299. combine(&pubkey, t, &coalition, msg2, &r1_outputs, &sigshares),
  300. None
  301. );
  302. }