Skip to content

Commit

Permalink
feat!: ready for noir 0.36.0
Browse files Browse the repository at this point in the history
  • Loading branch information
signorecello committed Nov 23, 2024
1 parent 37e2cd2 commit 0e2ef9e
Show file tree
Hide file tree
Showing 10 changed files with 467 additions and 456 deletions.
2 changes: 1 addition & 1 deletion packages/ecdh/src/globals.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Globals Edward Curves supported Baby JubJub
use dep::std::ec::consts::te::{baby_jubjub};
use dep::std::ec::consts::te::baby_jubjub;

global BJJ = baby_jubjub();
global G = BJJ.base8;
70 changes: 35 additions & 35 deletions packages/ecdh/tests/ecdh.test.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
import { BarretenbergBackend } from '@noir-lang/backend_barretenberg'
import { Noir } from '@noir-lang/noir_js'
import { ProofData } from '@noir-lang/types'
import { expect } from 'chai'
import { randomBytes } from 'crypto'
import { readFileSync } from 'fs'
import { resolve } from 'path'
import 'mocha'
import { BarretenbergBackend } from "@noir-lang/backend_barretenberg"
import { Noir } from "@noir-lang/noir_js"
import { ProofData } from "@noir-lang/types"
import { expect } from "chai"
import { randomBytes } from "crypto"
import { readFileSync } from "fs"
import { resolve } from "path"
import "mocha"

function generatePrivateKey(): Uint8Array {
return randomBytes(32)
return randomBytes(32)
}

