-
Notifications
You must be signed in to change notification settings - Fork 483
feat(plonk,fflonk): add keccak256-compressed transcript for Cardano/Plutus on-chain verification #625
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
Open
perturbing
wants to merge
26
commits into
iden3:master
Choose a base branch
from
cardano-scaling:perturbing/cardano-compatibility
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat(plonk,fflonk): add keccak256-compressed transcript for Cardano/Plutus on-chain verification #625
Changes from 15 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
7bc6642
feat: add Keccak256CompressedTranscript for Cardano/Plutus compatibility
perturbing 54cf7a6
feat: add transcript option to PLONK prover and verifier
perturbing 9c10964
feat: add transcript option to FFLONK prover and verifier
perturbing 0075266
feat: wire --transcript option to CLI and export transcript classes
perturbing dbf365c
test: add Cardano transcript tests for PLONK and FFLONK
perturbing c71157c
feat: add Cardano/Plutus proof and verification key export commands
perturbing 14c7c6c
fix: compute FFLONK cubic root of unity curve-agnostically
perturbing ef06a6e
build: rebuild bundles with Keccak256CompressedTranscript
perturbing ac9b4a6
fix: restore bn128 FFLONK w3 computation, add bls12381 branch
perturbing b02dce2
refactor: drop shadowing Fr parameter from getOmegaCubicRoot
perturbing 0bc098a
refactor: extract shared ZCash point compression helpers
perturbing d76a31f
fix: reject non-bls12381 curves in Cardano export commands
perturbing 3db7366
fix: reject unknown transcript option values
perturbing a8ee709
refactor: collapse duplicate export-cardano-proof CLI handlers
perturbing e36bd30
build: rebuild bundles
perturbing e0425ea
fix: accept absent --transcript option from the CLI
perturbing cbf7e7a
test: pin ZCash point compression to IETF generator vectors
perturbing b066503
fix: reject non-bls12381 curves in the compressed transcript
perturbing 85a52d8
refactor: derive compression sign flag from a half-field comparison
perturbing 9243c16
refactor: share transcript implementation between variants
perturbing 0aae369
fix: reject unknown curves in FFLONK w3 computation
perturbing d0d4197
fix: assert getOmegaCubicRoot cubes to the domain root
perturbing 75df902
feat: include protocol and curve in Cardano proof export
perturbing 8045204
chore: tidy Cardano export helpers and new-file headers
perturbing 901c320
docs: document --transcript option and Cardano export commands
perturbing 3d5e2fe
build: rebuild bundles
perturbing 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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,78 @@ | ||
| /* | ||
| Copyright 2022 iden3 association. | ||
|
|
||
| This file is part of snarkjs. | ||
|
|
||
| snarkjs is a free software: you can redistribute it and/or | ||
| modify it under the terms of the GNU General Public License as published by the | ||
| Free Software Foundation, either version 3 of the License, or (at your option) | ||
| any later version. | ||
|
|
||
| snarkjs is distributed in the hope that it will be useful, | ||
| but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
| or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
| more details. | ||
|
|
||
| You should have received a copy of the GNU General Public License along with | ||
| snarkjs. If not, see <https://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| // Fiat-Shamir transcript using Keccak-256 with compressed G1 points (ZCash/IETF flag format). | ||
| // Use this instead of Keccak256Transcript when the on-chain verifier operates on compressed | ||
| // BLS12-381 points, e.g. Cardano/Plutus via CIP-0381 + CIP-0101. | ||
|
|
||
| import {Scalar} from "ffjavascript"; | ||
| import {keccak_256} from "@noble/hashes/sha3"; | ||
| import {compressG1} from "./point_compress.js"; | ||
|
|
||
| const POLYNOMIAL = 0; | ||
| const SCALAR = 1; | ||
|
|
||
| export class Keccak256CompressedTranscript { | ||
| constructor(curve) { | ||
| this.G1 = curve.G1; | ||
| this.Fr = curve.Fr; | ||
|
|
||
| this.reset(); | ||
| } | ||
|
|
||
| reset() { | ||
| this.data = []; | ||
| } | ||
|
|
||
| addPolCommitment(polynomialCommitment) { | ||
| this.data.push({type: POLYNOMIAL, data: polynomialCommitment}); | ||
| } | ||
|
|
||
| addScalar(scalar) { | ||
| this.data.push({type: SCALAR, data: scalar}); | ||
| } | ||
|
|
||
| getChallenge() { | ||
| if (0 === this.data.length) { | ||
| throw new Error("Keccak256CompressedTranscript: No data to generate a transcript"); | ||
| } | ||
|
|
||
| let nPolynomials = 0; | ||
| let nScalars = 0; | ||
|
|
||
| this.data.forEach(element => POLYNOMIAL === element.type ? nPolynomials++ : nScalars++); | ||
|
|
||
| // Compressed G1 points are G1.F.n8 bytes (vs 2*n8 for uncompressed). | ||
| let buffer = new Uint8Array(nScalars * this.Fr.n8 + nPolynomials * this.G1.F.n8); | ||
| let offset = 0; | ||
|
|
||
| for (let i = 0; i < this.data.length; i++) { | ||
| if (POLYNOMIAL === this.data[i].type) { | ||
| buffer.set(compressG1(this.G1, this.data[i].data), offset); | ||
| offset += this.G1.F.n8; | ||
| } else { | ||
| this.Fr.toRprBE(buffer, offset, this.data[i].data); | ||
| offset += this.Fr.n8; | ||
| } | ||
| } | ||
|
|
||
| const value = Scalar.fromRprBE(keccak_256(buffer)); | ||
| return this.Fr.e(value); | ||
| } | ||
| } |
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,121 @@ | ||
| /* | ||
| Copyright 2018 0KIMS association. | ||
|
|
||
| This file is part of snarkJS. | ||
|
|
||
| snarkJS is a free software: you can redistribute it and/or modify it | ||
| under the terms of the GNU General Public License as published by | ||
| the Free Software Foundation, either version 3 of the License, or | ||
| (at your option) any later version. | ||
|
|
||
| snarkJS is distributed in the hope that it will be useful, but WITHOUT | ||
| ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
| or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public | ||
| License for more details. | ||
|
|
||
| You should have received a copy of the GNU General Public License | ||
| along with snarkJS. If not, see <https://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| // Converts a snarkjs proof.json (projective/affine [x, y, z] coordinates) into | ||
| // a Cardano/Plutus-friendly JSON where all G1 points are 48-byte ZCash/IETF | ||
| // compressed hex strings and all G2 points are 96-byte compressed hex strings. | ||
| // Scalar field evaluations (eval_* / ql / qr / ...) are left as decimal strings. | ||
|
|
||
| import {getCurveFromName} from "./curves.js"; | ||
| import {compressG1Hex, compressG2Hex} from "./point_compress.js"; | ||
| import {utils} from "ffjavascript"; | ||
|
|
||
| const {unstringifyBigInts} = utils; | ||
|
|
||
| // ---------- public API ---------- | ||
|
|
||
| export default async function exportCardanoProof(_proof) { | ||
| const proof = unstringifyBigInts(_proof); | ||
|
|
||
| const curve = await getCurveFromName(proof.curve); | ||
|
perturbing marked this conversation as resolved.
|
||
|
|
||
| // The ZCash compressed encoding stores flags in the three high bits of the | ||
| // first byte, which only works when the base field leaves them free (as the | ||
| // 381-bit bls12381 field does in its 48-byte serialization). On other | ||
| // curves the flags would overwrite x-coordinate data. | ||
| if (curve.name !== "bls12381") { | ||
| throw new Error(`exportCardanoProof: only bls12381 proofs are supported, got '${curve.name}'`); | ||
| } | ||
|
|
||
| if (proof.protocol === "groth16") { | ||
| return groth16CardanoProof(curve, proof); | ||
| } else if (proof.protocol === "plonk") { | ||
| return plonkCardanoProof(curve, proof); | ||
| } else if (proof.protocol === "fflonk") { | ||
| return fflonkCardanoProof(curve, proof); | ||
| } else { | ||
| throw new Error(`exportCardanoProof: unknown protocol '${proof.protocol}'`); | ||
| } | ||
| } | ||
|
|
||
| // ---------- per-protocol converters ---------- | ||
|
|
||
| function g1FromObj(curve, obj) { | ||
| return curve.G1.fromObject(obj); | ||
| } | ||
|
|
||
| function g2FromObj(curve, obj) { | ||
| return curve.G2.fromObject(obj); | ||
| } | ||
|
|
||
| function groth16CardanoProof(curve, proof) { | ||
| return { | ||
| pi_a: compressG1Hex(curve.G1, g1FromObj(curve, proof.pi_a)), | ||
| pi_b: compressG2Hex(curve.G2, g2FromObj(curve, proof.pi_b)), | ||
| pi_c: compressG1Hex(curve.G1, g1FromObj(curve, proof.pi_c)), | ||
| }; | ||
| } | ||
|
|
||
| // PLONK proof.json uses a flat layout: | ||
| // A, B, C, Z, T1, T2, T3, Wxi, Wxiw → G1 points as [x, y, "1"] | ||
| // eval_a, eval_b, eval_c, eval_s1, eval_s2, eval_zw → Fr scalars (decimal strings) | ||
| function plonkCardanoProof(curve, proof) { | ||
| const g1 = (key) => compressG1Hex(curve.G1, g1FromObj(curve, proof[key])); | ||
| return { | ||
| A: g1("A"), | ||
| B: g1("B"), | ||
| C: g1("C"), | ||
| Z: g1("Z"), | ||
| T1: g1("T1"), | ||
| T2: g1("T2"), | ||
| T3: g1("T3"), | ||
| Wxi: g1("Wxi"), | ||
| Wxiw: g1("Wxiw"), | ||
| // Scalar evaluations: keep as decimal strings (already stringified by caller) | ||
| eval_a: String(proof.eval_a), | ||
| eval_b: String(proof.eval_b), | ||
| eval_c: String(proof.eval_c), | ||
| eval_s1: String(proof.eval_s1), | ||
| eval_s2: String(proof.eval_s2), | ||
| eval_zw: String(proof.eval_zw), | ||
| }; | ||
| } | ||
|
|
||
| // FFLONK proof.json uses a nested layout: | ||
| // polynomials: { C1, C2, W1, W2 } → G1 points | ||
| // evaluations: { ql, qr, qm, qo, qc, s1, s2, s3, a, b, c, z, zw, t1w, t2w, inv } → Fr scalars | ||
| function fflonkCardanoProof(curve, proof) { | ||
| const poly = proof.polynomials; | ||
| const eval_ = proof.evaluations; | ||
| const g1 = (p) => compressG1Hex(curve.G1, g1FromObj(curve, p)); | ||
| const sc = (v) => String(v); | ||
| return { | ||
| c1: g1(poly.C1), | ||
| c2: g1(poly.C2), | ||
| w1: g1(poly.W1), | ||
| w2: g1(poly.W2), | ||
| ql: sc(eval_.ql), qr: sc(eval_.qr), qm: sc(eval_.qm), | ||
| qo: sc(eval_.qo), qc: sc(eval_.qc), | ||
| s1: sc(eval_.s1), s2: sc(eval_.s2), s3: sc(eval_.s3), | ||
| a: sc(eval_.a), b: sc(eval_.b), c: sc(eval_.c), | ||
| z: sc(eval_.z), zw: sc(eval_.zw), | ||
| t1w: sc(eval_.t1w), t2w: sc(eval_.t2w), | ||
| inv: sc(eval_.inv), | ||
| }; | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.