Skip to content

Commit

Permalink
remove println
Browse files Browse the repository at this point in the history
  • Loading branch information
alinat committed Oct 11, 2024
1 parent c88207a commit 6b9e4d4
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 143 deletions.
7 changes: 0 additions & 7 deletions tvm_vm/src/executor/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,6 @@ fn preprocess_signed_data<'a>(_engine: &Engine, data: &'a [u8]) -> Cow<'a, [u8]>
}

fn check_signature(engine: &mut Engine, name: &'static str, hash: bool) -> Status {
let start = Instant::now();

engine.load_instruction(Instruction::new(name))?;
fetch_stack(engine, 3)?;
let pub_key = engine
Expand Down Expand Up @@ -173,12 +171,7 @@ fn check_signature(engine: &mut Engine, name: &'static str, hash: bool) -> Statu
#[cfg(not(feature = "signature_no_check"))]
let result = pub_key.verify(&data, &signature).is_ok();

let duration = start.elapsed();

println!("Time elapsed by chcksign is: {:?}", duration);

engine.cc.stack.push(boolean!(result));
println!("%%%result: {:?}", result);
Ok(())
}

Expand Down
140 changes: 4 additions & 136 deletions tvm_vm/src/executor/zk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,8 +582,6 @@ fn my_test_pvk_1() -> PreparedVerifyingKey<Bn254> {
Bn254FqElementWrapper::from_str("1").unwrap(),
])
.unwrap();
// "16083174311393072332126484955039141051820368387551336007741432494536231879877",
// "11995344593741129498206341608147577676708407993917230939676252851997423446210",

