Skip to content

Commit

Permalink
Rename constants
Browse files Browse the repository at this point in the history
  • Loading branch information
r0qs committed Aug 10, 2022
1 parent c0f83b4 commit 6a5ea44
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 10 deletions.
6 changes: 3 additions & 3 deletions contracts/MerkleTreeWithHistory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface IHasher {
}

contract MerkleTreeWithHistory {
uint256 public constant FIELD_SIZE = 21888242871839275222246405745257275088548364400416034343698204186575808495617;
uint256 public constant SCALAR_FIELD_SIZE = 21888242871839275222246405745257275088548364400416034343698204186575808495617;
uint256 public constant ZERO_VALUE = 0;

IHasher public immutable hasher;
Expand Down Expand Up @@ -38,8 +38,8 @@ contract MerkleTreeWithHistory {
* @return Poseidon(_left, _right)
*/
function hashLeftRight(bytes32 _left, bytes32 _right) public view returns (bytes32) {
require(uint256(_left) < FIELD_SIZE, "_left should be inside the field");
require(uint256(_right) < FIELD_SIZE, "_right should be inside the field");
require(uint256(_left) < SCALAR_FIELD_SIZE, "_left should be inside the field");
require(uint256(_right) < SCALAR_FIELD_SIZE, "_right should be inside the field");
bytes32[2] memory input;
input[0] = _left;
input[1] = _right;
Expand Down
6 changes: 6 additions & 0 deletions contracts/Notary.sol
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ abstract contract Notary is MerkleTreeWithHistory {
pubSignals[0] = uint256(_root);
pubSignals[1] = uint256(_nullifierHash);
pubSignals[2] = uint256(uint160(msg.sender));

// Ensure that every input must be less than the snark scalar field
// This is already checked in the verifiers' code, but we are failing fast here before attempting to verify the proof.
for (uint256 i = 0; i < pubSignals.length; i++) {
require(pubSignals[i] < SCALAR_FIELD_SIZE, "Input too large");
}
require(verifier.verifyProof(_proof, pubSignals), "Invalid issuance proof");

nullifierHashes[_nullifierHash] = CredentialState(true, false);
Expand Down
4 changes: 2 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const { unstringifyBigInts } = require('ffjavascript').utils
// TODO: load from config
const MERKLE_TREE_HEIGHT = process.env.MERKLE_TREE_HEIGHT || 12
const ZERO_VALUE = process.env.ZERO_VALUE || 0
const FIELD_SIZE = BigNumber.from(
const SCALAR_FIELD_SIZE = BigNumber.from(
'21888242871839275222246405745257275088548364400416034343698204186575808495617',
)

Expand Down Expand Up @@ -80,7 +80,7 @@ function bitArrayToDecimal(array) {

module.exports = {
MERKLE_TREE_HEIGHT,
FIELD_SIZE,
SCALAR_FIELD_SIZE,
randomBN,
toFixedHex,
toBuffer,
Expand Down
6 changes: 3 additions & 3 deletions test/js/MerkleTreeWithHistory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { MerkleTree } = require('fixed-merkle-tree')
const { toFixedHex, deploy } = require('../../src/utils')
const Poseidon = require('../../src/poseidon')

const DEFAULT_ZERO_VALUE = 0
const ZERO_VALUE = 0
const MERKLE_TREE_HEIGHT = 5

describe('MerkleTreeWithHistory', function () {
Expand All @@ -24,7 +24,7 @@ describe('MerkleTreeWithHistory', function () {
return poseidonHash([a, b])
}

function getNewTree(tree_height = MERKLE_TREE_HEIGHT, zero = DEFAULT_ZERO_VALUE) {
function getNewTree(tree_height = MERKLE_TREE_HEIGHT, zero = ZERO_VALUE) {
return new MerkleTree(tree_height, [], { hashFunction: poseidonHash2, zeroElement: zero })
}

Expand Down Expand Up @@ -127,7 +127,7 @@ describe('MerkleTreeWithHistory', function () {
// initial subtree should be zero
let subtree0 = await mtContract.filledSubtrees(0)
let subtree1 = await mtContract.filledSubtrees(1)
expect(subtree0).to.equal(toFixedHex(DEFAULT_ZERO_VALUE))
expect(subtree0).to.equal(toFixedHex(ZERO_VALUE))
expect(subtree1).to.equal(toFixedHex(poseidonHash2(0, 0)))
let nextIdx = await mtContract.nextIndex()
expect(nextIdx).to.equal(0)
Expand Down
4 changes: 2 additions & 2 deletions test/js/Notary.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const { plonk } = require('snarkjs')
const { buildEddsa } = require('circomlibjs')
const { MerkleTree } = require('fixed-merkle-tree')
const {
FIELD_SIZE,
SCALAR_FIELD_SIZE,
deploy,
randomBN,
toFixedHex } = require('../../src/utils')
Expand Down Expand Up @@ -96,7 +96,7 @@ describe('PrivateNotary', function () {
expect(await pvtNotaryImpl.getLastRoot()).to.equal(toFixedHex(tree.root))
expect(await pvtNotaryImpl.levels()).to.equal(MERKLE_TREE_HEIGHT)
expect(await pvtNotaryImpl.levels()).to.equal(tree.levels)
expect(await pvtNotaryImpl.FIELD_SIZE()).to.equal(FIELD_SIZE)
expect(await pvtNotaryImpl.SCALAR_FIELD_SIZE()).to.equal(SCALAR_FIELD_SIZE)
expect(await pvtNotaryImpl.ZERO_VALUE()).to.equal(ZERO_VALUE)
})
})
Expand Down

0 comments on commit 6a5ea44

Please sign in to comment.