tree leaf dbg and wip proving

This commit is contained in:
psippl 2022-01-29 23:46:49 +01:00
parent 4d8b87364c
commit 4975536d5e
7 changed files with 106 additions and 11 deletions

BIN
snarkfiles/semaphore.r1cs Normal file

Binary file not shown.

BIN
snarkfiles/semaphore.wasm Normal file

Binary file not shown.

View File

@ -1,4 +1,5 @@
use ethers::types::U256;
use num_bigint::{BigInt, Sign};
use serde::{
de::{Error as DeError, Visitor},
ser::Error as _,
@ -55,6 +56,28 @@ impl From<U256> for Hash {
}
}
/// Conversion from vec
impl From<Vec<u8>> for Hash {
fn from(vec: Vec<u8>) -> Self {
let mut bytes = [0_u8; 32];
bytes.copy_from_slice(&vec[0..32]);
Self::from_bytes_be(bytes)
}
}
/// Conversion to BigInt
impl From<Hash> for BigInt {
fn from(hash: Hash) -> Self {
Self::from_bytes_be(Sign::Plus, hash.as_bytes_be())
}
}
impl From<&Hash> for BigInt {
fn from(hash: &Hash) -> Self {
Self::from_bytes_be(Sign::Plus, hash.as_bytes_be())
}
}
/// Parse Hash from hex string.
/// Hex strings can be upper/lower/mixed case and have an optional `0x` prefix
/// but they must always be exactly 32 bytes.

View File

@ -32,8 +32,8 @@ fn fr_to_bigint(fr: Fr) -> BigInt {
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Identity {
identity_trapdoor: BigInt,
identity_nullifier: BigInt,
pub identity_trapdoor: BigInt,
pub identity_nullifier: BigInt,
}
// todo: improve

View File

@ -4,9 +4,10 @@ mod merkle_tree;
mod poseidon_tree;
mod hash;
use num_bigint::BigInt;
use poseidon_rs::Poseidon;
use hex_literal::hex;
use {identity::*, poseidon_tree::*, hash::*};
use {identity::*, poseidon_tree::*, hash::*, proof::*};
fn main() {
@ -21,12 +22,25 @@ fn main() {
));
let mut tree = PoseidonTree::new(3, LEAF);
tree.set(0, id.identity_commitment_leaf());
dbg!(tree.root());
let (_, leaf) = id.identity_commitment().to_bytes_be();
dbg!(&leaf);
tree.set(0, leaf.into());
let root: BigInt = tree.root().into();
dbg!(root);
let proof = tree.proof(0).expect("proof should exist");
dbg!(proof);
// let proof: Vec<BigInt> = proof.0.iter().map(|x| {
// match x {
// Branch::Left(value) => value.into(),
// Branch::Right(value) => value.into(),
// }
// }).collect();
// dbg!(proof);
proof_signal(&id, &proof);
}

View File

@ -9,6 +9,7 @@ use std::{
iter::{once, repeat, successors},
};
use num_bigint::{BigInt, Sign};
use serde::Serialize;
/// Hash types, values and algorithms for a Merkle tree

View File

@ -2,15 +2,42 @@ use ark_circom::{CircomConfig, CircomBuilder};
use ark_std::rand::thread_rng;
use ark_bn254::Bn254;
use color_eyre::Result;
use num_bigint::BigInt;
use crate::identity::*;
use crate::{identity::*, poseidon_tree::{Proof}, merkle_tree::Branch};
use ark_groth16::{
create_random_proof as prove, generate_random_parameters, prepare_verifying_key, verify_proof,
};
// fn to_array32(s: &BigInt, size: usize) -> Vec<i32> {
// let mut res = vec![0; size as usize];
// let mut rem = s.clone();
// let radix = BigInt::from(0x100000000u64);
// let mut c = size - 1;
// while !rem.is_zero() {
// !dbg(&rem);
// !dbg(&radix);
// !dbg((&rem % &radix));
// res[c] = (&rem % &radix).to_i32().unwrap();
// rem /= &radix;
// c -= 1;
// }
// res
// }
// WIP: uses dummy proofs for now
fn proof_signal(identity: Identity) -> Result<()> {
pub fn proof_signal(identity: &Identity, proof: &Proof) -> Result<()> {
// TODO: we should create a From trait for this
let proof = proof.0.iter().map(|x| {
match x {
Branch::Left(value) => value.into(),
Branch::Right(value) => value.into(),
}
}).collect::<Vec<BigInt>>();
let cfg = CircomConfig::<Bn254>::new(
"./snarkfiles/circom2_multiplier2.wasm",
"./snarkfiles/circom2_multiplier2.r1cs",
@ -24,8 +51,38 @@ fn proof_signal(identity: Identity) -> Result<()> {
// signal_hash: shouldHash ? genSignalHash(signal) : signal
let mut builder = CircomBuilder::new(cfg);
// builder.push_input("a", 3);
// builder.push_input("b", 11);
let tmp = BigInt::parse_bytes(
b"4344141139294650952352150677542411196253771789435022697920397562624821372579",
10,
)
.unwrap();
builder.push_input("identity_nullifier", tmp);
// dbg!(&tmp % BigInt::from(0x100000000u64));
// builder.push_input("identity_trapdoor", BigInt::parse_bytes(
// b"57215223214535428002775309386374815284773502419290683020798284477163412139477",
// 10,
// )
// .unwrap());
// // TODO: calculate vec
// builder.push_input("identity_path_index", BigInt::from(0 as i32));
// builder.push_input("identity_path_index", BigInt::from(0 as i32));
// for el in proof {
// builder.push_input("path_elements", el);
// }
// builder.push_input("external_nullifier", BigInt::from(123 as i32));
// builder.push_input("signal_hash", BigInt::parse_bytes(
// b"426814738191208581806614072441429636075448095566621754358249936829881365458n",
// 10,
// )
// .unwrap());
// builder.push_input("nullifierHash", BigInt::from(0 as i32));
// builder.push_input("root", BigInt::from(0 as i32));
// create an empty instance for setting it up
let circom = builder.setup();