arctic.rs 7.4 KB

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