let vk_beta_2 = g2_affine_from_str_projective(&vec![
vec![
Expand Down Expand Up @@ -612,14 +610,6 @@ fn my_test_pvk_1() -> PreparedVerifyingKey<Bn254> {
],
])
.unwrap();
// [
// "7017589137241388812217334676878160715759313595646525247042913539379033763831",
// "9588720105182136304988839277158105754318461657916765428451866781594135026063"
// ],
// [
// "2484424409632768920146683103978991861859052149379216050446911519906662584090",
// "3390288516800701266276631045627865236740814264026178914799455551851945389106"
// ]

let vk_gamma_2 = g2_affine_from_str_projective(&vec![
vec![
Expand Down Expand Up @@ -649,15 +639,6 @@ fn my_test_pvk_1() -> PreparedVerifyingKey<Bn254> {
])
.unwrap();

// [
// "10857046999023057135944570762232829481370756359578518086990519993285655852781",
// "11559732032986387107991004021392285783925812861821192530917403151452391805634"
// ],
// [
// "8495653923123431417604973247489272438418190587263600148770280649306958101930",
// "4082367875863433681332203403145435568316851327593401208105741076214120093531"
// ]

let vk_delta_2 = g2_affine_from_str_projective(&vec![
vec![
Bn254FqElementWrapper::from_str(
Expand Down Expand Up @@ -686,15 +667,6 @@ fn my_test_pvk_1() -> PreparedVerifyingKey<Bn254> {
])
.unwrap();

// [
// "10857046999023057135944570762232829481370756359578518086990519993285655852781",
// "11559732032986387107991004021392285783925812861821192530917403151452391805634"
// ],
// [
// "8495653923123431417604973247489272438418190587263600148770280649306958101930",
// "4082367875863433681332203403145435568316851327593401208105741076214120093531"
// ]

// Create a vector of G1Affine elements from the IC
let mut vk_gamma_abc_g1 = Vec::new();
for e in [
Expand Down Expand Up @@ -725,19 +697,6 @@ fn my_test_pvk_1() -> PreparedVerifyingKey<Bn254> {
vk_gamma_abc_g1.push(g1);
}

// [
// [
// "11760611693671517707466601638901224388668992590928868758649168369215563295744",
// "15842561259007247784907604255150260908812200067246900457940460682994649597353",
// "1"
// ],
// [
// "9960247968913608540350443520882802417817484595360267448450266543686043480996",
// "11040490439713280236989540698814598402024610465375008410116396264618122562865",
// "1"
// ]
// ]

let vk = VerifyingKey {
alpha_g1: vk_alpha_1,
beta_g2: vk_beta_2,
Expand All @@ -750,73 +709,19 @@ fn my_test_pvk_1() -> PreparedVerifyingKey<Bn254> {
PreparedVerifyingKey::from(vk)
}

// pub(crate) fn execute_vergrth16_new(engine: &mut Engine) -> Status {
// let start = Instant::now();
// engine.load_instruction(crate::executor::types::Instruction::new("
// VERGRTH16_NEW"))?; fetch_stack(engine, 3)?;
//
// let vk_index = engine.cmd.var(0).as_small_integer().unwrap() as u32;
// println!("from vergrth16 vk_index: {:?}", vk_index);
//
// let public_inputs_slice =
// SliceData::load_cell_ref(engine.cmd.var(1).as_cell()?)?;
// let public_inputs_as_bytes = unpack_data_from_cell(public_inputs_slice,
// engine)?; println!("from vergrth16 value public_inputs_as_bytes: {:?}",
// public_inputs_as_bytes);
//
// let proof_slice = SliceData::load_cell_ref(engine.cmd.var(2).as_cell()?)?;
// let proof_as_bytes = unpack_data_from_cell(proof_slice, engine)?;
// println!("from vergrth16 value proof_as_bytes: {:?}", proof_as_bytes);
//
// let proof = ProofWrapper::deserialize(&proof_as_bytes)?;
// let public_inputs =
// FieldElementWrapper::deserialize_vector(&public_inputs_as_bytes)?;
// let x: Vec<Fr> = public_inputs.iter().map(|x| x.0).collect();
//
// let vk = if vk_index == 0 {
// insecure_pvk()
// } else if vk_index == 1 {
// global_pvk()
// } else {
// my_test_pvk_1()
// };
//
// ZKP_VERIFYING_KEYS.get(&vk_index).unwrap();//&GLOBAL_VERIFYING_KEY;
// println!("vk data = {:?}", vk.alpha_g1_beta_g2.to_string());
// todo: add alternative for elliptic curve (may be we need bls curve also?),
// read from stack curve id
// let res = Groth16::<Bn254>::verify_with_processed_vk(&vk, &x, &proof.0)
// .map_err(|e| ZkCryptoError::GeneralError(e.to_string()));
//
// let duration = start.elapsed();
//
// println!("Time elapsed by vergrth16 is: {:?}", duration);
//
// let succes = res.is_ok();
// println!("res: {:?}", res);
// let res = if succes { boolean!(res.unwrap()) } else { boolean!(false) };
// println!("res: {:?}", res);
//
// engine.cc.stack.push(res);
//
// Ok(())
// }

pub(crate) fn execute_vergrth16(engine: &mut Engine) -> Status {
let start = Instant::now();
engine.load_instruction(crate::executor::types::Instruction::new("VERGRTH16"))?;
fetch_stack(engine, 3)?;

let vk_index = engine.cmd.var(0).as_small_integer().unwrap() as u32;
println!("from vergrth16 vk_index: {:?}", vk_index);

let public_inputs_slice = SliceData::load_cell_ref(engine.cmd.var(1).as_cell()?)?;
let public_inputs_as_bytes = unpack_data_from_cell(public_inputs_slice, engine)?;
println!("from vergrth16 value public_inputs_as_bytes: {:?}", public_inputs_as_bytes);

let proof_slice = SliceData::load_cell_ref(engine.cmd.var(2).as_cell()?)?;
let proof_as_bytes = unpack_data_from_cell(proof_slice, engine)?;
println!("from vergrth16 value proof_as_bytes: {:?}", proof_as_bytes);

let proof = ProofWrapper::deserialize(&proof_as_bytes)?;
let public_inputs = FieldElementWrapper::deserialize_vector(&public_inputs_as_bytes)?;
Expand All @@ -830,21 +735,14 @@ pub(crate) fn execute_vergrth16(engine: &mut Engine) -> Status {
my_test_pvk_1()
};

// ZKP_VERIFYING_KEYS.get(&vk_index).unwrap();//&GLOBAL_VERIFYING_KEY;
println!("vk data = {:?}", vk.alpha_g1_beta_g2.to_string());
// todo: add alternative for elliptic curve (may be we need bls curve also?),
// read from stack curve id
let res = Groth16::<Bn254>::verify_with_processed_vk(&vk, &x, &proof.0)
.map_err(|e| ZkCryptoError::GeneralError(e.to_string()));

let duration = start.elapsed();

println!("Time elapsed by vergrth16 is: {:?}", duration);

let succes = res.is_ok();
println!("res: {:?}", res);

let res = if succes { boolean!(res.unwrap()) } else { boolean!(false) };
println!("res: {:?}", res);

engine.cc.stack.push(res);

Expand All @@ -857,55 +755,37 @@ fn pop(barry: &[u8]) -> &[u8; 8] {

pub(crate) fn execute_poseidon_zk_login(engine: &mut Engine) -> Status {
engine.load_instruction(crate::executor::types::Instruction::new("POSEIDON"))?;
// fetch_stack(engine, 4);
fetch_stack(engine, 5)?;

let zkaddr_slice = SliceData::load_cell_ref(engine.cmd.var(0).as_cell()?)?;
let zkaddr = unpack_string_from_cell(zkaddr_slice, engine)?;
println!("from poseidon value zkaddr: {:?}", zkaddr);

// let epk_slice = SliceData::load_cell_ref(engine.cmd.var(1).as_cell()?)?;
// let epk_as_bytes = unpack_data_from_cell(epk_slice, engine)?;
// println!("from poseidon value epk_as_bytes: {:?}",
// hex::encode(epk_as_bytes.clone()));

let header_and_iss_base64_slice =
SliceData::load_cell_ref(engine.cmd.var(1 /* 2 */).as_cell()?)?;
let header_and_iss_base64 = unpack_string_from_cell(header_and_iss_base64_slice, engine)?;
println!("from poseidon value header_and_iss_base64: {:?}", header_and_iss_base64);

let modulus_slice = SliceData::load_cell_ref(engine.cmd.var(2 /* 3 */).as_cell()?)?;
let modulus = unpack_data_from_cell(modulus_slice, engine)?;
println!("from poseidon value modulus: {:?}", modulus);

let eph_pub_key = engine
.cmd
.var(3 /* 4 */)
.var(3 )
.as_integer()?
.as_builder::<UnsignedIntegerBigEndianEncoding>(/* PUBLIC_KEY_BITS */ 256)?;

let eph_pub_key_bytes = eph_pub_key.data();

println!("from poseidon value eph_pub_key_bytes: {:?}", eph_pub_key_bytes.len());
println!("from poseidon value eph_pub_key_bytes: {:?}", eph_pub_key_bytes);

let max_epoch_ = engine
.cmd
.var(4 /* 4 */)
.var(4 )
.as_integer()?
.as_builder::<UnsignedIntegerBigEndianEncoding>(/* PUBLIC_KEY_BITS */ 64)?;

let max_epoch_bytes = pop(max_epoch_.data());
let max_epoch = u64::from_be_bytes(*max_epoch_bytes);

println!("from poseidon value max_epoch: {:?}", max_epoch);

println!("from poseidon value max_epoch_bytes: {:?}", max_epoch_bytes.len());
println!("from poseidon value max_epoch_bytes: {:?}", max_epoch_bytes);

// let max_epoch = 10; //todo: read from stack later
// let max_epoch = 142;

let public_inputs = calculate_poseidon_hash(
&*zkaddr,
&*header_and_iss_base64,
Expand All @@ -917,9 +797,6 @@ pub(crate) fn execute_poseidon_zk_login(engine: &mut Engine) -> Status {

let mut public_inputs_as_bytes = vec![];
public_inputs.serialize_compressed(&mut public_inputs_as_bytes).unwrap();
println!("from poseidon public_inputs_as_bytes : {:?}", public_inputs_as_bytes.clone());
// println!("from poseidon public_inputs_as_bytes len : {:?}",
// public_inputs_as_bytes.len());

let public_inputs_cell = pack_data_to_cell(&public_inputs_as_bytes, &mut 0).unwrap();

Expand All @@ -935,10 +812,7 @@ pub fn calculate_poseidon_hash(
modulus: &[u8],
max_epoch: u64,
) -> Result<Bn254Fr, ZkCryptoError> /**/ {
// if header_base64.len() > MAX_HEADER_LEN as usize {
// return Err(ZkCryptoError::GeneralError("Header too long".to_string()));
// }


let address_seed = Bn254FrElement::from_str(address_seed).unwrap();
let addr_seed = (&address_seed).into();

Expand All @@ -949,19 +823,13 @@ pub fn calculate_poseidon_hash(
let v: Value = serde_json::from_str(header_and_iss_base64).unwrap();

let header_base64 = v["headerBase64"].as_str().unwrap();
println!("header_base64 {}", header_base64);

let iss_base64_details = v["issBase64Details"].as_object().unwrap();
println!("issBase64Details {:?}", iss_base64_details);

let index_mod_4 = iss_base64_details["indexMod4"].as_i64().unwrap().to_string();

println!("index_mod_4 {:?}", index_mod_4);

let iss_base64_details_value = iss_base64_details["value"].as_str().unwrap();

println!("iss_base64_details_value {:?}", iss_base64_details_value);

let index_mod_4_f = (&Bn254FrElement::from_str(&index_mod_4).unwrap()).into();

let iss_base64_f = hash_ascii_str_to_field(&iss_base64_details_value, MAX_ISS_LEN_B64).unwrap();
Expand Down

0 comments on commit 6b9e4d4

Please sign in to comment.