Skip to content

Commit

Permalink
Merge branch 'master' into backend-interface
Browse files Browse the repository at this point in the history
* master:
  chore(noir_js)!: Rename inner and outer proof methods (#2845)
  chore: remove unnecessary `AcirValue`s (#2823)
  • Loading branch information
TomAFrench committed Sep 26, 2023
2 parents 3c43507 + 71dbbb8 commit 105d68c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 54 deletions.
16 changes: 4 additions & 12 deletions compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,13 +285,12 @@ impl AcirContext {

// Compute the inverse with brillig code
let inverse_code = brillig_directive::directive_invert();
let field_type = AcirType::NumericType(NumericType::NativeField);

let results = self.brillig(
predicate,
inverse_code,
vec![AcirValue::Var(var, field_type.clone())],
vec![field_type],
vec![AcirValue::Var(var, AcirType::field())],
vec![AcirType::field()],
)?;
let inverted_var = Self::expect_one_var(results);

Expand Down Expand Up @@ -917,17 +916,10 @@ impl AcirContext {
AcirValue::DynamicArray(AcirDynamicArray { block_id, len }) => {
for i in 0..len {
// We generate witnesses corresponding to the array values
let index = AcirValue::Var(
self.add_constant(FieldElement::from(i as u128)),
AcirType::NumericType(NumericType::NativeField),
);
let index_var = index.into_var()?;
let index_var = self.add_constant(FieldElement::from(i as u128));

let value_read_var = self.read_from_memory(block_id, &index_var)?;
let value_read = AcirValue::Var(
value_read_var,
AcirType::NumericType(NumericType::NativeField),
);
let value_read = AcirValue::Var(value_read_var, AcirType::field());

self.brillig_array_input(var_expressions, value_read)?;
}
Expand Down
32 changes: 8 additions & 24 deletions compiler/noirc_evaluator/src/ssa/acir_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,11 +400,8 @@ impl Context {

let mut read_dynamic_array_index =
|block_id: BlockId, array_index: usize| -> Result<AcirVar, InternalError> {
let index = AcirValue::Var(
self.acir_context.add_constant(FieldElement::from(array_index as u128)),
AcirType::NumericType(NumericType::NativeField),
);
let index_var = index.into_var()?;
let index_var =
self.acir_context.add_constant(FieldElement::from(array_index as u128));

self.acir_context.read_from_memory(block_id, &index_var)
};
Expand Down Expand Up @@ -896,16 +893,10 @@ impl Context {
// Initialize the new array with the values from the old array
result_block_id = self.block_id(result_id);
let init_values = try_vecmap(0..array_len, |i| {
let index = AcirValue::Var(
self.acir_context.add_constant(FieldElement::from(i as u128)),
AcirType::NumericType(NumericType::NativeField),
);
let var = index.into_var()?;
let read = self.acir_context.read_from_memory(block_id, &var)?;
Ok::<AcirValue, RuntimeError>(AcirValue::Var(
read,
AcirType::NumericType(NumericType::NativeField),
))
let index_var = self.acir_context.add_constant(FieldElement::from(i as u128));

let read = self.acir_context.read_from_memory(block_id, &index_var)?;
Ok::<AcirValue, RuntimeError>(AcirValue::Var(read, AcirType::field()))
})?;
self.initialize_array(
result_block_id,
Expand Down Expand Up @@ -1609,18 +1600,11 @@ impl Context {
AcirValue::DynamicArray(AcirDynamicArray { block_id, len }) => {
for i in 0..len {
// We generate witnesses corresponding to the array values
let index = AcirValue::Var(
self.acir_context.add_constant(FieldElement::from(i as u128)),
AcirType::NumericType(NumericType::NativeField),
);
let index_var = self.acir_context.add_constant(FieldElement::from(i as u128));

let index_var = index.into_var()?;
let value_read_var =
self.acir_context.read_from_memory(block_id, &index_var)?;
let value_read = AcirValue::Var(
value_read_var,
AcirType::NumericType(NumericType::NativeField),
);
let value_read = AcirValue::Var(value_read_var, AcirType::field());

old_slice.push_back(value_read);
}
Expand Down
8 changes: 4 additions & 4 deletions tooling/noir_js/test/backend/backend_interface.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
export interface Backend {
// Generate an outer proof. This is the proof for the circuit which will verify
// inner proofs and or can be seen as the proof created for regular circuits.
generateOuterProof(decompressedWitness: Uint8Array): Promise<Uint8Array>;
generateFinalProof(decompressedWitness: Uint8Array): Promise<Uint8Array>;

// Generates an inner proof. This is the proof that will be verified
// in another circuit.
generateInnerProof(decompressedWitness: Uint8Array): Promise<Uint8Array>;
generateIntermediateProof(decompressedWitness: Uint8Array): Promise<Uint8Array>;

verifyOuterProof(proof: Uint8Array): Promise<boolean>;
verifyFinalProof(proof: Uint8Array): Promise<boolean>;

verifyInnerProof(proof: Uint8Array): Promise<boolean>;
verifyIntermediateProof(proof: Uint8Array): Promise<boolean>;
}
10 changes: 5 additions & 5 deletions tooling/noir_js/test/backend/barretenberg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class BarretenbergBackend implements Backend {
//
// The settings for this proof are the same as the settings for a "normal" proof
// ie one that is not in the recursive setting.
async generateOuterProof(decompressedWitness: Uint8Array): Promise<Uint8Array> {
async generateFinalProof(decompressedWitness: Uint8Array): Promise<Uint8Array> {
const makeEasyToVerifyInCircuit = false;
return this.generateProof(decompressedWitness, makeEasyToVerifyInCircuit);
}
Expand All @@ -60,7 +60,7 @@ export class BarretenbergBackend implements Backend {
// We set `makeEasyToVerifyInCircuit` to true, which will tell the backend to
// generate the proof using components that will make the proof
// easier to verify in a circuit.
async generateInnerProof(witness: Uint8Array): Promise<Uint8Array> {
async generateIntermediateProof(witness: Uint8Array): Promise<Uint8Array> {
const makeEasyToVerifyInCircuit = true;
return this.generateProof(witness, makeEasyToVerifyInCircuit);
}
Expand All @@ -85,7 +85,7 @@ export class BarretenbergBackend implements Backend {
// method.
//
// The number of public inputs denotes how many public inputs are in the inner proof.
async generateInnerProofArtifacts(
async generateIntermediateProofArtifacts(
proof: Uint8Array,
numOfPublicInputs = 0,
): Promise<{
Expand All @@ -109,13 +109,13 @@ export class BarretenbergBackend implements Backend {
};
}

async verifyOuterProof(proof: Uint8Array): Promise<boolean> {
async verifyFinalProof(proof: Uint8Array): Promise<boolean> {
const makeEasyToVerifyInCircuit = false;
const verified = await this.verifyProof(proof, makeEasyToVerifyInCircuit);
return verified;
}

async verifyInnerProof(proof: Uint8Array): Promise<boolean> {
async verifyIntermediateProof(proof: Uint8Array): Promise<boolean> {
const makeEasyToVerifyInCircuit = true;
return this.verifyProof(proof, makeEasyToVerifyInCircuit);
}
Expand Down
18 changes: 9 additions & 9 deletions tooling/noir_js/test/node/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ it('end-to-end proof creation and verification (outer)', async () => {
// Proof creation
const prover = new Backend(assert_lt_json.bytecode);
await prover.init();
const proof = await prover.generateOuterProof(serializedWitness);
const proof = await prover.generateFinalProof(serializedWitness);

// Proof verification
const isValid = await prover.verifyOuterProof(proof);
const isValid = await prover.verifyFinalProof(proof);
expect(isValid).to.be.true;
});

Expand All @@ -36,10 +36,10 @@ it('end-to-end proof creation and verification (inner)', async () => {
// Proof creation
const prover = new Backend(assert_lt_json.bytecode);
await prover.init();
const proof = await prover.generateInnerProof(serializedWitness);
const proof = await prover.generateIntermediateProof(serializedWitness);

// Proof verification
const isValid = await prover.verifyInnerProof(proof);
const isValid = await prover.verifyIntermediateProof(proof);
expect(isValid).to.be.true;
});

Expand Down Expand Up @@ -67,12 +67,12 @@ it('[BUG] -- bb.js null function or function signature mismatch (different insta
const prover = new Backend(assert_lt_json.bytecode);
await prover.init();

const proof = await prover.generateOuterProof(serializedWitness);
const proof = await prover.generateFinalProof(serializedWitness);

try {
const verifier = new Backend(assert_lt_json.bytecode);
await verifier.init();
await verifier.verifyOuterProof(proof);
await verifier.verifyFinalProof(proof);
expect.fail(
'bb.js currently returns a bug when we try to verify a proof with a different Barretenberg instance that created it.',
);
Expand Down Expand Up @@ -105,13 +105,13 @@ it('[BUG] -- bb.js null function or function signature mismatch (outer-inner) ',
await prover.init();
// Create a proof using both proving systems, the majority of the time
// one would only use outer proofs.
const proofOuter = await prover.generateOuterProof(serializedWitness);
const _proofInner = await prover.generateInnerProof(serializedWitness);
const proofOuter = await prover.generateFinalProof(serializedWitness);
const _proofInner = await prover.generateIntermediateProof(serializedWitness);

// Proof verification
//
try {
const isValidOuter = await prover.verifyOuterProof(proofOuter);
const isValidOuter = await prover.verifyFinalProof(proofOuter);
expect(isValidOuter).to.be.true;
// We can also try verifying an inner proof and it will fail.
// const isValidInner = await prover.verifyInnerProof(_proofInner);
Expand Down

0 comments on commit 105d68c

Please sign in to comment.