Skip to content

Commit

Permalink
feat: implement tool to compare output of upstream BB with our implem…
Browse files Browse the repository at this point in the history
…entation

---------

Co-authored-by: Roman Walch <[email protected]>
Co-authored-by: Daniel Kales <[email protected]>
  • Loading branch information
3 people authored Nov 20, 2024
1 parent f8cf39d commit 8af8540
Show file tree
Hide file tree
Showing 10 changed files with 483 additions and 12 deletions.
34 changes: 34 additions & 0 deletions co-noir/co-builder/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,40 @@ impl<P: Pairing> UltraCircuitBuilder<P> {

Ok((pk, vk))
}

pub fn create_keys_barretenberg(
self,
crs: ProverCrs<P>,
driver: &mut PlainAcvmSolver<P::ScalarField>,
) -> HonkProofResult<(ProvingKey<P>, VerifyingKeyBarretenberg<P>)> {
let contains_recursive_proof = self.contains_recursive_proof;
let recursive_proof_public_input_indices = self.recursive_proof_public_input_indices;

let pk = ProvingKey::create::<PlainAcvmSolver<_>>(self, crs, driver)?;
let circuit_size = pk.circuit_size;

let mut commitments = PrecomputedEntities::default();
for (des, src) in commitments
.iter_mut()
.zip(pk.polynomials.precomputed.iter())
{
let comm = Utils::commit(src.as_ref(), &pk.crs)?;
*des = P::G1Affine::from(comm);
}

// Create and return the VerifyingKey instance
let vk = VerifyingKeyBarretenberg {
circuit_size: circuit_size as u64,
log_circuit_size: Utils::get_msb64(circuit_size as u64) as u64,
num_public_inputs: pk.num_public_inputs as u64,
pub_inputs_offset: pk.pub_inputs_offset as u64,
contains_recursive_proof,
recursive_proof_public_input_indices,
commitments,
};

Ok((pk, vk))
}
}

pub struct GenericUltraCircuitBuilder<P: Pairing, T: NoirWitnessExtensionProtocol<P::ScalarField>> {
Expand Down
11 changes: 11 additions & 0 deletions co-noir/co-builder/src/crs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,14 @@ pub struct ProverCrs<P: Pairing> {
)]
pub monomials: Vec<P::G1Affine>,
}

impl<P: Pairing> Crs<P> {
pub fn split(self) -> (ProverCrs<P>, P::G2Affine) {
(
ProverCrs {
monomials: self.monomials,
},
self.g2_x,
)
}
}
14 changes: 7 additions & 7 deletions co-noir/co-builder/src/keys/verification_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ impl<P: Pairing> VerifyingKey<P> {
}

pub struct VerifyingKeyBarretenberg<P: Pairing> {
pub(crate) circuit_size: u64,
pub(crate) log_circuit_size: u64,
pub(crate) num_public_inputs: u64,
pub(crate) pub_inputs_offset: u64,
pub(crate) contains_recursive_proof: bool,
pub(crate) recursive_proof_public_input_indices: AggregationObjectPubInputIndices,
pub(crate) commitments: PrecomputedEntities<P::G1Affine>,
pub circuit_size: u64,
pub log_circuit_size: u64,
pub num_public_inputs: u64,
pub pub_inputs_offset: u64,
pub contains_recursive_proof: bool,
pub recursive_proof_public_input_indices: AggregationObjectPubInputIndices,
pub commitments: PrecomputedEntities<P::G1Affine>,
}

