You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
4.3 KiB

  1. pragma circom 2.1.6;
  2. include "node_modules/circomlib/circuits/poseidon.circom";
  3. include "node_modules/circomlib/circuits/mux1.circom";
  4. include "node_modules/circomlib/circuits/comparators.circom";
  5. include "node_modules/circomlib/circuits/gates.circom";
  6. include "./templates/chaff.circom";
  7. template grapevine(num_felts) {
  8. // in_out schema
  9. // 0: degrees of separation
  10. // 1: secret hash from previous step
  11. // 2: hash of username + secret hash from previous step
  12. // 3: chaff
  13. signal input ivc_input[4];
  14. signal output ivc_output[4];
  15. // external inputs at each folding step
  16. signal input external_inputs[num_felts+2+2];
  17. signal phrase[num_felts]; // secret phrase, if first iteration
  18. for (var i=0; i<num_felts; i++) {
  19. phrase[i] <== external_inputs[i];
  20. }
  21. signal usernames[2]; // prev username, current username
  22. usernames[0]<==external_inputs[num_felts];
  23. usernames[1]<==external_inputs[num_felts+1];
  24. signal auth_secrets[2]; // prev degree's user secret, current degree's user secret
  25. auth_secrets[0]<==external_inputs[num_felts+2];
  26. auth_secrets[1]<==external_inputs[num_felts+3];
  27. // name inputs from step_in
  28. signal degrees_of_separation <== ivc_input[0];
  29. signal given_phrase_hash <== ivc_input[1];
  30. signal given_degree_secret_hash <== ivc_input[2];
  31. signal is_chaff_step <== ivc_input[3];
  32. // determine whether degrees of separation from secret is zero
  33. component is_degree_zero = IsZero();
  34. is_degree_zero.in <== degrees_of_separation;
  35. // compute poseidon hash of secret
  36. // same as the word essentially
  37. component phrase_hasher = Poseidon(num_felts);
  38. phrase_hasher.inputs <== phrase;
  39. // mux between computed hash and previous iteration's hash to get phrase hash to use
  40. // if degrees of separation = 0 use computed hash, else use hash from previous step
  41. component phrase_mux = Mux1();
  42. phrase_mux.c[0] <== given_phrase_hash;
  43. phrase_mux.c[1] <== phrase_hasher.out;
  44. phrase_mux.s <== is_degree_zero.out;
  45. // compute hash of given degree secret
  46. // H(H(preimage), username, auth_secret[0])
  47. // where preimage is muxed depending on whether degree N is 1 or > 1
  48. component degree_secret_hasher = Poseidon(3);
  49. degree_secret_hasher.inputs[0] <== phrase_mux.out;
  50. degree_secret_hasher.inputs[1] <== usernames[0];
  51. degree_secret_hasher.inputs[2] <== auth_secrets[0];
  52. // compare computed degree secret hash to prev degree secret hash
  53. component degree_secret_hash_match = IsEqual();
  54. degree_secret_hash_match.in[0] <== degree_secret_hasher.out;
  55. degree_secret_hash_match.in[1] <== given_degree_secret_hash;
  56. // create boolean that is true if either is true:
  57. // - given degree secret hash matches computed hash
  58. // - is a chaff step
  59. component degree_secret_match_or_chaff = OR();
  60. degree_secret_match_or_chaff.a <== degree_secret_hash_match.out;
  61. degree_secret_match_or_chaff.b <== is_chaff_step;
  62. // create boolean that is muxes according to:
  63. // - if degrees of separation = 0, always true (no check needed)
  64. // - if degree of separation > 0, return output of degree_secret_match_or_chaff
  65. component degree_secret_satisfied_mux = Mux1();
  66. degree_secret_satisfied_mux.c[0] <== degree_secret_match_or_chaff.out;
  67. degree_secret_satisfied_mux.c[1] <== 1;
  68. degree_secret_satisfied_mux.s <== is_degree_zero.out;
  69. // constrain degree_secret_satisfied_mux to be true
  70. degree_secret_satisfied_mux.out === 1;
  71. // compute the next username hash
  72. component next_degree_secret_hash = Poseidon(3);
  73. next_degree_secret_hash.inputs[0] <== phrase_mux.out;
  74. next_degree_secret_hash.inputs[1] <== usernames[1];
  75. next_degree_secret_hash.inputs[2] <== auth_secrets[1];
  76. // mux step_out signal according to whether or not this is a chaff step
  77. component chaff_mux = ChaffMux();
  78. chaff_mux.degrees_of_separation <== degrees_of_separation;
  79. chaff_mux.given_phrase_hash <== given_phrase_hash;
  80. chaff_mux.given_degree_secret_hash <== given_degree_secret_hash;
  81. chaff_mux.is_chaff_step <== is_chaff_step;
  82. chaff_mux.computed_phrase_hash <== phrase_mux.out;
  83. chaff_mux.computed_degree_secret_hash <== next_degree_secret_hash.out;
  84. // wire output signals
  85. ivc_output <== chaff_mux.out;
  86. }
  87. component main { public [ivc_input] } = grapevine(6);