Skip to content

Commit

Permalink
Merge pull request #25 from brolag/feature/fix-challenge-2
Browse files Browse the repository at this point in the history
feat: correct downloadable files and verify logic for Challenge #2
  • Loading branch information
brolag authored Sep 6, 2024
2 parents e6c14d8 + 1f32b4a commit 40f48cd
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
pragma circom 2.1.6;

template KnightsKnaves() {
signal input A_is_knight; // 1 if A is a knight, 0 if A is a knave
signal input B_is_knight; // 1 if B is a knight, 0 if B is a knave

// Intermediate signals for A's statement
signal A_statement_part1;
signal A_statement_part2;

// A says "B is a knave"
A_statement_part1 <== A_is_knight * (1 - B_is_knight);
A_statement_part2 <== (1 - A_is_knight) * B_is_knight;
signal A_statement_valid;
A_statement_valid <== A_statement_part1 + A_statement_part2;

// Intermediate signals for B's statement
signal B_statement_part1;
signal B_statement_part2;
signal B_statement_part1_1;
signal B_statement_part1_2;
signal B_statement_part1_3;
signal B_statement_part2_1;
signal B_statement_part2_2;
signal B_statement_part2_3;

// If B is a knight (1), then A and B must be of opposite types
//B_statement_part1 <== B_is_knight * (A_is_knight * (1 - B_is_knight) + (1 - A_is_knight) * B_is_knight);
B_statement_part1_1 <== A_is_knight * (1 - B_is_knight);
B_statement_part1_2 <== (1 - A_is_knight) * B_is_knight;
B_statement_part1_3 <== (B_statement_part1_1 + B_statement_part1_2);
B_statement_part1 <== B_is_knight * B_statement_part1_3;

// If B is a knave (0), then A and B must be of the same type
//B_statement_part2 <== (1 - B_is_knight) * (A_is_knight * B_is_knight + (1 - A_is_knight) * (1 - B_is_knight));
B_statement_part2_1 <== A_is_knight * B_is_knight;
B_statement_part2_2 <== (1 - A_is_knight) * (1 - B_is_knight);
B_statement_part2_3 <== B_statement_part2_1 + B_statement_part2_2;
B_statement_part2 <== (1 - B_is_knight) * B_statement_part2_3;

signal B_statement_valid;
B_statement_valid <== B_statement_part1 + B_statement_part2;

// Enforce that both statements must be valid
signal output is_consistent;
is_consistent <== A_statement_valid * B_statement_valid;

// Enforce that is_consistent must be 1
// This makes sure the circuit will only accept valid proofs
is_consistent * (is_consistent - 1) === 0; // This ensures is_consistent is either 0 or 1
is_consistent === 1; // This ensures is_consistent is 1

}

component main = KnightsKnaves();
41 changes: 28 additions & 13 deletions packages/nextjs/public/challenges/challenge_2/statement.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
const Challenge2Content = () => (
<div className="w-full max-w-[64rem] p-4 bg-gray-700 rounded-lg mb-6 font-play shadow-lg">
<div className="mb-4">
<h3 className="text-white text-xl font-bold">Statements</h3>
<ul className="text-gray-300">
<li>- Knights always tell the truth.</li>
<li>- Knaves always lie.</li>
</ul>
<div className="w-full max-w-[64rem] p-4 bg-gray-700 rounded-lg mb-6 font-play shadow-lg">
<div className="mb-4">
<h3 className="text-white text-xl font-bold">Puzzle: Knights and Knaves</h3>
<p className="text-gray-300">
This is a classic logic puzzle involving two characters, A and B. Each character is either a knight, who always tells the truth, or a knave, who always lies. Your task is to determine who is the knight and who is the knave based on their statements.
</p>
</div>
<div className="mb-4">
<h4 className="text-white text-lg font-bold">Rules</h4>
<ul className="text-gray-300">
<li>- Knights always tell the truth.</li>
<li>- Knaves always lie.</li>
</ul>
</div>
<div className="mb-4">
<h4 className="text-white text-lg font-bold">Statements</h4>
<ul className="text-gray-300">
<li>Character A: "B is a knave.</li>
<li>Character B: "A and I are of opposite types.</li>
</ul>
</div>
<div className="mb-4">
<h4 className="text-white text-lg font-bold">Task</h4>
<p className="text-gray-300">
Write a circuit in Circom that receives the selected value for each character and that the circuit can identify whether the selection of characters is correct or not. The circuit must be compiled using the Groth16 protocol.
</p>
</div>
</div>
<div className="mb-4">
<p className="text-gray-300">Character A: B is a knave.</p>
<p className="text-gray-300">Character B: A and I are of opposite types.</p>
</div>
</div>
);
);

export default Challenge2Content;
6 changes: 3 additions & 3 deletions packages/nextjs/public/challenges/challenge_2/steps.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"title": "Knights and Knaves",
"title": "Knights and Knaves - Groth16",
"steps": [
{
"step": "Step 1",
"description": ["Create the circuit in Circom (Knightsknaves.circom)"],
"commands": [""],
"files": [""]
"files": "KnightsKnaves.circom"
},
{
"step": "Step 2",
Expand Down Expand Up @@ -34,7 +34,7 @@
"step": "Step 5",
"description": "Create an input file named input.json, containing the inputs required for your circuit. This file should be in JSON format.",
"commands": [""],
"files": ["input.json", ""]
"files": "input.json"
},
{
"step": "Step 6",
Expand Down

0 comments on commit 40f48cd

Please sign in to comment.