minauth-merkle-membership-plugin
Version:
This package contains an implementation of a simple MinAuth plugin that extends the concept of password authentication into authenticating within sets that provide a level of anonymity. The proofs are built and verified with MINA's `o1js` library & proof
58 lines • 2.05 kB
JavaScript
import { Field, MerkleWitness, Poseidon, SelfProof, Struct, ZkProgram } from 'o1js';
// TODO how can this be made dynamic
export const TREE_HEIGHT = 10;
export class TreeWitness extends MerkleWitness(TREE_HEIGHT) {
}
export class PrivateInput extends Struct({
witness: TreeWitness,
secret: Field
}) {
}
export class PublicInput extends Struct({
merkleRoot: Field
}) {
}
export class PublicOutput extends Struct({
recursiveHash: Field
}) {
}
/** Prove knowledge of a preimage of a hash in a merkle tree.
* The proof does not reveal the preimage nor the hash.
* The output contains a recursive hash of all the roots for which the preimage is known.
* output = hash(lastRoot + hash(secondLastRoot, ... hash(xLastRoot, lastRoot) ...)
* Therefore the order of the proofs matters.
*/
export const Program = ZkProgram({
name: 'MerkleMemberships',
publicInput: PublicInput,
publicOutput: PublicOutput,
methods: {
baseCase: {
privateInputs: [PrivateInput],
method(publicInput, privateInput) {
privateInput.witness
.calculateRoot(Poseidon.hash([privateInput.secret]))
.assertEquals(publicInput.merkleRoot);
return new PublicOutput({
recursiveHash: publicInput.merkleRoot
});
}
},
inductiveCase: {
privateInputs: [SelfProof, PrivateInput],
method(publicInput, earlierProof, privateInput) {
earlierProof.verify();
privateInput.witness
.calculateRoot(Poseidon.hash([privateInput.secret]))
.assertEquals(publicInput.merkleRoot);
return new PublicOutput({
recursiveHash: Poseidon.hash([
publicInput.merkleRoot,
earlierProof.publicOutput.recursiveHash
])
});
}
}
}
});
//# sourceMappingURL=merklemembershipsprogram.js.map