Skip to content

Commit

Permalink
test: Adds suite Schnorr verify differential fuzz test
Browse files Browse the repository at this point in the history
  • Loading branch information
pmerkleplant committed Aug 13, 2024
1 parent 840bda2 commit c63457d
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 13 deletions.
34 changes: 34 additions & 0 deletions script/libs/LibOracleSuite.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ pragma solidity ^0.8.16;

import {Vm} from "forge-std/Vm.sol";

import {LibSecp256k1} from "src/libs/LibSecp256k1.sol";

/**
* @title LibOracleSuite
*
Expand Down Expand Up @@ -42,6 +44,38 @@ library LibOracleSuite {
return (signature, commitment);
}

/// @dev Verifies public key `pubKey` signs via `signature` and `commitment`
/// message `message`.
///
/// Verified via:
/// ```bash
/// $ ./bin/schnorr verify \
/// <message> \
/// <pubKey.x> \
/// <pubKey.y> \
/// <signature> \
/// <commitment>
/// ```
function verify(
LibSecp256k1.Point memory pubKey,
bytes32 message,
bytes32 signature,
address commitment
) internal returns (bool) {
string[] memory inputs = new string[](7);
inputs[0] = "bin/schnorr";
inputs[1] = "verify";
inputs[2] = vm.toString(message);
inputs[3] = vm.toString(pubKey.x);
inputs[4] = vm.toString(pubKey.y);
inputs[5] = vm.toString(signature);
inputs[6] = vm.toString(commitment);

uint result = abi.decode(vm.ffi(inputs), (uint));

return result == 1;
}

/// @dev Constructs poke message for `wat` with value `val` and age `age`.
///
/// Constructed via:
Expand Down
51 changes: 38 additions & 13 deletions test/LibSchnorrTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,46 @@ abstract contract LibSchnorrTest is Test {
}

// Create signature via oracle-suite.
uint signature;
address commitment;
(signature, commitment) = LibOracleSuite.sign(privKeys, message);

// IMPORTANT: Don't do anything if signature is invalid.
if (signature == 0) {
console2.log("Signature is zero");
return;
}
uint signatureSuite;
address commitmentSuite;
(signatureSuite, commitmentSuite) =
LibOracleSuite.sign(privKeys, message);

// Create signature via LibSchnorrExtended.
uint signatureLibSchnorr;
address commitmentLibSchnorr;
(signatureLibSchnorr, commitmentLibSchnorr) =
LibSchnorrExtended.signMessage(privKeys, message);

// Expect both signatures to be verifiable via LibSchnorr.
assertTrue(
LibSchnorr.verifySignature(
aggPubKey, message, bytes32(signatureSuite), commitmentSuite
)
);
assertTrue(
LibSchnorr.verifySignature(
aggPubKey,
message,
bytes32(signatureLibSchnorr),
commitmentLibSchnorr
)
);

// Expect oracle-suite's signature to be verifiable.
bool ok = LibSchnorr.verifySignature(
aggPubKey, message, bytes32(signature), commitment
// Expect both signatures to be verifiable via oracle-suite.
assertTrue(
LibOracleSuite.verify(
aggPubKey, message, bytes32(signatureSuite), commitmentSuite
)
);
assertTrue(
LibOracleSuite.verify(
aggPubKey,
message,
bytes32(signatureLibSchnorr),
commitmentLibSchnorr
)
);
assertTrue(ok);
}

function testFuzz_verifySignature_SingleSigner(
Expand Down

0 comments on commit c63457d

Please sign in to comment.