impl<P: HonkCurve<TranscriptFieldType>> VerifyingKeyBarretenberg<P> {
Expand Down
8 changes: 8 additions & 0 deletions co-noir/co-noir/examples/cleanup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,11 @@ find . -name "*.shared" -type f -delete
# delete public input file
rm -rf public_input.json
find . -name "verification_key" -type f -delete
# delete all bb proof files
find . -name "proof.bb*" -type f -delete
# delete all bb vk files
find . -name "vk.bb*" -type f -delete
# delete all proofs and vks in test_vectors files
cd test_vectors
find . -name "vk" -type f -delete
find . -name "proof" -type f -delete
131 changes: 131 additions & 0 deletions co-noir/co-noir/examples/compare.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
export CARGO_TERM_QUIET=true
BARRETENBERG_BINARY=~/.bb/bb ##specify the $BARRETENBERG_BINARY path here

NARGO_VERSION=0.38.0 ##specify the desired nargo version here
BARRETENBERG_VERSION=0.62.0 ##specify the desired barretenberg version here or use the corresponding one for this nargo version
PLAINDRIVER="../../../target/release/plaindriver"
exit_code=0

REMOVE_OUTPUT=1
PIPE=""
if [[ $REMOVE_OUTPUT -eq 1 ]];
then
PIPE=" > /dev/null 2>&1"
fi

# build the plaindriver binary
cargo build --release --bin plaindriver

## install noirup: curl -L https://raw.githubusercontent.com/noir-lang/noirup/main/install | bash
r=$(bash -c "nargo --version")
if [[ $r != "nargo version = $NARGO_VERSION"* ]];
then
bash -c "noirup -v ${NARGO_VERSION}"
fi

## use one of these two methods
## install bbup: curl -L bbup.dev | bash
# bash -c "bbup -nv 0.${NARGO_VERSION}.0"
r=$(bash -c "$BARRETENBERG_BINARY --version")
if [[ $r != "$BARRETENBERG_VERSION" ]];
then
bash -c "bbup -v ${BARRETENBERG_VERSION}"
fi

echo "Using nargo version $NARGO_VERSION"
echo "Using bb version $BARRETENBERG_VERSION"
echo ""

test_cases=("add3" "add3_assert" "add3u64" "mul3u64" "poseidon" "poseidon_input2")

run_proof_verification() {
local name=$1
local algorithm=$2

if [[ "$algorithm" == "poseidon" ]]; then
proof_file="proof.bb${BARRETENBERG_VERSION}.poseidon"
vk_file="vk.bb${BARRETENBERG_VERSION}.poseidon"
prove_command="prove_ultra_honk"
write_command="write_vk_ultra_honk"
verify_command="verify_ultra_honk"
else
proof_file="proof.bb${BARRETENBERG_VERSION}.keccak"
vk_file="vk.bb${BARRETENBERG_VERSION}.keccak"
prove_command="prove_ultra_keccak_honk"
write_command="write_vk_ultra_keccak_honk"
verify_command="verify_ultra_keccak_honk"
fi

echo "comparing" $name "with bb and $algorithm transcript"

bash -c "$BARRETENBERG_BINARY $prove_command -b test_vectors/${name}/target/${name}.json -w test_vectors/${name}/target/${name}.gz -o test_vectors/${name}/${proof_file} $PIPE"

diff test_vectors/${name}/${proof_file} test_vectors/${name}/proof
if [[ $? -ne 0 ]]; then
exit_code=1
echo "::error::$name diff check of proofs failed"
fi

bash -c "$BARRETENBERG_BINARY $write_command -b test_vectors/${name}/target/${name}.json -o test_vectors/${name}/${vk_file} $PIPE"

bash -c "$BARRETENBERG_BINARY $verify_command -p test_vectors/${name}/proof -k test_vectors/${name}/vk"
if [[ $? -ne 0 ]]; then
exit_code=1
echo "::error::$name verifying with bb, our proof and our key failed"
fi

bash -c "$BARRETENBERG_BINARY $verify_command -p test_vectors/${name}/proof -k test_vectors/${name}/${vk_file}"
if [[ $? -ne 0 ]]; then
exit_code=1
echo "::error::$name verifying with bb, our proof and their key failed"
fi

bash -c "$BARRETENBERG_BINARY $verify_command -p test_vectors/${name}/${proof_file} -k test_vectors/${name}/vk"
if [[ $? -ne 0 ]]; then
exit_code=1
echo "::error::$name verifying with bb, their proof and our key failed"
fi

bash -c "$BARRETENBERG_BINARY $verify_command -p test_vectors/${name}/${proof_file} -k test_vectors/${name}/${vk_file}"
if [[ $? -ne 0 ]]; then
exit_code=1
echo "::error::$name verifying with bb, their proof and their key failed"
fi
return $exit_code
}

# comparing works with all test scripts where "run_full_" is followed by the precise test case name
for f in "${test_cases[@]}"; do
echo "running ultrahonk example" $f

failed=0

# compile witnesses and bytecode with specified nargo version
echo "computing witnesses with nargo"
bash -c "(cd test_vectors/${f} && nargo execute) $PIPE"

# -e to exit on first error
bash -c "${PLAINDRIVER} --prover-crs test_vectors/bn254_g1.dat --verifier-crs test_vectors/bn254_g2.dat --input test_vectors/${f}/Prover.toml --circuit test_vectors/${f}/target/${f}.json --hasher POSEIDON --out-dir test_vectors/${f} $PIPE" || failed=1

if [ "$failed" -ne 0 ]
then
exit_code=1
echo "::error::" $f "failed"
fi
run_proof_verification "$f" "poseidon"
bash cleanup.sh

# -e to exit on first error
bash -c "${PLAINDRIVER} --prover-crs test_vectors/bn254_g1.dat --verifier-crs test_vectors/bn254_g2.dat --input test_vectors/${f}/Prover.toml --circuit test_vectors/${f}/target/${f}.json --hasher KECCAK --out-dir test_vectors/${f} $PIPE" || failed=1

if [ "$failed" -ne 0 ]
then
exit_code=1
echo "::error::" $f "failed"
fi
run_proof_verification "$f" "keccak"
bash cleanup.sh
echo ""
done

exit "$exit_code"
Loading

0 comments on commit 8af8540

Please sign in to comment.