Trait plonk_pallet::Circuit [−][src]
pub trait Circuit {
const CIRCUIT_ID: [u8; 32];
fn gadget(&mut self, composer: &mut TurboComposer) -> Result<(), Error>;
fn public_inputs(&self) -> Vec<PublicInputValue, Global>ⓘ;
fn padded_gates(&self) -> usize;
fn compile(
&mut self,
pub_params: &PublicParameters
) -> Result<(ProverKey, VerifierData), Error> { ... }
fn prove(
&mut self,
pub_params: &PublicParameters,
prover_key: &ProverKey,
transcript_init: &'static [u8]
) -> Result<Proof, Error> { ... }
fn verify(
pub_params: &PublicParameters,
verifier_data: &VerifierData,
proof: &Proof,
public_inputs: &[PublicInputValue],
transcript_init: &'static [u8]
) -> Result<(), Error> { ... }
}
Expand description
Trait that should be implemented for any circuit function to provide to it the capabilities of automatically being able to generate, and verify proofs as well as compile the circuit.
Example
use fullcodec_plonk::prelude::*;
use rand::SeedableRng;
use rand_xorshift::XorShiftRng;
fn main() -> Result<(), Error> {
// Implements a circuit that checks:
// 1) a + b = c where C is a PI
// 2) a <= 2^6
// 3) b <= 2^5
// 4) a * b = d where D is a PI
// 5) JubJub::GENERATOR * e(JubJubScalar) = f where F is a PI
#[derive(Debug, Default)]
pub struct TestCircuit {
a: BlsScalar,
b: BlsScalar,
c: BlsScalar,
d: BlsScalar,
e: JubJubScalar,
f: JubJubAffine,
}
impl Circuit for TestCircuit {
const CIRCUIT_ID: [u8; 32] = [0xff; 32];
fn gadget(
&mut self,
composer: &mut TurboComposer,
) -> Result<(), Error> {
// Add fixed witness zero
let zero = TurboComposer::constant_zero();
let a = composer.append_witness(self.a);
let b = composer.append_witness(self.b);
// Make first constraint a + b = c
let constraint = Constraint::new()
.left(1)
.right(1)
.public(-self.c)
.a(a)
.b(b);
composer.append_gate(constraint);
// Check that a and b are in range
composer.component_range(a, 1 << 6);
composer.component_range(b, 1 << 5);
// Make second constraint a * b = d
let constraint = Constraint::new()
.mult(1)
.output(1)
.public(-self.d)
.a(a)
.b(b);
composer.append_gate(constraint);
let e = composer.append_witness(self.e);
let scalar_mul_result =
composer.component_mul_generator(
e, dusk_jubjub::GENERATOR_EXTENDED,
);
// Apply the constrain
composer
.assert_equal_public_point(scalar_mul_result, self.f);
Ok(())
}
fn public_inputs(&self) -> Vec<PublicInputValue> {
vec![self.c.into(), self.d.into(), self.f.into()]
}
fn padded_gates(&self) -> usize {
1 << 11
}
}
let rng = XorShiftRng::from_seed([
0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37,
0x32, 0x54, 0x06, 0xbc, 0xe5,
]);
let pp = PublicParameters::setup(1 << 12, rng)?;
// Initialize the circuit
let mut circuit = TestCircuit::default();
// Compile the circuit
let (pk, vd) = circuit.compile(&pp)?;
// Prover POV
let proof = {
let mut circuit = TestCircuit {
a: BlsScalar::from(20u64),
b: BlsScalar::from(5u64),
c: BlsScalar::from(25u64),
d: BlsScalar::from(100u64),
e: JubJubScalar::from(2u64),
f: JubJubAffine::from(
dusk_jubjub::GENERATOR_EXTENDED * JubJubScalar::from(2u64),
),
};
circuit.prove(&pp, &pk, b"Test")
}?;
// Verifier POV
let public_inputs: Vec<PublicInputValue> = vec![
BlsScalar::from(25u64).into(),
BlsScalar::from(100u64).into(),
JubJubAffine::from(
dusk_jubjub::GENERATOR_EXTENDED * JubJubScalar::from(2u64),
)
.into(),
];
TestCircuit::verify(
&pp,
&vd,
&proof,
&public_inputs,
b"Test",
)
}
Associated Constants
const CIRCUIT_ID: [u8; 32]
const CIRCUIT_ID: [u8; 32]
Circuit identifier associated constant.
Required methods
Gadget implementation used to fill the composer.
Return the list of public inputs generated by the gadget
fn padded_gates(&self) -> usize
fn padded_gates(&self) -> usize
Returns the Circuit size padded to the next power of two.
Provided methods
fn compile(
&mut self,
pub_params: &PublicParameters
) -> Result<(ProverKey, VerifierData), Error>
fn compile(
&mut self,
pub_params: &PublicParameters
) -> Result<(ProverKey, VerifierData), Error>
Compiles the circuit by using a function that returns a Result
with the ProverKey
, VerifierKey
and the circuit size.
fn prove(
&mut self,
pub_params: &PublicParameters,
prover_key: &ProverKey,
transcript_init: &'static [u8]
) -> Result<Proof, Error>
fn prove(
&mut self,
pub_params: &PublicParameters,
prover_key: &ProverKey,
transcript_init: &'static [u8]
) -> Result<Proof, Error>
Generates a proof using the provided CircuitInputs
& ProverKey
instances.
fn verify(
pub_params: &PublicParameters,
verifier_data: &VerifierData,
proof: &Proof,
public_inputs: &[PublicInputValue],
transcript_init: &'static [u8]
) -> Result<(), Error>
fn verify(
pub_params: &PublicParameters,
verifier_data: &VerifierData,
proof: &Proof,
public_inputs: &[PublicInputValue],
transcript_init: &'static [u8]
) -> Result<(), Error>
Verify the provided proof for the compiled verifier data