use crate::easycodec::*;
use crate::sim_context::*;
const CONTRACT_METHOD_GET_BULLETPROOFS_RESULT: &str = "GetBulletproofsResult";
const CONTRACT_METHOD_GET_BULLETPROOFS_RESULT_LEN: &str = "GetBulletproofsResultLen";
const BULLETPROOFS_OPERATION_TYPE_PEDERSEN_ADD_NUM: &str = "PedersenAddNum";
const BULLETPROOFS_OPERATION_TYPE_PEDERSEN_ADD_COMMITMENT: &str = "PedersenAddCommitment";
const BULLETPROOFS_OPERATION_TYPE_PEDERSEN_SUB_NUM: &str = "PedersenSubNum";
const BULLETPROOFS_OPERATION_TYPE_PEDERSEN_SUB_COMMITMENT: &str = "PedersenSubCommitment";
const BULLETPROOFS_OPERATION_TYPE_PEDERSEN_MUL_NUM: &str = "PedersenMulNum";
const BULLETPROOFS_VERIFY: &str = "BulletproofsVerify";
pub trait BulletproofsSimContext {
fn pedersen_add_num(&self, commitment: Vec<u8>, num: &str) -> Result<Vec<u8>, result_code>;
fn pedersen_add_commitment(
&self,
commitment1: Vec<u8>,
commitment2: Vec<u8>,
) -> Result<Vec<u8>, result_code>;
fn pedersen_sub_num(&self, commitment: Vec<u8>, num: &str) -> Result<Vec<u8>, result_code>;
fn pedersen_sub_commitment(
&self,
commitment1: Vec<u8>,
commitment2: Vec<u8>,
) -> Result<Vec<u8>, result_code>;
fn pedersen_mul_num(&self, commitment: Vec<u8>, num: &str) -> Result<Vec<u8>, result_code>;
fn verify(&self, proof: Vec<u8>, commitment: Vec<u8>) -> Result<Vec<u8>, result_code>;
}
pub struct BulletproofsSimContextImpl {
pub common: CommonUtils,
}
impl BulletproofsSimContextImpl {
pub fn new(ctx_ptr: result_code) -> BulletproofsSimContextImpl {
BulletproofsSimContextImpl {
common: CommonUtils { ctx_ptr },
}
}
}
impl BulletproofsSimContext for BulletproofsSimContextImpl {
fn pedersen_add_num(&self, commitment: Vec<u8>, num: &str) -> Result<Vec<u8>, result_code> {
bulletproofs_operation(
self,
commitment,
num.to_string().into_bytes(),
BULLETPROOFS_OPERATION_TYPE_PEDERSEN_ADD_NUM,
)
}
fn pedersen_add_commitment(
&self,
commitment1: Vec<u8>,
commitment2: Vec<u8>,
) -> Result<Vec<u8>, result_code> {
bulletproofs_operation(
self,
commitment1,
commitment2,
BULLETPROOFS_OPERATION_TYPE_PEDERSEN_ADD_COMMITMENT,
)
}
fn pedersen_sub_num(&self, commitment: Vec<u8>, num: &str) -> Result<Vec<u8>, result_code> {
bulletproofs_operation(
self,
commitment,
num.to_string().into_bytes(),
BULLETPROOFS_OPERATION_TYPE_PEDERSEN_SUB_NUM,
)
}
fn pedersen_sub_commitment(
&self,
commitment1: Vec<u8>,
commitment2: Vec<u8>,
) -> Result<Vec<u8>, result_code> {
bulletproofs_operation(
self,
commitment1,
commitment2,
BULLETPROOFS_OPERATION_TYPE_PEDERSEN_SUB_COMMITMENT,
)
}
fn pedersen_mul_num(&self, commitment: Vec<u8>, num: &str) -> Result<Vec<u8>, result_code> {
bulletproofs_operation(
self,
commitment,
num.to_string().into_bytes(),
BULLETPROOFS_OPERATION_TYPE_PEDERSEN_MUL_NUM,
)
}
fn verify(&self, proof: Vec<u8>, commitment: Vec<u8>) -> Result<Vec<u8>, result_code> {
bulletproofs_operation(self, proof, commitment, BULLETPROOFS_VERIFY)
}
}
fn bulletproofs_operation(
bulletproofs_sim_context: &BulletproofsSimContextImpl,
param1: Vec<u8>,
param2: Vec<u8>,
bulletproofs_func_name: &str,
) -> Result<Vec<u8>, result_code> {
let ec = &mut EasyCodec::new();
ec.add_bytes("param1", param1);
ec.add_bytes("param2", param2);
ec.add_string("bulletproofsFuncName", bulletproofs_func_name);
bulletproofs_sim_context.common.get_bytes_from_chain(
ec,
CONTRACT_METHOD_GET_BULLETPROOFS_RESULT_LEN,
CONTRACT_METHOD_GET_BULLETPROOFS_RESULT,
)
}