-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #363 from semaphore-protocol/chore/snarkjs-circoml…
…ibjs-types New Snarkjs/Circomlibjs type definitions
- Loading branch information
Showing
16 changed files
with
111 additions
and
1,038 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 |
---|---|---|
@@ -1,15 +1,16 @@ | ||
import { BigNumber } from "@ethersproject/bignumber" | ||
import { BytesLike, Hexable, zeroPad } from "@ethersproject/bytes" | ||
import { keccak256 } from "@ethersproject/keccak256" | ||
import { NumericString } from "snarkjs" | ||
|
||
/** | ||
* Creates a keccak256 hash of a message compatible with the SNARK scalar modulus. | ||
* @param message The message to be hashed. | ||
* @returns The message digest. | ||
*/ | ||
export default function hash(message: BytesLike | Hexable | number | bigint): bigint { | ||
export default function hash(message: BytesLike | Hexable | number | bigint): NumericString { | ||
message = BigNumber.from(message).toTwos(256).toHexString() | ||
message = zeroPad(message, 32) | ||
|
||
return BigInt(keccak256(message)) >> BigInt(8) | ||
return (BigInt(keccak256(message)) >> BigInt(8)).toString() as NumericString | ||
} |
This file contains 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 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 |
---|---|---|
@@ -1,19 +1,20 @@ | ||
import { SnarkJSProof, Proof } from "./types" | ||
import { Groth16Proof } from "snarkjs" | ||
import { PackedProof } from "./types" | ||
|
||
/** | ||
* Packs a proof into a format compatible with Semaphore. | ||
* @param originalProof The proof generated with SnarkJS. | ||
* @param proof The Groth16 proof generated with SnarkJS. | ||
* @returns The proof compatible with Semaphore. | ||
*/ | ||
export default function packProof(originalProof: SnarkJSProof): Proof { | ||
export default function packProof(proof: Groth16Proof): PackedProof { | ||
return [ | ||
originalProof.pi_a[0], | ||
originalProof.pi_a[1], | ||
originalProof.pi_b[0][1], | ||
originalProof.pi_b[0][0], | ||
originalProof.pi_b[1][1], | ||
originalProof.pi_b[1][0], | ||
originalProof.pi_c[0], | ||
originalProof.pi_c[1] | ||
proof.pi_a[0], | ||
proof.pi_a[1], | ||
proof.pi_b[0][1], | ||
proof.pi_b[0][0], | ||
proof.pi_b[1][1], | ||
proof.pi_b[1][0], | ||
proof.pi_c[0], | ||
proof.pi_c[1] | ||
] | ||
} |
This file contains 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 |
---|---|---|
@@ -1,33 +1,25 @@ | ||
export type BigNumberish = string | bigint | ||
export type NumericString = `${number}` | ||
|
||
export type SnarkArtifacts = { | ||
wasmFilePath: string | ||
zkeyFilePath: string | ||
} | ||
|
||
export type SnarkJSProof = { | ||
pi_a: BigNumberish[] | ||
pi_b: BigNumberish[][] | ||
pi_c: BigNumberish[] | ||
protocol: string | ||
curve: string | ||
export type SemaphoreProof = { | ||
merkleTreeRoot: NumericString | ||
signal: NumericString | ||
nullifierHash: NumericString | ||
externalNullifier: NumericString | ||
proof: PackedProof | ||
} | ||
|
||
export type FullProof = { | ||
merkleTreeRoot: BigNumberish | ||
signal: BigNumberish | ||
nullifierHash: BigNumberish | ||
externalNullifier: BigNumberish | ||
proof: Proof | ||
} | ||
|
||
export type Proof = [ | ||
BigNumberish, | ||
BigNumberish, | ||
BigNumberish, | ||
BigNumberish, | ||
BigNumberish, | ||
BigNumberish, | ||
BigNumberish, | ||
BigNumberish | ||
export type PackedProof = [ | ||
NumericString, | ||
NumericString, | ||
NumericString, | ||
NumericString, | ||
NumericString, | ||
NumericString, | ||
NumericString, | ||
NumericString | ||
] |
This file contains 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 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
Oops, something went wrong.