Skip to content

Commit

Permalink
Site updated: 2023-12-25 18:47:30
Browse files Browse the repository at this point in the history
  • Loading branch information
liqi16 committed Dec 25, 2023
1 parent e462194 commit 559fc14
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion 2023/12/25/ZKP-Schnorr-Protocol-Implementation/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ <h1 class="post-title">ZKP Schnorr Protocol Implementation</h1>
</header>

<div class="post-content">
<h1 id="Schnorr-Protocol"><a href="#Schnorr-Protocol" class="headerlink" title="Schnorr Protocol"></a>Schnorr Protocol</h1><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> random</span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">generate_random_value</span>(<span class="params">n</span>):</span><br><span class="line"> <span class="string">&quot;&quot;&quot;Generates a random value in the range [1, n-1]&quot;&quot;&quot;</span></span><br><span class="line"> <span class="keyword">return</span> random.randrange(<span class="number">1</span>, n)</span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">mod_pow</span>(<span class="params">base, exponent, modulus</span>):</span><br><span class="line"> <span class="string">&quot;&quot;&quot;Calculates (base^exponent) % modulus efficiently&quot;&quot;&quot;</span></span><br><span class="line"> result = <span class="number">1</span></span><br><span class="line"> <span class="keyword">while</span> exponent &gt; <span class="number">0</span>:</span><br><span class="line"> <span class="keyword">if</span> exponent % <span class="number">2</span> == <span class="number">1</span>:</span><br><span class="line"> result = (result * base) % modulus</span><br><span class="line"> exponent = exponent &gt;&gt; <span class="number">1</span></span><br><span class="line"> base = (base * base) % modulus</span><br><span class="line"> <span class="keyword">return</span> result</span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">prover</span>(<span class="params">secret, generator, modulus, challenge</span>):</span><br><span class="line"> <span class="string">&quot;&quot;&quot;Prover&#x27;s actions in the Schnorr Protocol&quot;&quot;&quot;</span></span><br><span class="line"> r = generate_random_value(modulus)</span><br><span class="line"> commitment = mod_pow(generator, r, modulus)</span><br><span class="line"> response = (r + challenge * secret) % modulus</span><br><span class="line"> <span class="keyword">return</span> commitment, response</span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">verifier</span>(<span class="params">commitment, response, generator, modulus, public_key, challenge</span>):</span><br><span class="line"> <span class="string">&quot;&quot;&quot;Verifier&#x27;s actions in the Schnorr Protocol&quot;&quot;&quot;</span></span><br><span class="line"> verification = mod_pow(generator, response, modulus)</span><br><span class="line"> check = (commitment * mod_pow(public_key, challenge, modulus)) % modulus</span><br><span class="line"> <span class="keyword">return</span> verification == check</span><br><span class="line"></span><br><span class="line"><span class="comment"># Example usage (non-interactive)</span></span><br><span class="line">secret = <span class="number">1234</span> <span class="comment"># Example secret</span></span><br><span class="line">generator = <span class="number">5</span> <span class="comment"># Example generator</span></span><br><span class="line">modulus = <span class="number">2147483647</span> <span class="comment"># Prime modulus</span></span><br><span class="line">public_key = mod_pow(generator, secret, modulus)</span><br><span class="line"></span><br><span class="line">verifier_challenge = <span class="number">999</span> <span class="comment"># Example verifier challenge</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&#x27;Verifier challenge:&#x27;</span>, verifier_challenge)</span><br><span class="line"></span><br><span class="line">commitment, response = prover(secret, generator, modulus, verifier_challenge)</span><br><span class="line"><span class="keyword">if</span> verifier(commitment, response, generator, modulus, public_key, verifier_challenge):</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">&quot;Proof successful!&quot;</span>)</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">&quot;Proof failed!&quot;</span>)</span><br></pre></td></tr></table></figure>
<h1 id="Schnorr-Protocol"><a href="#Schnorr-Protocol" class="headerlink" title="Schnorr Protocol"></a>Schnorr Protocol</h1><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> random</span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">generate_random_value</span>(<span class="params">n</span>):</span><br><span class="line"> <span class="string">&quot;&quot;&quot;Generates a random value in the range [1, n-1]&quot;&quot;&quot;</span></span><br><span class="line"> <span class="keyword">return</span> random.randrange(<span class="number">1</span>, n)</span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">mod_pow</span>(<span class="params">base, exponent, modulus</span>):</span><br><span class="line"> <span class="string">&quot;&quot;&quot;Calculates (base^exponent) % modulus efficiently&quot;&quot;&quot;</span></span><br><span class="line"> result = <span class="number">1</span></span><br><span class="line"> <span class="keyword">while</span> exponent &gt; <span class="number">0</span>:</span><br><span class="line"> <span class="keyword">if</span> exponent % <span class="number">2</span> == <span class="number">1</span>:</span><br><span class="line"> result = (result * base) % modulus</span><br><span class="line"> exponent = exponent &gt;&gt; <span class="number">1</span></span><br><span class="line"> base = (base * base) % modulus</span><br><span class="line"> <span class="keyword">return</span> result</span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">prover</span>(<span class="params">secret, generator, modulus, challenge</span>):</span><br><span class="line"> <span class="string">&quot;&quot;&quot;Prover&#x27;s actions in the Schnorr Protocol&quot;&quot;&quot;</span></span><br><span class="line"> r = generate_random_value(modulus)</span><br><span class="line"> commitment = mod_pow(generator, r, modulus)</span><br><span class="line"> response = (r + challenge * secret) % modulus</span><br><span class="line"> <span class="keyword">return</span> commitment, response</span><br><span class="line"></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">verifier</span>(<span class="params">commitment, response, generator, modulus, public_key, challenge</span>):</span><br><span class="line"> <span class="string">&quot;&quot;&quot;Verifier&#x27;s actions in the Schnorr Protocol&quot;&quot;&quot;</span></span><br><span class="line"> verification = mod_pow(generator, response, modulus)</span><br><span class="line"> check = (commitment * mod_pow(public_key, challenge, modulus)) % modulus</span><br><span class="line"> <span class="keyword">return</span> verification == check</span><br><span class="line"></span><br><span class="line"><span class="comment"># Example usage (non-interactive)</span></span><br><span class="line">secret = <span class="number">1234</span> <span class="comment"># Example secret</span></span><br><span class="line">generator = <span class="number">5</span> <span class="comment"># Example generator</span></span><br><span class="line">modulus = <span class="number">2147483647</span> <span class="comment"># Prime modulus</span></span><br><span class="line">public_key = mod_pow(generator, secret, modulus)</span><br><span class="line"></span><br><span class="line">verifier_challenge = <span class="number">233</span> <span class="comment"># Example verifier challenge</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&#x27;Verifier challenge:&#x27;</span>, verifier_challenge)</span><br><span class="line"></span><br><span class="line">commitment, response = prover(secret, generator, modulus, verifier_challenge)</span><br><span class="line"><span class="keyword">if</span> verifier(commitment, response, generator, modulus, public_key, verifier_challenge):</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">&quot;Proof successful!&quot;</span>)</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line"> <span class="built_in">print</span>(<span class="string">&quot;Proof failed!&quot;</span>)</span><br></pre></td></tr></table></figure>
</div>


Expand Down

0 comments on commit 559fc14

Please sign in to comment.