Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement many featuers for the co-brillig rep3 backend #284

Merged
merged 60 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
ed5e107
feat: started impl of rep3 brillig
0xThemis Nov 22, 2024
a13d740
feat: first unconstrained fn test working for rep3
0xThemis Nov 25, 2024
9d13a72
more operations
rw0x0 Nov 25, 2024
b6e2350
:
rw0x0 Nov 26, 2024
3fd89b3
.
rw0x0 Nov 26, 2024
6fd494e
.
rw0x0 Nov 26, 2024
dea4517
.
rw0x0 Nov 26, 2024
f43de7e
.
rw0x0 Nov 26, 2024
08bd4f0
.
rw0x0 Nov 26, 2024
ade8e1d
.
rw0x0 Nov 26, 2024
0d3a0cd
.
rw0x0 Nov 26, 2024
84dc448
.
rw0x0 Nov 26, 2024
486b500
.
rw0x0 Nov 26, 2024
73b081a
.
rw0x0 Nov 26, 2024
96e8742
.
rw0x0 Nov 26, 2024
ce88afe
.
rw0x0 Nov 26, 2024
4f1b7cc
.
rw0x0 Nov 26, 2024
e99781f
.
rw0x0 Nov 26, 2024
68d1bc4
.
rw0x0 Nov 26, 2024
aeb29a6
.
rw0x0 Nov 26, 2024
eecc966
.
rw0x0 Nov 26, 2024
9e2a0f5
.
florin5f Nov 26, 2024
b57726a
.
rw0x0 Nov 26, 2024
1e60c41
.
rw0x0 Nov 26, 2024
2988c04
update the plain to_radix function
rw0x0 Nov 26, 2024
d103d92
.
rw0x0 Nov 26, 2024
9e18a81
.
rw0x0 Nov 26, 2024
f67109d
.
rw0x0 Nov 26, 2024
37f1db7
.
rw0x0 Nov 26, 2024
2dfde33
to_radix in mpc for some special case
rw0x0 Nov 26, 2024
cc02cfb
.
rw0x0 Nov 26, 2024
78bc87a
.
florin5f Nov 26, 2024
949d1b5
.
florin5f Nov 26, 2024
bd63ef1
.
rw0x0 Nov 26, 2024
d6607ea
.
rw0x0 Nov 26, 2024
6d4d723
comment
rw0x0 Nov 27, 2024
07c6227
a fix in to_radix, still not working
rw0x0 Nov 27, 2024
22bb08b
fix the field_to_ring decomposition binary circuit
rw0x0 Nov 27, 2024
8927688
.
rw0x0 Nov 27, 2024
7442f11
testcase for decomposing field to rings
rw0x0 Nov 27, 2024
247a248
fix the shr/shl and test for ring_div_pow_2
rw0x0 Nov 27, 2024
e5dd180
.
rw0x0 Nov 27, 2024
644dbf9
.
rw0x0 Nov 27, 2024
4768e99
.
rw0x0 Nov 27, 2024
9e902d4
..
florin5f Nov 27, 2024
c2f4e5c
.
florin5f Nov 27, 2024
938c234
.
rw0x0 Nov 27, 2024
997a587
.
rw0x0 Nov 27, 2024
481bfcc
optimize the field div
rw0x0 Nov 27, 2024
721a9b1
.
rw0x0 Nov 27, 2024
1b36497
.
rw0x0 Nov 27, 2024
15bea2f
.
rw0x0 Nov 27, 2024
8640333
.
rw0x0 Nov 27, 2024
6463eb1
.
rw0x0 Nov 27, 2024
b31a8f7
uncomment some tests
rw0x0 Nov 27, 2024
3a25a65
remove mut from sub
rw0x0 Nov 27, 2024
c8e9255
.
rw0x0 Nov 27, 2024
f0d9be1
.
rw0x0 Nov 27, 2024
ecbe468
.
rw0x0 Nov 27, 2024
58b289e
Merge branch 'main' into feat/co_brillig_rep3
dkales Nov 27, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions co-noir/co-acvm/src/mpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ pub trait NoirWitnessExtensionProtocol<F: PrimeField> {

type BrilligDriver: BrilligDriver<F>;

fn init_brillig_driver(&self) -> Self::BrilligDriver;
fn init_brillig_driver(&mut self) -> std::io::Result<Self::BrilligDriver>;

fn from_brillig_result(
fn parse_brillig_result(
&mut self,
brillig_result: Vec<<Self::BrilligDriver as BrilligDriver<F>>::BrilligType>,
) -> Vec<Self::AcvmType>;
) -> eyre::Result<Vec<Self::AcvmType>>;

/// Returns F::zero() as a ACVM-type. The default implementation uses the `Default` trait. If `Default` does not return 0, this function has to be overwritten.
fn public_zero() -> Self::AcvmType {
Expand Down
11 changes: 7 additions & 4 deletions co-noir/co-acvm/src/mpc/plain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@ impl<F: PrimeField> NoirWitnessExtensionProtocol<F> for PlainAcvmSolver<F> {

type BrilligDriver = PlainBrilligDriver<F>;

fn init_brillig_driver(&self) -> Self::BrilligDriver {
PlainBrilligDriver::default()
fn init_brillig_driver(&mut self) -> std::io::Result<Self::BrilligDriver> {
Ok(PlainBrilligDriver::default())
}

fn from_brillig_result(brillig_result: Vec<PlainBrilligType<F>>) -> Vec<Self::AcvmType> {
brillig_result.into_iter().map(|v| v.into_field()).collect()
fn parse_brillig_result(
&mut self,
brillig_result: Vec<PlainBrilligType<F>>,
) -> eyre::Result<Vec<Self::AcvmType>> {
Ok(brillig_result.into_iter().map(|v| v.into_field()).collect())
}

fn is_public_zero(a: &Self::AcvmType) -> bool {
Expand Down
38 changes: 28 additions & 10 deletions co-noir/co-acvm/src/mpc/rep3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,26 @@ impl<F: PrimeField> From<ArithmeticShare<F>> for Rep3AcvmType<F> {
}

impl<F: PrimeField> From<Rep3AcvmType<F>> for Rep3BrilligType<F> {
fn from(_val: Rep3AcvmType<F>) -> Self {
todo!()
fn from(val: Rep3AcvmType<F>) -> Self {
match val {
Rep3AcvmType::Public(public) => Rep3BrilligType::public_field(public),
Rep3AcvmType::Shared(share) => Rep3BrilligType::shared_field(share),
}
}
}

impl<F: PrimeField> From<Rep3BrilligType<F>> for Rep3AcvmType<F> {
fn from(_value: Rep3BrilligType<F>) -> Self {
todo!()
impl<F: PrimeField> Rep3AcvmType<F> {
fn from_brillig_type<N: Rep3Network>(
value: Rep3BrilligType<F>,
io_context: &mut IoContext<N>,
) -> eyre::Result<Self> {
match value {
Rep3BrilligType::Public(public) => Ok(Rep3AcvmType::Public(public.into_field())),
Rep3BrilligType::Shared(shared) => {
let shared = Rep3BrilligType::into_arithmetic_share(io_context, shared)?;
Ok(Rep3AcvmType::Shared(shared))
}
}
}
}

Expand All @@ -124,14 +136,20 @@ impl<F: PrimeField, N: Rep3Network> NoirWitnessExtensionProtocol<F> for Rep3Acvm

type AcvmType = Rep3AcvmType<F>;

type BrilligDriver = Rep3BrilligDriver<F>;
type BrilligDriver = Rep3BrilligDriver<F, N>;

fn init_brillig_driver(&self) -> Self::BrilligDriver {
Rep3BrilligDriver::default()
fn init_brillig_driver(&mut self) -> std::io::Result<Self::BrilligDriver> {
Ok(Rep3BrilligDriver::with_io_context(self.io_context.fork()?))
}

fn from_brillig_result(_brillig_result: Vec<Rep3BrilligType<F>>) -> Vec<Self::AcvmType> {
todo!()
fn parse_brillig_result(
&mut self,
brillig_result: Vec<Rep3BrilligType<F>>,
) -> eyre::Result<Vec<Self::AcvmType>> {
brillig_result
.into_iter()
.map(|value| Rep3AcvmType::from_brillig_type(value, &mut self.io_context))
.collect()
}

fn is_public_zero(a: &Self::AcvmType) -> bool {
Expand Down
32 changes: 23 additions & 9 deletions co-noir/co-acvm/src/mpc/shamir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,20 @@ impl<F: PrimeField> From<ShamirPrimeFieldShare<F>> for ShamirAcvmType<F> {
}

impl<F: PrimeField> From<ShamirAcvmType<F>> for ShamirBrilligType<F> {
fn from(_val: ShamirAcvmType<F>) -> Self {
todo!()
fn from(val: ShamirAcvmType<F>) -> Self {
match val {
ShamirAcvmType::Public(public) => ShamirBrilligType::from(public),
ShamirAcvmType::Shared(share) => ShamirBrilligType::Shared(share),
}
}
}

impl<F: PrimeField> From<ShamirBrilligType<F>> for ShamirAcvmType<F> {
fn from(_value: ShamirBrilligType<F>) -> Self {
todo!()
fn from(value: ShamirBrilligType<F>) -> Self {
match value {
ShamirBrilligType::Public(public) => ShamirAcvmType::Public(public.into_field()),
ShamirBrilligType::Shared(shared) => ShamirAcvmType::Shared(shared),
}
}
}

Expand All @@ -104,18 +110,26 @@ impl<F: PrimeField, N: ShamirNetwork> NoirWitnessExtensionProtocol<F> for Shamir

type AcvmType = ShamirAcvmType<F>;

type BrilligDriver = ShamirBrilligDriver<F>;
type BrilligDriver = ShamirBrilligDriver<F, N>;

fn init_brillig_driver(&self) -> Self::BrilligDriver {
todo!()
fn init_brillig_driver(&mut self) -> std::io::Result<Self::BrilligDriver> {
Ok(ShamirBrilligDriver::with_protocol(
self.protocol.fork_with_pairs(0)?, // TODO maybe have some pairs here
))
}

fn public_zero() -> Self::AcvmType {
Self::AcvmType::default()
}

fn from_brillig_result(_brillig_result: Vec<ShamirBrilligType<F>>) -> Vec<Self::AcvmType> {
todo!()
fn parse_brillig_result(
&mut self,
brillig_result: Vec<ShamirBrilligType<F>>,
) -> eyre::Result<Vec<Self::AcvmType>> {
Ok(brillig_result
.into_iter()
.map(ShamirAcvmType::from)
.collect())
}

fn is_public_zero(a: &Self::AcvmType) -> bool {
Expand Down
8 changes: 4 additions & 4 deletions co-noir/co-acvm/src/solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ where
}

pub fn new_bn254<P>(
driver: T,
mut driver: T,
compiled_program: ProgramArtifact,
prover_path: P,
) -> eyre::Result<Self>
Expand All @@ -164,7 +164,7 @@ where
witness_map[Self::DEFAULT_FUNCTION_INDEX] =
Self::read_abi_bn254(prover_path, &compiled_program.abi)?;
let brillig = CoBrilligVM::init(
driver.init_brillig_driver(),
driver.init_brillig_driver()?,
compiled_program.bytecode.unconstrained_functions,
);
Ok(Self {
Expand All @@ -185,7 +185,7 @@ where
}

pub fn new_bn254_with_witness(
driver: T,
mut driver: T,
compiled_program: ProgramArtifact,
witness: WitnessMap<T::AcvmType>,
) -> eyre::Result<Self> {
Expand All @@ -194,7 +194,7 @@ where
witness_map[Self::DEFAULT_FUNCTION_INDEX] = witness;

let brillig = CoBrilligVM::init(
driver.init_brillig_driver(),
driver.init_brillig_driver()?,
compiled_program.bytecode.unconstrained_functions,
);
Ok(Self {
Expand Down
3 changes: 2 additions & 1 deletion co-noir/co-acvm/src/solver/brillig_call_solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ where
BrilligInputs::MemoryArray(_) => todo!("memory array calldata TODO"),
}
}
let brillig_result = T::from_brillig_result(self.brillig.run(id, calldata)?);
let brillig_result =
T::parse_brillig_result(&mut self.driver, self.brillig.run(id, calldata)?)?;
self.fill_output(brillig_result, outputs);
Ok(())
}
Expand Down
1 change: 1 addition & 0 deletions co-noir/co-brillig/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ noirc-abi.workspace = true
noirc-artifacts.workspace = true
num-bigint.workspace = true
num-traits.workspace = true
rand.workspace = true
rayon.workspace = true
serde.workspace = true
thiserror.workspace = true
Expand Down
23 changes: 15 additions & 8 deletions co-noir/co-brillig/src/mpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ pub trait BrilligDriver<F: PrimeField> {
/// Casts the provided value to the provided bit size. This includes upcasts
/// and downcasts between integer types, but also between fields to integers
/// and vice verca.
fn cast(&self, src: Self::BrilligType, bit_size: BitSize) -> eyre::Result<Self::BrilligType>;
fn cast(
&mut self,
src: Self::BrilligType,
bit_size: BitSize,
) -> eyre::Result<Self::BrilligType>;

/// Tries to convert the provided value to a `usize`. Returns an error
/// if it is not possible (e.g., is a shared value).
Expand Down Expand Up @@ -80,7 +84,7 @@ pub trait BrilligDriver<F: PrimeField> {
/// This operation returns an error if the provided inputs
/// are not the same type.
fn sub(
&mut self,
&self,
lhs: Self::BrilligType,
rhs: Self::BrilligType,
) -> eyre::Result<Self::BrilligType>;
Expand Down Expand Up @@ -140,8 +144,11 @@ pub trait BrilligDriver<F: PrimeField> {
///
/// Similar to Noir's brillig-VM, this method compares fields
/// by casting them `to u128` and compares the integer values.
fn lt(&self, lhs: Self::BrilligType, rhs: Self::BrilligType)
-> eyre::Result<Self::BrilligType>;
fn lt(
&mut self,
lhs: Self::BrilligType,
rhs: Self::BrilligType,
) -> eyre::Result<Self::BrilligType>;

/// Checks whether `lhs <= rhs`. The result
/// is a brillig integer type with bit size 1 (BitSize = U1).
Expand All @@ -152,7 +159,7 @@ pub trait BrilligDriver<F: PrimeField> {
/// Similar to Noir's brillig-VM, this method compares fields
/// by casting them `to u128` and compares the integer values.
fn le(
&self,
&mut self,
lhs: Self::BrilligType,
rhs: Self::BrilligType,
) -> eyre::Result<Self::BrilligType> {
Expand All @@ -169,7 +176,7 @@ pub trait BrilligDriver<F: PrimeField> {
/// Similar to Noir's brillig-VM, this method compares fields
/// by casting them `to u128` and compares the integer values.
fn gt(
&self,
&mut self,
lhs: Self::BrilligType,
rhs: Self::BrilligType,
) -> eyre::Result<Self::BrilligType> {
Expand All @@ -186,7 +193,7 @@ pub trait BrilligDriver<F: PrimeField> {
/// Similar to Noir's brillig-VM, this method compares fields
/// by casting them `to u128` and compares the integer values.
fn ge(
&self,
&mut self,
lhs: Self::BrilligType,
rhs: Self::BrilligType,
) -> eyre::Result<Self::BrilligType> {
Expand All @@ -209,7 +216,7 @@ pub trait BrilligDriver<F: PrimeField> {
/// Similar to Noir's brillig-VM, this method compares fields
/// by casting them to `u128` and compares the integer values.
fn to_radix(
&self,
&mut self,
val: Self::BrilligType,
radix: Self::BrilligType,
output_size: usize,
Expand Down
Loading