Skip to content

Commit

Permalink
added pow/mod opcodes (todo still for MPC)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xThemis committed Jun 6, 2024
1 parent d396ca6 commit f82c151
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 2 deletions.
11 changes: 11 additions & 0 deletions circom-mpc-vm/src/mpc_vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,17 @@ impl<P: Pairing, C: CircomWitnessExtensionProtocol<P::ScalarField>> Component<P,
let lhs = self.pop_field();
self.push_field(protocol.vm_div(lhs, rhs)?);
}
op_codes::MpcOpCode::Pow => {
let rhs = self.pop_field();
let lhs = self.pop_field();
self.push_field(protocol.vm_pow(lhs, rhs)?);
}
op_codes::MpcOpCode::Mod => {
let rhs = self.pop_field();
let lhs = self.pop_field();
self.push_field(protocol.vm_mod(lhs, rhs)?);
}

op_codes::MpcOpCode::Neg => {
let x = self.pop_field();
self.push_field(protocol.vm_neg(x));
Expand Down
4 changes: 4 additions & 0 deletions circom-mpc-vm/src/op_codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pub enum MpcOpCode {
Mul,
Div,
IntDiv,
Pow,
Mod,
Neg,
Lt,
Le,
Expand Down Expand Up @@ -67,6 +69,8 @@ impl std::fmt::Display for MpcOpCode {
MpcOpCode::Mul => "MUL_OP".to_owned(),
MpcOpCode::Div => "DIV_OP".to_owned(),
MpcOpCode::IntDiv => "INT_DIV_OP".to_owned(),
MpcOpCode::Pow => "POW_OP".to_owned(),
MpcOpCode::Mod => "MOD_OP".to_owned(),
MpcOpCode::Neg => "NEG_OP".to_owned(),
MpcOpCode::Lt => "LESS_THAN_OP".to_owned(),
MpcOpCode::Le => "LESS_EQ_OP".to_owned(),
Expand Down
8 changes: 8 additions & 0 deletions mpc-core/src/protocols/aby3/witness_extension_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,14 @@ impl<F: PrimeField, N: Aby3Network> CircomWitnessExtensionProtocol<F> for Aby3Pr
Self::VmType::div(self, a, b)
}

fn vm_pow(&mut self, _a: Self::VmType, _b: Self::VmType) -> Result<Self::VmType> {
todo!()
}

fn vm_mod(&mut self, _a: Self::VmType, _b: Self::VmType) -> Result<Self::VmType> {
todo!()
}

fn vm_int_div(&mut self, a: Self::VmType, b: Self::VmType) -> Result<Self::VmType> {
Self::VmType::int_div(self, a, b)
}
Expand Down
8 changes: 8 additions & 0 deletions mpc-core/src/protocols/gsz/witness_extension_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,14 @@ impl<F: PrimeField, N: GSZNetwork> CircomWitnessExtensionProtocol<F> for GSZProt
Self::VmType::div(self, a, b)
}

fn vm_pow(&mut self, _a: Self::VmType, _b: Self::VmType) -> Result<Self::VmType> {
todo!()
}

fn vm_mod(&mut self, _a: Self::VmType, _b: Self::VmType) -> Result<Self::VmType> {
todo!()
}

fn vm_int_div(&mut self, a: Self::VmType, b: Self::VmType) -> Result<Self::VmType> {
Self::VmType::int_div(self, a, b)
}
Expand Down
10 changes: 10 additions & 0 deletions mpc-core/src/protocols/plain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,16 @@ impl<F: PrimeField> CircomWitnessExtensionProtocol<F> for PlainDriver {
Ok(a / b)
}

fn vm_pow(&mut self, a: Self::VmType, b: Self::VmType) -> Result<Self::VmType> {
Ok(a.pow(b.into_bigint()))
}

fn vm_mod(&mut self, a: Self::VmType, b: Self::VmType) -> Result<Self::VmType> {
let a = to_bigint!(a);
let b = to_bigint!(b);
Ok(F::from(a % b))
}

fn vm_int_div(&mut self, a: Self::VmType, b: Self::VmType) -> Result<Self::VmType> {
let lhs = to_u64!(a);
let rhs = to_u64!(b);
Expand Down
7 changes: 5 additions & 2 deletions mpc-core/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,13 @@ pub trait CircomWitnessExtensionProtocol<F: PrimeField>: PrimeFieldMpcProtocol<F
fn vm_sub(&mut self, a: Self::VmType, b: Self::VmType) -> Self::VmType;
fn vm_mul(&mut self, a: Self::VmType, b: Self::VmType) -> Result<Self::VmType>;
fn vm_div(&mut self, a: Self::VmType, b: Self::VmType) -> Result<Self::VmType>;
fn vm_neg(&mut self, a: Self::VmType) -> Self::VmType;

fn vm_int_div(&mut self, a: Self::VmType, b: Self::VmType) -> Result<Self::VmType>;

fn vm_pow(&mut self, a: Self::VmType, b: Self::VmType) -> Result<Self::VmType>;
fn vm_mod(&mut self, a: Self::VmType, b: Self::VmType) -> Result<Self::VmType>;

fn vm_neg(&mut self, a: Self::VmType) -> Self::VmType;

fn vm_lt(&mut self, a: Self::VmType, b: Self::VmType) -> Self::VmType;
fn vm_le(&mut self, a: Self::VmType, b: Self::VmType) -> Self::VmType;
fn vm_gt(&mut self, a: Self::VmType, b: Self::VmType) -> Self::VmType;
Expand Down

0 comments on commit f82c151

Please sign in to comment.