-
Notifications
You must be signed in to change notification settings - Fork 0
Simple password tree plugin #1
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { JsonProof } from "snarkyjs"; | ||
|
||
export type PluginType = { | ||
compile: () => Promise<string>; | ||
getInputs: () => Promise<string[]>; | ||
verify: ( | ||
jsonProof: JsonProof, | ||
verificationKey: string, | ||
) => Promise<[string | boolean | undefined, string]>; | ||
prove: (inputs: string[]) => Promise<undefined | JsonProof>; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { Experimental, Field, JsonProof, MerkleTree, Poseidon, verify } from "snarkyjs"; | ||
import ProvePasswordInTreeProgram, { PASSWORD_TREE_HEIGHT, PasswordTreePublicInput, PasswordTreeWitness } from "../zkPrograms/passwordTreeProof"; | ||
import { PluginType } from "../plugin"; | ||
|
||
const PasswordInTreeProofClass = Experimental.ZkProgram.Proof(ProvePasswordInTreeProgram); | ||
|
||
abstract class TreeStorage { | ||
abstract getRoot(): Promise<Field>; | ||
abstract getWitness(uid: bigint): Promise<undefined | PasswordTreeWitness>; | ||
abstract getRole(uid: bigint): Promise<undefined | string>; | ||
} | ||
|
||
class InMemoryStorage implements TreeStorage { | ||
roles: Map<bigint, string>; | ||
merkleTree: MerkleTree; | ||
|
||
constructor(roleMappings: Array<[bigint, Field, string]> = []) { | ||
this.roles = new Map(); | ||
this.merkleTree = new MerkleTree(PASSWORD_TREE_HEIGHT); | ||
|
||
roleMappings.forEach(([uid, password, role]) => { | ||
this.roles.set(uid, role); | ||
this.merkleTree.setLeaf(uid, Poseidon.hash([password])); | ||
}) | ||
} | ||
|
||
async getRoot() { return this.merkleTree.getRoot(); } | ||
|
||
async getWitness(uid: bigint) { | ||
if (!this.roles.has(uid)) return undefined; | ||
return new PasswordTreeWitness(this.merkleTree.getWitness(uid)) | ||
} | ||
|
||
async getRole(uid: bigint) { return this.roles.get(uid); } | ||
} | ||
|
||
const storage = new InMemoryStorage([ | ||
[BigInt(0), Field('7555220006856562833147743033256142154591945963958408607501861037584894828141'), 'admin'], | ||
[BigInt(1), Field('21565680844461314807147611702860246336805372493508489110556896454939225549736'), 'member'] | ||
]); | ||
|
||
const compile = async (): Promise<string> => { | ||
console.log('Compiling SimplePasswordTree program'); | ||
console.log(ProvePasswordInTreeProgram); | ||
const { verificationKey } = await ProvePasswordInTreeProgram.compile(); | ||
return verificationKey; | ||
} | ||
|
||
const verifyAndGetRoleProgram = async ( | ||
jsonProof: JsonProof, | ||
verificationKey: string, | ||
): Promise<[string | boolean | undefined, string]> => { | ||
if (!verify(jsonProof, verificationKey)) { | ||
return [false, 'proof invalid']; | ||
} | ||
const proof = PasswordInTreeProofClass.fromJSON(jsonProof); | ||
const role = await storage.getRole(proof.publicInput.witness.calculateIndex().toBigInt()); | ||
if (!role) { return [undefined, 'unknown public input']; } | ||
return [role, 'role proved']; | ||
} | ||
|
||
async function fetchPublicInput(uid: bigint): Promise<undefined | PasswordTreePublicInput> { | ||
const root = await storage.getRoot(); | ||
const witness = await storage.getWitness(uid); | ||
if (!witness) return undefined; | ||
return new PasswordTreePublicInput({ root, witness }); | ||
} | ||
|
||
const prove = async (inputs: string[]): Promise<undefined | JsonProof> => { | ||
const [uidStr, secretInput] = inputs; | ||
const uid: bigint = BigInt(uidStr); | ||
const publicInput = await fetchPublicInput(uid); | ||
if (!publicInput) return undefined; | ||
const proof = await ProvePasswordInTreeProgram.baseCase( | ||
publicInput, Field(secretInput)); | ||
return proof.toJSON(); | ||
} | ||
|
||
// FIXME: I have no idea what this should do | ||
const getInputs = async (): Promise<string[]> => { | ||
return Array.from(storage.roles.keys()).map(k => k.toString()); | ||
}; | ||
|
||
export const SimplePasswordTree: PluginType = { | ||
compile, | ||
getInputs, | ||
verify: verifyAndGetRoleProgram, | ||
prove, | ||
} | ||
|
||
export default SimplePasswordTree; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Experimental, Field, MerkleWitness, Poseidon, Struct } from "snarkyjs"; | ||
|
||
export const PASSWORD_TREE_HEIGHT = 10; | ||
|
||
export class PasswordTreeWitness extends MerkleWitness(PASSWORD_TREE_HEIGHT) { } | ||
|
||
export class PasswordTreePublicInput extends Struct({ | ||
witness: PasswordTreeWitness, | ||
root: Field | ||
}) { }; | ||
|
||
export const ProvePasswordInTreeProgram = Experimental.ZkProgram({ | ||
publicInput: PasswordTreePublicInput, | ||
publicOutput: Field, | ||
|
||
methods: { | ||
baseCase: { | ||
privateInputs: [Field], | ||
method(publicInput: PasswordTreePublicInput, privateInput: Field): Field { | ||
publicInput.witness | ||
.calculateRoot(Poseidon.hash([privateInput])) | ||
.assertEquals(publicInput.root); | ||
return publicInput.witness.calculateIndex(); | ||
} | ||
} | ||
} | ||
}); | ||
|
||
export default ProvePasswordInTreeProgram; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What this does is basically implemented by
fetchPublicInput
you wrote