Skip to content
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

Witness creation #14

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions kzg_prover/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use kimchi::mina_poseidon::sponge::{DefaultFqSponge, DefaultFrSponge};
use kimchi::poly_commitment::evaluation_proof::OpeningProof;
use kimchi::poly_commitment::srs::SRS;
use kimchi::prover_index::ProverIndex;
use kimchi::snarky::constraint_system::SnarkyConstraintSystem;
use kimchi::verifier::verify;
use kimchi::{poly_commitment::commitment::CommitmentCurve, proof::ProverProof};

Expand Down Expand Up @@ -54,16 +55,12 @@ fn main() {
})
.collect();

let constants = kimchi::snarky::constants::Constants::new::<Curve>();
let mut snarky_cs =
kimchi::snarky::constraint_system::SnarkyConstraintSystem::create(constants);
create_snarky_constraint_system();

let mut kimchi_cs = kimchi::circuits::constraints::ConstraintSystem::<Field>::create(gates)
let kimchi_cs = kimchi::circuits::constraints::ConstraintSystem::<Field>::create(gates)
.public(3)
.build()
.unwrap();
kimchi_cs.feature_flags.foreign_field_add = true;
kimchi_cs.feature_flags.foreign_field_mul = true;

let srs_json = std::fs::read_to_string("./test_data/srs_pasta.json").unwrap();
let mut kimchi_srs: SRS<Curve> = serde_json::from_str(srs_json.as_str()).unwrap();
Expand All @@ -73,7 +70,7 @@ fn main() {
let &endo_q = <Curve as KimchiCurve>::other_curve_endo();

let group_map = <Curve as CommitmentCurve>::Map::setup();
let witness: [Vec<Field>; 15] = array::from_fn(|_| vec![Field::from(0); 4]);
let witness = create_witness();
let prover_index: ProverIndex<_, OpeningProof<Curve>> =
ProverIndex::create(kimchi_cs, endo_q, kimchi_srs_arc);
let proof =
Expand All @@ -93,6 +90,19 @@ fn main() {
println!("Done!");
}

fn create_snarky_constraint_system() -> SnarkyConstraintSystem<Field> {
let constants = kimchi::snarky::constants::Constants::new::<Curve>();
let mut snarky_cs = SnarkyConstraintSystem::create(constants);
snarky_cs.set_public_input_size(4);

snarky_cs
}

fn create_witness() -> [Vec<Field>; 15] {
let witness: [Vec<Field>; 15] = array::from_fn(|_| vec![Field::from(0); 4]);
witness
}

/*

let srs = SRS::<Curve>::create(kimchi_cs.domain.d1.size as usize);
Expand Down
70 changes: 35 additions & 35 deletions test_prover/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test_prover/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@
"typescript": "^4.7.2"
},
"peerDependencies": {
"snarkyjs": "0.12.*"
"o1js": "0.12.*"
}
}
24 changes: 5 additions & 19 deletions test_prover/src/Addition.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,8 @@
import {
Field,
SmartContract,
state,
State,
method
} from 'snarkyjs';
import { Field } from 'o1js';

export class Addition extends SmartContract {
@state(Field) num = State<Field>();

init() {
super.init();
this.num.set(Field(0));
}

@method update(operand: Field) {
const currentState = this.num.get();
this.num.assertEquals(currentState);
this.num.set(currentState.add(operand));
export class Addition {
// (a + b) * c = d
static main(a: Field, b: Field, c: Field, d: Field) {
a.add(b).mul(c).assertEquals(d);
}
}
50 changes: 11 additions & 39 deletions test_prover/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,17 @@
import { Field, Provable } from "o1js";
import { Addition } from "./Addition.js";
import {
Field,
Mina,
PrivateKey,
AccountUpdate,
verify,
JsonProof,
Bool,
} from 'snarkyjs';

console.log('SnarkyJS loaded');
const Local = Mina.LocalBlockchain();
Mina.setActiveInstance(Local);
const { privateKey: deployerKey, publicKey: deployerAccount } = Local.testAccounts[0];
const { privateKey: senderKey, publicKey: senderAccount } = Local.testAccounts[1];
// ----------------------------------------------------
// Create a public/private key pair. The public key is your address and where you deploy the zkApp to
const zkAppPrivateKey = PrivateKey.random();
const zkAppAddress = zkAppPrivateKey.toPublicKey();
// create an instance of Addition - and deploy it to zkAppAddress
const zkAppInstance = new Addition(zkAppAddress);
const { verificationKey } = await Addition.compile();
const deployTxn = await Mina.transaction(deployerAccount, () => {
AccountUpdate.fundNewAccount(deployerAccount);
zkAppInstance.deploy();
});
await deployTxn.sign([deployerKey, zkAppPrivateKey]).send();
// get the initial state of Addition after deployment
const num0 = zkAppInstance.num.get();
console.log('state after init:', num0.toString());
// ----------------------------------------------------
const txn1 = await Mina.transaction(senderAccount, () => {
zkAppInstance.update(Field(5));

console.log("Generating test constraint system");
let cs = Provable.constraintSystem(() => {
let a = Provable.witness(Field, () => Field(3));
let b = Provable.witness(Field, () => Field(5));
let c = Provable.witness(Field, () => Field(2));
let d = Provable.witness(Field, () => Field(16));

Addition.main(a, b, c, d);
});
let proof = (await txn1.prove())[0];
let isValidProof = await verify(proof?.toJSON() as JsonProof, verificationKey.data);
Bool(isValidProof).assertTrue();
console.log('Proof was verified successfully!');
await txn1.sign([senderKey]).send();
console.log("Constraint system:", JSON.stringify(cs));

const num1 = zkAppInstance.num.get();
console.log('state after txn1:', num1.toString());
// ----------------------------------------------------
console.log('Shutting down');