arctic.rs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. type PubKey = RistrettoPoint;
  9. pub struct SecKey {
  10. t: u32,
  11. k: u32,
  12. sk: Scalar,
  13. rk: shine::PreprocKey,
  14. }
  15. impl SecKey {
  16. pub fn delta(&self) -> usize {
  17. self.rk.delta()
  18. }
  19. }
  20. type Signature = (RistrettoPoint, Scalar);
  21. pub fn keygen(n: u32, t: u32) -> (PubKey, Vec<SecKey>) {
  22. assert!(t >= 1);
  23. assert!(n >= 2 * t - 1);
  24. let mut seckeys: Vec<SecKey> = Vec::new();
  25. // The Shine key shares
  26. let shinekeys = shine::Key::keygen(n, t);
  27. // The signature key shares
  28. let shamirpoly = ScalarPoly::rand((t as usize) - 1);
  29. let pubkey = shine::commit(&shamirpoly.coeffs[0]);
  30. for k in 1..=n {
  31. seckeys.push(SecKey {
  32. t,
  33. k,
  34. sk: shamirpoly.eval(&Scalar::from(k)),
  35. rk: shine::PreprocKey::preproc(&shinekeys[(k as usize) - 1]),
  36. });
  37. }
  38. (pubkey, seckeys)
  39. }
  40. fn hash2(pk: &PubKey, msg: &[u8]) -> [u8; 32] {
  41. let mut hash = Sha256::new();
  42. hash.update(pk.compress().as_bytes());
  43. hash.update(msg);
  44. hash.finalize().into()
  45. }
  46. fn hash3(combcomm: &RistrettoPoint, pk: &PubKey, msg: &[u8]) -> Scalar {
  47. let mut hash = Sha256::new();
  48. hash.update(combcomm.compress().as_bytes());
  49. hash.update(pk.compress().as_bytes());
  50. hash.update(msg);
  51. let mut hashval = [0u8; 32];
  52. hashval[0..32].copy_from_slice(&hash.finalize());
  53. Scalar::from_bytes_mod_order(hashval)
  54. }
  55. pub fn sign1(pk: &PubKey, sk: &SecKey, coalition: &[u32], msg: &[u8]) -> RistrettoPoint {
  56. assert!(coalition.len() >= 2 * (sk.t as usize) - 1);
  57. let w = hash2(pk, msg);
  58. sk.rk.gen(&w).1
  59. }
  60. pub fn sign2_polys(
  61. pk: &PubKey,
  62. sk: &SecKey,
  63. coalition: &[u32],
  64. lag_polys: &[ScalarPoly],
  65. msg: &[u8],
  66. commitments: &[RistrettoPoint],
  67. ) -> Option<Scalar> {
  68. // If the inputs are _malformed_, abort
  69. assert!(coalition.len() == lag_polys.len());
  70. assert!(coalition.len() == commitments.len());
  71. assert!(coalition.len() >= 2 * (sk.t as usize) - 1);
  72. // Find my own entry in the coalition; abort if it's not there
  73. let kindex = coalition.iter().position(|&k| k == sk.k).unwrap();
  74. let w = hash2(pk, msg);
  75. let (my_eval, my_commit) = sk.rk.gen(&w);
  76. assert!(commitments[kindex] == my_commit);
  77. // If the inputs are just corrupt values from malicious other
  78. // parties, return None but don't crash
  79. let combcomm = shine::combinecomm_polys(sk.t, lag_polys, commitments)?;
  80. let c = hash3(&combcomm, pk, msg);
  81. Some(my_eval + c * sk.sk)
  82. }
  83. pub fn sign2(
  84. pk: &PubKey,
  85. sk: &SecKey,
  86. coalition: &[u32],
  87. msg: &[u8],
  88. commitments: &[RistrettoPoint],
  89. ) -> Option<Scalar> {
  90. let polys = lagrange_polys(coalition);
  91. sign2_polys(pk, sk, coalition, &polys, msg, commitments)
  92. }
  93. pub fn combine_polys(
  94. pk: &PubKey,
  95. t: u32,
  96. coalition: &[u32],
  97. lag_polys: &[ScalarPoly],
  98. msg: &[u8],
  99. commitments: &[RistrettoPoint],
  100. sigshares: &[Scalar],
  101. ) -> Option<Signature> {
  102. assert!(coalition.len() == lag_polys.len());
  103. assert!(coalition.len() == commitments.len());
  104. assert!(coalition.len() == sigshares.len());
  105. assert!(coalition.len() >= 2 * (t as usize) - 1);
  106. let z = interpolate_polys_0(lag_polys, sigshares);
  107. // Check the answer
  108. let combcomm = shine::agg_polys(t, lag_polys, commitments);
  109. let c = hash3(&combcomm, pk, msg);
  110. if shine::commit(&z) == combcomm + c * pk {
  111. return Some((combcomm, z));
  112. }
  113. None
  114. }
  115. pub fn combine(
  116. pk: &PubKey,
  117. t: u32,
  118. coalition: &[u32],
  119. msg: &[u8],
  120. commitments: &[RistrettoPoint],
  121. sigshares: &[Scalar],
  122. ) -> Option<Signature> {
  123. let polys = lagrange_polys(coalition);
  124. combine_polys(pk, t, coalition, &polys, msg, commitments, sigshares)
  125. }
  126. pub fn verify(pk: &PubKey, msg: &[u8], sig: &Signature) -> bool {
  127. let c = hash3(&sig.0, pk, msg);
  128. shine::commit(&sig.1) == sig.0 + c * pk
  129. }
  130. #[test]
  131. pub fn test_arctic_good() {
  132. let n = 7u32;
  133. let t = 4u32;
  134. let (pubkey, seckeys) = keygen(n, t);
  135. let coalition = (1..=n).collect::<Vec<u32>>();
  136. let msg = b"A message to be signed";
  137. let commits: Vec<RistrettoPoint> = seckeys
  138. .iter()
  139. .map(|key| sign1(&pubkey, key, &coalition, msg))
  140. .collect();
  141. let sigshares: Vec<Scalar> = seckeys
  142. .iter()
  143. .map(|key| sign2(&pubkey, key, &coalition, msg, &commits).unwrap())
  144. .collect();
  145. let sig = combine(&pubkey, t, &coalition, msg, &commits, &sigshares).unwrap();
  146. assert!(verify(&pubkey, msg, &sig));
  147. }
  148. #[test]
  149. #[should_panic]
  150. pub fn test_arctic_bad1() {
  151. let n = 7u32;
  152. let t = 4u32;
  153. let (pubkey, seckeys) = keygen(n, t);
  154. let coalition = (1..=n).collect::<Vec<u32>>();
  155. let msg = b"A message to be signed";
  156. let mut commits: Vec<RistrettoPoint> = seckeys
  157. .iter()
  158. .map(|key| sign1(&pubkey, key, &coalition, msg))
  159. .collect();
  160. // Modify player 1's commitment
  161. let v = commits[1];
  162. commits[0] += v;
  163. // Player 1 should abort because its own commit is no longer in the
  164. // list
  165. sign2(&pubkey, &seckeys[0], &coalition, msg, &commits);
  166. }
  167. #[test]
  168. pub fn test_arctic_bad2() {
  169. let n = 7u32;
  170. let t = 4u32;
  171. let (pubkey, seckeys) = keygen(n, t);
  172. let coalition = (1..=n).collect::<Vec<u32>>();
  173. let msg = b"A message to be signed";
  174. let mut commits: Vec<RistrettoPoint> = seckeys
  175. .iter()
  176. .map(|key| sign1(&pubkey, key, &coalition, msg))
  177. .collect();
  178. // Modify player 1's commitment
  179. let v = commits[1];
  180. commits[0] += v;
  181. // Player 2 should return None because the commitments are
  182. // inconsistent
  183. assert_eq!(sign2(&pubkey, &seckeys[1], &coalition, msg, &commits), None);
  184. }
  185. #[test]
  186. pub fn test_arctic_bad3() {
  187. let n = 7u32;
  188. let t = 4u32;
  189. let (pubkey, seckeys) = keygen(n, t);
  190. let coalition = (1..=n).collect::<Vec<u32>>();
  191. let msg = b"A message to be signed";
  192. let commits: Vec<RistrettoPoint> = seckeys
  193. .iter()
  194. .map(|key| sign1(&pubkey, key, &coalition, msg))
  195. .collect();
  196. let mut sigshares: Vec<Scalar> = seckeys
  197. .iter()
  198. .map(|key| sign2(&pubkey, key, &coalition, msg, &commits).unwrap())
  199. .collect();
  200. // Modify player 0's signature share
  201. sigshares[0] += Scalar::one();
  202. // Combine should return None because the shares don't combine to a
  203. // valid signature
  204. assert_eq!(
  205. combine(&pubkey, t, &coalition, msg, &commits, &sigshares),
  206. None
  207. );
  208. }
  209. #[test]
  210. pub fn test_arctic_bad4() {
  211. let n = 7u32;
  212. let t = 4u32;
  213. let (pubkey, seckeys) = keygen(n, t);
  214. let coalition = (1..=n).collect::<Vec<u32>>();
  215. let msg = b"A message to be signed";
  216. let commits: Vec<RistrettoPoint> = seckeys
  217. .iter()
  218. .map(|key| sign1(&pubkey, key, &coalition, msg))
  219. .collect();
  220. let sigshares: Vec<Scalar> = seckeys
  221. .iter()
  222. .map(|key| sign2(&pubkey, key, &coalition, msg, &commits).unwrap())
  223. .collect();
  224. // Modify the message
  225. let msg2 = b"A message to be signef";
  226. assert_eq!(
  227. combine(&pubkey, t, &coalition, msg2, &commits, &sigshares),
  228. None
  229. );
  230. }