describe('ECDH Circuit Tests', function() {
let noir: Noir
let backend: BarretenbergBackend
let correctProof: ProofData
describe("ECDH Circuit Tests", function () {
let noir: Noir
let backend: BarretenbergBackend
let correctProof: ProofData

beforeEach(async () => {
const circuitFile = readFileSync(resolve(__dirname, '../../../target/ecdh.json'), 'utf-8')
const circuit = JSON.parse(circuitFile)
backend = new BarretenbergBackend(circuit)
noir = new Noir(circuit, backend)
const pk1 = generatePrivateKey()
const pk2 = generatePrivateKey()
beforeEach(async () => {
const circuitFile = readFileSync(resolve(__dirname, "../../../target/ecdh.json"), "utf-8")
const circuit = JSON.parse(circuitFile)
backend = new BarretenbergBackend(circuit)
noir = new Noir(circuit, backend)
const pk1 = generatePrivateKey()
const pk2 = generatePrivateKey()

// Convert Uint8Array to regular arrays
const input = {
private_key1: Array.from(pk1),
private_key2: Array.from(pk2),
}
// Convert Uint8Array to regular arrays
const input = {
private_key1: Array.from(pk1),
private_key2: Array.from(pk2)
}

correctProof = await noir.generateProof(input)
})
correctProof = await noir.generateProof(input)
})

it('Should generate valid proof for correct input', async function() {
expect(correctProof.proof).to.be.instanceOf(Uint8Array)
})
it("Should generate valid proof for correct input", async function () {
expect(correctProof.proof).to.be.instanceOf(Uint8Array)
})

it('Should verify valid proof for correct input', async function() {
expect(correctProof).to.not.be.undefined // Ensure proof is generated
const verification = await noir.verifyProof(correctProof)
expect(verification).to.be.true
})
it("Should verify valid proof for correct input", async function () {
expect(correctProof).to.not.be.undefined // Ensure proof is generated
const verification = await noir.verifyProof(correctProof)
expect(verification).to.be.true
})
})
30 changes: 18 additions & 12 deletions packages/merkle-trees/src/lib.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod merkle;
// mod sparse_merkle;
mod sparse_merkle;

trait Calculator<T> {
fn calculate_root<let N: u32>(self, entry: T, indexes: Field, hash_path: [Field; N]) -> Field;
Expand All @@ -12,7 +12,7 @@ trait Calculator<T> {
}

trait SMT_Creator {
fn default(
pub fn default(
root: Field,
leaf_hasher: fn([Field; 3]) -> Field,
hasher: fn([Field; 2]) -> Field,
Expand All @@ -23,7 +23,7 @@ trait SMT_Creator {
* @param hasher The hash function that is used to hash the nodes of the tree
* @param root The root of the tree
*/
fn from(
pub fn from(
root: Field,
leaf_hasher: fn([Field; 3]) -> Field,
hasher: fn([Field; 2]) -> Field,
Expand All @@ -35,28 +35,28 @@ trait SMT_Creator {
* Creates a new Sparse Merkle Tree (SparseMerkleTree) instance.
* @param hasher The hash function that is used to hash the nodes of the tree
*/
fn new(leaf_hasher: fn([Field; 3]) -> Field, hasher: fn([Field; 2]) -> Field) -> Self {
pub fn new(leaf_hasher: fn([Field; 3]) -> Field, hasher: fn([Field; 2]) -> Field) -> Self {
Self::from(0, leaf_hasher, hasher)
}
}

trait MT_Creator {
fn default(root: Field, hasher: fn([Field; 2]) -> Field) -> Self;
pub fn default(root: Field, hasher: fn([Field; 2]) -> Field) -> Self;

/**
* Imports an existing Sparse Merkle Tree (SparseMerkleTree) instance.
* @param hasher The hash function that is used to hash the nodes of the tree
* @param root The root of the tree
*/
fn from(root: Field, hasher: fn([Field; 2]) -> Field) -> Self {
pub fn from(root: Field, hasher: fn([Field; 2]) -> Field) -> Self {
Self::default(root, hasher)
}

/**
* Creates a new Sparse Merkle Tree (SparseMerkleTree) instance.
* @param hasher The hash function that is used to hash the nodes of the tree
*/
fn new(hasher: fn([Field; 2]) -> Field) -> Self {
pub fn new(hasher: fn([Field; 2]) -> Field) -> Self {
Self::from(0, hasher)
}
}
Expand All @@ -67,7 +67,7 @@ trait MembershipProver<T, K, U> {
* @param leaf The leaf to prove
* @param path The hash path and indices
*/
fn membership<let N: u32>(self, entry: T, indexes: K, hash_path: [U; N]);
pub fn membership<let N: u32>(self, entry: T, indexes: K, hash_path: [U; N]);
}

trait NonMembershipProver {
Expand All @@ -79,7 +79,7 @@ trait NonMembershipProver {
* @param matching_entry Contains (key, value) of a matching entry
* @param siblings Contains array of siblings the matching_entry
*/
fn non_membership(
pub fn non_membership(
self,
entry: (Field, Field),
matching_entry: (Field, Field),
Expand All @@ -96,15 +96,15 @@ trait Modifier<T, K, U> {
* @param new_entry The new entry to prove addition
* @param siblings The siblings (and indices for MT proofs)
*/
fn add<let N: u32>(&mut self, new_entry: T, indexes: K, hash_path: [U; N]);
pub fn add<let N: u32>(&mut self, new_entry: T, indexes: K, hash_path: [U; N]);

/**
* Proves the deletion of an existing entry from a tree. Based on the siblings first does a membership proof
* of that existing entry and then calculates the new root (without the entry).
* @param entry Contains key and value of the to-be-deleted entry: (key, value)
* @param siblings The siblings (and indices for MT proofs)
*/
fn delete<let N: u32>(&mut self, entry: T, indexes: K, hash_path: [U; N]);
pub fn delete<let N: u32>(&mut self, entry: T, indexes: K, hash_path: [U; N]);

/**
* Proves the update of the value of an existing entry in a tree. Based on the siblings first does a membership proof
Expand All @@ -113,5 +113,11 @@ trait Modifier<T, K, U> {
* @param old_entry Contains key and value of the entry to be updated: (key, value)
* @param siblings The siblings (and indices for MT proofs)
*/
fn update<let N: u32>(&mut self, new_value: Field, old_entry: T, indexes: K, hash_path: [U; N]);
pub fn update<let N: u32>(
&mut self,
new_value: Field,
old_entry: T,
indexes: K,
hash_path: [U; N],
);
}
4 changes: 2 additions & 2 deletions packages/merkle-trees/src/merkle.nr
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::{MT_Creator, MembershipProver, Modifier};
use crate::{MembershipProver, Modifier, MT_Creator};
mod tests;
mod tree;

struct MerkleTree {
hasher: fn([Field; 2]) -> Field,
root: Field,
pub root: Field,
}

impl MT_Creator for MerkleTree {
Expand Down
4 changes: 2 additions & 2 deletions packages/merkle-trees/src/sparse_merkle.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{MembershipProver, Modifier, NonMembershipProver, SMT_Creator};
mod tests;
mod tree;

struct SparseMerkleTree {
pub struct SparseMerkleTree {
root: Field,
leaf_hasher: fn([Field; 3]) -> Field,
hasher: fn([Field; 2]) -> Field,
Expand Down Expand Up @@ -85,7 +85,7 @@ impl Modifier<(Field, Field), Field, Field> for SparseMerkleTree {
let key = old_entry.0;
let old_value = old_entry.1;
// both the old entry and new entry share the same key that is used to calculate the path
let path = key.to_be_bits();
let path: [u1; N] = key.to_be_bits();

// old_parent is a container to temporarily store the nodes that ultimately lead to the OLD root
let mut old_parent: Field = (self.leaf_hasher)([key, old_value, 1]);
Expand Down
6 changes: 3 additions & 3 deletions packages/merkle-trees/src/sparse_merkle/tests.nr
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// mod pedersen;
// mod poseidon;
// mod poseidon2;
mod pedersen;
mod poseidon;
mod poseidon2;
Loading

0 comments on commit 0e2ef9e

Please sign in to comment.