Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref: Refactor Statement component and adjust challenge 4 #21

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/nextjs/app/engineering/circom/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ export default async function Page({ params }: PageProps) {

{params.slug !== "1" && (
<>
<Statement />
<Statement challengeId={challengeId} />
<hr className="border-0 h-px bg-blue-500 shadow-[0_0_10px_2px_rgba(59,130,246,0.7)]" />
<Verifier />
<Verifier challengeId={challengeId} />
<hr className="border-0 h-px bg-blue-500 shadow-[0_0_10px_2px_rgba(59,130,246,0.7)] mb-10" />
</>
)}
Expand Down
2 changes: 1 addition & 1 deletion packages/nextjs/app/engineering/circom/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const Home: NextPage = () => {
<div key={rowIndex} className="flex flex-row items-center mb-8">
{row.map((step, index) => {
// TODO: Implement the logic to lock the challenges
const isLocked = step.id > 2;
const isLocked = step.id > 2 && step.id !== 4;
const content = (
<div className={`flex flex-col items-center mx-4 ${isLocked ? "opacity-50" : ""} group`}>
<div className="flex flex-col items-center">
Expand Down
42 changes: 26 additions & 16 deletions packages/nextjs/components/Statement.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
const Statement = () => {
return (
<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>
<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>
);
import React from "react";
// Import the statement for every challenge
import Challenge2Content from "../public/challenges/challenge_2/statement";
import Challenge4Content from "../public/challenges/challenge_4/statement";

type StatementProps = {
challengeId: string;
};

const NotFoundContent = () => <p>Challenge not found</p>;

const Statement = ({ challengeId }: StatementProps) => {
let ChallengeContent;

switch (challengeId) {
case "2":
ChallengeContent = Challenge2Content;
break;
case "4":
ChallengeContent = Challenge4Content;
break;
default:
ChallengeContent = NotFoundContent;
}

return <ChallengeContent />;
};

export default Statement;
17 changes: 13 additions & 4 deletions packages/nextjs/components/Verifier.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,25 @@

import React, { useState } from "react";
import Image from "next/image";
import { generateAndVerifyProof } from "~~/utils/generateAndVerifyProof";
import { generateAndVerifyProof2, generateAndVerifyProof4 } from "~~/utils/generateAndVerifyProof";

function Verifier() {
type VerifierProps = {
challengeId: string;
};

function Verifier({ challengeId }: VerifierProps) {
const [characterA, setCharacterA] = useState(0);
const [characterB, setCharacterB] = useState(0);
const [result, setResult] = useState("No proof yet");

const verify = async () => {
const result = (await generateAndVerifyProof(characterA, characterB)) ? "Valid proof" : "Invalid proof";
setResult(result);
if (challengeId == "2") {
const result = (await generateAndVerifyProof2(characterA, characterB)) ? "Valid proof" : "Invalid proof";
setResult(result);
} else if (challengeId == "4") {
const result = (await generateAndVerifyProof4(characterA, characterB)) ? "Valid proof" : "Invalid proof";
setResult(result);
}
};

return (
Expand Down
17 changes: 17 additions & 0 deletions packages/nextjs/public/challenges/challenge_2/statement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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>
<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;
17 changes: 17 additions & 0 deletions packages/nextjs/public/challenges/challenge_4/statement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const Challenge4Content = () => (
<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>
<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 Challenge4Content;
28 changes: 26 additions & 2 deletions packages/nextjs/utils/generateAndVerifyProof.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { groth16 } from "snarkjs";
import { groth16, plonk } from "snarkjs";

export async function generateAndVerifyProof(a: number, b: number) {
export async function generateAndVerifyProof2(a: number, b: number) {
try {
const input = {
A_is_knight: a, // Ensure 'a' is either "Knight" or "Knave"
Expand All @@ -23,3 +23,27 @@ export async function generateAndVerifyProof(a: number, b: number) {
return 0;
}
}

export async function generateAndVerifyProof4(a: number, b: number) {
try {
const input = {
A_is_knight: a, // Ensure 'a' is either "Knight" or "Knave"
B_is_knight: b, // Ensure 'b' is either "Knight" or "Knave"
};

const { proof, publicSignals } = await plonk.fullProve(
input,
"/challenges/challenge_4/files/Knightsknaves.wasm",
"/challenges/challenge_4/files/knightsknaves_0000.zkey",
);

const vKey = await fetch("/challenges/challenge_4/files/verification_key.json").then(response => response.json());
const isValid = await plonk.verify(vKey, publicSignals, proof);

console.log(`Is the proof valid? ${isValid}`);
return isValid ? 1 : 0;
} catch (error) {
console.error("Error generating or verifying proof:", error);
return 0;
}
}
Loading