Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 3 additions & 1 deletion Nargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
[package]
name="noir_rln"
type="bin"
authors = [""]
compiler_version = "0.6.0"
compiler_version = ">=0.19.2"

[dependencies]
12 changes: 6 additions & 6 deletions src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ struct Output {

fn main(
identitySecret: Field,
userMessageLimit: Field,
messageId: Field,
pathSelectors: [Field; DEPTH],
userMessageLimit: u16,
messageId: u16,
pathSelectors: [u1; DEPTH],
pathElements: [Field; DEPTH],
x: pub Field,
externalNullifier: pub Field
) -> pub Output {
let identityCommitment = hash_1([identitySecret]);
let rateCommitment = hash_2([identityCommitment, userMessageLimit]);
let rateCommitment = hash_2([identityCommitment, userMessageLimit as Field]);

let root = merkle_proof(rateCommitment, pathSelectors, pathElements);

assert(messageId as u16 < userMessageLimit as u16);
assert(messageId < userMessageLimit, "messageId exceeds limit");

let a1 = hash_3([identitySecret, externalNullifier, messageId]);
let a1 = hash_3([identitySecret, externalNullifier, messageId as Field]);
let y = identitySecret + a1 * x;

let nullifier = hash_1([a1]);
Expand Down
30 changes: 9 additions & 21 deletions src/utils.nr
Original file line number Diff line number Diff line change
@@ -1,31 +1,19 @@
use dep::std::hash::poseidon::bn254::hash_2;

global DEPTH = 16;
global HASHES_LENGTH = 17;

fn merkle_proof<N>(leaf: Field, pathSelectors: [Field; N], pathElements: [Field; N]) -> Field {
let mut levelHashes = [0; HASHES_LENGTH];
levelHashes[0] = leaf;
pub fn merkle_proof<N>(leaf: Field, pathSelectors: [u1; N], pathElements: [Field; N]) -> Field {
let mut hash = leaf;

for i in 0..N {
assert(pathSelectors[i] * (pathSelectors[i] - 1) == 0);
let hash_input = if pathSelectors[i] == 1 {
[pathElements[i], hash]
} else {
[hash, pathElements[i]]
};

let leaves = mux(
[levelHashes[i], pathElements[i], pathElements[i], levelHashes[i]],
pathSelectors[i]
);

levelHashes[i + 1] = hash_2([leaves[0], leaves[1]]);
hash = hash_2(hash_input);
}

levelHashes[N]
}

fn mux(c: [Field; 4], s: Field) -> [Field; 2] {
let mut out: [Field; 2] = [0; 2];

out[0] = (c[1] - c[0]) * s + c[0];
out[1] = (c[3] - c[2]) * s + c[2];

out
hash
}