-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: move to new BoundedVec Implementation
- Loading branch information
Showing
27 changed files
with
515 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,4 +26,4 @@ jobs: | |
types: | | ||
fix | ||
feat | ||
chore | ||
chore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,4 +19,4 @@ jobs: | |
with: | ||
token: ${{ secrets.GITHUB_TOKEN }} | ||
command: manifest | ||
release-type: simple | ||
release-type: simple |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
target | ||
target | ||
.vscode | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
".": "1.0.0" | ||
"lib": "1.0.0" | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[workspace] | ||
members = ["jwt_verify", "jwt_string_verify"] | ||
default-member = "jwt_verify" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[package] | ||
name = "jwt_string_verify" | ||
type = "bin" | ||
authors = ["Vedant Chainani <[email protected]>"] | ||
compiler_version = ">=0.36.0" | ||
|
||
[dependencies] | ||
noir_jwt = { path = "../../lib" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use noir_jwt::{types::{JWT, SecretKey}, verify_jwt}; | ||
|
||
fn main( | ||
jwt_string: BoundedVec<u8, 1133>, | ||
secret_key: SecretKey, | ||
header_length: u32, | ||
payload_length: u32, | ||
signature_length: u32, | ||
) -> pub bool { | ||
let jwt: JWT = JWT::from_vec(jwt_string, header_length, payload_length, signature_length); | ||
let result: bool = verify_jwt(jwt, secret_key); | ||
result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[package] | ||
name = "jwt_verify" | ||
type = "bin" | ||
authors = ["Vedant Chainani <[email protected]>"] | ||
compiler_version = ">=0.36.0" | ||
|
||
[dependencies] | ||
noir_jwt = { path = "../../lib" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
use noir_jwt::{types::{JWT, SecretKey}, verify_jwt}; | ||
|
||
fn main(jwt: JWT, secret_key: SecretKey) -> pub bool { | ||
let result = verify_jwt(jwt, secret_key); | ||
result | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"name": "js", | ||
"version": "1.0.0", | ||
"scripts": { | ||
"test": "jest" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"description": "", | ||
"jest": { | ||
"roots": [ | ||
"<rootDir>" | ||
], | ||
"testPathIgnorePatterns": [ | ||
"/dist/", | ||
"/node_modules/" | ||
], | ||
"testTimeout": 10000000000 | ||
}, | ||
"dependencies": { | ||
"@noir-lang/backend_barretenberg": "^0.36.0", | ||
"@noir-lang/noir_js": "^0.36.0", | ||
"@noir-lang/noirc_abi": "^0.36.0", | ||
"@types/jest": "^29.5.13", | ||
"@types/mocha": "^10.0.9", | ||
"@types/node": "^22.7.7", | ||
"jest": "^29.7.0", | ||
"jose": "^5.9.6", | ||
"mocha": "^10.7.3", | ||
"ts-jest": "^29.2.5", | ||
"typescript": "^5.6.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
import { | ||
BarretenbergBackend, | ||
CompiledCircuit, | ||
ProofData, | ||
UltraHonkBackend, | ||
} from '@noir-lang/backend_barretenberg'; | ||
import { Noir } from '@noir-lang/noir_js'; | ||
import { InputValue, InputMap } from '@noir-lang/noirc_abi'; | ||
|
||
type ProvingBackend = 'honk' | 'plonk' | 'all'; | ||
|
||
export class Prover { | ||
private plonk?: BarretenbergBackend; | ||
|
||
private honk?: UltraHonkBackend; | ||
|
||
private noir: Noir; | ||
|
||
constructor( | ||
/* The ACIR of the Noir circuit to prove */ | ||
circuit: CompiledCircuit, | ||
/* Define the prover backend to use */ | ||
private provingBackend: ProvingBackend = 'plonk' | ||
) { | ||
// initialize the backends | ||
if (provingBackend === 'plonk' || provingBackend === 'all') { | ||
this.plonk = new BarretenbergBackend(circuit); | ||
} | ||
if (provingBackend === 'honk' || provingBackend === 'all') { | ||
this.honk = new UltraHonkBackend(circuit); | ||
} | ||
// initialize the Noir instance | ||
this.noir = new Noir(circuit); | ||
} | ||
|
||
/** | ||
* Compute the witness for a given input to the circuit without generating a proof | ||
* | ||
* @param input - the input that should produce a satisfying witness for the circuit | ||
* @returns - the witness for the input and the output of the circuit if satisfiable | ||
*/ | ||
async simulateWitness( | ||
input: InputMap | ||
): Promise<{ witness: Uint8Array; returnValue: InputValue }> { | ||
return this.noir.execute(input); | ||
} | ||
|
||
/** | ||
* Generate a proof of a satisfying input to the circuit using a provided witness | ||
* | ||
* @param input - a satisfying witness for the circuit | ||
* @param provingBackend - optionally provided if the class was initialized with both proving schemes | ||
* @returns proof of valid execution of the circuit | ||
*/ | ||
async prove( | ||
witness: Uint8Array, | ||
provingBackend?: ProvingBackend | ||
): Promise<ProofData> { | ||
// determine proving backend to use | ||
let backend: BarretenbergBackend | UltraHonkBackend; | ||
if ( | ||
(provingBackend && this.plonk) || | ||
(this.provingBackend === 'plonk' && this.plonk) | ||
) { | ||
backend = this.plonk; | ||
} else if ( | ||
(provingBackend === 'honk' && this.honk) || | ||
(this.provingBackend === 'honk' && this.honk) | ||
) { | ||
backend = this.honk; | ||
} else { | ||
throw new Error(`Proving scheme ${this.provingBackend} not initialized`); | ||
} | ||
|
||
// generate the proof | ||
return backend.generateProof(witness); | ||
} | ||
|
||
/** | ||
* Simulate the witness for a given input and generate a proof | ||
* | ||
* @param input - the input that should produce a satisfying witness for the circuit | ||
* @param provingBackend - optionally provided if the class was initialized with both proving schemes | ||
* @returns proof of valid execution of the circuit | ||
*/ | ||
async fullProve( | ||
input: InputMap, | ||
provingBackend?: ProvingBackend | ||
): Promise<ProofData> { | ||
const { witness } = await this.simulateWitness(input); | ||
return this.prove(witness, provingBackend); | ||
} | ||
|
||
/** | ||
* Verify a proof of a satisfying input to the circuit for a given proving scheme | ||
* | ||
* @param proof - the proof to verify | ||
* @param provingBackend - optionally provided if the class was initialized with both proving schemes | ||
* @returns true if the proof is valid, false otherwise | ||
*/ | ||
async verify( | ||
proof: ProofData, | ||
provingBackend?: ProvingBackend | ||
): Promise<boolean> { | ||
// determine proving backend to use | ||
let backend: BarretenbergBackend | UltraHonkBackend; | ||
if ( | ||
(provingBackend && this.plonk) || | ||
(this.provingBackend === 'plonk' && this.plonk) | ||
) { | ||
backend = this.plonk; | ||
} else if ( | ||
(provingBackend === 'honk' && this.honk) || | ||
(this.provingBackend === 'honk' && this.honk) | ||
) { | ||
backend = this.honk; | ||
} else { | ||
throw new Error(`Proving scheme ${this.provingBackend} not initialized`); | ||
} | ||
// verify the proof | ||
return backend.verifyProof(proof); | ||
} | ||
|
||
/** | ||
* End the prover wasm instance(s) and clean up resources | ||
*/ | ||
async destroy() { | ||
if (this.plonk) { | ||
await this.plonk.destroy(); | ||
} | ||
if (this.honk) { | ||
await this.honk.destroy(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
export interface JWT { | ||
header: BoundedVec; | ||
payload: BoundedVec; | ||
signature: BoundedVec; | ||
} | ||
|
||
export type BoundedVec = { | ||
storage: number[]; | ||
len: number; | ||
}; | ||
|
||
const pad = (arr: number[], length: number, value?: number): number[] => { | ||
const res = arr.slice(); | ||
while (res.length < length) { | ||
res.push(value || 0); | ||
} | ||
return res; | ||
}; | ||
|
||
export const toBoundedVec = (data: string, maxLength: number): BoundedVec => { | ||
const storage = pad( | ||
data.split('').map((c) => c.charCodeAt(0)), | ||
maxLength | ||
); | ||
return { storage, len: data.length }; | ||
}; | ||
|
||
export const toJWT = (data: string) => { | ||
isValidJWT(data); | ||
const [h, p, s] = data.split('.'); | ||
const header = toBoundedVec(h, 64); | ||
const payload = toBoundedVec(p, 256); | ||
const signature = toBoundedVec(s, 43); | ||
|
||
return { header, payload, signature }; | ||
}; | ||
|
||
const isValidJWT = (data: string) => { | ||
const [header, payload, signature] = data.split('.'); | ||
if (!header || !payload || !signature) { | ||
throw new Error('Invalid JWT'); | ||
} | ||
}; |
Oops, something went wrong.