signal hashing and format

This commit is contained in:
psippl 2022-01-31 22:24:40 +01:00
parent 2fa89d6580
commit 9cc93c98e3
2 changed files with 34 additions and 29 deletions

View File

@ -44,6 +44,6 @@ fn main() {
// dbg!(proof);
proof_signal(&id, &proof);
generate(&id, &proof, BigInt::from(123), b"xxx");
}

View File

@ -1,30 +1,39 @@
use ark_circom::{read_zkey, WitnessCalculator, CircomReduction};
use ark_std::rand::thread_rng;
use ark_bn254::Bn254;
use ark_circom::{read_zkey, CircomReduction, WitnessCalculator};
use ark_std::rand::thread_rng;
use color_eyre::Result;
use num_bigint::BigInt;
use ethers::utils::keccak256;
use num_bigint::{BigInt, Sign};
use std::{collections::HashMap, fs::File};
use std::{collections::HashMap, fs::File, ops::Shr};
use crate::{identity::*, poseidon_tree::{Proof}, merkle_tree::Branch};
use crate::{identity::*, merkle_tree::Branch, poseidon_tree::Proof};
use ark_groth16::{
prepare_verifying_key, verify_proof, create_proof_with_reduction_and_matrices,
};
use ark_groth16::{create_proof_with_reduction_and_matrices, prepare_verifying_key, verify_proof};
// TODO: we should create a From trait for this
fn proof_to_vec(proof: &Proof) -> Vec<BigInt> {
proof.0.iter().map(|x| {
match x {
proof
.0
.iter()
.map(|x| match x {
Branch::Left(value) => value.into(),
Branch::Right(value) => value.into(),
}
}).collect::<Vec<BigInt>>()
})
.collect::<Vec<BigInt>>()
}
fn hash_signal(signal: &[u8]) -> BigInt {
BigInt::from_bytes_be(Sign::Plus, &keccak256(signal)).shr(8)
}
// WIP: uses dummy proofs for now
pub fn proof_signal(identity: &Identity, merkle_proof: &Proof, external_nullifier: BigInt) -> Result<()> {
pub fn generate(
identity: &Identity,
merkle_proof: &Proof,
external_nullifier: BigInt,
signal: &[u8],
) -> Result<()> {
let mut file = File::open("./snarkfiles/semaphore.zkey").unwrap();
let (params, matrices) = read_zkey(&mut file).unwrap();
let num_inputs = matrices.num_instance_variables;
@ -33,26 +42,23 @@ pub fn proof_signal(identity: &Identity, merkle_proof: &Proof, external_nullifie
let inputs = {
let mut inputs: HashMap<String, Vec<BigInt>> = HashMap::new();
inputs.insert("identity_nullifier".to_string(), vec![identity.nullifier.clone()]);
inputs.insert("identity_trapdoor".to_string(), vec![identity.trapdoor.clone()]);
inputs.insert(
"identity_nullifier".to_string(),
vec![identity.nullifier.clone()],
);
inputs.insert(
"identity_trapdoor".to_string(),
vec![identity.trapdoor.clone()],
);
inputs.insert("identity_path_index".to_string(), merkle_proof.path_index());
inputs.insert("path_elements".to_string(), proof_to_vec(merkle_proof));
inputs.insert("external_nullifier".to_string(), vec![external_nullifier]);
//
let values = inputs.entry("signal_hash".to_string()).or_insert_with(Vec::new);
values.push(BigInt::parse_bytes(
b"426814738191208581806614072441429636075448095566621754358249936829881365458",
10,
)
.unwrap());
inputs.insert("signal_hash".to_string(), vec![hash_signal(signal)]);
inputs
};
let mut wtns = WitnessCalculator::new("./snarkfiles/semaphore.wasm")
.unwrap();
let mut wtns = WitnessCalculator::new("./snarkfiles/semaphore.wasm").unwrap();
let full_assignment = wtns
.calculate_witness_element::<Bn254, _>(inputs, false)
@ -92,4 +98,3 @@ pub fn proof_signal(identity: &Identity, merkle_proof: &Proof, external_nullifie
Ok(())
}